5

I want to align text in Spinner to the left but how can i achieve this require help

here is my code & screen shot

 <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/roundshape" >

        <!-- Lable Area -->

        <TableRow
            android:id="@+id/tblRwspnLbl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"           
            android:gravity="center" 
            android:padding="1dip">

            <TextView
                android:id="@+id/lblCust"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="@string/lblCust"
                android:textSize="14sp"
                android:textStyle="bold" 
                android:gravity="left"/>

            <TextView
                android:id="@+id/lblPros"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="@string/lblPros"
                android:textSize="14sp"
                android:textStyle="bold" />
        </TableRow>

        <!-- Spinner Area -->

        <TableRow
            android:id="@+id/tblRwspn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="1dip"                
            android:gravity="center" >

            <Spinner
                android:id="@+id/spnAECust"
                android:layout_width="75dp"
                android:layout_height="35dp"
                android:fontFamily="verdana,arial,helvetica"
                android:hint="@string/SelectCust"
                android:textSize="14sp" 
                android:layout_gravity="left"/>

            <Spinner
                android:id="@+id/spnAEProspect"
                android:layout_width="75dp"
                android:layout_height="35dp"
                android:fontFamily="verdana,arial,helvetica"
                android:hint="@string/SelectProspect"
                android:textSize="14sp" />
        </TableRow>

        <!-- Text Area -->

Here is my Screen the red dot is the space which i want to remove so tht my all labels and spinners will come in same aligned line

enter image description here

Aditi K
  • 1,534
  • 5
  • 22
  • 43

5 Answers5

2

Just remove android:padding="1dip" from Spinner TableRow and try i am not sure just try

Edit: You have to use a custom view here to give alignment to the Spinner values. Create a CustomView like this answer has created and add android:gravity' to the TextView asright`.

And set the CustomView to your adapter using

adapter.setDropDownViewResource(android.R.layout.your_custom_created_view);
Community
  • 1
  • 1
M D
  • 47,665
  • 9
  • 93
  • 114
2

Thank you all for giving all possible answers here is solution i get

 android:layout_width="75dp"

  android:layout_height="35dp"

  android:layout_marginLeft="5dp"

  android:paddingLeft="5dp"

  android:gravity="left"
Aditi K
  • 1,534
  • 5
  • 22
  • 43
1

I think you should use custom ArrayAdapter with TextView and play with textview property as per your requirement.

Example :

MyAdapter.java

    public class MyAdapter extends ArrayAdapter {
Context context;
    public MyAdapter(Context context, int resource) {
        super(context, resource);
        this.context=context;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=inflater.inflate(R.layout.mylayout, parent,false);
        TextView textView=(TextView)view.findViewById(R.id.text);

        return view;
    }
}

mylayout.xml

 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <TextView
         android:id="@+id/text"
         android:layout_width="match-parent"
         android:layout_height="wrap_content"
         android:text="@string/lblPros"
         android:textSize="14sp"
         android:gravity="right"
         android:textStyle="bold" />
</LinearLayout>
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
1

Try this:-

use android:layout_span="1". if required then give left margin for the first row.

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/roundshape" >

    <!-- Lable Area -->

    <TableRow
        android:id="@+id/tblRwspnLbl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"           
        android:gravity="center" ">

        <TextView
            android:id="@+id/lblCust"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/lblCust"
            android:layout_span="1"
            android:textSize="14sp"
            android:textStyle="bold" 
            android:gravity="left"/>

        <TextView
            android:id="@+id/lblPros"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="1"
            android:text="@string/lblPros"
            android:textSize="14sp"
            android:textStyle="bold" />
    </TableRow>

    <!-- Spinner Area -->

    <TableRow
        android:id="@+id/tblRwspn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"             
        android:gravity="center" >

        <Spinner
            android:id="@+id/spnAECust"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="1"
            android:fontFamily="verdana,arial,helvetica"
            android:hint="@string/SelectCust"
            android:textSize="14sp" 
            android:layout_gravity="left"/>

        <Spinner
            android:id="@+id/spnAEProspect"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="1"
            android:fontFamily="verdana,arial,helvetica"
            android:hint="@string/SelectProspect"
            android:textSize="14sp" />
    </TableRow>

    <!-- Text Area -->
Vinit ...
  • 1,409
  • 10
  • 37
  • 66
1

This is sample code it may useful

<Spinner
                    android:id="@+id/spinner1"
                    style="?android:attr/spinnerStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginBottom="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/dropdown"
                    android:dropDownVerticalOffset="1dp"
                    android:focusable="false"
                    android:spinnerMode="dropdown" />

take one more layout for the text view following is the code for layout

spinner_text_layout.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text_for_spinner"
    style="?android:attr/spinnerItemStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawablePadding="10dp"
    android:drawableRight="@drawable/arrow"
    android:gravity="center"
    android:textColor="@android:color/white" >

</TextView>

now in the custom adapter these are the two methods

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.spinner_text_layout, null);
        }
        TextView textView = (TextView) convertView
                .findViewById(R.id.text_for_spinner);
        textView.setText((String) getItem(position));
        notifyDataSetChanged();
        return convertView;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.spinner_text_layout, null);
        }
        TextView textView = (TextView) convertView
                .findViewById(R.id.text_for_spinner);
        textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        textView.setText((String) getItem(position));
        textView.setTextColor(Color.BLACK);
        textView.setBackgroundResource(R.drawable.drop_down_selector);
        return convertView;
    }
hareesh J
  • 520
  • 1
  • 6
  • 21