Android Button Panel - android

I am creating an android app, and need a navigation panel at the bottom of the screen, which the user can use to navigate through the app, and skip any steps they don't wish to make, for example they could just select steps 5 and 10 and leave the rest out. This will be a panel will 7 buttons across by 3 buttons down, giving 21 buttons. The XML code is below;
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:weightSum="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_below="#+id/button2"
android:weightSum="7">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="01" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="02" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="03" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="04" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="05" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="06" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="07" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_below="#+id/button2"
android:weightSum="7">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="08" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="09" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="11" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="12" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="13" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="14" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_below="#+id/button2"
android:weightSum="7">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="15" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="16" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="17" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="18" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="19" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="20" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Previous"
android:text="21" />
</LinearLayout>
</LinearLayout>
The are approximately 50 classes (or different screens in this app), and this button panel needs to be present on each of them. Each button will also have to have an onClickListener to tell it what to do.
My question relates to 2 points, the first in relation to the XML file.
As this 'button panel' will be present on every screen, is there a way of creating a master XML file that can be used to create this panel on every screen, rather than having to re write identical code 50 times in one app-which seems highly in efficient.
The second points relates to the onClickListener. Again, is it possible to create a master listener that will apply to this button panel every time it appears. I will make an master listener class that will handle the events, but without a proper master class, each individual class will have to have the listener set for every individual button in every class-which again seems highly inefficient as there will be significant repetition of identical code in 50 classes.
I have considered using ice cream sandwich, but this does not quite seem to meet my needs.

Create a Re-usable Layout
Create a separate Navigation panel layout.
Suppose this is your navigation panel layout ( panelbar.xml)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:background="#color/footerbar_bg">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gafricalogo" />
Just Include it in all your XML
Now include your navigation panel in each XML like this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="#color/app_bg"
android:gravity="center_horizontal">
<include layout="#layout/panelbar"/>
<TextView android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:text="#string/hello"
android:padding="10dp" />
...
source : For more detail info

You can create an xml file with button panel separately, and use include tag in other xmls like this:
<include
android:id="#+id/panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
layout="#layout/button_panel" />
As far as onClickListener is concerned, you can check if this post helps you.

Related

Android layout - How to make the controls appear to the left/right without padding?

I have defined 3 controls on my screen, placed in the following order:
button
- textView
- button
I want to place the first button on the left side and place the textView in the middle. The second button should be on the right side.
However, I cannot find a way to force the buttons to stick to the left/right side and for the textView too stick to the middle.
This is the code:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<<"
android:id="#+id/button2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/TodayDataTextView"
android:text="Today Data"
android:textAlignment="center"
android:textColor="#0000ff"
android:background="#00000000"
android:layout_margin="5dp"
android:textSize="20dp"
android:textStyle="bold|italic" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">>"
android:id="#+id/button" />
</LinearLayout>
And this is the screenshot:
And this is what I want to have (without the padding):
Try this code :
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<<"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/TodayDataTextView"
android:gravity="center_vertical"
android:text="Today Data"
android:textAlignment="center"
android:textColor="#0000ff"
android:background="#00000000"
android:textSize="20dp"
android:textStyle="bold|italic"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">>"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
Output :
set layoutgravity and gravity as left, center and right respectively for all the three widgets
You can try a RelativeLayout. Like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<<"
android:layout_alignParentLeft="true"
android:id="#+id/button2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/TodayDataTextView"
android:text="Today Data"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:textColor="#0000ff"
android:background="#00000000"
android:layout_margin="5dp"
android:textSize="20dp"
android:textStyle="bold|italic" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">>"
android:layout_alignParentRight="true"
android:id="#+id/button" />
</RelativeLayout>
Please use a Relative Layout instead of a LinearLayout.
Add to Button
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
Add to TextView
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
Add to the Right Button
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
You could use layout weights to get exactly what you are looking for:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<<"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Today Data"
android:textAlignment="center"
android:textStyle="bold|italic"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<<"
android:id="#+id/button"
/>
Remember that Android makes two passes when determining the size and position of the components in a layout. The first pass uses the layout_width and layout_height settings. In this case it is wrap_content for most of the widgets which will make them their "natural" size to fit their content. The second pass uses the layout_weight attribute to allocate any remaining pixels. Given that this is a horizontal linear layout, those pixels will be allocate to the horizontal size of the widgets. Only the TextView specifies a weight attribute, so it will get 100% of the remaining pixels.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:gravity="left">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<<" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.60"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="data"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:gravity="right">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<<" />
</LinearLayout>
</LinearLayout>
This is also working
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="0sp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:text="<<"
android:id="#+id/button2" />
<TextView
android:layout_width="0sp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:id="#+id/TodayDataTextView"
android:text="Today Data"
android:textAlignment="center"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#0000ff"
android:background="#00000000"
android:layout_margin="5dp"
android:textSize="20dp"
android:textStyle="bold|italic" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="0sp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:text=">>"
android:id="#+id/button" />
</LinearLayout>

Working with relative layout in android XML

