I have created 4 buttons in linear layout. This is my xml.
<?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="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<Button android:id="#+id/button1"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:text="Account"
/>
<Button android:id="#+id/button2"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:text="History"
/>
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:text="Settings"
/>
</LinearLayout>
But above layout adding margins all sides for all 3 buttons. Why is that margins is added by default? How to avoid that?
Try this,it works like you want,
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button android:id="#+id/button1"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="Account"
android:background="#80ffdd10"
/>
<Button android:id="#+id/button2"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#800aad10"
android:text="History"
/>
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="Settings"
android:background="#8c9eff"
/>
</LinearLayout>
These are the shadow by default of Android Button Widget. However If you want to attach one button with another forcefully then add the following properties to each buttons as per needed
android:layout_marginRight="-5dp"
android:layout_marginLeft="-5dp"
Note : Increase or decrease the value (Currently -5dp) of the properties as it suits.
In button, set android:layout_width="0dp" rather than wrap_content
It's because you have given weight attribute to buttons in horizontal LinearLayout, each Button will fill 1/3 of the root layout horizontally.
And if you are using a vertical Linear Layout, you should set android:layout_height="0dp"
Hope this will help you.
You can use custom background, it will solve your problem
android:background="#8c9eff"
Related
I have a linear layout which contains a set of child views (mixture of text, images and other layouts).
I want to grey out the linear layout and all the child views so they look disabled (similar to how a button works).
Is this possible to achieve? I know i can set a colour filter when painting on the canvas, is there something similar for layouts?
Another way is to call setEnabled() on each child (for example if you want to do some extra check on child before disabling), check out this answer:
https://stackoverflow.com/a/7069377/4840812
You can use Frame-layout
Using TEST ONE you can achieve above text view like disabled. You can set root layout clickable false for actually disable views inside layout.
TEST TWO is for understanding purpose. Like you need to manage order of view to show effect.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--THIS IS TEST ONE-->
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="150dp"
android:background="#000000"
android:gravity="center"
android:text="TEST DATA"
android:textColor="#android:color/white"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#DAD6D6D6"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data"
android:textColor="#color/colorAccent"
android:textSize="30sp" />
</LinearLayout>
<!--THIS IS TEST TWO-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="300dp"
android:background="#DAD6D6D6"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data"
android:textColor="#color/colorAccent"
android:textSize="30sp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="400dp"
android:background="#000000"
android:gravity="center"
android:text="TEST DATA"
android:textColor="#android:color/white"
android:textSize="30dp" />
</FrameLayout>
To achieve your layout you can use the RelativeLayout. In RelativeLayout you can use the 2 different views or LinearLayout. In first layout you can add views according to your need, and In second layout you can set the alpha colorcode for disable the layout.
Please try below code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/demo_img_1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/demo_img_1"
android:layout_marginLeft="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/trans"/>
</RelativeLayout>
Here is output for code:
I have to use LinearLayout to make this little app type thing in Android Studio.
The instructions say we have to use vertical orientation for the root tag (and this text at the top says "Here are my widgets") and then horizontal orientation for each row. My only issue is how to place things on different rows?
It's supposed to look like:
Row 1: "Here are my widgets" (Vert orientation)
Row 2: BUTTON IMAGEBUTTON
Row 3: CHECKBOX SWITCH
My issue is, when I make a new nested LinearLayout, it shows up on row 2. How do I make a row 3 with horizontal orientation? I tried putting an empty LinearLayout thing with vertical orientation to maybe split the two rows up, but that didn't work. How do I make the CheckBox and switch show up beneath the buttons? I have to use LinearLayout, and the orientation has to be horizontal. Here's my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_message"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:gravity="fill"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="200dp"
android:text="#string/ButtonText" />
<ImageButton
android:gravity="fill"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="200dp"
android:src="#drawable/flag"
android:scaleType="centerInside"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Switch
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Here is your layout will look like briefly:
<LinearLayout
android:id="#+id/root_linear_layout"
android:orientation="vertical"
// other attributes... >
<TextView
// This is ROW 1!
//welcome text goes here />
<LinearLayout
// This is ROW 2!
android:orientation="horizontal">
<Button
... />
<ImageButton
.../>
</LinearLayout> <-- Closing of the Row2 horizontal LinearLayout
<LinearLayout
// This is ROW 3!
android:orientation="horizontal">
<CheckBox
... />
<Switch
.../>
</LinearLayout> <-- Closing of the Row3 horizontal LinearLayout
</LinearLayout> <-- Closing of the root vertical LinearLayout
You can fill in additional attributes (id, height, width, etc... ) depending on your needs.
I noticed you used android:layout_weight attribute to each Views. If you use this attribute, it is desirable to use android:weightSum to the Views parent layout. For example, if you need 50:50 width ratio of your Button and ImageButton:
<LinearLayout
android:weightSum="2"> <--- starting of Row2 LinearLayout
<Button
android:layout_weight="1" ... />
<ImageButton
android:layout_weight="1 ... />
</LinearLayout> <--- closing of Row2 LinearLayout
For more details about weight, see here :)
Like this:
<?xml version="1.0" encoding="utf-8"?>
<Button
android:gravity="center"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="Button one" />
<ImageButton
android:gravity="center"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp"
android:scaleType="centerInside"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="50dp"
android:text="Check Box"
android:layout_weight="1"/>
<Switch
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_weight="1"/>
</LinearLayout>
GridLayout inside a LinearLayout not showing at all and am not sure why. trying to make a simple calculator but i can't get the buttons to display.
changed height, width background color but not sure why is not displaying.
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="horizontal"
>
<TextView
android:id="#+id/main_display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123456789"
android:gravity="end"
android:background="#color/mainDisplay" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:columnCount="4"
android:rowCount="5"
>
<Button
android:id="#+id/bc"
android:text="#string/bc"
/>
<Button
android:id="#+id/bparentesis"
android:text="#string/bparentesis"
/>
<Button
android:id="#+id/b7"
android:text="#string/b7"
/>
<Button
android:id="#+id/b8"
android:text="#string/b8"
/>
<Button
android:id="#+id/b9"
android:text="#string/b9"
/>
<Button
android:id="#+id/bmulti"
android:text="#string/bmulti"
/>
<Button
android:id="#+id/b4"
android:text="#string/b4"
/>
</GridLayout>
i trying to make a 4x5 grid of buttons for a calculator
I think you should use this :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
And I think for calculator if you use horizontal linearlayout instead of gridlayout its better too.
in the other hands for per row use horizontal linearlayout.
TextView's width is match_parent change it to wrap_content it will work and show your GridLayout or
chanage your orientation of LinearLayout to vertical
I am developing an application, where I need to put two buttons at the bottom regardless of any screen resolution.
What will be best to implement this?
Use a RelativeLayout with a LinearLayout at the bottom. Just one way to do it , and use layout_weight to set buttons with equal size.
Updated
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<Button
android:id="#+id/idBtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1" />
<Button
android:id="#+id/idBtn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2" />
</LinearLayout>
set those buttons inside an new layout ( Relative / Linear - respective to your layout view ) and give android:layout_alignParentBottom="true"
Try this way to use footer for common use:
First create one footer.xml file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#id/idFooterMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/footerBtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1" />
<Button
android:id="#+id/footerBtn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button2" />
</LinearLayout>
</RelativeLayout>
And then you can use this footer.xml file in any other screen without repeating the code again.
<include layout="#layout/footer" />
Thanks.
I have been working on an app for my school recently and wanted to clean it up a bit before possibly publishing it. On the scheduling portion of the app, I have 5 buttons that perform actions on a ListView that is also on the screen at the same time. However, I have the issue when I have around 6 or more events on the screen as once the list view takes over the screen and pushes the buttons off the screen, making it so that I cannot delete the events, make new ones, and so on.
I tried setting the list view to a static size (400px) which worked for normal screen orientation, but if the phone is set to landscape view you cannot see the buttons either. With my current code it would appear to work in the XML viewer but in practice is not the case.
This is the code without the static size setting:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#551A8B"
android:textColor="#FFD700"
>
<Button android:text="#string/New"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/button3">
</Button>
<Button android:text="#string/Edit"
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/button3"
android:layout_alignTop="#id/button3">
</Button>
<Button android:text="#string/delete"
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/button4"
android:layout_alignTop="#id/button4">
</Button>
<Button android:layout_height="wrap_content"
android:id="#+id/button7"
android:layout_width="wrap_content"
android:text="#string/Previousweek"
android:layout_below="#id/button3">
</Button>
<Button android:layout_height="wrap_content"
android:id="#+id/button6"
android:layout_width="wrap_content"
android:text="#string/Next"
android:layout_below = "#id/button3"
android:layout_toRightOf = "#id/button7">
</Button>
<ListView android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#FFD700"
android:layout_below="#id/button7"
android:textSize="10sp" >
</ListView>
</RelativeLayout>
The XML viewer for this code is:
Which would lead me to believe it would work fine. I tested it on my emulator and got the following result after entering a bunch of silly events however:
This result is consistent with multiple versions of the emulator.
How I can fix this problem without using static size constraints that cause landscape orientation issues?
Separate the buttons into a separate RelativeLayout and enclose this and the ListView in a vertical LinearLayout.
Then:
<LinearLayout android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout [...]
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Your buttons -->
</RelativeLayout>
<ListView android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout
The key point here is the height and weight on the ListView. This means that it fills the remaining space in the LinearLayout after space has been correctly allocated for the buttons.
Add a android:weigth in your listView tag and set the android:weigth value to 1. This will work when your list view height and width is set to fill_parent and your list view is covering entire layout. So try it, it will work.
One simple solution would be to separate the buttons in their own relative layout and put the whole thing in a linear layout, eg:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#551A8B"
android:textColor="#FFD700">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#551A8B"
android:textColor="#FFD700">
<!-- your buttons -->
</RelativeLayout>
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#FFD700"
android:layout_below="#id/button7"
android:textSize="10sp">
</ListView>
</LinearLayout>
Use a vertical LinearLayout with two rows of Buttons (each row as a LinearLayout), then give the ListView a layout_weight value of "1". In fact, use layout_weight to clean up the size of your buttons too.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button android:text="#string/New"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:id="#+id/button3" />
<Button android:text="#string/Edit"
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button android:text="#string/Delete"
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button android:layout_height="wrap_content"
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="#string/Previousweek" />
<Button android:layout_height="wrap_content"
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="#string/Next" />
</LinearLayout>
<ListView android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textColor="#FFD700"
android:textSize="10sp" >
</ListView>