Reputation: 1739
I am developing an app in arabic, and there are some values in spinner which are English and some are arabic. So, English text is aligned to left and arabic is aligned to right.
But, I want all the text to the right. I tried number of ways, but not getting exact solution.
My Code is, I have used custom spinner
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
public class SpinnerModified extends Spinner
{
public SpinnerModified(Context context)
{
super(context);
}
public SpinnerModified(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public SpinnerModified(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
public void setAdapter(SpinnerAdapter orig)
{
final SpinnerAdapter adapter = newProxy(orig);
super.setAdapter(adapter);
try
{
final Method m = AdapterView.class.getDeclaredMethod("setNextSelectedPositionInt", int.class);
m.setAccessible(true);
m.invoke(this, -1);
final Method n = AdapterView.class.getDeclaredMethod("setSelectedPositionInt", int.class);
n.setAccessible(true);
n.invoke(this, -1);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
protected SpinnerAdapter newProxy(SpinnerAdapter obj)
{
return (SpinnerAdapter) java.lang.reflect.Proxy.newProxyInstance(obj.getClass().getClassLoader(),
new Class[] { SpinnerAdapter.class }, new SpinnerAdapterProxy(obj));
}
/**
* Intercepts getView() to display the prompt if position < 0
*/
protected class SpinnerAdapterProxy implements InvocationHandler
{
protected SpinnerAdapter obj;
protected Method getView;
protected SpinnerAdapterProxy(SpinnerAdapter obj)
{
this.obj = obj;
try
{
this.getView = SpinnerAdapter.class.getMethod("getView",int.class, View.class, ViewGroup.class);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable
{
try
{
return m.equals(getView) && (Integer) (args[0]) < 0 ? getView(
(Integer) args[0], (View) args[1], (ViewGroup) args[2])
: m.invoke(obj, args);
}
catch (InvocationTargetException e)
{
throw e.getTargetException();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
protected View getView(int position, View convertView, ViewGroup parent)throws IllegalAccessException
{
if (position < 0)
{
final TextView v = (TextView) ((LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.spinner_item, parent,
false);
v.setText(getPrompt());
v.setTextColor(Color.GRAY);
return v;
}
return obj.getView(position, convertView, parent);
}
}
}
spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_width="match_parent"
android:gravity="right"/>
my_lyout.xml
<com.demo.demoapp.utils.SpinnerModified
android:id="@+id/search_list_spinner_2"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_centerHorizontal="true"
android:prompt="@string/select"
android:layout_centerVertical="true"
android:layout_marginRight="30dp"
android:layout_marginLeft="5dp"
android:layout_toLeftOf="@+id/search_logo_2"
android:background="@color/transparent"
/>
Screen shot of spinner
I want that 1000-2500 and 2500-5000 to the right
How can I achieve this??
Please Help..!!
Upvotes: 0
Views: 2095
Reputation: 179
just set these two attributes :
android:textAlignment="gravity"
android:gravity="right"
Sample: spinner_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:textAlignment="gravity"
android:gravity="right"
/>
Upvotes: 0
Reputation: 3051
If you wish to give both English (L2R) and Arabic (R2L) support to your spinner, just add below line to your spinner layout item
android:textDirection="locale"
And add
android:supportsRtl="true"
In your manifest file.
This will align the spinner text from Left for English language selection, and will align the text from Right when Arabic language is selected.
Upvotes: 1
Reputation: 39836
I believe that's one of those Android oddities. If you wrap the TextView inside another layout, it will work. For example:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_width="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="right"/>
</FrameLayout>
don't forget to give an ID to your text view and call findViewById
to get it
Upvotes: 2