Add a Textview to a FrameLayout in a determined position
Asked Answered
E

6

7

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..

Escheat answered 18/6, 2012 at 6:1 Comment(0)
P
15

Try this:

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);

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

Change Gravity to suit your need.

BTW, the frame layout should use fill_parent for width and height

Parchment answered 18/6, 2012 at 6:7 Comment(3)
Thanks, this is the only thing that worked!! Changing the frame layout to fill_parent ruins stuff for me, it doesn't show the views that are under this view.Escheat
instead of fill_parent better to use match_parent.Kincaid
You might also use LayoutParams.WRAP_CONTENT for width property in case your textview has a small text.Affidavit
M
0

Try to gravity or Layout gravity to your Framelayout or the Textview. It makes your Layout to the center of the screen. Use like this,

android:layout_gravity="center"

or

android:gravity="center"
Mignon answered 18/6, 2012 at 6:7 Comment(1)
doesn't help, he need to insert dynamicallyParchment
L
0

you can try it by set the gravity of text use it like-

textview.setGravity(Gravity.center);

othervise by xml set gravity to center so it will display you in center and if you'll use centerinparents so it will always apperas at the center of screen.

Lawerencelawes answered 18/6, 2012 at 6:8 Comment(1)
Doesn't work. The view still appears at the top left, but the cursor is now in the middle of the viewEscheat
G
0

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

Thanks

Guenon answered 18/6, 2012 at 6:9 Comment(0)
B
0

Add android:layout_gravity="center" in your 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>
Bullish answered 18/6, 2012 at 6:19 Comment(0)
B
0

Setting the gravity programatically definitely works Please set the layout_gravity to right in the xml TextView.setGravity(Gravity.RIGHT);

Bettinabettine answered 14/1, 2015 at 5:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.