Styling bottom buttons android - android

I have two buttons in bottom of screen, but how can I style stock buttons to style of my concept:
Here my code layout file:
<?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"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_width="180sp"
android:text="Далее"
android:layout_weight="1"
android:layout_gravity="bottom"
android:layout_alignTop="#+id/button2"
android:layout_alignParentRight="true">
</Button>
<Button
android:id="#+id/button2"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="Пропустить"
android:layout_weight="1"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true">
</Button>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Добро пожаловать!"
android:id="#+id/textView"
android:layout_marginTop="29dp"
android:fontFamily="sans-serif-thin"
android:phoneNumber="false"
android:textSize="33sp"
android:textColor="#color/text_color"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="25dp" />
</RelativeLayout>

There is multiple ways to do this when building an android application.
The first and simplest is to set your background to an image. To do this add a image to your drawable folders in the res folder, add the correct size images to each drawable folder to correct display across multiple platforms. Once you have imported your image simply put the following line in the layout xml for your button
android:background = "#drawable/imported_image"
The second is to create an selector android XML file. This will let you set the background image for different states of the buttons. I usually create a file under my res folder in my project called drawable and put the xml file there. The selector file would look something like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/large_button_on" />
<item android:drawable="#drawable/large_button_off" />
</selector>
the code to put in your Layout xml inside your button should be
android:background="#drawable/custom_button"
The final way without using any images is to define a shape android xml file for the button. This will instruct how you want the button to be drawn on screen. This xml should be placed in your project the same way we did with the other.
?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#000000"
android:endColor="#0000ff"
android:angle="270" />
<stroke android:width="1dp" android:color="#ffffff" />
</shape>
again if you use this method put the following in your layout xml for your button
android:background="#drawable/custom_button"
Hope these methods of styling help.

You can create an drawable xml file like this and have a different drawable for the different states of you button
res/drawable/my_button_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/button_focused"
android:state_focused="true" />
<item android:drawable="#drawable/button_default" />
</selector>
Then in your button's declaration add the line:
android:background="#drawable/my_button_drawable" />
Android Developer Buttons

Related

Android switch custom left-right drawables

