How to set customize different font style in Android - java

I want to set different font style my ‘button’ text and ‘edit text box’ text like times New Roman, Calibri,Cambria and Georgia. How can i set different font,example for i want to change my login button text to Calibri font. I don't know how can i set or import font from MS Office or Font files. Please suggest me, Thank you..
MY XML CODE HERE
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint=" User Name " />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint=" Password "
android:fontFamily="Tekton Pro Ext"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="Tekton Pro Ext"
android:text=" Login " />
</LinearLayout>
Layout

You need to create fonts folder under assets folder in your project and put your TTF into it and in the main or in your activity you can set the fonts as
TextView myTextView=(TextView)findViewById(R.id.textBox);
Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf");
myTextView.setTypeface(typeFace);

Maybe its better to take a look at this posts which exactly answered what you are looking for:
1- How to change fontFamily of TextView in Android
2- How to change the font on the TextView?
for short you must do something like this :
put the font in your assets folder in /fonts directory and then use this line of code:
Button bt = (Button) findViewById(R.id.myButton);
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/epimodem.ttf");
bt.setTypeface(face);

All the above answers are right. If you want to use a Button or TextView with a different font throughout the app, here's what you do.
public class FontsArePriceyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCostlyFont();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCostlyFont();
}
public MyTextView(Context context) {
super(context);
setCostlyFont();
}
private void setCostlyFont() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "filenameofyourfont.ttf");
setTypeface(tf);
}
}
}
Then in your layouts, replace TextView with yourpackagename.FontsArePriceyTextView. Eg.
<com.company.projectname.widgets.FontsArePriceyTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your awesome stylised text" />
You should be good to go.

Related

Setting Nested TextView Text

I've created a layout for a button format I use multiple times. The button format has a TextView and an ImageView. With the way I'm including this layout in my main activity, I don't think I'm able to change the text of the inner TextView dynamically in Java or in the XML. Is there a different way I can do this such that I can set the text of the inner TextView?
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/settingslayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="13dp"
android:textSize="20dp"
android:text="CHANGE ME"
android:id="#+id/text" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginRight="25dp"
android:layout_alignParentRight="true"
android:src="#drawable/left"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:maxHeight="30dp"
android:maxWidth="30dp"/>
</RelativeLayout>
Main Activity:
...
<include layout="#layout/settings_layout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/accountStaticUnderline"
android:id="#+id/termBegin"
android:text="TEST" /> //DOESNT WORK
...
</RelativeLayout>
Setting the text that way won't work because android:text is not applicable to the <include> tag. More specifically, it's not applicable to the thing being included, which is a RelativeLayout. (You couldn't put the android:text on the RelativeLayout and have it apply to the TextView, nor would you expect that to work.)
My first suggestion (and the easiest immediate solution) is to use TextView's built in support for compound drawables so you can simply use TextViews instead of includes and have a style resource for the attributes you want.
If that's not good enough for your use case, then you might need to make a custom View. This view will replace the RelativeLayout and have the TextView and ImageView as children. The main thing to decide is where and how the children are created and their references are obtained: you can create them manually in Java when the parent is being constructed; or you can use some combination of layouts with <include>s and/or <merge>s. Making the text attribute work then requires some use of a <declare-styleable>.
I assume you want the children to always appear the same and that you want to reuse the layout you already made (i.e. you don't want to set all the attributes manually), so this is what I would probably do:
public class MyButton extends RelativeLayout {
private TextView mTextView;
private ImageView mImageView;
public MyButton(Context context) {
super(context);
init(context, null);
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attributeSet) {
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.my_button, this, true); // used with merge tag
mTextView = (TextView) findViewById(R.id.text);
mImageView = (ImageView) findViewById(R.id.image);
int[] attrs = {android.R.attr.text};
TypedArray a = context.obtainStyleAttributes(attributeSet, attrs);
String text = a.getString(0, null);
mTextView.setText(text);
a.recycle();
}
}
In res/layout/my_button.xml:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="13dp"
android:textSize="20dp"
android:text="CHANGE ME"
android:id="#+id/text" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginRight="25dp"
android:layout_alignParentRight="true"
android:src="#drawable/left"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:maxHeight="30dp"
android:maxWidth="30dp"/>
</merge>
In your activity layout:
...
<!-- no more include -->
<com.package.MyButton
android:id="#+id/something"
android:layout_width="..."
android:layout_height="..."
android:text="..." />
<!-- you can have multiple instances with different IDs -->
<com.package.MyButton
android:id="#+id/something_else"
android:layout_width="..."
android:layout_height="..."
android:text="..." />
...
You could also use your own <declare-styleable> for the custom view, which will be necessary if later you want to have custom XML attributes for it, but the approach above should be sufficient.
sure, just type this code in your activity
((TextView)findViewById(R.id.text)).setText("enter_text_here");

Create custom layout in XML to initialize in code or in another layout xml

I have encountered an issue i am trying to resolve (or understand better the way it should be done) in creation of custom Layout in Android.
I want to create a custom RelativeLayout class for my use, which is defined in a layout XML file.
my_relative_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<com.mypackage.MyRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:scaleType="fitStart"
android:src="#drawable/my_drawable"/>
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/title_gray"
android:layout_below="#id/image_view"
android:gravity="center"
android:text="#string/placeholder" />
</com.mypackage.MyRelativeLayout>
Usage
public class MyRelativeLayout extends RelativeLayout {
private AttributeSet attrs;
private ImageView imageView;
private TextView textView;
public MyRelativeLayout(Context context) {
super(context);
}
public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.attrs = attrs;
}
public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.attrs = attrs;
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
if (attrs != null) {
TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.MyRelativeLayout, 0, 0);
drawableResource = a.getResourceId(R.styleable.MyRelativeLayout.image_view, 0);
a.recycle();
}
imageView = (ImageView) findViewById(R.id.image_view);
textView = (TextView) findViewById(R.id.text_view);
if (drawableResource != 0 && imageView != null) {
imageView.setImageResource(drawableResource);
}
}
}
My issue is that i want to initialise this layout both in another XML and in code.
But as I wrote my class and XML, i can only use it in code by doing:
myLayout = (MyRelativeLayout) LayoutInflater.from(this.getActivity()).inflate(R.layout.my_relative_layout, container, false);
When writing the following in another XML causes the onFinishInflate to fail on getViewById (returns null) and the childrenCount is 0
<com.mypackage.MyRelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#id/another_layout"
app:image_view="#drawable/my_image" />
and doing the following, won't let me configure the custom image_view attribute.
<include layout="#layout/my_relative_layout"/>
To fix that, i can change the custom layout XML root element to be of type RelativeLayout and add the following to the beginning of onFinishInflate method:
LayoutInflater.from(getContext()).inflate(R.layout.my_relative_layout, this, true);
But the XML won't reference my class.
My questions are,
1. Am i missing something in the definition of the custom layout?
2. What is the correct definition for custom layout?
Thank you in advance!
First you should use the styled attributes in the contractor as shown in this example.
How can you expect from MyRelativeLayout that defined as fallow:
<com.mypackage.MyRelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#id/another_layout"
app:image_view="#drawable/my_image" />
To be aware of views that are defined in my_relative_layout.xml?
To make it work you should create a costume layout and add it to com.mypackage.MyRelativeLayout manually.
Some thing like that:
costume_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:scaleType="fitStart"
android:src="#drawable/my_drawable"/>
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/title_gray"
android:layout_below="#id/image_view"
android:gravity="center"
android:text="#string/placeholder" />
</RelativeLayout>
Your costume view
public class MyRelativeLayout extends RelativeLayout {
private AttributeSet attrs;
private ImageView imageView;
private TextView textView;
public MyRelativeLayout(Context context) {
super(context);
init();
}
public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(Context context){
LayoutInflater.from(context).inflate(R.layout.costume_layout, this);
}
}

