Select item from xml to apply it on a button - java

Here's what I have :
Buttonshape.xml :
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF008AB8"/>
<stroke android:color="#0299D0"/>
<corners android:radius="15dp"/>
</shape>
</item>
<item android:drawable="#drawable/b_popcorn"></item></layer-list>
BlueFragment.xml :
<Button
android:id="#+id/BtePlay" ...
android:background="#drawable/Buttonshape"
/>
Everything is working greate, my button corner is rounded and it apply the drawable, but I have a question :
Is it possible to add more items in my buttonshape.xml and select a specific drawable in my BlueFragment.xml
For example doing something like this :
Buttonshape.xml :
...
<item android:drawable="#drawable/b_popcorn"></item></layer-list>
<item android:drawable="#drawable/b_2"></item></layer-list>
and in my BlueFragment.xml :
<Button
android:id="#+id/BtePlay" ...
android:background="#drawable/Buttonshape|b_popcorn"
/>
<Button
android:id="#+id/BtePlay2" ...
android:background="#drawable/Buttonshape|b_2"
/>
Something like this to set only one drawable for each button instead of making 50+ xml files for each buttons ...
Thanks

we have only option that is using selector, but its only depend on view state
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_checked" android:state_checked="true"/>
<item android:drawable="#drawable/bg_unchecked" android:state_checked="false"/>
</selector>
so I think we can not do any thing like you expected, so sorry for that !

Related

state_pressed remains true while onClick is running

I want to set a different background color for a button only while the button is being touched by the user.
But that color remains that way during the whole time onClick for that button is running even though I am no longer touching the button
Please help me find a solution to this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#777777" />
<item android:color="#android:color/black" />
</selector>
Assuming your on-click method takes a long time, You should add a second item for stating the case of the button not being pressed:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#777777" />
<item android:state_pressed="false" android:color="#000000"/>
<item android:color="#android:color/black" />
</selector>

my application ignores any changes in background color of buttons

i'm trying to customize my alert dialog. but i can not change the background color of buttons.
i tried drawable button designing, style, setting direct color in xml and also setting color by java codes in the activity but i could not change it.
the shape of the button changes but the color only can be either the default color or transparent color.
the xml coding of button itself:
<Button
android:id="#+id/btnDenied"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="35dp"
android:layout_marginBottom="15dp"
android:background="#drawable/button_selected" //// or #drawable/button_costume
android:text="#string/button_no" />
the selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed = "true">
<shape>
<solid android:color="#FFEB3B"/>
</shape>
</item>
<item android:state_pressed = "false">
<shape>
<solid android:color="#FFC107"/>
</shape>
</item>
</selector>
and another drawable file:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="50dp"/>
<solid
android:color="#color/colorHeader"/>
</shape>
and i also tried this style:
<style name="Button_Style"
parent="#android:style/Widget.Button">
<item name="android:textSize">14sp</item>
<item name="android:background">#color/colorHeader</item>
<item name="android:textColor">#1B1B22</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_margin">2dp</item>
</style>
but non of them could change the color of the button.
The code that I have written is 100 percent correct, but sometimes Android Studio does some wrong tasks, so solve these issues either by running the program again, or doing the following steps:
Clean Project , From the build menu .
Rebuild Project .
And I got this problem with me not to show errors in xml file,
I hope this thing helped you.

How can I add shadow to ImageButton?

this is my first time posting. And I've been using this website as a great resource.
But I have come upon an issue. I've tried applying a shadow effect to my ImageButton but it doest work? What I'm trying to achieve is the shadow you can see on the floating bubble (Material Design). I have a circle ImageButton, but I wish to add a shadow around it. How can I achieve that?
This is circle_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#C62666"/>
</shape>
This is circle_button_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#E92D6C"/>
</shape>
This is my selector which combines both drawable's into one. button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/circle_button"
android:state_focused="true"
android:state_pressed="true" />
<item android:drawable="#drawable/circle_button_pressed"
android:state_focused="false"
android:state_pressed="true" />
<item android:drawable="#drawable/circle_button_pressed"
android:state_focused="true" />
<item android:drawable="#drawable/circle_button"
android:state_focused="false"
android:state_pressed="false" />
</selector>
And this is how I apply it to my ImageButton
<ImageButton
android:id="#+id/btn_apply"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/scrollView1"
android:layout_marginRight="45dp"
android:layout_marginTop="94dp"
android:adjustViewBounds="true"
android:background="#drawable/button"
android:scaleType="fitCenter"
android:src="#drawable/apply_two" />
So again? How can I add a shadow to my button? Thanks for your time.
may help you :
You can use a Button:
<ImageButton
android:id="#+id/fab"
android:background="#drawable/ripple"
android:stateListAnimator="#anim/anim"
android:src="#drawable/ic_action_add"
android:elevation="4dp"
/>
where the ic_action_add is your icon.
drawable/ripple.xml is:
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight">
<item>
<shape android:shape="oval">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
anim/anim.xml is:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="true">
<objectAnimator
android:duration="#android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="#dimen/button_elevation"
android:valueTo="#dimen/button_press_elevation"
android:valueType="floatType" />
</item>
<item>
<objectAnimator
android:duration="#android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="#dimen/button_press_elevation"
android:valueTo="#dimen/button_elevation"
android:valueType="floatType" />
</item>
</selector>
Dimens.xml is :
<resources>
<dimen name="fab_size">56dp</dimen>
<dimen name="button_elevation">2dp</dimen>
<dimen name="button_press_elevation">4dp</dimen>
With the elevation attribute you should set the Outline via code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutfab);
//Outline
int size = getResources().getDimensionPixelSize(R.dimen.fab_size);
Outline outline = new Outline();
outline.setOval(0, 0, size, size);
findViewById(R.id.fab).setOutline(outline);
}
}

