简体   繁体   中英

Align textview and spinner on same row

I want to align my textview and spinner on the same row like this image but my screen looks like this

Also how can I update price textview with the spinner's value? Suppose that the product price is $200, when I select 2 on spinner textview change to $400. How do I do that?

    <LinearLayout
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="#E5E5E5"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/TextViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:text="Qty"
        android:textSize="10pt"></TextView>

    <Spinner
        android:id="@+id/SpinnerFeedbackType"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:layout_width="fill_parent"
        android:entries="@array/feedbacktypelist"></Spinner>

    <EditText
        android:id="@+id/EditTextName"
        android:layout_height="wrap_content"
        android:hint="Name"
        android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:inputType="textPersonName"
        android:layout_width="fill_parent"></EditText>

    <EditText
        android:id="@+id/EditTextEmail"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:hint="Email"
        android:inputType="textEmailAddress"
        android:layout_width="fill_parent"></EditText>
     <EditText
        android:id="@+id/EditTextMobile"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:hint="Mobile Number"
        android:inputType="textEmailAddress"
        android:layout_width="fill_parent"></EditText>
      <EditText
        android:id="@+id/EditCity"
        android:layout_height="wrap_content"
          android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:hint="City"
        android:inputType="textEmailAddress"
        android:layout_width="fill_parent"></EditText>


    <EditText
        android:id="@+id/EditTextFeedbackBody"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:hint="Address"
        android:inputType="textMultiLine"
        android:lines="5"
        android:layout_width="fill_parent"></EditText>


    <EditText
        android:id="@+id/EditTextComments"
        android:layout_height="wrap_content"
         android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:hint="Comments"
        android:inputType="textMultiLine"
        android:lines="5"
        android:layout_width="fill_parent"></EditText>


     <TextView
        android:id="@+id/TextViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:text="Total Order Price:"
        android:textSize="10pt"></TextView>



     <TextView
        android:id="@+id/TextViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:text="Rs.6,849"
        android:textSize="10pt"></TextView>





    <CheckBox
        android:id="@+id/CheckBoxResponse"
        android:layout_height="wrap_content"
         android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:text="@string/feedbackresponse"
        android:layout_width="fill_parent"></CheckBox>

    <Button
        android:id="@+id/ButtonSendFeedback"
        android:layout_height="wrap_content"
         android:padding="5dp"
        android:text="@string/feedbackbutton"
        android:onClick="sendFeedback"
        android:layout_width="fill_parent"></Button>

  </LinearLayout>

This is because you are using orientation as vertical on the parent layout.And you have not given any parentLayout to the set of Views you want to show horizontally. Put the two textview and spinner in a LinearLayout with orientation as horizontal

Try the below data :

<LinearLayout
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    android:layout_height="fill_parent">
<TextView
        android:id="@+id/TextViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:text="Qty"
        android:textSize="10pt"></TextView>

    <Spinner
        android:id="@+id/SpinnerFeedbackType"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_width="wrap_content"
        android:weight=".5"
        android:entries="@array/feedbacktypelist"></Spinner>
</LinearLayout>

Do calculation in your setOnItemSelectedListener

priceTextView.setText(Integer.parseInt(Spinner.getSelectedItem().toString())*
              Integer.parseInt(productPrice));

Considering your productPrice is String

Make xml like this , this will solve your issue

<LinearLayout
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="#E5E5E5"
    android:layout_height="fill_parent">

<LinearLayout
 android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"

>

    <TextView
        android:id="@+id/TextViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:text="Qty"
        android:textSize="10pt"></TextView>

    <Spinner
        android:id="@+id/SpinnerFeedbackType"
        android:layout_height="wrap_content"

           android:layout_marginRight="10dp"
        android:layout_width="40dp"
        android:entries="@array/feedbacktypelist"></Spinner>

</LinearLayout>

    <EditText
        android:id="@+id/EditTextName"
        android:layout_height="wrap_content"
        android:hint="Name"
        android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:inputType="textPersonName"
        android:layout_width="fill_parent"></EditText>

    <EditText
        android:id="@+id/EditTextEmail"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:hint="Email"
        android:inputType="textEmailAddress"
        android:layout_width="fill_parent"></EditText>
     <EditText
        android:id="@+id/EditTextMobile"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:hint="Mobile Number"
        android:inputType="textEmailAddress"
        android:layout_width="fill_parent"></EditText>
      <EditText
        android:id="@+id/EditCity"
        android:layout_height="wrap_content"
          android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:hint="City"
        android:inputType="textEmailAddress"
        android:layout_width="fill_parent"></EditText>


    <EditText
        android:id="@+id/EditTextFeedbackBody"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:hint="Address"
        android:inputType="textMultiLine"
        android:lines="5"
        android:layout_width="fill_parent"></EditText>


    <EditText
        android:id="@+id/EditTextComments"
        android:layout_height="wrap_content"
         android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:hint="Comments"
        android:inputType="textMultiLine"
        android:lines="5"
        android:layout_width="fill_parent"></EditText>


     <TextView
        android:id="@+id/TextViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:text="Total Order Price:"
        android:textSize="10pt"></TextView>



     <TextView
        android:id="@+id/TextViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:text="Rs.6,849"
        android:textSize="10pt"></TextView>





    <CheckBox
        android:id="@+id/CheckBoxResponse"
        android:layout_height="wrap_content"
         android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
        android:text="@string/feedbackresponse"
        android:layout_width="fill_parent"></CheckBox>

    <Button
        android:id="@+id/ButtonSendFeedback"
        android:layout_height="wrap_content"
         android:padding="5dp"
        android:text="@string/feedbackbutton"
        android:onClick="sendFeedback"
        android:layout_width="fill_parent"></Button>

  </LinearLayout>

put spinner and text view in horizontal linear layout.

Implement spinner.setOnItemSelectedListener(AdapterView.OnItemSelectedListener). and change the TextView text as you need.

 spinner.setOnItemSelectedListener(new OnItemSelectedListener()
 {

   public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)

    {         
              String txtNo=textView.getText().toString().replaceAll("[^0-9]", "");
              textView.setText(Integer.parseInt(Spinner.getSelectedItem().toString())*
              Integer.parseInt(txtNo));
    }

  });     

Wrap them into a linear layout of horizontal orientation, like this...

<LinearLayout
    android:id="@+id/dd"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/TextViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:text="Qty"
        android:textSize="10pt" >
    </TextView>
    <Spinner
        android:id="@+id/SpinnerFeedbackType"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" >
    </Spinner>
</LinearLayout>

Inside of your current vertical layout, wrap the textview and spinner with a LinearLayout that has horizontal orientation.

as for your second question, you would simply multiply the price by the number in your quantity field, which can be obtained from mySpinner.getSelectedItem() and cast it as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM