Inflate custom android widget - java

I know there are dozens similar post, but it looks to me everything is correct here:
The custom widget:
public class DoubleTextItem extends LinearLayout {
private TextView txtMain;
private TextView txtDescription;
public DoubleTextItem(Context context) {
super(context);
}
public DoubleTextItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
((Activity)getContext()).getLayoutInflater().inflate(R.layout.widget_double_text_item, this);
setupViewItems();
}
private void setupViewItems() {
txtMain = (TextView) findViewById(R.id.txtMain);
txtDescription = (TextView) findViewById(R.id.txtDecription);
}
public void setDescription(String text) {
txtDescription.setText(text);
}
}
The custom widget layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txtMain"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txtDecription"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
ANd here inside an activity function i get a casting error,
LayoutInflater inflater = LayoutInflater.from(this);
DoubleTextItem item = (DoubleTextItem) inflater.inflate(R.layout.widget_double_text_item, layout);
item.setText(som-txt);
item.setDescription("#"+athlete.getString("position"));

Here, the root View is a LinearLayout but you try to cast it your custom class:
DoubleTextItem item = (DoubleTextItem) inflater.inflate(R.layout.widget_double_text_item, layout);
The standard advice is:
All DoubleTextItems are LinearLayouts, but not all LinearLayouts are DoubleTextItems.
Meaning you cannot downcast objects from a LinearLayout to a DoubleTextItem, there are too many assumptions and Java won't let you do it.
If you want a DoubleTextItem in your layout you need to use:
<your.package.name.DoubleTextItem
... />
(Also, calling inflate inside onFinishInflate() seems a little silly especially since you don't save the inflated item... If you want to inflate a different layout, don't inflate the first one.)
Overall it looks like you are trying to recreate the now deprecated TwoLineListItem, perhaps you can learn some pointers from it's source code (or just use the TwoLineListItem.)

Related

How can I change the info below a RecyclerView depending of the item selected?

I'm making a recycler view with a linear layout horizontal, it looks like this:
HorizontalRecyclerView
As you can see under it there are two text view that I want to fill with the item info when the user press an item. I've do two Adapters to show a menu before, this way when you make a long click the item change its layout, but it doesn´t change the information below. The way of manage a selected item usually is using the adapter like this:
if (holder instanceof AdaptadorProyectosViewHolder) {
((AdaptadorProyectosViewHolder) holder).tv_nombreProyecto.setText(nombreProyecto.toString());
((AdaptadorProyectosViewHolder) holder).tv_nombreClienteProyecto.setText("UN CLIENTE");
//When it's a long click the menu is shown in the place of the item
((AdaptadorProyectosViewHolder) holder).itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mostrarMenu(position); //showMenu(position)
return true;
}
});
What this do is that the layout of an item change (displaying more information, options,etc), but i want the info to be below the Recycler View, not inside it into an item. There's any way to do it?
Here's the xml of the fragment that contains the Recycler View:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Vistas.Proyectos">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_proyectos"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
Relying on little details from your question, here's a quick solution.
Add an onClickListener to your adapter:
public interface OnItemClickListener {
void onItemClick(int position);
}
Declare the listener's variable and add it to the adapter's constructor:
private final OnItemClickListener listener;
public MyAdapter ( List<Item> items , Context context , OnItemClickListener listener ) {
mItems = items;
mContext = context;
this.listener = listener;
}
Inside ViewHolder, add OnClickListener:
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
private TextView txt;
public ViewHolder(#NonNull View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt = itemView.findViewById(R.id.tvItemTitle);
}
#Override
public void onClick(View v)
{
listener.onItemClick(getAdapterPosition());
}
}
After adding these to the adapter, now move to the activity or fragment hosting the recyclerview and implement the onClickListener you just created. Here, I used a fragment:
public class LibraryFragment extends MainActivityFragment implements MyAdapter.OnItemClickListener
Add the following to the newly implemented method:
#Override
public void onItemClick(int position)
{
mtext.setText(mItems.get(position).getName());
}
All done.
Notice that mItems is your list of items, and mtext is the TextView hosting the results!
Also, note the getName() method is a method I created. Replace mItems, mtext and getName with your own codes!
Extra note: you need to add the listener to the adapter in the fragment:
adapter = new MyAdapter( mItems , getActivity() , this );