Extended SeekBar (Custom View) not showing

In my main layout xml file I have this:
<view class="com.mysite.MainActivity$MySeekBar"
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
And in MainActivity.java inside the MainActivity class I have this:
public static class MySeekBar extends SeekBar {
public MySeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
In the above case, nothing displays. However it does work if instead of the first snippet I have this:
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
I have also tried using uppercase 'View' instead of 'view' in the xml.
What am I doing wrong?
Edit: I have tried following this google developer page on custom components which at the bottom gives a simple example of implementing with an inner class.
put this class in its own file
<com.mypackagename.MySeekBar <== this is missing
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

How to render both an xml view and a java view in android

In my app I have a very basic looking compass which is rendered within my activity through a class. I am trying to display the compass with a layout. So rather than having just a circle with a line pointing north, I can include text box and buttons. How do I render this within a layout? Currently my activity sets the content view like so:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
compassView = new CompassView(this);
setContentView(compassView);
I have tried setContentView(R.layout.activity_display_compass) which is my xml file however it only display "hello world" (the TextView), not the compass. See my xml file below.
xml
<LinearLayout 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"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"/>
<View
class = "com.example.gpsfinder.CompassView"
android:id="#+id/compassView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Any guidance would be greatly appreciated.
To use a custom View subclass in your xml layout file:
<LinearLayout 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"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"/>
<com.example.gpsfinder.CompassView
android:id="#+id/compassView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
And then you probably need to add some of these constructors in your Java code:
// you used this ctor when creating the view programmatically
public CompassView(Context context) {
this(context, null);
// add additional initialization here
}
// this constructor is needed for the class to be used in XML layout files!
public CompassView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
// add additional initialization here
}
// this constructor is needed for the class to be used in XML layout files,
// with a class-specific base style
public CompassView(Context context, AttributeSet attributeSet, int defStyle) {
super(context, attributeSet, defStyle);
// add additional initialization here
}

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