Question related to button in android - android

Is it possible to represent dimensions for layout_width or layout_height in percentage for button in android?? if not is there any alternative so that the effect will be same???

use android:layout_width="100dip" and android:layout_height="100dip"

i believe that the best practice on changing sizes is textAppearance attribute.
the doc about it is in here: http://developer.android.com/reference/android/widget/TextView.html#attr_android:textAppearance and here: http://developer.android.com/reference/android/R.attr.html#textAppearance
so if you don't really need static size and only want it to be relatively smaller or bigger, you should use this approach.

So, you want a view to occupy i.e. 30% of parent's space? You can do the trick using linear layout with dummy view occupying 70% of space (it can be done using weights). But best approach is just write your own layout for this purpose.

As per my knowledge you cannot specify size in percentage for any layout. But there is one layout introduced in support library example called PercentageRelativeLayout:
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
app:layout_marginTopPercent="25%"
app:layout_marginLeftPercent="25%"/>
</android.support.percent.PercentRelativeLayout>
Another alternative solution is to use weightSum and layout_weight attribute for any parent and child layout. An example is shown below. This will work same as percentage and both child layouts will be alloted 50% width on screen.weightSum is used for parent layout and layout_weight is used for child layout.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/bg"
android:orientation="horizontal"
android:tileMode="repeat" >
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/bg2"
android:orientation="horizontal"
android:tileMode="repeat" >
</LinearLayout>
</LinearLayout>

Related

android:Why "layout_weight=1" makes the view align parent bottom in linearlayout? [duplicate]