I'm trying to create a custom switch like this:
What I feel like I need is something like a left/right drawable each in a green/white state, or alternatively a green outline with a drawable for when the choice should be selected.
What I don't understand in posts like this is how all the sample drawables provided slant to the right, and yet the 'on' button slants to the left.
I'm trying with the following 'thumb' drawable.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/choice_right"/>
<item android:drawable="#drawable/choice_left"/>
</selector>
but it seems to cut the ends off the drawables. If I also set a track to something like this:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#color/text_input"/>
<corners
android:radius="1dp"
android:bottomLeftRadius="4.5dp"
android:bottomRightRadius="4.5dp"
android:topLeftRadius="4.5dp"
android:topRightRadius="4.5dp" >
</corners>
</shape>
then all I get is a thin line. So, I'm not sure what to try next.
This cannot be with styling alone, this needs custom view implementation. Thats lot of code so my suggestion for you would be to use 3rd party libraries like
https://github.com/hoang8f/android-segmented-control
This library is tried and tested so its safe to use it instead of rewriting it.
I have created an example app that you can find here that should solve your problem. You can have a look at this library that has a proper implementation of the segment control.
Basically I created a custom control that extends LinearLayout with a default weightSum of 2. Two Buttons are added with a weight of 1 and some logic is applied to toggle between them. On toggle an custom event is fired that is built on this interface.
public interface SwitchToggleListener {
void onSwitchToggle(SwitchToggleState switchToggleState);
}
I used drawable resources to style the buttons.
The final result looked like this
you can used RadioGroup with two RadioButton:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<RadioButton
android:id="#+id/male_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#drawable/selector_radio_btn_text"
android:background="#drawable/selector_radio_btn_left_bg"
android:gravity="center"
android:button="#null"
android:text="#string/male_radio_text"
/>
<RadioButton
android:id="#+id/female_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#drawable/selector_radio_btn_text"
android:background="#drawable/selector_radio_btn_right_bg"
android:gravity="center"
android:button="#null"
android:text="#string/female_radio_text"/>
</RadioGroup>
where selector_radio_btn_text.xml is text color selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#color/checkbox_text_color_checked"/>
<item android:state_checked="false" android:color="#color/checkbox_text_color"/>
and selector_radio_btn_left_bg(right bg).xml is backgorund of left and right side
Instead of his using any external libaray or support library this can be done easily by using RadioGroup and radio buttons. As "Dimitry " has mentioned in his answer I too have used the same thing to achieve.
Here is the copy paste of my answer given for the question How to custom switch button?
<RadioGroup
android:checkedButton="#+id/offer"
android:id="#+id/toggle"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="#dimen/margin_medium"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="#dimen/margin_medium"
android:background="#drawable/pink_out_line"
android:orientation="horizontal">
<RadioButton
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:id="#+id/search"
android:background="#drawable/toggle_widget_background"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="#null"
android:gravity="center"
android:text="Search"
android:textColor="#color/white" />
<RadioButton
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:id="#+id/offer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/toggle_widget_background"
android:button="#null"
android:gravity="center"
android:text="Offers"
android:textColor="#color/white" />
</RadioGroup>
pink_out_line.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="#80000000" />
<stroke
android:width="1dp"
android:color="#color/pink" />
</shape>
toggle_widget_background.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/pink" android:state_checked="true" />
<item android:drawable="#color/dark_pink" android:state_pressed="true" />
<item android:drawable="#color/transparent" />
</selector>
Update answer
to do this you should create layer-list in drawable folder
res/drawable/toggle_layerlist.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/choice_right">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#color/text_input"/>
<corners
android:radius="1dp"
android:bottomLeftRadius="4.5dp"
android:bottomRightRadius="4.5dp"
android:topLeftRadius="4.5dp"
android:topRightRadius="4.5dp" >
</corners>
</shape>
</item>
</layer-list>
add layer list to your selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/toggle_layerlist" android:state_checked="true" />
</selector>
use this selector as background , but make sure you empty text in on/off state
by set android:textOff="" android:textOn=""
<ToggleButton
android:id="#+id/chkState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/toggle_selector"
android:textOff=""
android:textOn=""/>
do the same to other state

Change Color onClick and background on Focus

i have a ListView in my Navigation Drawer and want it looks like the Playstore App or Youtube App.
Means onClick changing FontColor for Example to red and when i just hold it the background should turn grey.
Do i have to use 2 onitemclicklistener?
<?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="48dp">
<ImageView
android:id="#+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="#string/DrawerListIcon"
android:src="#drawable/ic_home"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/icon"
android:textSize="14sp"
android:textStyle="bold"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/DrawerListFontColor"
android:gravity="center_vertical"
android:paddingRight="40dp"
android:paddingLeft="0dp"/>
</RelativeLayout>
Tried with Item and Selector. First thing Font Color don't change when i pressed the Button. Second the background of the Relative Layout turns blue insead of Grey.
drawer_list_font.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:color="#color/DrawerListFontColorPressed"
/>
<item android:color="#color/DrawerListFontColor"/></selector>
drawer_list_background.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:color="#color/DrawerListBackgroundFocused"
/>
<item
android:color="#color/DrawerBackground"
/></selector>
I had to use android:drawable instead of background on the relative Layout. On the Textview Android Stuio accepted android:color.
This is changed by changing view from ur java file at every click event u can change the view. U have to make multiple view ie xml files for that. Which are dynamically changed or u can also send parameters to a function and create an xml at runtime but that will be too deep for u
No need for listeners at all.
A better approach here is to create drawable XML's with definitions for every state ("pressed", "focused", etc).
Check out Color State List resources
For example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false"android:color="#80000" />
<item android:state_focused="true" android:state_pressed="true" android:color="#ff0000" />
<item android:state_focused="false" android:state_pressed="true" android:color="#ff0000" />
<item android:color="#color/DrawerListFontColor" />
</selector>

