My question is very easy. I have layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#drawable/mobile_background"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="fill_parent"
>
<Button
android:text="GET MESSAGES"
android:
android:id="#+id/buttonMessages"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:text="REGISTRATION"
android:id="#+id/buttonRegistration"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:text="SETTINGS"
android:id="#+id/buttonSettings"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
But I need to set non-fixed size for button - I need to set size 30% of screen width. How can I do it?
Try nesting your buttons in horizontal LinearLayouts with another view to the side for padding. Assign both your button and view android:layout_width="0dp", then give your button android:layout_weight="3" and the view android:layout_weight="7". This will give your buttons 30% and the empty view 70%.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonMessages"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="GET MESSAGES" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="7" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonRegistration"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="REGISTRATION" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="7" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonSettings"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="SETTINGS" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="7" />
</LinearLayout>
</LinearLayout>
With three buttons it's hard to set 30-70 :) I'll show you how to do it with two, you'll get it.
If you use fill_parent for width, then the space will be distributed in reciprocal proportions. So you should set a weight of 7 for the smaller button, and 3 for the larger one. With more buttons it's trickier, but you can do the math: 1/x + 1/y + 1/z = 1 and you'll have equations for the proportions, in a case of 30%, 20%, 50%:
1/x / 3 = 1/y / 2 = 1/z / 5 Or you can just try and set values that look good :)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#drawable/mobile_background"
android:layout_width="fill_parent"
android:gravity="center"/>
<Button
android:text="Narrow button"
android:
android:id="#+id/buttonMessages"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="7"/>
<Button
android:text="Wide button"
android:id="#+id/buttonRegistration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"/>
</LinearLayout>
This can't be done in the XML. You would have to specify the size in the code. I would do that in the onCreate() of the activity:
obj.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,0.25));
This means "use 100% of the parent's height but 25% of the parent's width".
source
Related
I created linearlayout with two buttons in center (buttons 1 and 2), but I can't manage it to create the view 3 on top, which I need to make it fill whole width and adjust it's height by it (keep proportions). How do I create third view?
Image:
Code:
<?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:background="#drawable/bg"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content" >
<ImageButton
android:background="#drawable/btn_one"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0" />
<ImageButton
android:background="#drawable/btn_second"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0" />
</LinearLayout>
I would recommend you use relative layouts, they are the closest thing in android to an "absolute layout" replace the button tags with Image button tags
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="215dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignStart="#+id/button2" />
</RelativeLayout>
try this code set height and width what size of image button you need and simple look what you need.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:orientation="vertical"
android:gravity="center">
<ImageButton
android:layout_width="200dp"
android:layout_height="50dp"/>
<ImageButton
android:layout_width="200dp"
android:layout_height="50dp" />
</LinearLayout>
Im having a grid view that has blank space between two rows. I'm trying to remove the white space but without success. So can anyone tell me how can I remove the white spacing?
Here is my xml:
<?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:background="#android:color/white"
android:orientation="vertical" >
<GridView
android:id="#+id/bg_chooser_grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"/>
</LinearLayout>
here is gridItem xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_item_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:scaleType="centerInside"
android:src="#drawable/ic_launcher" />
The "important part of the adapter":
gridImg = (ImageView) vi.findViewById(R.id.grid_item_img);
gridImg.setImageResource(data.get(position));
And here is how it looks:
Try This Code :-
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="90dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>
and customGridAdapter xml view:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#drawable/grid_border"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/item_image"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="center"
android:layout_margin="10dp"
android:scaleType="fitXY"
android:src="#drawable/home" >
</ImageView>
</LinearLayout>
This image use in LinearLayout android:background="#drawable/grid_border"
Please have a look at the following code
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayResult" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/zodiac1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/aries"
/>
<TextView
android:id="#+id/orTxt"
android:text="Or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/zodiac2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gemini"
/>
</LinearLayout>
</ScrollView>
I need to center everything inside the LinearLayout. I must use scrollview because when the app is 100% done, there will be lot of information which cannot be viewed by a single glance, without scrolling. How can make these center? Please help!
UPDATE
use any layout inside the scroll, no prob. But I need those elements to be in the same order (one after another) and centered.
you need to set gravity as centered for linear layout
< LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" >
This one worked for me:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
Try using layout_gravity on your LinearLayout to center_horizontal:
<LinearLayout
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayResult"
scrollbars="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/zodiac1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/aries"
/>
<TextView
android:id="#+id/orTxt"
android:text="Or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/zodiac2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gemini"
/>
</LinearLayout>
</ScrollView>
#If you want to Linear layout in a center you need to write android:gravity="center_horizontal" in your linear layout.
I figured out how to get the menu bar on top of the screen. It stays in position while I move around the main menu I created using expandableListView. However, I would like the menu bar to be on the bottom of the screen instead of the top. I played around with relativeView and got the bar on the bottom, however, the main menu disappears.
Here is my main xml file
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonbefore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="back" />
<Button
android:id="#+id/buttonnext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="Next" />
</LinearLayout>
<ExpandableListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/main_no_items" />
Then I used this for java to get it to appear in my program.
LinearLayout bottomMenu = (LinearLayout)findViewById(R.id.buttons);
I tried playing around with the gravity and it did nothing. I can add more code from my program if you think it will help.
I'm assuming your buttons LinearLayout is the 'menu bar' you're referring to?
You can actually accomplish what you're looking for using either a RelativeLayout or a LinearLayout.
LinearLayout:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<ExpandableListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/main_no_items" />
</LinearLayout>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonbefore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="back" />
<Button
android:id="#+id/buttonnext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" />
</LinearLayout>
</LinearLayout>
The buttons are bottom-aligned by setting the gravity, while the list is told to take up all other available space by giving it a weight of '1' and setting a height of '0dp'. That will cause it to stretch as much as possible without pushing the buttons off-screen.
RelativeLayout:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/buttons"
android:orientation="vertical" >
<ExpandableListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/main_no_items" />
</LinearLayout>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonbefore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="back" />
<Button
android:id="#+id/buttonnext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" />
</LinearLayout>
</RelativeLayout>
The buttons are bottom-aligned by setting the alignment on the parent. The list is told to fill all height, but stay 'above' the buttons.
On a general note: in order for this to work, I'm pretty sure that the list and empty views need to be combined together in their own layout.
The way to align the buttons to the bottom is to change your top level LinearLayout into a RelativeLayout. Then you can add the attribute android:layout_alignParentBottom="true" to your LinearLayout #+id/buttons, which holds your two buttons.
I want 2 buttons to appear in the center of the screen, but at the bottom of the screen..
Here is what I have so far.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableLayout android:layout_width="match_parent"
android:id="#+id/tableLayout2"
android:layout_gravity="center"
android:layout_height="fill_parent"
android:stretchColumns="#string/app_name">
<TableRow android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:text="History"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_width="fill_parent"
android:id="#+id/button1"
></Button>
<Button android:text="RecordSpending"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/button2"
android:gravity="center_vertical"></Button>
</TableRow>
</TableLayout>
</LinearLayout>
I want this whole layout to appear at the bottom of the screen..(no matter what phone resolution the user has)..
I want the two columns to be equal in size and the bottoms located in the center horizontally.
To put two buttons at the bottom of the screen you can do this, psuedo code add relevant attributes.
<!-- This is the parent layout -->
<RelativeLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent" >
<!-- Layout for your buttons at bottom -->
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true">
<!-- weight = 1 would mean both buttons are of equal width -->
<Button
android:layout_height="wrap_parent"
android:layout_width="0dp"
android:layout_weight="1.0" />
<Button
android:layout_height="wrap_parent"
android:layout_width="0dp"
android:layout_weight="1.0" />
</LinearLayout>
</RelativeLayout>