Changing checkbox style to Cross - Android [duplicate]

This question already has answers here:
How to change the color of a CheckBox?
(26 answers)
Closed 9 years ago.
I have been coding an Android Application, and using CheckBox for GUI.
Generally, the style of CheckBox is a green colored tick, but I want to make it red colored cross.
How to do it.? Please Help.
You can set any color of tick mark of Checkbox. but you need to make the custom checkbox for that.
Look here and here.
Source : Android: Set color of CheckBox
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="false">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/blue_dark" />
<gradient android:endColor="#color/white" android:startColor="#color/blue_dark" android:type="sweep" />
<size android:height="30dp" android:width="30dp" />
</shape>
</item>
<item android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="true">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/white" />
<gradient android:endColor="#color/white" android:startColor="#color/blue_dark" android:type="sweep" />
<size android:height="30dp" android:width="30dp" />
</shape>
</item>
<item android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="false">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/blue_dark" />
<size android:height="30dp" android:width="30dp" />
</shape>
</item>
<item android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="true">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/white" />
<size android:height="30dp" android:width="30dp" />
</shape>
</item>
Take a Custom Drawable selector with one image for unchecked state and another for checked state and set it as drawable resource for your Checkbox
Example:
Create a xml file in res/drawable folder
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="#drawable/crossimagecheckbox" />
<item android:state_checked="false" android:drawable="#drawable/uncheckecheckbox" />
</selector>
You need to create a selector for checkbox.
in layout checkbox add the following:
android:background="#drawable/checkbox_selector"
in checkbox_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="#drawable/normal_picture" />
<item android:state_checked="true"
android:drawable="#drawable/checked_picture" />
</selector>
Now create normal_picture and checked_picture according to your requirement.
I believe this link is highly relevant. Read Jean's answer:
Checkboxes being children of Button you can just give your checkbox a
background image with several states as described here, under "Button
style":
http://developer.android.com/reference/android/widget/Button.html
...and exemplified here:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Android Button vs TextView - Hyperlink like behavior

On an Android View, I want to have a some clickable text that works similar to a webpage hyperlink.
It looks just like normal text (no button border), but when touched, I want the text color to change and I may also want its background to turn a reverse color.
Is it better to use a Button with a transparent background or a TextView. When should I choose one over the other?
Thanks much
you can use Button and make selector for background and text both.
textselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#0000ff"/> <!-- pressed -->
<item android:state_focused="true" android:color="#android:color/white"/> <!-- focused -->
<item android:color="#android:color/white"/> <!-- default -->
</selector>
buttonselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#android:color/white"/> <!-- pressed -->
<item android:state_focused="true" android:color="#0000ff"/> <!-- focused -->
<item android:color="#0000ff"/> <!-- default -->
</selector>
In your layout xml
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonselector"
android:text="some text"
android:textColor="#drawable/textselector" />
hotveryspicy gave a great answer and really pointed me in the right direction. Thank you, but I had a few issues with the response.
What I want is a button that has Black text and a Gray background in its normal state and a Red background with White text in its selected state. Like this:
Using hotveryspicy's suggestions I had the following issues:
I get an error when trying to assign a drawable to textColor. Funny but I see other references to doing this sort of thing. Maybe the XML validation is stricter now?
I also got an error if I didn't define a android:drawable in the buttonselector.xml. It seems you can set the color of a drawable in a selector eiter, you need to actually create a drawable.
Anyhow this is what I did:
I created a Color State Resource that looks like this:
File: res/colors/link_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#ffffff"/>
<item android:color="#000000"/>
</selector>
Then the following 3 drawables:
My button background in its normal state using a Shape Drawable:
File: res/drawable/link_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#cccccc"/>
</shape>
My button background in its selected state, also a Shape Drawable:
File: res/drawable/link_button_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ff0000"/>
</shape>
Then I created a Drawable Selector that chooses between the 2 shapes.
File: res/drawable/link_button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/link_button_selected"
android:state_pressed="true" />
<item
android:drawable="#drawable/link_button" />
</selector>
Then in my layout my button looks like this:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Button"
android:textColor="#color/link_button"
android:background="#drawable/link_button_selector"
/>
Thanks for the help everyone!
Add String like
<string name="link">'Google'</string>
In your layout
<TextView
android:id="#+id/link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:linksClickable="true"
android:text="#string/link" />
You can linkify your text using the linkify class.
Have a look at this and this with few examples
For Google's sake:
Rather than make a button look like a link why not make a TextView clickable? This was effectively what I wanted and worked for my use. I wanted text to be clickable to spawn an intent.
TextView.setOnClickListener(new View.OnClickListener()

Categories