Link to home
Start Free TrialLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

Align AutoCompleteTextView text position

Hello there,

I am developing a RTL app and want my AutoCompleteTextView widget to display the text from right to left but now it is showing on the left side. i tried doing this in my xml file but no luck.can somebody help me please.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dip"
android:gravity="right">

<TextView
    android:id="@+id/cusNameTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/customerName"
    android:textColor="#FFFFFF"
    android:layout_gravity="right"
    />

<AutoCompleteTextView
    android:id="@+id/cusName_CB"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="right"/>
Avatar of PCableGuy
PCableGuy
Flag of United States of America image

I tried your Layout code in Eclipse on an Android Phone and the text appears correctly on the right side. A screen shot is attached.

What you posted above is missing the </LinearLayout> tag at the end.

Is it possible the Java code is doing something to your layout?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dip"
android:gravity="right">

<TextView
    android:id="@+id/cusNameTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="customerName"
    android:textColor="#000000"
    android:layout_gravity="right"
    />

<AutoCompleteTextView
    android:id="@+id/cusName_CB"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:textColor="#000000"/> 
    
</LinearLayout>

Open in new window

screenshot.png
Avatar of Zolf

ASKER

THANKS FOR YOUR FEEDBACK. I JUST POSTED part of my xml code. i have attached a screenshot of the issue.
SC20130206-101728.png
I think you might need to make a custom adapter to do that. Try something along those lines.

I can't find a specific example at the moment, will check later tonight.
The individual dropdown rows that appear in an AutoCompleteTextView is a TextView.

So you can create an XML file with a TextView, then your call to ArrayAdapter changes to accept the new TextView that you created. A screenshot of the end result is attached.

Step 1, create a custom xml file with TextView with right align gravity and other attributes. Save it in your Layout folder. Name the file text_view.xml.

<?xml version="1.0" encoding="UTF-8"?> 
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge"
      android:gravity="right" 
      android:minHeight="?android:attr/listPreferredItemHeight"/>      

Open in new window

Step 2, changes to your code (your code may be slightly different):
autoText=(AutoCompleteTextView)findViewById(R.id.cusName_CB);
autoText.addTextChangedListener(this);
              
// Notice how the second argument when the ArrayAdapter is created is
// R.layout.text_view, which is the name of the xml file that you created
// The third argument is is the id of the TextView in text_view.xml
ArrayAdapter <String> ma = new ArrayAdapter<String>(this, R.layout.text_view, R.id.textView1, items)
autoText.setAdapter(ma);
screenshot.png
Avatar of Zolf

ASKER

PCableGuy

thanks a lot for your help. when i use your code for the xml,i get error in my xml but when i remove the line <?xml version="1.0" encoding="UTF-8"?>
then it saves without error. then when i ran my app, i could not see the names inmy autocompletetextview btut when i put my finger on the row i get to see the name and the good news is that it is positioned to the right. but as i mentioned it does not show the list names untill i put my finger pressed on it.can you please help me solve this issue.is it got to do with the


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/text_dropDown" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge"
      android:gravity="right" 
      android:minHeight="?android:attr/listPreferredItemHeight"/>

Open in new window



ArrayAdapter <Customer> customerSpinner = new ArrayAdapter<Customer>(MainMenuActivity.this, R.layout.dropdowntext,R.id.text_dropDown, customers);
				customerTextView.setAdapter(customerSpinner); 

Open in new window



then i tried my own xml code,but i get error when i type in the autocomplete

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/text_dropDown"
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:background="@android:color/white"
    android:gravity="right"
    android:textColor="@android:color/black"
    android:textSize="@android:style/TextAppearance.Medium" />

Open in new window



the error i get is

02-07 10:53:07.373: E/AndroidRuntime(16891): FATAL EXCEPTION: main
02-07 10:53:07.373: E/AndroidRuntime(16891): android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.view.LayoutInflater.createView(LayoutInflater.java:518)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.widget.AbsListView.obtainView(AbsListView.java:1616)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.widget.AutoCompleteTextView$DropDownListView.obtainView(AutoCompleteTextView.java:1559)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.widget.ListView.measureHeightOfChildren(ListView.java:1241)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.widget.AutoCompleteTextView.buildDropDown(AutoCompleteTextView.java:1380)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1144)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:1026)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:1009)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.os.Looper.loop(Looper.java:130)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.app.ActivityThread.main(ActivityThread.java:3693)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at java.lang.reflect.Method.invokeNative(Native Method)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at java.lang.reflect.Method.invoke(Method.java:507)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at dalvik.system.NativeStart.main(Native Method)
02-07 10:53:07.373: E/AndroidRuntime(16891): Caused by: java.lang.reflect.InvocationTargetException
02-07 10:53:07.373: E/AndroidRuntime(16891):       at java.lang.reflect.Constructor.constructNative(Native Method)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.view.LayoutInflater.createView(LayoutInflater.java:505)
02-07 10:53:07.373: E/AndroidRuntime(16891):       ... 22 more
02-07 10:53:07.373: E/AndroidRuntime(16891): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x1
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:515)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.widget.TextView.<init>(TextView.java:781)
02-07 10:53:07.373: E/AndroidRuntime(16891):       at android.widget.TextView.<init>(TextView.java:461)
02-07 10:53:07.373: E/AndroidRuntime(16891):       ... 25 more
Avatar of Zolf

ASKER

ok got it fixed,the issue was with this line
android:textSize="@android:style/TextAppearance.Medium"

got rid of it and all worked fine. only that now the names are smaller compared to what i had before. any idea how i can increase the size
ASKER CERTIFIED SOLUTION
Avatar of PCableGuy
PCableGuy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zolf

ASKER

cheers mate