Responsive UI GridView layout

I have surveyed in other sites and got different responses about how I can solve my problem. In fact the problem is that I am trying to make a responsive gridView layout to display 25 textviews with numbers. I had hard time in constructing it the way I want but here is what I got:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
android:layout_gravity="center"
android:gravity="center"
tools:context="com.example.hristodraganov.bingo.MainActivity"
tools:showIn="#layout/activity_main">
<RelativeLayout
android:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4">
</RelativeLayout>
<GridView
android:paddingTop="8dp"
android:id="#+id/gridview"
android:background="#000"
android:layout_width="match_parent"
android:layout_weight="6"
android:layout_height="0dp"
android:gravity="center"
android:columnWidth="70dp"
android:numColumns="5"
android:verticalSpacing="10dp"
android:horizontalSpacing="1dp"
android:stretchMode="spacingWidth"
/>
This is the layout that is used on the MainActivity. I inflate it with textview item with this code:
<?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:background="#d3d3d3"
android:gravity="center">
<com.example.****.****.SquareView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cell"
android:textColor="#D0583B"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp">
</com.example.****.****.SquareView>
There is the extension of the BaseAdapter class that is used to inflate the grid with the textviews:
public class GridAdapter extends BaseAdapter {
private final Context mContext;
private String[] numbers;
public GridAdapter(Context context, String[] numbers) {
this.mContext = context;
this.numbers = numbers;
}
#Override
public int getCount() {
return numbers.length;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(mContext);
gridView = inflater.inflate(R.layout.textview_layout, null);
TextView textView = (TextView) gridView.findViewById(R.id.cell);
textView.setText(numbers[position]);
} else {
gridView = (View) convertView;
}
return gridView;
}
}
Also, I am forcing the textView to be squared in this SquareView class:
public class SquareView extends android.support.v7.widget.AppCompatTextView {
public SquareView(Context context) {
super(context);
}
public SquareView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}}
The layout is doing fine by now, here is an image of the size I used to construct it Nexus 5X. It looks good but when I change to 4" -Nexus S the whole last row is somewhere below the screen. Also this happens to 5"-Nexus 5 where the last row is slightly visible. Above 5.2" to 6.0" the layout fits perfectly. So my question is what should I do in this scenario to make the layout fit for small-sized screens without making a duplicate layout for them. (Note: I was told that the BaseAdapter implementation would fix the responsiveness of the layout). Any ideas what could I do? (Sorry for the long texts.)
Because there are a lot of different Android devices out there your best choice would be to encapsulate your layout in a ScrollView.
Another option is to get rid of the weights and let the top RelativeLayout grow/shrink after the GridView has taken all the space it needs. This still might not work on smaller screens or in landscape.
```
<RelativeLayout
android:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</RelativeLayout>
<GridView
android:paddingTop="8dp"
android:id="#+id/gridview"
android:background="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:columnWidth="70dp"
android:numColumns="5"
android:verticalSpacing="10dp"
android:horizontalSpacing="1dp"
android:stretchMode="spacingWidth"/>
```

How to android inflate XML Layout in ConstraintLayout class?

i create a ConstrantLayout class
public class AboutView extends ConstraintLayout {
TextView about_txt;
TextView dr_txt;
public AboutView(Context context) {
super( context );
init();
}
public AboutView(Context context, AttributeSet attrs, int defStyleAttr) {
super( context, attrs, defStyleAttr );
init();
}
private void init() {
LayoutInflater inflater = LayoutInflater.from( getContext() );
inflater.inflate( R.layout.about_layout,this );
about_txt = (TextView) findViewById(R.id.about_txt);
dr_txt = (TextView) findViewById(R.id.dr_txt);
}
}
Layout about_layout.XML file to inflate into class
Create layout how you want your custom view to look like. There is nothing complicated about it.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:id="#+id/about_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
tools:text="About" />
<TextView
android:id="#+id/dr_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
tools:text="Text" />
</android.support.constraint.ConstraintLayout>
This way you can set constraints and edit the layout with editor.
AFTER you are done with editting I would recommend changing the root view ConstraintLayout to merge.
By merging you won't have extra layout inside the custom view. Be careful - merge attributes are ignored.

