GridLayout with equal size for each element - java

I'm trying to make a layout that is laid out like this.
But I need the elements (the images) to have equal and full size to create the spacing between them and not use margins to space them out.

You can achieve the required result with android TableLayout.
Here's the developer link for TableLayout

some times grid layout is not an answer. try 3 linear layout over each other like this its simple
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#000000">
<Button
android:layout_height="60dp"
android:layout_width="match_parent"
android:background="#drawable/icon_you_should_know"
android:maxWidth="360dp"/>
<LinearLayout
android:layout_height="120dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="top|center">
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_wounded_demon"/>
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_maze_desert"/>
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_working_with_darkness"/>
</LinearLayout>
<LinearLayout
android:layout_height="120dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="top|center">
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_weak_spot"/>
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_none"/>
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_none"/>
</LinearLayout>
<LinearLayout
android:layout_height="120dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="top|center">
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_none"/>
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_none"/>
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_none"/>
</LinearLayout>
also u can remove or add a button to each line using max width and height and dont forget to add layout_weight="1" its works me hope it work for u.
and for margins u could put picture in a bigger picture without background in PNG format.

Related

How do you set a scrollView to fill the remainder of the screen with a static navbar at the bottom?

My goal is to have 2 static buttons at the bottom of the screen at all times "Back" and "Add Event." I have a scrollView that holds a list of items, currently that view just shows one row instead of all that rows that can fit on the screen. The reason I used a scrollView was because I wanted the user to be able to scroll through more options if it exceeds the screen length. Is there a way to expand the Scrollview/Listview without using DP(since that doesn't always correlate well with different screen sizes).
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<ListView
android:id="#+id/user_event_table"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left|bottom"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/back_button"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:text="back" />
<Button
android:id="#+id/add_button"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:text="add Event" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Do not nest ListView inside ScrollView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<Button
android:id="#+id/back_button"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:text="back"/>
<Button
android:id="#+id/add_button"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:text="add Event"/>
</LinearLayout>
<ListView
android:id="#+id/user_event_table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/button_layout"
android:layout_alignParentTop="true"/>
</RelativeLayout>
Here is the solution
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/user_event_table"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|bottom"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/back_button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:text="back" />
<Button
android:id="#+id/add_button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:text="add Event" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

How to align title,description and url in equal space

Here is a screenshot of layout as it currently appears on my screen:
Here is the xml code snippet for the layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/icon"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_alignParentRight="true"
android:layout_marginRight="-10dp" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="20dp"
android:fontFamily="Cabin-Regular"
android:paddingLeft="10dp"
android:textColor="#E35B5B"
android:textSize="14dp" />
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:height="44dp"
android:fontFamily="Cabin-Regular"
android:paddingLeft="10dp"
android:paddingRight="100dp"
android:textColor="#2D2D2D"
android:textSize="16dp" />
<TextView
android:id="#+id/url_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_marginTop="50dp"
android:height="15dp"
android:paddingLeft="10dp"
android:paddingRight="80dp"
android:textColor="#BDBDBD"
android:textSize="10dp" />
</RelativeLayout>
Just want to have the three textviews properly aligned and have equal space among each other.
Here is your layout,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/icon"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_alignParentRight="true"
android:layout_marginRight="-10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="110dp"
android:layout_toLeftOf="#+id/icon"
android:orientation="vertical"
android:weightSum="3">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fontFamily="Cabin-Regular"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Title"
android:textColor="#E35B5B"
android:textSize="14dp" />
<TextView
android:id="#+id/desc"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#+id/title"
android:layout_weight="1"
android:fontFamily="Cabin-Regular"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Description"
android:textColor="#2D2D2D"
android:textSize="16dp" />
<TextView
android:id="#+id/url_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#+id/title"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="URL"
android:textColor="#BDBDBD"
android:textSize="10dp" />
</LinearLayout>
</RelativeLayout>
Pawan Thakur Use the same paddingLeft for all test and layoutBelow like below
<TextView
android:id="#+id/url_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/desc"
android:height="15dp"
android:paddingRight="80dp"
android:paddingLeft="10dp"
android:text="url"
android:textColor="#BDBDBD"
android:textSize="10dp" />
here is the code. Try maximum to use LinearLayout instead of Relative. Relative layout are used in cases where 2 or more elements have to be overlapped each other. Don't include every elements in a single layout. Divide your design into different sections, and use LinearLayout for each section and within each section, include elements belonging to that section.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".25"
android:orientation="vertical"
>
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#E35B5B"
android:fontFamily="Cabin-Regular"
android:textSize="14dp"
android:layout_weight="0.33"
android:singleLine="true"
android:ellipsize="end"
android:gravity="center|left"
android:paddingLeft="10dp"
android:text="Google"
/>
<TextView
android:id="#+id/desc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#2D2D2D"
android:fontFamily="Cabin-Regular"
android:textSize="16dp"
android:layout_weight="0.33"
android:singleLine="true"
android:ellipsize="end"
android:gravity="center|left"
android:paddingLeft="10dp"
android:text="Google is a Search engine"
/>
<TextView
android:id="#+id/url_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/title"
android:textColor="#BDBDBD"
android:textSize="10dp"
android:layout_weight="0.33"
android:singleLine="true"
android:ellipsize="end"
android:gravity="center|left"
android:paddingLeft="10dp"
android:text="http://www.google.com"
/>
</LinearLayout>
<LinearLayout
android:layout_width="110dp"
android:layout_height="110dp"
>
<ImageView
android:id="#+id/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</LinearLayout>
u can use a android:weight="" to child of relative layout
Create LinearLayout for title, description and url with vertical orientation and weightsum=3 in Layout and for each of them use width=wrap_content, height=0dp, weight=1.
Okey.. First remove android:height from each TextView and in last TextView you have set android:layout_marginTop="50 remove it.
Tip : Use LinearLayout if your want to show your all view vertically or horizontally. if you want to use RelativeLayout make sure you give margin and padding properly so they look good.
Use Linear layout as parent with weightsum and equally distribute weight amongs the child layouts. ex : If android:weightsum = "3" for parent then android:layout_weight = "1" for each child layout(if you want equal space for each one of the 3 child).
ps: dont forget to mention orientation for Linearlayout.
Just Check this,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#E35B5B"
android:fontFamily="Cabin-Regular"
android:textSize="14dp"
android:text="Google"/>
<TextView
android:id="#+id/desc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#2D2D2D"
android:fontFamily="Cabin-Regular"
android:textSize="16dp"
android:margin_top="5dp"
android:text="Google is a Search engine"/>
<TextView
android:id="#+id/url_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#BDBDBD"
android:textSize="10dp"
android:margin_top="5dp"
android:text="http://www.google.com"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
</LinearLayout>
</LinearLayout>

Center button in a LinearLayout

I just want to center "CenterButton" (center of the root LinearLayout) but Androids just not cooperating..
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.00"
android:padding="5dp"
android:background="#zegerg"
android:id="#+id/LinearLayout1">
<ImageView
android:id="#+id/ImageView1"
android:layout_height="match_parent"
android:layout_width="151dp"
android:src="#mipmap/image_1"
android:contentDescription="#string/abcdef"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="#+id/CenterButton"
android:layout_gravity="center_horizontal|center_vertical|center"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text !"
android:id="#+id/TextView"
android:paddingBottom="30dp"
android:textColor="#37a52c" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ProgressBar"
android:paddingBottom="30dp"
android:progressTint="#37a52c" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_text"
android:id="#+id/Button2"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
Tried putting it at the end of the XML but doesn't work because the other elements push it aside.. not really sure what's happening.
if you want to have all three first level children to be in equally-sized columns, then you need to add to all of them the followings attribute:
android:layout_width="0dp"
android:layout_weight="1"
(don't forget the dimension "dp" to the width parameter, otherwise the IDE will complain).
...
<ImageView
android:id="#+id/ImageView1"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:src="#mipmap/image_1"
android:contentDescription="#string/abcdef"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button"
android:id="#+id/CenterButton"
android:layout_gravity="center_horizontal|center_vertical|center"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:padding="20dp">
....
EDITED:
If you want your CenterButton to overlap all the other views, then what you have to do is:
wrap everything in a relative layout with height equal to 200dp, as its first child
remove the button CenterButton from the current position and add it at the bottom of everything, still inside the relative layout, but below the linear layout
add those properties to the button to make it center in the parent
android:layout_centerInParent="true"
So finally you will have something like this (I have changed some resources):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.00"
android:padding="5dp"
android:background="#android:color/holo_orange_dark"
android:id="#+id/LinearLayout1">
<ImageView
android:id="#+id/ImageView1"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:src="#drawable/plus"
android:contentDescription="abcdef"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text !"
android:id="#+id/TextView"
android:paddingBottom="30dp"
android:textColor="#37a52c" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ProgressBar"
android:paddingBottom="30dp"
android:progressTint="#37a52c" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"
android:id="#+id/Button2"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button"
android:id="#+id/CenterButton"
android:layout_gravity="center_horizontal|center_vertical|center"/>
</RelativeLayout>
You can use weight for such purposes for child items. Make layout_width for all of them as 0dp and set layout_weight as 1.
Give the three children in the topmost linear layout a gravity of 1 and a width of 0.
It's not clear what exactly is your problem. However, here are some general notes :
To center a View or a Layout inside RelativeView use layout_centerInParent="true"
To make a Layout on top of other layouts use FrameLayout
The order of the views/layouts in your xml file is important. The one you write the last will be on the top (in our example : the FrameLayout or just the CenterButton should be written after the other views and layouts in your xml file)
To distribute the available space inside LinearLayout among its views use android:layout_weight="1" (change the value according to your needs)
you can play aground with this example :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="200dp">
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#fff"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="#+id/ImageView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:src="#mipmap/ic_launcher" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="#+id/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="30dp"
android:text="Text !"
android:textColor="#37a52c" />
<ProgressBar
android:id="#+id/ProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="30dp"
android:progressTint="#37a52c" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text_text" />
</LinearLayout>
</LinearLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<Button
android:id="#+id/CenterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical|center"
android:text="Button" />
</FrameLayout>
</RelativeLayout>

Android button width half of available width - Layouts

I have made a layout with imageview and some buttons i want the buttons to have half the available screen width and height.
This is what I have tried, my xml is as follows.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/imageidid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="#drawable/actionbar_bg" />
</LinearLayout>
<LinearLayout
android:id="#+id/1st row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/imageid"
>
<Button
android:id="#+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="row1 col1" />
<Button
android:id="#+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="row1 col2" />
</LinearLayout>
<LinearLayout
android:id="#+id/2nd row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/1st row"
>
<Button
android:id="#+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="row2 col1" />
<Button
android:id="#+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="row2 col2" />
</LinearLayout>
</RelativeLayout>
and the output looks like this.
and I want something like this.(note imageview to take 40% of screen height)
here is the code you need to achive what you want, you can just change the wight of the views to set the percentage of screen you want the view to take:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="40"
android:scaleType="centerCrop"
android:src="#drawable/actionbar_bg" />
<LinearLayout
android:id="#+id/1st row"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/imageid"
android:layout_weight="30"
android:orientation="horizontal" >
<Button
android:id="#+id/btn1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="row1 col1" />
<Button
android:id="#+id/btn2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="row1 col2" />
</LinearLayout>
<LinearLayout
android:id="#+id/2nd row"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/1st row"
android:layout_weight="30"
android:orientation="horizontal" >
<Button
android:id="#+id/btn1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="row2 col1" />
<Button
android:id="#+id/btn2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="row2 col2" />
</LinearLayout>
</LinearLayout>
Try this, notes:
according to the "hint" nested weights are bad for performance, nevertheless look at the code,
Fill_parent is deprecated, use match_parent
Your "image" is not representative of the percentages visually, in any case, it is easy to change,
Code:
<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:id="#+id/imageid"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:contentDescription="#string/description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/actionbar_bg" />
</LinearLayout>
<LinearLayout
android:id="#+id/1strow"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="horizontal" >
<Button
android:id="#+id/btn11"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/row1col1" />
<Button
android:id="#+id/btn21"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/row1col2" />
</LinearLayout>
<LinearLayout
android:id="#+id/2ndrow"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="horizontal" >
<Button
android:id="#+id/btn12"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/row2col1" />
<Button
android:id="#+id/btn22"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/row2col2" />
</LinearLayout>
</LinearLayout>
Your RelativeLayout isn't doing anything special, so change it to a LinearLayout (leave both width and height attributes as "match_parent" but add
android:orientation="vertical"
Then for each of your smaller LinearLayouts, add:
android:layout_weight=".xx"
where xx is the percentage of the large LinearLayout that you want your child layouts to use.
Background reference here, please do make sure to read it:
http://developer.android.com/guide/topics/ui/layout/linear.html
You can use LinearLayout as a main layout and then use weightsum property of linear layout

How can I make a two images sit on opposite ends using a LinearLayout?

I have 2 images in a horizontal linear layout. I want one to sit on the left and another to sit on the very right. Like this
X----------------------------X
Here is my XML
<LinearLayout
android:id="#+id/headerBarLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/batteryImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/battery" android:layout_weight="0"/>
<ImageView
android:id="#+id/exit_button"
style="#style/MotionMetrics.ExitButton"
android:src="#drawable/exit_button" android:layout_width="wrap_content"/>
</LinearLayout>
Not sure why they aren't on opposite ends.
Photo:
Use a relative layout for this. Like so:
<RelativeLayout
android:id="#+id/headerBarLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/batteryImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/battery" android:layout_weight="0"
android:layout_alignParentLeft="true"
/>
<ImageView
android:id="#+id/exit_button"
style="#style/MotionMetrics.ExitButton"
android:src="#drawable/exit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right"
/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1" android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<ImageView android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:src="#drawable/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/batteryImageView" android:layout_gravity="right"></ImageView>
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/icon" android:id="#+id/exit_button" android:layout_alignParentTop="true"></ImageView>
Set the android:layoutandroid:layout_gravity="left" and the other to right. It works I tested it.

Categories

Resources