Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically set the width of the LinearLayout?

Is the source of the slider and the lesson link:

http://www.oodlestechnologies.com/blogs/Facebook-Style-Slide-Menu-In-Android

In this case, the width of the file "left_menu.xml" determined TextView (android:layout_width="260dp") How do I set the width to "LinearLayout" file "left_menu.xml" depending on the device screen? For example, I want the width of the "LinearLayout" was always 1/3 of the screen device? Or any way to set the width of the TextView 1/3 of the width of the device screen.

like image 789
GAAAN Avatar asked Jan 12 '15 13:01

GAAAN


People also ask

How do I change LinearLayout orientation in android programmatically?

You can't change LinearLayout 's orientation using its LayoutParams . It can be done only with a LinearLayout object.


1 Answers

To set the LinearLayout or TextView width to 1/3 of the device screen:

first of all get the device screen width:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
try {
    display.getRealSize(size);
} catch (NoSuchMethodError err) {
    display.getSize(size);
}
int width = size.x;
int height = size.y;

now just create a LayoutParams and set it to the Text:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)(width/3),
                    LinearLayout.LayoutParams.WRAP_CONTENT); // or set height to any fixed value you want

your_layout.setLayoutParams(lp);
// OR
your_textView.setLayoutParams(lp);
like image 190
Muhammed Refaat Avatar answered Oct 02 '22 06:10

Muhammed Refaat