Programmatically center TextView text

AndroidTextview

Android Problem Overview


I beg some leniency here, I'm just starting with the Android SDK tutorials and I'm attempting something out of interest that's not in the tutorial itself, but I would hope would be easy.

I am trying to center a TextView item via code horizontally and vertically (I can do it in XML just fine). I've seen several examples of how to do this when the parent is a table or some other object, but I hope this would be easier for me to grasp. (p.s. Feel free to correct my terminology).

Here is the example code from the tutorial / my working model:

package com.example.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;


public class DisplayMessageActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    	Intent intent = getIntent();
    	String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    	
    	TextView textView = new TextView(this);
    	textView.setTextSize(40);
    	textView.setText(message);
    	textView.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
	
    	setContentView(textView);
    }
}

I've managed to locate the setGravity method, and I've tried to dabble in the setLayoutParams for it, but I'm not sure what the scope is for it as I can't locate what I should be importing to get the WRAP_CONTENT constant to resolve. From what I understood, centering and content_wrapping+gravity are two separate things. I'd like an example of how to do both in this case and maybe how/where I would have found the answer in the API documentation?

Android Solutions


Solution 1 - Android

yourTextView.setGravity(Gravity.CENTER);

Solution 2 - Android

For dynamically center

textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

Solution 3 - Android

this will work for sure..

RelativeLayout layout = new RelativeLayout(R.layout.your_layour); 
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
textView.setLayoutParams(params);
textView.setGravity(Gravity.CENTER);

layout.addView(textView);

setcontentView(layout);

Solution 4 - Android

TextView text = new TextView(this);
	
text.setGravity(Gravity.CENTER);

and

text.setGravity(Gravity.TOP);

and

text.setGravity(Gravity.BOTTOM);

and

text.setGravity(Gravity.LEFT);

and

text.setGravity(Gravity.RIGHT);

and

text.setGravity(Gravity.CENTER_VERTICAL);

and

text.setGravity(Gravity.CENTER_HORIZONTAL);

And More Also Avaliable

Solution 5 - Android

Try adding the following code for applying the layout params to the TextView

LayoutParams lp = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(LinearLayout.CENTER_IN_PARENT);
textView.setLayoutParams(lp);

Solution 6 - Android

if your text size is small, you should make the width of your text view to be "fill_parent". After that, you can set your TextView Gravity to center :

TextView textView = new TextView(this);
textView.setText(message);
textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER);

Solution 7 - Android

You can use the following to programmatically center TextView text in Kotlin:

textview.gravity = Gravity.CENTER

Solution 8 - Android

try this method

  public void centerTextView(LinearLayout linearLayout) {
    TextView textView = new TextView(context);
    textView.setText(context.getString(R.string.no_records));
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(18.0f);
    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    linearLayout.addView(textView);
}

Solution 9 - Android

These two need to go together for it to work. Been scratching my head for a while.

numberView.textAlignment = View.TEXT_ALIGNMENT_CENTER
 numberView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex SummersView Question on Stackoverflow
Solution 1 - AndroidSyn3stheteView Answer on Stackoverflow
Solution 2 - AndroidNirav RanparaView Answer on Stackoverflow
Solution 3 - AndroidKalpesh LakhaniView Answer on Stackoverflow
Solution 4 - AndroidNaveen TamrakarView Answer on Stackoverflow
Solution 5 - AndroidAntrrometView Answer on Stackoverflow
Solution 6 - Androidarif ardiansyahView Answer on Stackoverflow
Solution 7 - AndroidAdsView Answer on Stackoverflow
Solution 8 - Androidsaigopi.meView Answer on Stackoverflow
Solution 9 - AndroidthesamoanppprogrammerView Answer on Stackoverflow