How to Set Align (Right or Left) of Text in Textview, Programmatically

Right align text in android TextView

I think that you are doing this: android:layout_width = "wrap_content"

If this is the case, do this: android:layout_width = "match_parent"

Programmatically create and align an edittext to the right of a textview

Use RelativLayout and set Rule like android:layout_toRightOf="@+id/textview"

Android TextView Justify Text

I do not believe Android supports full justification.

UPDATE 2018-01-01: Android 8.0+ supports justification modes with TextView.

Programmatically center TextView text

yourTextView.setGravity(Gravity.CENTER);

How to programmatically align an Android TextView to the right of another TextView

Try the following:

  1. Set the id of textView[0] to 1 instead of 0 (id needs to be a positive integer)
  2. Add to the relativeLayoutParams of textView[1] a rule for RelativeLayout.ALIGN_TOP

The following worked for me:

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.test);
RelativeLayout.LayoutParams relativeLayoutParams;
TextView[] textView = new TextView[2];

// 1st TextView
textView[0] = new TextView(this);

relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);

textView[0].setId(1); // changed id from 0 to 1
textView[0].setText("1");

relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

relativeLayout.addView(textView[0], relativeLayoutParams);

// 2nd TextView
textView[1] = new TextView(this);

relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);

textView[1].setText("2");

relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF,
textView[0].getId());
relativeLayoutParams.addRule(RelativeLayout.ALIGN_TOP,
textView[0].getId()); // added top alignment rule

relativeLayout.addView(textView[1], relativeLayoutParams);

How to right-align text in an Android TextView or EditText?

You should use android:gravity="right". layout_gravity is for the view (EditText) alignment against the container.

Android TextView right alignment on a right-to-left device

Answering my own question.

After looking all over I have found no answer. Because the ROM is custom made for people living in Israel and speaking Hebrew, it seems that the alignment inside a width MATCH_PARENT TextView is flipped. Meaning, Align left means right and right means left.
The only way to change that is to use TextView with WRAP_CONTENT and align the TextView itself to the right inside it's parent.
Because I've already written the app and all the screens layouts, I did not want to change everything.
So what I did is implemented a custom control which wraps a TextView inside a LinearLayout and exposes it for easy use from the out side.
This way, the MATCH_PARENT will effect the LinearLayout while the control will take care of aligning the wrapped TextView to the right.

And this is how:

First, the control class itself (HebrewTextView.java):

public class HebrewTextView extends LinearLayout
{
private TextView mTextView;

public HebrewTextView(Context context)
{
super(context);
init(null);
}

public HebrewTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
init(attrs);
}

public TextView getTextView()
{
return mTextView;
}

private void init(AttributeSet attrs)
{
mTextView = new TextView(getContext());
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
lp.gravity = Gravity.RIGHT;
mTextView.setLayoutParams(lp);
mTextView.setGravity(Gravity.CENTER_VERTICAL);

addView(mTextView);

if(attrs!=null)
{
TypedArray params = getContext().obtainStyledAttributes(attrs, R.styleable.HebrewTextView);
mTextView.setText(params.getString(R.styleable.HebrewTextView_android_text));
mTextView.setTextColor(params.getColor(R.styleable.HebrewTextView_android_textColor, Color.WHITE));
mTextView.setTextSize(params.getDimension(R.styleable.HebrewTextView_android_textSize, 10));
mTextView.setSingleLine(params.getBoolean(R.styleable.HebrewTextView_android_singleLine, false));
mTextView.setLines(params.getInt(R.styleable.HebrewTextView_android_lines, 1));
params.recycle();
}
}
}

The control XML attributes under values/attrs.xml. Notice that the custom attributes match the TextView attributes so I wont have to change the layout files:

Note: these are the attributes I needed, if you are using some more, just add to the XML and read it in the init() of the class.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="HebrewTextView">
<attr name="android:text"/>
<attr name="android:textColor"/>
<attr name="android:lines"/>
<attr name="android:textSize"/>
<attr name="android:singleLine"/>
</declare-styleable>
</resources>

After that is done all I had to do is to run over the problematic layouts and replace TextView with HebrewTextView and if it was a TextView that is referenced by the code, change the casting.
The method getTextView() was defined in the control class so that sections in the code that changes the text and/or other TextView properties will work after adding the .getTextView() postfix.

Hope that helps.



Related Topics



Leave a reply



Submit