Are you faced to set height and width of Relative layout and Linear layout programmtically in android, here is the simple solution to set height and width of layouts.
First, we have to get height and width of screen and then do these things
// getting screen size of mobile or tablet devices
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Or
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Or
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.heightPixels;
metrics.widthPixels;
Linear Layout:
--------------
final LinearLayout linearLeft = (LinearLayout) findViewById(R.id.left);
LayoutParams params = linearLeft .getLayoutParams();
// Changes the height and width to the specified *pixels*
params.width = screenWidth / 2;
Relative Layout
---------------------
final RelativeLayout relavtiveLeft = (RelativeLayout) findViewById(R.id.right);
LayoutParams homeLayoutsparams = relavtiveLeft .getLayoutParams();
homeLayoutsparams.width = screenWidth / 2;
Happy coding !!!
Android Tutorials
Home
Android Layouts
Android Tutorials
Programmatically set height and width of Relative and Linear Layout in android
Great collection and thanks for sharing this info with us. Waiting for more like this.
ReplyDeleteBest AngularJS Training in Chennai
Angularjs Training institute in Chennai
AngularJS Training in Chennai
Angular 2 Training in Chennai
Angular 7 Training in Chennai
PHP Training in Chennai
Web Designing course in Chennai
AngularJS Training in Porur
AngularJS Training in Tambaram
AngularJS Training in Adyar
This was an excellent post and very good information provided, Thanks for sharing.
ReplyDeletePython Training in Chennai
Python Training in Bangalore
Python Training in Coimbatore
Python course in bangalore
angular training in bangalore
Web Designing Course in bangalore
python training in hyderabad
Best Python Training in Bangalore
python training in marathahalli
Python Classes in Bangalore