Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically center TextView text

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?

like image 782
Alex Summers Avatar asked Oct 08 '12 05:10

Alex Summers


People also ask

How do you center text in TextView?

android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.

How can I center text programmatically in android?

To center align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “center” in layout file, or programmatically set the textAlignment property of the TextView object with View. TEXT_ALIGNMENT_CENTER in activity file.

How do I center text in a text box in android Studio?

To align text in TextView at the center vertically and horizontally we need to make use of gravity attribute. Example 1 : You can use center_vertical | center_horizontal value to set the text at the center of the TextView area.

How do you center text in XML?

Show activity on this post. android:gravity="center_horizontal" for align text Center horizontally. android:gravity="center_vertical" for align text Center vertically. android:gravity="center" for align text Center both vertically and horizontally.


1 Answers

yourTextView.setGravity(Gravity.CENTER); 
like image 67
Syn3sthete Avatar answered Oct 23 '22 06:10

Syn3sthete