Add a text view to FrameLayout at a specific location

I am trying to add textView to frameLayout . textView has textView properties, so it grows when text grows. I add it to frameLayout using this function:

 ((FrameLayout) findViewById(R.id.mainLayout)).addView(mEditText); 

This is the xml of the frame layout:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainLayout" android:layout_width="wrap_content" android:layout_height="wrap_content"> </FrameLayout> 

TextView always appears in the upper left corner of the screen. I would like to place it in the middle or in the middle. How can I do that?
I looked through the answers in How can I dynamically set the position of a view in Android? , and they didn’t help much :( Mostly caused by crashes ..

+7
source share
6 answers

Try the following:

 FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER); ((FrameLayout) findViewById(R.id.mainLayout)).addView(mEditText, params); 

Modify Gravity to suit your needs.

By the way, the frame layout should use fill_parent for width and height

+15
source

Try pulling gravity or gravity layout to your framelayout or textview. This makes your layout centered on the screen. Use it

 android:layout_gravity="center" 

or

 android:gravity="center" 
0
source

you can try setting the gravity of the text like this -

 textview.setGravity(Gravity.center); 

othervise xml set the value of gravity so that it displays you in the center, and if you use centerinparents so that it is always in the center of the screen.

0
source

Set the FrameLayout properties of android:layout_width and android:layout_height to fill_parent , and also set the TextView LayoutGravity property. such as Center , Right or Top .

thanks

0
source

Add android:layout_gravity="center" to the frame layout

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" > </FrameLayout> 
0
source

Setting gravity programmatically definitely works Set layout_gravity directly in xml TextView.setGravity (Gravity.RIGHT);

0
source

All Articles