Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, December 12, 2015

Defining a percentage width for a LinearLayout?

Defining a percentage width for a LinearLayout?


I want to define a percentage width (70%) for a LinearLayout that contains some buttons, so that I can center it and so that the child buttons can fill_parent. Here's a picture showing what I mean:

example

My current layout looks like this:

                                                                            

The LinearLayout im referring to has the id: linearLayout1. How do I do this?

Answer by Lyrkan for Defining a percentage width for a LinearLayout?


You can't define width/height/margins/... using percents in your XML. But what you would want to use is the "weight" attribute, which is, IMO, the most similar thing.

Another method would be to set the sizes programmatically after you inflate the layout in your code, by getting the size of your screen and calculating needed margins.

Answer by Emiam for Defining a percentage width for a LinearLayout?


You have to set the weight property of your elements. Create three RelativeLayouts as children to your LinearLayout and set weights 0.15, 0.70, 0.15. Then add your buttons to the second RelativeLayout(the one with weight 0.70).

Like this:

                                                                                                                                                         

Why are the weights 0.15, 0.7 and 0.15? Because the total weight is 1 and 0.7 is 70% of the total.

Result:

enter image description here

Edit: Thanks to @SimonVeloper for pointing out that the orientation should be horizontal and not vertical and to @Andrew for pointing out that weights can be decimals instead of integers.

Answer by mobile app Beginner for Defining a percentage width for a LinearLayout?


Hope this can help

                                                                                

(1) Set layout_width to "0dip" (2) Set the layout_height to .xx (% you want)

Answer by Hissain for Defining a percentage width for a LinearLayout?


*You can use layout_weight properties. If you want to take 70% of parent width you must set child layout_width property value 0dp, like android:layout_width="0dp"*

Answer by Hamlet Kraskian for Defining a percentage width for a LinearLayout?


I think Emiam's approach is right. But also agree with Simon Veloper (one of commentators of Emiam's answer) : When we need the buttons in 70% of width, we should use horizontal orientation for Linearlayout and set width of each Relativelayout to 0dip and their weight as specified So I suggest this version :

                                  //This is where you add buttons. You can make them "fill_parent".                      

Answer by EnigmaSAC for Defining a percentage width for a LinearLayout?


I solved a similar issue applying some padding to the LinearLayout like this:

            

This probably won't give you an exact percentage but can be easily graduated and avoid extra unnecessary layout elements.

Answer by taha for Defining a percentage width for a LinearLayout?


i know another solution, that work with weight:

                      

Answer by Danny Teo for Defining a percentage width for a LinearLayout?


As a latest update to android in 2015, Google has included percent support library

com.android.support:percent:23.1.0

You can refer to this site for example of using it

https://github.com/JulienGenoud/android-percent-support-lib-sample

Answer by Niv for Defining a percentage width for a LinearLayout?


Google introduced new API called PercentRelativeLayout

Add compile dependency like

compile 'com.android.support:percent:22.2.0'

in that PercentRelativeLayout is what we can do percentagewise layout


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 71

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.