Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a Textview to a FrameLayout in a determined position

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

like image 629
n00b programmer Avatar asked Jun 18 '12 06:06

n00b programmer


1 Answers

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

like image 142
xandy Avatar answered Nov 20 '22 10:11

xandy