Custom onClickListner in custom widget - how?

I have a custom widget thats extends Linear layout.
It just block of text with litle.
Here is xml of widget:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:background="#drawable/actionbar_background"
android:layout_height="35dp"
android:paddingLeft="15dp"
android:gravity="center_vertical">
<TextView
android:id="#+id/title"
android:layout_height="35dp"
android:textColor="#color/white"
android:layout_width="match_parent"
android:textSize="18dp"
android:clickable="true"
android:gravity="center_vertical" />
</LinearLayout>
<TextView
android:id="#+id/ingreds"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Some text"
android:paddingLeft="15dp" />
</LinearLayout>
If I creating onClickListener for that custom widget somewhere in Activity - it reacts only if I click on last TextView in that layout. But I need to set onClickListener for horizontal LinearLayout which contains "title" textView, or directly on title TextView.
Important: I want to set these listeners OUTSIDE the custom widget class.
I think that I need to override setOnclickListener method in my custom class or something like that, but I'm not sure how it can be done.
Here is the class of custom widget:
public class MyTextBlock extends LinearLayout {
private TextView title;
private TextView ingreds;
public MyTextBlock(Context context) {
this(context, null);
}
public MyTextBlock(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_text_block, this, true);
title = (TextView) findViewById(R.id.title);
ingreds = (TextView) findViewById(R.id.ingreds);
}
public void setText(String text) {
ingreds.setText(text);
}
public void setTitle(String titleText) {
title.setText(titleText);
}
}
Thanks for answering.
Just implement the OnClickListener to this view class and set textview.setOnClickLister(this);
now handle the click event under the onClick(View v) method.
MyTextBlock textviewCustom = ....
textviewCustom.setonclickListner(MainClass.this);
public class MyTextBlock extends LinearLayout {
private TextView title;
private TextView ingreds;
public MyTextBlock(Context context) {
this(context, null);
}
public MyTextBlock(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_text_block, this, true);
title = (TextView) findViewById(R.id.title);
ingreds = (TextView) findViewById(R.id.ingreds);
}
public void setonclickListner(OnclickListner listner)
{
title.setonclicklistner(listner);
ingreds.setonclicklistner(listner);
}
public void setText(String text) {
ingreds.setText(text);
}
public void setTitle(String titleText) {
title.setText(titleText);
}
}

How to right align PreferencesActivity in android?

