Reputation: 433
Guys I need to make a footer with three buttons in such a way that left and right button should take 25% of the screen from left and right and image which is in center should take the 50% of the screen without leaving any space between them.
It would be really great if anyone can help.
I wanted to post the image of the footer needed,but I am not allowed to share images.
Upvotes: 1
Views: 8580
Reputation: 664
Write layout XML as following.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4">
<Button android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="25%"
android:layout_weight="1"/>
<Button android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="50%"
android:layout_weight="2"/>
<Button android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="25%"
android:layout_weight="1"/>
</LinearLayout>
Linear layout should placed in a relative layout to make it to align parent bottom.
android:layout_alignParentBottom="true"
So your final layout will be similar to following
<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"
>
<!-- Root element should wrap to parent size. -->
<!-- Your view xml codes. -->
<!--Bottom bar layout should be in root element. Parent should be Relative layout so that we can always align to parent bottom-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="4">"
<Button android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="25%"
android:layout_weight="1"/>
<Button android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="50%"
android:layout_weight="2"/>
<Button android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="25%"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
Hope this fix your issue.
Upvotes: 0
Reputation: 1189
If you want custom bottombar in all activities then you need to include layout in all activities. i think below code might help you :
Create bottomlayout.xml in layout derectory and copy below code inside it and use appropriate images.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="@android:color:black"
android:gravity="center_horizontal" >
<ImageButton
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:background="@drawable/img1" />
<ImageButton
android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="2dp"
android:background="@drawable/img2" />
<ImageButton
android:id="@+id/btn_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="2dp"
android:background="@drawable/img3" />
</LinearLayout>
Include above xml in all activities at bottom like :
< LinearLayout android:id="@+id/bottombar" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:background="@android:color:black" android:gravity="center_horizontal" >
Write Below code in all .Java files where you have include bottonlayout in .xml layout file.
ImageButton btn1=(ImageButton)findViewById(R.id.btn_1);
ImageButton btn2=(ImageButton)findViewById(R.id.btn_2);
ImageButton btn3=(ImageButton)findViewById(R.id.btn_3);
btn1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// Your Code Here....
}
});
btn2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// Your Code Here....
}
});
btn3.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// Your Code Here....
}
});
Upvotes: 2
Reputation: 14622
Here it is:
<?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" >
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="4" android:layout_alignParentBottom="true">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="hello"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:text="hello"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="hello"/>
</LinearLayout>
</RelativeLayout>
This is how it looks:
Upvotes: 4