Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView and EditText in single Row in Android?

I am writing XML in following way, list_item2 contains TextView and EditText, while list_item is another XML contains only TextView,

<?xml version="1.0" encoding="utf-8"?>

<TextView android:id="@+id/textItem"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

    <EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</EditText>

I am using the xml in ListView, such as

public View getView(int position, View convertView, ViewGroup parent) {
        View row;

        if(position!=2) {
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.list_item, parent, false);
        }
        else {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.list_item2, parent, false);
        }
        TextView tv = (TextView) row.findViewById(R.id.textItem);
        tv.setText(getItem(position));

        tv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

but It is creating output like

 TextView

 EditText

I want both on same row, such as

   TextView        EditText

Any one edit the XML and let me know the hierarchy for creating GUI in Android Apps

like image 658
Chatar Veer Suthar Avatar asked Dec 07 '22 17:12

Chatar Veer Suthar


2 Answers

Take your EditText and TextView inside a LinearLayout having horizontal orientation (which is applied by default) and set layout_weight = 1 or whatever do you want to EditText and TextView

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<TextView android:id="@+id/textItem"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:padding="10dp"
    android:textSize="16sp" />
    <EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/ >
</LinearLayout>
like image 106
Adil Soomro Avatar answered Dec 21 '22 23:12

Adil Soomro


use following layout

<?xml version="1.0" encoding="utf-8"?
    <LinearLayout
            android:id="@+id/lnrTitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

    <TextView android:id="@+id/textItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weight="1"
        android:textSize="16sp" >
    </TextView>

        <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
    android:weight="1"

        android:layout_height="wrap_content" >

    </EditText>
    </LinearLayout>
like image 33
bindal Avatar answered Dec 21 '22 23:12

bindal