18

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.

GAAAN
  • 399
  • 2
  • 3
  • 14

6 Answers6

30

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);
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • 1
    Thanks for the reply! Thank you all! I used all the answers and got code that works for me. – GAAAN Jan 12 '15 at 15:08
6
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)     
saimOneFcs
  • 198
  • 1
  • 11
5
LinearLayout layout = (LinearLayout)findViewById(R.id.ll);
LayoutParams params = (LayoutParams) layout.getLayoutParams();
params.height = 100;
params.width = 100;
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
  • Thanks for the reply! I am new in programming. I tried this already. I can't get rid of the error. I Chose almost all...(http://f-picture.net/lfp/s017.radikal.ru/i410/1501/ce/d36745f64384.png/htm) Which of them is correct? – GAAAN Jan 12 '15 at 13:37
  • the first one, android.widget.linearlayout – Murtaza Khursheed Hussain Jan 12 '15 at 13:40
  • there is a thingk in android know as log cat, which shows error and debug messages, when you app crashed it gives reason for that. open logcat and search for red content. – Murtaza Khursheed Hussain Jan 12 '15 at 13:51
  • Thank you! The application started, but now my container is not opened. Rather, it opens, but its width is equal to 0. You can try to insert your code in the source code that I left in my question, just remember to replace TextView (android:layout_width="260dp") на (android:layout_width="wrap_content") – GAAAN Jan 12 '15 at 14:13
2

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.

Saulo Aguiar
  • 509
  • 6
  • 12
1

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();
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
0
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);
Kapil Parmar
  • 881
  • 8
  • 19