Change spinner background

I have created a spinner which looks like
My layout file is:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/round_corners"
android:padding="7dip" >
<Spinner
android:id="#+id/select_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:prompt="#string/selectCity" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="14dp"
android:text="#string/City"
android:textColor="#color/dark_grey"
android:textSize="18dp" />
</RelativeLayout>
What I want is to make the background of the spinner transparent but still show the downward arrow. Is there any way that I can do this?
Create xml file and place it in drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/slim_spinner_pressed" />
<item android:drawable="#drawable/slim_spinner_normal" />
</selector>
.
<Spinner ..... android:background="#drawable/selector_slim_spinner" ..... />
For more Information Check this Change Spinner Style In Android
You should create a slector and give that selector as background to your spinner
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/..." />
<item android:drawable="#drawable/..." />
</selector>
you can use this resource Android Holo Colors Generator to generate android components like Android 4.0

making imagebutton changed when presses

i want to make image button, so when it pressed i will replase the not-pressed image to pressed image of the button, and then go to other activity. i have two different images in drawable.
i have two xml files:
first one- load the main activity: background and image button
<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="fill_parent"
android:background="#android:color/black"
android:orientation="vertical" >
<ImageButton
android:id="#+id/b1_l"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/s1"
android:scaleType="centerCrop"
android:background="#color/trans"
android:src="#drawable/b1_l" />
</RelativeLayout>
second one:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="#drawable/image_not_pressed" />
<item android:state_pressed="true"
android:drawable="#drawable/image_pressed"" />
</selector>
what is the code that i need to write in addition to my xml?
or can i pass the xml and write it only as code...?
thanks!
you need to usually have drawable for pressed and default(unpressed) state. You can have the xml file like this(below) for that. You need to put this xml file in the drawable folder and then use as src for ImageButton or background source in case of Button.
Lets name it is mybutton.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/your image name" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/your image name" />
<item android:drawable="#drawable/your image name" />
</selector>
You layout file for main activity:
<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="fill_parent"
android:background="#android:color/black"
android:orientation="vertical" >
<ImageButton
android:id="#+id/b1_l"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/s1"
android:scaleType="centerCrop"
android:background="#color/trans"
android:src="#drawable/mybutton" />
</RelativeLayout>
You migth want do check out android developer guide on this
http://developer.android.com/guide/topics/ui/controls/button.html

Android PNG transparent image on background

I have a problem with changing button background image.
So i have 2 images (png-24 transparent background).
Images are a simple circle and the same circle but with some outerglow ).
I have the following code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ticketing_row_topup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#layout/pressed_topup_button"
android:orientation="horizontal" >
<TextView
android:id="#+id/topup_name"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:gravity="center_vertical|right"
android:paddingRight="5dip"
android:paddingTop="22dip"
android:text="€"
android:textColor="#color/black"
android:textSize="35sp"
android:textStyle="bold" />
<TextView
android:id="#+id/topup_display_amount"
android:layout_width="90dp"
android:layout_height="match_parent"
android:gravity="center_vertical|left"
android:textSize="65sp"
android:textColor="#0d457f"
android:textStyle="bold" />
</LinearLayout>
And the pressed_topup_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:state_enabled="false">
<item android:state_pressed="true" android:state_enabled="true" android:drawable="#drawable/button_topup_glow" />
<item android:state_focused="true" android:state_enabled="true" android:drawable="#drawable/button_topup" />
<item android:state_enabled="true" android:drawable="#drawable/button_topup" />
</selector>
The LinearLayout is used to populate a GridView in each cell.
When i click the cell the behavior is correct (the glow is displayed) but also a yellow color is display on the corner of the image (where suppose to be transparent). How can i fix that?
Thanks
You need to add
android:listSelector="#android:color/transparent"
to your GridView declaration.
See this:
Android: Disable highlighting in GridView

Categories

Resources