I'm trying to add a textView
to a frameLayout
. The TextView
has wrap_content
properties, so it grows when the text grows. I'm adding it to a FrameLayout
with 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>
The textView always appears in the top left corner of the screen. I'd like to position it in the middle, or top middle. How can this be done?
I looked over the answers in How can I dynamically set the position of view in Android? and they weren't much help :( Mainly caused crashes..
Setting the gravity programatically definitely works Please set the layout_gravity to right in the xml TextView.setGravity(Gravity.RIGHT);
Try this:
Change Gravity to suit your need.
BTW, the frame layout should use
fill_parent
for width and heightTry to gravity or Layout gravity to your Framelayout or the Textview. It makes your Layout to the center of the screen. Use like this,
or
Set
FrameLayout
propertiesandroid:layout_width
andandroid:layout_height
tofill_parent
and also setTextView
propertyLayoutGravity
. such asCenter
,Right
orTop
.Thanks
Add
android:layout_gravity="center"
in your frame layoutyou can try it by set the gravity of text use it like-
othervise by
xml
set gravity to center so it will display you in center and if you'll usecenterinparents
so it will always apperas at the center of screen.