I have an image button:
<ImageButton
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageButton"
android:layout_weight="0.5"
android:layout_column="2"
android:background="#null"
android:src="#drawable/gender_choice"/>
the gender_choice.xml is as follows:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/male_gender"
android:state_checked="true" />
<item
android:drawable="#drawable/female_gender"
android:state_checked="false"/>
</selector>
For some reason when I run the application and click on the button it doesn't change. Is there something I have to implement in an on click listener?
try using your drawable as background not src and see if it works that way.
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageButton"
android:layout_weight="0.5"
android:layout_column="2"
android:background="#drawable/gender_choice"/>
also try changing your drawable and use state_selected and state_pressed insteead of state_checked :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/male_gender" android:state_selected="true" />
<item android:drawable="#drawable/male_gender" android:state_pressed="true" />
<item android:drawable="#drawable/female_gender" />
</selector>
Figured it out guys, so I switched to a ToggleButton, the big issue here is that the background stretched it so it was far too big for the TableRow. Put in a FrameLayout for it though and that solved everything.
So here is my FrameLayout with the ToggleButton which makes a call to gender_choice.xml created in the res/drawable folder.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_column="2">
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/gender_choice"
android:textOn=""
android:textOff=""
android:id="#+id/toggleButton"
android:layout_gravity="center"/>
</FrameLayout>
Inside gender_choice.xml is a simple selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/male_gender"
android:state_checked="true" />
<item
android:drawable="#drawable/female_gender"
android:state_checked="false"/>
</selector>
That's basically all there was too it, I can't believe I spent like 4 hours on this.
Related
Im trying to design a custom TabWidget with FragentTabHost following these two guides for the tab layout:
https://maxalley.wordpress.com/2014/09/08/android-styling-a-tab-layout-with-fragmenttabhost-and-fragments/
http://joshclemm.com/blog/custom-android-tabs/
Im building FragmentTabHost with three fragments, the first fragment have a expandable list view.
Everything works fine, i can set backgroundcolor, the icon (if i want to) and the indicator works as well, as the divider does(if i want to) but the tabtitle does not appear for some reason and i cant find the reason why it dont show up.
If i remove tab_indicator.xml from android:background="#drawable/tab_indicator" in tab_layout.xml and set the backgroundcolor to whatever color the tab title shows up, but then i obviuosly dont get the indicators.
Do anyone have a clue whats wrong and what im missing. I have tried to solve this problem and searching för some days now and it starts to get very frustrating because its seems like something simple.
tab_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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="#drawable/tab_indicator">
<TextView
android:id="#+id/tabText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center_vertical|center_horizontal"
android:textColor="#drawable/tab_text"
android:textSize="15dp"
/>
</LinearLayout>
tab_indicator.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states-->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected" />
<!-- Focused states-->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected" />
<!-- Pressed-->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_pressed" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_pressed" />
</selector>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.app.FragmentTabHost
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tabHost"
android:layout_gravity="center_horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="35dp"
android:showDividers="middle"
android:layout_weight="0"
android:orientation="horizontal"
/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
tab_selected.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:top="-7dp" android:bottom="0dp" android:left="-7dp" android:right="-7dp">
<shape
android:shape="rectangle">
<padding android:left="10dp"
android:top="15dp"
android:right="10dp"
android:bottom="15dp" />
<stroke android:width="4dp" android:color="#FF00ccff" />
<solid android:color="#2fff00"
/>
</shape>
</item>
</layer-list>
Solved it, if i change android:layout_height in FragmentTabHost and TabWidget it displays the text, simple as that. But in my tast the height is to big ^^ I want it to be smaller like 35dp and it can be done if i change the padding in tab_selected.xml, _unselected and _pressed, and then i can change the height in TabWidget activity_main.xml else the text wont fit the tab.
I want to change my default Rating bar Star(*) icon to another icon.
Can anybody help me out..
My Code: XML
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
style="#style/foodRatingBar"
android:isIndicator="false"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp"
android:numStars="4" />
Styles.xml:
<style name="foodRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/selector</item>
<item name="android:minHeight">23dip</item>
<item name="android:maxHeight">25dip</item>
</style>
Selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#android:id/background"
android:drawable="#drawable/ic_fuel_gray"/>
<item
android:id="#android:id/secondaryProgress"
android:drawable="#drawable/ic_fuel_gray"/>
<item
android:id="#android:id/progress"
android:drawable="#drawable/ic_fuel_color"/>
</layer-list>
But outPut is:
Just add below two snippets and you are done:
<style name="customRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/custom_image1</item>
<item name="android:indeterminateDrawable">#drawable/custom_image2</item>
</style>
and Use above style in your xml where rating bar is used:
<RatingBar style="#style/customRatingBar" ... />
Add replace your Rating bar code with this and check if it is showing in preview?
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/customRatingBar"
/>
<RatingBar
android:id="#+id/ratingBar"
style="#style/foodRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:numStars="5"
android:rating="5"
android:transformPivotX="0dp"
android:transformPivotY="0dp"
android:isIndicator="false"/>
try this
i'm trying to create fully customize radio button in my app (android).
i have 3 radio button that helps the user pick size, this is why each radio button is in different size (L,M,S). i have one picture i want to be shown in each radio button in different size. when the user pick the relevant size it should change the background of it.
i'm having very hard time with it and running in with lots of problems...
this is the Radio group (i tried different methods in each button to check what is right)
*this is inside a relative layout
<RadioGroup
android:id="#+id/pizzaSizeRadioGroup"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/Largesize"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="10dp"
android:adjustViewBounds="true"
android:button="#drawable/size_selector"
android:contentDescription="#string/largesize"
android:padding="1dp"
android:scaleType="fitCenter" />
<RadioButton
android:id="#+id/Mediumsize"
android:layout_width="26dp"
android:layout_height="26dp"
android:adjustViewBounds="true"
android:background="#drawable/size_selector"
android:contentDescription="#string/mediumsize"
android:padding="3dp"
android:scaleType="fitCenter" />
<RadioButton
android:id="#+id/Smallsize"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginLeft="10dp"
android:adjustViewBounds="true"
android:button="#drawable/size_selector"
android:contentDescription="#string/smallsize"
android:padding="2dp"
android:scaleType="fitCenter" />
</RadioGroup>
and i built a selector
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/size_select"
android:state_checked="true"
android:state_pressed="true" />
<item
android:drawable="#drawable/size_select"
android:state_pressed="true" />
<item
android:drawable="#drawable/size_select"
android:state_checked="true" />
<item
android:drawable="#drawable/size_unselect" />
</selector>
and XML objects - size_select
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/pizzasize">
<shape android:shape="rectangle" >
<corners
android:radius="5dp" />
<solid
android:color="#A9D0F5" />
</shape>
</item>
</layer-list>
and size_unselect
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/pizzasize">
<shape android:shape="rectangle" >
<corners
android:radius="5dp" />
<solid
android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
this is what i get (left is L then M and then S)
I have a background set in my activity_main.xml simply as that:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background4">
And I also have this imageview:
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:layout_toLeftOf="#+id/textView5"
android:src="#drawable/contact" />
and my selector.xml looks like that:
<?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/contact_pressed"/>
<item
android:state_focused="true"
android:drawable="#drawable/contact"/>
<item android:drawable="#drawable/contact"/>
</selector>
And nothing happens on image press
try to change this:
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:layout_toLeftOf="#+id/textView5"
android:src="#drawable/contact" />
with this:
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:layout_toLeftOf="#+id/textView5"
android:src="#drawable/selector" />
Obviously your selector.xml should be into drawable folder
The selector you mention above IS a background. What View are you trying to use? Maybe use an ImageView, you can set your selector as the "background" then use the image as the "src"? Alternatively, you could use seperate overlapping views in a FrameLayout or a RelativeLayout
i am new in android development , it very difficult to design customized user interface in
android . i have a image button that have default shap is ractangle i need do it as
rounded
shap please help me .
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#82B210"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".SettingActivity" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/makeconfess" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton1"
android:layout_alignRight="#+id/imageButton1"
android:layout_below="#+id/imageButton1"
android:layout_marginTop="27dp"
android:text=" Make a\nConfess"
android:textColor="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceMedium" />
To create a circle shape of button firstly you have to create an xml in drawable folder
circle_shape_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#666666"/>
<size
android:width="120dp"
android:height="120dp"/>
</shape>
use this xml in button background as
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/circle_shape_drawable"
android:text="#string/hello_world" />
You can define a Button or ImageButton with a xml file and save it into the drawable folder. In this way you can provide different states for your button (pressed, focused, normal, ...).
For example:
<?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/btn_action_hover" />
<item android:state_selected="true" android:drawable="#drawable/btn_action_hover" />
<item android:state_focused="true" android:drawable="#drawable/btn_action_hover" />
<item android:drawable="#drawable/btn_action_normal" />
</selector>
To be sure your button will be displayed correctly on any resolution/screen size, I would suggest to use 9 patch drawables as explained in http://developer.android.com/tools/help/draw9patch.html.
in the drawable folder in the res folder
Create a android xml file and named as cbutton.xml
Then enter the bellow code
<?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/pressselected">
</item>
<item android:state_focused="true"
android:drawable="#drawable/presshighlight">
</item>
<item android:drawable="#drawable/press"></item>
</selector>
Then set this file as background of your ImageButton like in below.
<LinearLayout xmlns:android = http://schemas.android.com/apk/res/android
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Click to view Custom Button Action"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/press"
android:layout_gravity="center"
android:layout_marginTop="30dp"
/>
</LinearLayout>
xml to create a button in Android
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="#drawable/one" />
<! -- android:background property is used to apply background to a to button. And #drawable/one means there is a image name as one in your drawable folder we are applying it as background to this button. -->
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Code to define rounded corner button
<? xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> //Defines the shape of button you wants
<solid android:color="#eeffff"/>
<corners android:bottomLeftRadius="8dp" //how much curve you want from bottom left corner
android:bottomRightRadius="8dp"//how much curve you want from bottom right corner
android:topLeftRadius="8dp""//how much curve you want from top left corner
android:topRightRadius="8dp"/> "//how much curve you want from top right corner
</shape>
default shape of button is rectangle so now I will tell you how to create round button
For that you again need to create an xml file in your drawable folder, and name it as button_shape.xml
The codes for button_shape.xml are as follows:
Code to define button shapes
<? xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android=http://schemas.android.com/apk/res/android android:shape="oval" >
//There are many other shapes also available they are rectangle,ring,line and oval
<solid android:color="#ffcccc"/> //applying the colour to the button.
<stroke android:width="2dp"
android:color="#ffffff"/>
</shape>