Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set LAYOUTGRAVITY for a TextView in Android?

How do I set a TextView's layout_gravity by code?

I tried with layoutparams without any good result.

new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, Gravity.LEFT | Gravity.CENTER_VERTICAL); 

After using this my text just stays where it is standardly, at the top left corner. Neither the width: FILL_PARENT or the gravity codes do anything.

like image 435
Adam Varhegyi Avatar asked Dec 07 '22 14:12

Adam Varhegyi


2 Answers

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.LEFT | Gravity.CENTER_VERTICAL)
like image 167
Dawid Sajdak Avatar answered Dec 29 '22 19:12

Dawid Sajdak


u can use gravity in code by using below code :

    Button button = (Button) findViewById(R.id.MyButtonId);
    // need to cast to LinearLayout.LayoutParams to access the gravity field
    LayoutParams params = (LayoutParams)button.getLayoutParams(); 
    params.gravity = Gravity.BOTTOM;
    button.setLayoutParams(params);
like image 23
Nikhil Lamba Avatar answered Dec 29 '22 17:12

Nikhil Lamba