GAAAN
GAAAN

Reputation: 399

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.

Upvotes: 18

Views: 44524

Answers (6)

Md Imran Choudhury
Md Imran Choudhury

Reputation: 9997

The easy way to do that, set value of layoutParams, and please notice this size is not in DP.

view.layoutParams.width = 400;
view.layoutParams.height = 150;
view.requestLayout();

Upvotes: 1

Saulo Aguiar
Saulo Aguiar

Reputation: 519

The above answers are correct. I'll just suggest future readers to take a look at their xml layout file and be sure that the LinearLayout element is not using weight attribute. If so, consider updating weight in your LayoutParams object. Another good practice is to retrieve the layout params object from the view you're trying to adjust, instead of creating one from scratch and having to set all the values.

Upvotes: 2

Kapil Parmar
Kapil Parmar

Reputation: 911

LinearLayout YOUR_LinearLayout =(LinearLayout)findViewById(R.id.YOUR_LinearLayout)
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                    /*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
                   /*height*/ 100,
                   /*weight*/ 1.0f
                    );
                    YOUR_LinearLayout.setLayoutParams(param);

Upvotes: 0

Muhammed Refaat
Muhammed Refaat

Reputation: 9103

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);

Upvotes: 31

saimOneFcs
saimOneFcs

Reputation: 308

LinearLayout linear = (LinearLayout) findViewById(R.id.ll);    
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linear.setLayoutParams(params);

As you can see, you can set Integer values for the LinearLayout.LayoutParams() constructor, like this:

LinearLayout.LayoutParams cellParams = new LinearLayout.LayoutParams(0, 100);   

The costructor wants pixels and not dp(Density Pixels), here's a formula to convert PXs from DPs:

(int) (<numberOfDPs> * getContext().getResources().getDisplayMetrics().density + 0.5f)     

Upvotes: 6

Murtaza Khursheed Hussain
Murtaza Khursheed Hussain

Reputation: 15336

LinearLayout layout = (LinearLayout)findViewById(R.id.ll);
LayoutParams params = (LayoutParams) layout.getLayoutParams();
params.height = 100;
params.width = 100;

Upvotes: 5

Related Questions