This question already has answers here:
What does android:layout_weight mean?
(13 answers)
Closed 2 years ago.
The xml layout is like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bottom"/>
</LinearLayout>
And it turns out that the textview will be placed to the bottom of its parent.I think layout_weight is used to allocate unusesd space and it's very common to use layout_weight with code like :
android:layout_width="0dp"
But in this case,the first layout asks to occupy all spaces with:
android:layout_width="match_parent"
android:layout_height="match_parent
So how does layout weight work here?
PS:I have read this question: What does android:layout_weight mean? but I don't think it accounts for this question.
layout_weight specifies how much of the extra space in the layout to be allocated to the View.
The first linearlayout has already taken the whole space with attribute match_parent,why setting layout_weight enables the second view to showup at the bottom ?
I believe this is not the common usage of layout_weight.Hope somebody point out my mistake.
When you use layout_weight attribute it is used to calculate the weightage of child views of the single parent.
As you have not mentioned weight of all other views it's behaving wrong.
layout_weight is useful when you want your child views to be certain percentage of the parent view.
for example,
in parent view you need to mention:
android:weightSum="1"
So your parent view will have total weight as 1, and in both of the views you need to mention the:
android:layout_weight=".9" and android:layout_weight=".1"
so the first view will take 90%, and second view will take 10% of the space.
To be more clear Ideally the sum of weights of all the childs should be equal to the weightsum mentioned in parent, to it work as expected.
**And as you have provided android:layout_width,android:layout_height` of the textview, this is the mistake as it will make issue in the weight.
So to use weight attribute correctly you need to give other specs as 0dp in order to weight to apply successfully.**
Note: When you use weight other specs like android:layout_width,
android:layout_height should be set to 0dp.
To understand it practically, why don't you play around with below
layout:
Just Try to change the weight of linear_layout, text_view and you will see, how it's supposed to works ideally:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:background="#android:color/holo_blue_bright" >
</LinearLayout>
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="90"
android:background="#android:color/white"
android:text="bottom"
android:textColor="#android:color/black" />
</LinearLayout>
If you want to place your components in separate boxes in layout ,you should use LinearLayout.
You can define the manner of boxes place with orientation vertical or horizental.
You can define their size easily with layout_weight.
look here:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id=#+id/parent_linear>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4">
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="bottom"/>
</LinearLayout>
parent_linear divides your layout in 2 parts(because you use 2 components) vertically. Now you can set weights to child's components width. So,(for TextView) you set android:layout_width="0dp" for its width and android:layout_weight="2" ..follow it for LinearLayout- .
The result of this is parent_layout divide itself into 6 parts (2+4=6),and allocates 4 parts to LinearLayout and 2 for TextView.

Android Layout dynamic width

Is it possible to dynamically create Layouts which be 50% width, so 3rd one to be under first one? I have tried with android:layout_weight=".5", but it doesn't working.
EDIT: No proper answer by far
You can do the follwoing with a LinearLayout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:gravity="left"
android:orientation="horizontal">
<TextView
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Takes up half the width" />
</LinearLayout>
By setting the weightSum on the parent, you're saying that the weights of the children should equal that amount. By setting the single child's weight to half of that, it'll take up half the space. Make sure to set the width of the child to 0 so it knows to use the weight value to calculate its space relative to its parent.
You could use the Percent Support Library:
add the library
compile 'com.android.support:percent:23.3.0'
Example:
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
app:layout_marginTopPercent="25%"
app:layout_marginLeftPercent="25%"/>
</android.support.percent.PercentFrameLayout>

Android arrange layout with height as percentage

I am very new to android and practicing. I am trying to design a screen. which will contain a background image and a floating container with sliding menus. ( For more details please find the attached image )
My layout consists of a background image, a container with some icons which float at the bottom but with some margin to the bottom ( see attached photo )
As best of my knowledge this can be achieved by arranging a "Relative Layout" at the bottom and place images in it. Is it correct ?
Also I would likes to add a repeating transparent image as the background of the floating div.
Please give me a good advise or point me to a good tutorial
Thanks in advance
You can use LinearLayout and set the layout_weight as % in xml
As for the repeating background you can use tileMode
Example: Note that the weightSum is set to 100, to indicate the total weight will be 100. Having layout_weight=10 will give it 10% space allocation.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="bottom"
android:weightSum="100">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:background="#drawable/bg"
android:orientation="horizontal"
android:tileMode="repeat" >
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="5" />
</LinearLayout>
You can achieve this with the new percent support library:
https://developer.android.com/tools/support-library/features.html#percent
By doing something like this:
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentBottom="true"
app:layout_heightPercent="11%"
app:layout_widthPercent="100%" />
</android.support.percent.PercentRelativeLayout>
if you want to divide height in percentage then you need a linear layout with orientation horizontal and add layout_weight for each item.
linear layout guide
For your requirement any Layout is ok. You can achieve this by Linear layout too.
If I understand your requirement right, then check this discussion.
How to make android app's background image repeat
<view android:layout_width="wrap_content"
class="net.zel.percentage.PercentageButton"
android:id="#+id/button"
android:layout_height="wrap_content"
customView:percentage_width="50"
customView:percentage_height="50"
/>
so you can add the desired attributes in a library
https://github.com/metrolog3005/percentage_view

LinearLayout space distribution

I have a complex layout situation in which an horizontal LinearLayout holds two other LinearLayouts. The content for those layouts is dynamic, can be any kind of views, and is generated at runtime from different independent sources.
I want to display both of them as long as there is enough space, and limit them to 50% of the available space each otherwise. So I want those child LinearLayouts to have layout_width="wrap_content" when there is enough space, and layout_weight="0.5" when there isn't. This means that the space distribution could be 10-90, 25-75, 60-40; it would only be 50-50 when there isn't enough space to show the entire content of both views. So far I haven't find a way to do this from XML, so I'm doing it from code. My question is can I achieve what I want using only XML attributes? Will a different kind of layout be able to do it?
Here is my current layout XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="2dp" >
<LinearLayout
android:id="#+id/title_frame"
android:layout_width="wrap_content"
android:layout_height="48dp"/>
<LinearLayout
android:id="#+id/options_frame"
android:layout_width="wrap_content"
android:layout_height="48dp"/>
</LinearLayout>
Try this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="2dp" >
<LinearLayout
android:id="#+id/title_frame"
android:layout_width="wrap_content"
android:layout_height="48dp" android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:id="#+id/options_frame"
android:layout_width="wrap_content"
android:layout_height="48dp" android:layout_weight="1">
</LinearLayout>
</LinearLayout>
I tried it with textviews and this should work according to ure requirements.
It appears this cannot be achieved using only XML attributes.
Change layout_width="wrap_content" to layout_width="fill_parent" as you are using weight concept .

Android TableLayout expressed as a percentage of LinearLayout

I am trying to place a TableLayout as the only item inside a LinearLayout (It has a background icon I want to cover the whole screen) and I want to centre it and say use 60% of the width and 70% of the height for example. I have this, and it looks ok, but I am not sure it will work across many devices and am wondering whats the best, most general way to express it.
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/mylogo"
android:padding="0dip"
android:gravity="center_vertical|center_horizontal"
android:baselineAligned="false"
>
<TableLayout
android:background="#FFFF0000"
android:layout_width="200dp"
android:layout_height="250dp"
android:gravity="center_vertical|center_horizontal"
android:stretchColumns="field">
</TableLayout>
</LinearLayout>
I messed around with layout_Weights, but the only thing I managed to do was make the table disappear.
Thanks
Stephen
LinearLayout can only do child sizing by weights for one dimension but as you're only worried really about sizing a single view, you can accomplish this by nesting LinearLayouts.
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/mylogo"
android:gravity="center"
android:weightSum="1.0">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="1.0"
android:layout_weight="0.7">
<TableLayout
android:background="#FFFF0000"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:gravity="center"
android:stretchColumns="field"
android:layout_weight="0.6">
</TableLayout>
</LinearLayout>
</LinearLayout>
Whether this layout will look good across devices depends on your content. In general I tend towards reasonable paddings/margins, but again it's very dependent on content.
Try using a RelativeLayout instead of the LinearLayout, because if there is only one child in the linear, it will occupy the full space of its parent.
I think that the way you did is good. You'll probably have some differences between different devices, but you can set different layouts for other screens densities and orientations.
Using layout_weight would help only if you had more than one child inside the LinearLayout.

Categories

Resources