• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Set width of items in view dynamically in LinearLayout

I created a custom seekbar preference using this tutorial Android & Amir: Android Preferences. I would like to dynamically adjust the width of the seekbar based on the users screen size. Specifically, here is the code from the tutorial that sets the width:

Code:
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(80, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(30, LinearLayout.LayoutParams.WRAP_CONTENT);
I would like the values of 80 and 30 to by dynamic.

Here is what I've come up with so far but is not purely dynamic and still uses the hard coded 350.

Code:
DisplayMetrics size = getContext().getResources().getDisplayMetrics();

LinearLayout layout = new LinearLayout(getContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(size.widthPixels - 350, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
I would like to dynamically set the width of the three items that make up the seekbar (params1, params2, param3 in the tutorial) based on the width of the user's screen.
 
Back
Top Bottom