I have PreferencesActivity which I need it to be right aligned because I want to use Arabic language, I tried to use android:layout_gravity="right" for PreferenceScreen but it didn't work.
This is my XML:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:layout_gravity="right">
<PreferenceCategory android:title="General Settings">
<CheckBoxPreference
android:title="Full Screen"
android:defaultValue="false"
android:summary="Always view as Full Screen"
android:key="fullScreenPref" />
<Preference
android:title="Report Bugs"
android:summary="Notify us for any Bugs or Errors"
android:key="bugs"/>
<Preference
android:title="About"
android:summary="Version 1.0.0"
android:key="about"/>
</PreferenceCategory>
</PreferenceScreen>
This is how I use the XML inside PreferencesActivity:
addPreferencesFromResource(R.layout.preferences);
After spending some time searching about our shared question, I found nothing useful. obviosly android api doesn't support RTL prefefrences. it means no support for persian/hebrew/arabic languages. But afterall I tried to find some way to right align an editText. I drived EditTextPreference class and implemented onCreateView method again.
here is my code:
/**
* Created by omid on 1/12/14.
*/
public class RtlTextPreference extends EditTextPreference {
public RtlTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected View onCreateView(ViewGroup parent) {
View view = super.onCreateView(parent);
RelativeLayout layout = (RelativeLayout) ((LinearLayout) view).getChildAt(1);
layout.setGravity(Gravity.RIGHT);
return view;
}
}
the rest of the preferences in the standard android api should be the same(btw I haven't tried yet).
maybe someday we should fully implement rtl support in android preferences and host it on github.
Hope this code snippet helps you.
Unfortunately the base Preference components (PreferenceScreen, Preference, etc.) are implemented in a fairly non-configurable way. They are designed to work in a specific way and are not terribly customizable.
In order to do what you want, you will probably have to implement your own version of PreferenceScreen (or possible just the Preference types you are using such as CheckBoxPreference). The way it is designed, when it inflates XML layouts it assumes a lot of things about them and handles it for you (text size, padding, layout, wrapping, etc). This is great if you want it to look like the default, but not so great if you need to tweak these configurations.
(note: if you don't want to implement preferencescreen you could just use a listview and treat it as a preference page. However, either way you basically have to implement your own version of preferences.)
(note2: based on other questions I've seen about arabic text, it's POSSIBLE android is smart enough to try to right align it by default. However, I would be surprised if this was the case. Still, it's worth a try.)
Sorry I don't have a better answer for you.
In your manifest file under application tag add this line,
android:supportsRtl="true"
After checking for a few solutions i came upon something that might help.
It's not elegant and works only on 4 and up but it's better than nothing...
add this code to onResume()
Locale locale = new Locale("iw");// Hebrew in this case
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
Of course you can choose to change the locale in your preference activities onResume() method only in your MainActivity's onResume() change locale back to your preferred (or user defined) locale.
Hope this helps!
Alright, this might be quite late, but anyways if you are targeting Android 3.0 or higher, you should use PreferenceFragment with can be somehow made RTL.
So you should create a fragment in your code like this:
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
now you should create an Activity that adds this fragment to its layout.
You can create a layout for your activity and put the fragment in it via xml element or using the FragmentManager class. But the key to make the preferences Right-To-Left is to put a layout_direction="rtl" attribute in the layout file for a parent element of the fragment. i.e.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/rootView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl"
android:orientation="vertical">
<fragment
class="blah.blah.SettingsFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Notice the android:layoutDirection="rtl" on the RelativeLayout element that fixes the problem.
Now add the Activity Java code like any other activity and add to your manifest file.
Hope it's not too late :)
same as my problem and this is the solution:
inside your PreferenceFragment:
#Override
public View onCreateView(LayoutInflater paramLayoutInflater,
ViewGroup paramViewGroup, Bundle paramBundle) {
getActivity().setTitle(R.string.fragment_title_settings);
View v = super.onCreateView(paramLayoutInflater, paramViewGroup,
paramBundle);
rtlView(v);
return v;
}
public void rtlView(View v){
if(v instanceof ListView){
rtllater((ListView) v);
}
else
if(v instanceof ViewGroup){
if(v instanceof RelativeLayout){
((RelativeLayout)v).setGravity(Gravity.RIGHT);
}
else
if(v instanceof LinearLayout){
((LinearLayout)v).setGravity(Gravity.RIGHT);
}
for (int i=0;i<((ViewGroup)v).getChildCount();i++){
rtlView(((ViewGroup)v).getChildAt(i));
}
}
else
if(v instanceof TextView){
v.getLayoutParams().width=v.getLayoutParams().MATCH_PARENT;
((TextView)v).setGravity(Gravity.RIGHT);
}
}
public void rtllater(ListView v){
v.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
#Override
public void onChildViewAdded(View view, View view1) {
rtlView(view1);
}
#Override
public void onChildViewRemoved(View view, View view1) {
}
});
}
the result is like this image:right to left Preference
In my point of view this can be the solution:
Set each view's LayoutDirection to rtl;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
view.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
return view;}
for preferences fragment you can make its container to have this attribute
android:layoutDirection="rtl"
public class RtlEditTextPreference extends EditTextPreference {
#Override
protected View onCreateView(ViewGroup parent) {
View view = super.onCreateView(parent);
RelativeLayout relativeLayout = (RelativeLayout)(((LinearLayout)view).getChildAt(1));
relativeLayout.setGravity(Gravity.RIGHT);
TextView title = (TextView) relativeLayout.getChildAt(0);
TextView summary = (TextView) relativeLayout.getChildAt(1);
RelativeLayout.LayoutParams titleLayoutParams = (RelativeLayout.LayoutParams)title.getLayoutParams();
titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
title.setLayoutParams(titleLayoutParams);
RelativeLayout.LayoutParams summaryLayoutParams = (RelativeLayout.LayoutParams)summary.getLayoutParams();
summaryLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, title.getId());
summaryLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, -1); // Override default left alignment to the title
summary.setLayoutParams(summaryLayoutParams);
return view;
}
}
create a layout xml file called preference_checkbox.xml paste this (change the color to what you want, only the gravity and the width set to fill parent are important):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical" android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="15dip"
android:layout_marginRight="6dip" android:layout_marginTop="6dip"
android:layout_marginBottom="6dip" android:layout_weight="1">
<TextView android:id="#+android:id/title"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right"
android:singleLine="true" android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="marquee" android:fadingEdge="horizontal"
android:textColor=#000000 />
<TextView android:id="#+android:id/summary"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right"
android:layout_below="#android:id/title" android:layout_alignLeft="#android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall" android:textColor=#000000
android:maxLines="4" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="#+android:id/widget_frame"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:gravity="center_vertical" android:orientation="vertical" />
</LinearLayout>
and then in the preferences xml file for each row in the settings list that you want to right align add this: android:layout="#layout/preference_checkbox"
thats it!
got the solution from : http://stackoverflow.com/questions/7094584/checkboxpreference-with-own-layout
the question there was about check box with image but it's the same technique.
You could do that by using a custom layout with calling setLayoutResource(int layoutResId).
res/layout/preference_layout.xml: (Note that this is based on HoloEverywhere library org.holoeverywhere.preference.Preference)
<?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="wrap_content"
android:baselineAligned="false"
android:gravity="center_vertical"
android:minHeight="?attr/listPreferredItemHeight"
android:paddingRight="#dimen/preference_item_padding_side"
android:paddingLeft="?android:attr/scrollbarSize" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:minWidth="#dimen/preference_icon_minWidth"
android:orientation="horizontal" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:minWidth="48dp"
android:contentDescription="#string/emptyString"
android:paddingRight="#dimen/preference_item_padding_inner" >
</ImageView>
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="6dip"
android:gravity="right"
android:layout_gravity="right"
android:paddingLeft="#dimen/preference_item_padding_inner"
android:paddingTop="6dip" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="right"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium" >
</TextView>
<TextView
android:id="#+id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#id/title"
android:gravity="right"
android:layout_below="#id/title"
android:maxLines="10"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary" >
</TextView>
</RelativeLayout>
<LinearLayout
android:id="#+id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:minWidth="#dimen/preference_widget_width"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
In your custom Preference:
#Override
protected View onCreateView(ViewGroup parent)
{
setLayoutResource(R.layout.preference_layout);
return super.onCreateView(parent);
}
public class RtlEditTextPreference extends EditTextPreference {
public RtlEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RtlEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public RtlEditTextPreference(Context context) {
super(context);
}
#Override
protected View onCreateView(ViewGroup parent) {
View view = super.onCreateView(parent);
RelativeLayout relativeLayout = (RelativeLayout)(((LinearLayout)view).getChildAt(1));
relativeLayout.setGravity(Gravity.RIGHT);
TextView title = (TextView) relativeLayout.getChildAt(0);
TextView summary = (TextView) relativeLayout.getChildAt(1);
RelativeLayout.LayoutParams titleLayoutParams = (RelativeLayout.LayoutParams)title.getLayoutParams();
titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
title.setLayoutParams(titleLayoutParams);
RelativeLayout.LayoutParams summaryLayoutParams = (RelativeLayout.LayoutParams)summary.getLayoutParams();
summaryLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, title.getId());
summaryLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, -1); // Override default left alignment to the title
summary.setLayoutParams(summaryLayoutParams);
return view;
}
}
This method worked for me:
#Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs)
{
View v = super.onCreateView(parent, name, context, attrs);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
if(parent != null)
parent.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
if(v != null)
v.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
return v;
}
Arabic text has posed a problem for many a user which means that similar, if not the same, questions have been answered several times.
I'll just leave these here
Android Arabic text aligment and
How to support Arabic text in Android?

Categories

Resources