I am trying to create a layout in XML something along of the lines of http://i.stack.imgur.com/aPoeU.png but I am little confused as to how to position the buttons in that format. I have created the buttons and tried different things like alignParentBottom, alignParentRight etc but I can't seem to get it the way I want it to be. Can someone please help me out?
<RelativeLayout 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=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button1"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button2"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button3"
android:text="Button" />
We can manage it in all screen and in all resolution by weight_sum
Paste below code in your xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000"
android:weightSum="1" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bebebe"
android:layout_weight="0.30"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_weight="0.20"
android:background="#bebebe"
android:text="Button2" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_weight="0.20"
android:background="#bebebe"
android:text="Button3" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_weight="0.30"
android:background="#bebebe"
android:text="Button4" />
</LinearLayout>
Paste it in your XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2258A2"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
android:weightSum="1" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.40"
android:background="#bebebe"
android:text="Button1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:layout_weight="0.20"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="0.50"
android:background="#bebebe"
android:text="Button2" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:background="#bebebe"
android:text="Button3" />
</LinearLayout>
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:layout_weight="0.40"
android:background="#bebebe"
android:text="Button4" />
</LinearLayout>
Your code seems correct, but if you want to replicate the size of the buttons then you can't set the height as wrap_content. This will make it as tall as the text, change that to some given value:
android:layout_height="50dp"
Well, in RelativeLayout, as the name suggests, the elements are positioned relative to something else, like the parent view, or another widget in the layout. So you can align the widget in many many ways, like apending it to the right of a view, to the left, above, below, and so on.
Please refer to this link and this other link for more information.
So, if you want to make your buttons look exactly like your example image, try this:
<RelativeLayout 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=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Button 1" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button1"
android:text="Button 2" />
<Button
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button2"
android:text="Button 3" />
<Button
android:id="#+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button3"
android:text="Button 4" />
You can simply make the button width to fill_parent instead of align left and right. And the height, you can define the measures you'd like to use.
Btw, I used alignParentLeft and alignParentStart because I don't know which API level you're using, so I've used both parameters, old and new. But the effect is the same, your buttons will always start to the left of the parent view.
EDIT: As the user hasternet suggested, you could also use LinearLayout and get similar results.
EDIT 2: And if you like to change the look of your buttons, try to change the style (Take a look here to know what I mean).
Since you need to set the height of the buttons differently and also it depends on different screen size, you cannot achieve this with Relative Layout.
So go for LinearLayout and weightsum attribute. This way screen big or small, you can achieve your expected UI. Mentioned code below modified.
<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"
tools:context=".MainActivity"
android:Orientation="vertical"
android:weightsum="8" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button"
android:layout_weight="1"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button1"
android:text="Button"
android:layout_weight="3"
/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button2"
android:text="Button"
android:layout_weight="3"/>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button3"
android:text="Button"
android:layout_weight="1"/>
</LinearLayout>

making bottom bar in android

I am trying to make bottom bar that should look like below image. i.e., they should be separate only with line exactly like below image.
My output is like below image
My code:
<LinearLayout
android:id="#+id/buttonbar"
style="#android:style/ButtonBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#android:string/ok" />
<Button
android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#android:string/cancel" />
</LinearLayout>
I have imported an example and it's looking like the first image. But when I used the same code in my project and tested on the same emulator, it's looking like the second one. Can some one suggest what should be done to look like in the first image?
Try this code, it will give a result like the first image.
But I did some color changes only.
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/buttonbar"
style="#android:style/ButtonBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal" >
<Button
android:id="#+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:layout_weight="1"
android:text="#android:string/ok"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:layout_weight="1"
android:text="#android:string/cancel"
android:textColor="#FFFFFF" />
</LinearLayout>
Update: This is what you need.
Edited:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/buttonbar"
style="#android:style/ButtonBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal" >
<Button
android:id="#+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:layout_weight="1"
android:text="#android:string/ok"
android:textColor="#FFFFFF" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ffffff" />
<Button
android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:layout_weight="1"
android:text="#android:string/cancel"
android:textColor="#FFFFFF" />
</LinearLayout>
Increase the size of dp as much as you want to show the difference.
try this..
<Button
android:id="#+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#android:string/ok"
style="?android:attr/borderlessButtonStyle" />
<Button
android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#android:string/cancel"
style="?android:attr/borderlessButtonStyle" />
</LinearLayout>

How show buttons as if they are attached together in android

I want to show the customized buttons as if they are attached. Here is what I got so far:
<RelativeLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:orientation="horizontal"
android:scaleType="fitXY" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btntoggle_selector"
android:textColor="#android:color/white" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="62dp"
android:background="#drawable/btntoggle_selector"
/>
</RelativeLayout>
By the above code they are always showing as if there is some gap between them. How can I correct it?
How about you set the leftMargin to some negative value?
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btntoggle_selector"
android:textColor="#android:color/white" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="-5dp" //Modify this value to make them as close as possible
android:background="#drawable/btntoggle_selector"
/>

How could i divide my layout in two parts in Android

I want to dive my layout in two parts that have common design.Layout orientation is horizontal and i want to have 2 layout of that layout how could i do that .Here is my XML please suggest me how could i do that.
Copy and paste below code in your xml file, and made changes as you like,
You can ask for any help if you want
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="461dp"
android:layout_weight="1.00"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Take the references of layout objects at runtime using their id and depending upon requirement write condition in your code to show hide layouts.
For instance I have just replicated the same views everywhere, don't mind it...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Make the root layout which is a <RelativeLayout> currently as <LinearLayout>.
Set the orientation of that linear layout to horizontal.
set android:weight of all the child layouts to '1'.
use yourChildLayout.setVisibility(View.VISIBLE) to show the two layouts which are needed and yourChildLayout.setVisibility(View.GONE) to hide the rest.

Categories

Resources