Buttons spacing - android

I'm trying to set the margin of the buttons to 0, (so no spacing between the buttons).
Basically, I want my buttons to look something like that(with the following style and colors):
Any idea how can I accomplish this kind of task? I do not want to create a 9 patch image by myself (since I don't have any knowledge doing that).

In this specific case, you can do this task easily with XMLs.
This is how you can achieve it in two steps:
Step 1
Create 3 shapes in drawable folder:
First shape is for the left button: shape_button_left.xml. This shape has radial left corners and gradient background.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#BFBFBF" >
</stroke>
<corners
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp" >
</corners>
<gradient android:startColor="#D2D2D2" android:endColor="#F2F2F2" android:angle="90"/>
</shape>
Second shape is for the center button: shape_button_center.xml. This shape doesn't define anything for corners and also has gradient background.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#BFBFBF" >
</stroke>
<gradient android:startColor="#D2D2D2" android:endColor="#F2F2F2" android:angle="90"/>
</shape>
Third shape is for the right button: shape_button_right.xml. This shape has radial right corners and gradient background.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#BFBFBF" >
</stroke>
<corners
android:bottomRightRadius="10dp"
android:topRightRadius="10dp" >
</corners>
<gradient android:startColor="#D2D2D2" android:endColor="#F2F2F2" android:angle="90"/>
</shape>
Step 2
Now, we can use these shapes in simple views to get the effect of buttons.
In your layout XML add the next code:
<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" >
<!-- Button Left -->
<LinearLayout
android:id="#+id/button_left"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#drawable/shape_button_left"
android:gravity="center"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left"
android:textColor="#333333"
android:textSize="20sp" />
</LinearLayout>
<!-- End Button Left -->
<!-- Button Center -->
<LinearLayout
android:id="#+id/button_center"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#drawable/shape_button_center"
android:gravity="center"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Center"
android:textColor="#333333"
android:textSize="20sp" />
</LinearLayout>
<!-- End Button Center -->
<!-- Button Right -->
<LinearLayout
android:id="#+id/button_right"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#drawable/shape_button_right"
android:gravity="center"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right"
android:textColor="#333333"
android:textSize="20sp" />
</LinearLayout>
<!-- End Button Right -->
</LinearLayout>
That's it
Now, you can add onClick listener in your code to LinearLayouts and work with it like a button.
Testing this code on my mobile gives next result:

Any idea how can I accomplish this kind of task? I do not want to create a 9 patch image by myself (since I don't have any knowledge doing that).
I'm afraid you may not have much choice. The inherent spacing found in between each button is a result of extra transparent pixels built directly into the existing 9-patch backgrounds that the framework uses. To replace this behavior you must set the background of the buttons to a different Drawable that doesn't include this inherent spacing.
Another option would be for you that could be done in code is to create XML drawable shapes to use for each background. You can create an individual shape that has corner radii, a fill gradient, and a stroke just like your image. You can read more about creating XML Drawables in the docs.

Related

Android Button not filling full space in RelativeLayout

I am trying to create a button, aligned to the bottom of the page. I want it to fill 100% space, from left, right and bottom. Whatever attributes I will choose, there are still very small non covered areas. Can you please help?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.MainActivity">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_margin="0dp"
android:padding="0dp"
android:text="search"
/>
</RelativeLayout>
Here is the screenshot, showing not filled areas
Create a file in drawable folder like this, say simple_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="rectangle">
<!-- Normal color -->
<solid android:color="#android:color/white" />
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<!-- Color - when the view is pressed -->
<solid android:color="#android:color/black"/>
</shape>
</item>
</selector>
Now simply set the background as the above created drawable.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#drawable/simple_selector"
android:text="search"
/>
The above code can change color in both normal and pressed states of the button. You can change the colors as per your requirement.
The space you see is the shadow around the button in its background drawable. You need to create your own background drawable and it should then completely occupy the width.
Or, you could also just set the background to null.
android:background="#null"

How to make the Borders of a custom button in android

I want to achieve something like this :
I want to make the button transparent Which I have successfully done, now tell me how can I show the line on the upper border of the button and between the two button. How can I handle this. my xml of button is simply goes like this
<LinearLayout
android:id="#+id/buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_alignParentBottom="true"
android:weightSum="2">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=" Send Message"
android:layout_weight="1"
android:background="#android:color/transparent"
android:textColor="#ff4444"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_weight="1"
android:background="#android:color/transparent"
android:textColor="#ff4444"/>
</LinearLayout>
So How can I achieve the borders like this as shown in picture below.
pardon me , for the small size picture as I have this only single image to clear my question .
If you would like to add separator line, please add this:
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"/>
I would use realitveLayout instad of LinearLayout, so you can set the position of the separators more quickly. You are going to have 2 separators, one is the horizontal, with layout_width="match_parent" and one between the elements.
Android draw a Horizontal line between views
Android Drawing Separator/Divider Line in Layout?
You can define your own shape and add to the Button, as background:
use it as R.drawable.myshape:
place it to res/drawable/myshape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFEEE"
android:endColor="#00FFFF"
android:angle="270" />
<corners android:radius="5dp" />
<stroke android:width="7px" android:color="#EE0FF0" />
</shape>
if you would like to be transparent as well, try this:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.4"
android:useLevel="false" >
<solid android:color="#android:color/transparent" />
<stroke
android:width="4dp"
android:color="#android:color/darker_gray" />
</shape>

Make ImageView or its parent circular from left side

I have a very basic layout as follows. I'm just using a red background in the ImageView just to depict the situation.
<?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="wrap_content"
android:background="#drawable/custom_bg_grey_stroke_radius"
android:orientation="vertical" >
<ImageView
android:id="#+id/IV"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:background="#ff0000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:layout_toRightOf="#+id/IV" />
</RelativeLayout>
This is the custom background I'm using:
custom_bg_grey_stroke_radius.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- background color of the entire rectangle -->
<solid android:color="#FFFFFF" />
<!-- color of borders -->
<stroke
android:width="1dp"
android:color="#c1c1c1" />
<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp" />
</shape>
I found various links which make the images circular but from all the 4 corners.
This link tells how to make image circular from top or bottom.
Android round a Layouts background image, only top or bottom corners
Can someone please figure out how to make Image circular from the left.
It would be good if I could specify the exact radius. I want it just barely circular around 5dp.
And, I apologize for the repetitiveness, but I'm just not able to work this out.

round shape in button is not properly displaying

for background of first button i have used shape which is below
1) left_round_shape_button.xml
<solid android:color="#0084ff" />
<corners
android:radius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:bottomRightRadius="0dp"
android:topRightRadius="0dp" />
</shape>
for setting background of second button i have used below code
2) right_round_shape_button.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- you can use any color you want I used here gray color -->
<solid android:color="#0084ff" />
<corners
android:radius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:bottomRightRadius="0dp"
android:topRightRadius="0dp" />
</shape>
Now when i set this into buttons
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<Button
android:id="#+id/btnSticker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/left_round_shape_button"
android:text="Sticker"
android:textColor="#android:color/white" />
<Button
android:id="#+id/btnMySticker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/right_round_shape_button"
android:text="My Sticker" />
</LinearLayout>
but these are not looking proper :(
why first button is coming with all round shape and why second is not coming :(
from android docs i have read following
Note: Every corner must (initially) be provided a corner radius greater than 1, or else no
corners are rounded. If you want specific corners to not be rounded, a work-around is to use
android:radius to set a default corner radius greater than 1, but then override each and every
corner with the values you really want, providing zero ("0dp") where you don't want rounded corners.
Finally i got the solution, it's problem with XML rendering, when u run your app, you will get proper output,

How to make a view transparent as a function of length in android

I have a frame layout (semi transparent black), on which I have written "Swiss Chalet - score a free side" and "Missisauga, ON". It is the same color/transparency from top to bottom, I want it to be more opaque towards the bottom and transparent towards the top, so that just above "Swiss Chalet" line it feels the the frame layout is merging with the background image. How do I implement that?
Right now my code is,
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/frameLayout2"
android:layout_alignParentLeft="true"
android:background="#5A000000">
<TextView
android:id="#+id/campaignNameLabel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="2"
android:ellipsize="end"
android:text="Loading..."
android:layout_marginLeft="10dp"
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</FrameLayout>
You can use a GradientDrawable in your FrameLayout background to achieve what you want.
You can define a gradient in a xml file like this:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="270"
android:startColor="#5A000000"
android:endColor="#FF000000" />
</shape>
Or in your java code:
int[] colors = {0x5A000000, 0xFF000000};
GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
You can do something like that using a gradient background for the FrameLayout as follows:
Step 1) Create a gradent_background_selector.xml as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="270"
android:startColor="#00000000"
android:centerColor="#44000000"
android:endColor="#5A000000"
android:type="linear" />
</shape>
</item>
</selector>
Step 2) Apply it to you FrameLayout as follows:
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/frameLayout2"
android:layout_alignParentLeft="true"
android:background="#drawable/gradent_background_selector">
<TextView
android:id="#+id/campaignNameLabel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="2"
android:ellipsize="end"
android:text="Loading..."
android:layout_marginLeft="10dp"
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</FrameLayout>
Step 3) Play around with the values of startColor, centerColor and endColor to get the desired output.
Note: I used three color gradient because you mention "just above "Swiss Chalet" line it feels the the frame layout is merging with the background image". To get more precise results, you may want to read about multi-gradient shapes
Create your gradient selector in res/darawable like:
gradient_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#5A000000"
android:endColor="#00000000"
android:type="linear" />
</shape>
</item>
</selector>
And use it as background of your layout:
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/frameLayout2"
android:layout_alignParentLeft="true"
android:background="#drawable/gradient_selector">
</FrameLayout>

Categories

Resources