I have a drawable named btndonate.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true" android:dither="true"
android:variablePadding="true"
>
<item android:drawable="#drawable/donate_hover"
android:state_pressed="true" />
<item android:drawable="#drawable/btn_donate"
android:state_window_focused="true" />
<item android:state_focused="true" android:drawable="#drawable/donate_hover"> </item>
<item android:state_focused="false" android:drawable="#drawable/btn_donate"></item>
</selector>
I am using it in activity's xml like
<Button android:layout_centerInParent="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#drawable/btndonate" android:id="#+id/btn_home_donate"></Button>
i am using the same button with different id's in my other activities xmls.. but it shows up in some and hidden in others i dont know y the same button does not show up n all my activities though the drawable is same for all of them .. ??
any one plz help ??
That's odd... Are you sure, you didn't make any mistakes?
Do you use the same xml file for your script?
Try using this:
Button anyButton = (Button) findViewById(R.id.anyButton);
anyButton.setVisibility(View.VISIBLE);
Hope this helps. Don't forget to mark correct when it works. ;)
Related
I am trying to customize a Button UI in Android.
I tried the following things:
btn.setBackgroundColor
btn.setBackgroundResource
btn.setBackgroundColor
But all of these are increasing the size of the Button, and because of that the Buttons near by can not be segregated (??).
Please suggest something.
If you want to apply a Hover effect then you have to do this in your XML where button layout is like
Button
android:id="#+id/xyz"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="15dp"
android:background="#drawable/general_btn_hover_effect"
android:onClick="somefunction"
android:text="#string/search_number"
android:textColor="#ffffff"
android:textSize="20sp"
/>
Notice android:background="#drawable/general_btn_hover_effect" there and then in #drawable folder make a general_btn_hover_effect.xml and write this into it
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/blank_normal_bg" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="#drawable/blank_hover_bg" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="#drawable/blank_hover_bg" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="#drawable/blank_normal_bg"/>
</selector>
Changing background color can not change size of button. Maybe you changed size in xml layout. Check again
Im trying to create custom button and i cant see my problem.
i create in res/drawable/ custom.xml
<?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/forwardpress"></item>
<item android:state_focused="true" android:drawable="#drawable/forwardhover"></item>
<item android:drawable="#drawable/forward"></item>
</selector>
and on main.xml i have this button
<Button
android:id="#+id/bFor"
android:background="#drawable/custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right" />
The problem is, button is stay presed without animation, (yes i have 3 diferent picture in drawable folder)
Funny thing is, I for one button works but the other does not
i try this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/backpress" android:state_pressed="true"/>
<item android:drawable="#drawable/back"/>
its work but for second button i try the same process
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/forwardpress" android:state_pressed="true"/>
<item android:drawable="#drawable/forward"/>
</selector>
for second button doesnt work, wierd...
Clean and build your project again.
I've been trying to customize the toggle button look but with no success.
Here is how I want it to look like:
Can someone give me a tutorial?
create toggle_selector.xml in res/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/toggle_on" android:state_checked="true"/>
<item android:drawable="#drawable/toggle_off" android:state_checked="false"/>
</selector>
apply the selector to your toggle button
<ToggleButton
android:id="#+id/chkState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/toggle_selector"
android:textOff=""
android:textOn=""/>
Note: for removing the text i used following in above code
textOff=""
textOn=""
I don't know if this is the best solution but it worked fine for me:
1.- Decide how big do you want the toggle button. In my case width 56dp and height 76dp.
2.- Create the icon set 56px-76px for mdpi, 84px-113px hdpi, same for xhdpi and xxhdpi
3.- Move the icons in the corresponding drawable folder. In my case 20 icons 5 in each folder, named ic_name1_on, ic_name1_off [...] ic_name5_off
4.- Create the following xml files in a new folder called drawable (if it does not exist yet):
ic_name1_toggle.xml
ic_name1_toggle_bg.xml
ic_name2_toggle.xml
(...)
ic_name5_toggle_bg.xml
5.- In ic_name1_toggle.xml the code must be:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false"
android:drawable="#drawable/ic_name1_off" />
<item
android:state_checked="true"
android:drawable="#drawable/ic_name1_on" />
</selector>
6.- In ic_name1_toggle_bg.xml the code must be:
<?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="#android:color/transparent" />
<item android:id="#+android:id/toggle"
android:drawable="#drawable/ic_name1_toggle" />
</layer-list>
7.- Finally in your layout.xml:
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="56dp"
android:layout_height="76dp"
android:background="#android:color/transparent"
android:button="#drawable/ic_name1_toggle_bg"
android:textOff=""
android:textOn="" />
I think you need to define a custom background for your button.
Take a look at the Developer Guide on customizing a Button's background.
However, in step Three, Create a new XML file in the res/drawable/ directory Use this Xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_da"
android:state_checked="true" />
<item android:drawable="#drawable/button_nu" />
</selector>
The element android:state_checked="true" is what defines that state as the checked background.
Let me know if this works for you.
Create selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_da" android:state_checked="true"/>
<item android:drawable="#drawable/btn_nu"/>
</selector>
and use it as background for your ToggleButton.
I have an ImageButton and I swear I have everything set up right but it is not showing the different icon when a state is pressed. Here is what I have.
ImageButton
<!-- HOME BUTTON -->
<ImageButton
android:id="#+id/home"
android:layout_width="45dp"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
android:contentDescription="Home"
android:src="#drawable/title_home"
android:layout_alignParentLeft="true"
android:onClick="onClickHome" />
Title Home
<?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/title_home_pressed"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/title_home_pressed"/>
<item android:state_focused="true" android:drawable="#drawable/title_home_pressed"/>
<item android:state_focused="false" android:state_pressed="false" android:drawable="#drawable/title_home"/>
</selector>
And yes these are all valid images, and they are different. title_home is the only one that is showing up.
I would appreciate any ideas?
Notice how you are referring to #drawable/title_home in the first block of code, but you are intending to refer to the xml file. Although in the second block of code you are referring to another #drawable/title_home which I am assuming is an image.
Because of this I can only assume that from your information your first block of code is reading the image and not the xml file. To fix this, I believe changing the names of one of them would help.
android:src="#drawable/title_home"
<item android:state_focused="false" android:state_pressed="false" android:drawable="#drawable/title_home"/>
Same name. Change your xml file name to title_home_btn_selector.
Then make the first one as
android:src="#drawable/title_home_btn_selector"
in drawable folder:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/icon2" />
<item android:drawable="#drawable/icon3" />
</selector>
and the layout is just simple linear layout that fill the whole space
<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/on_touch_"
android:weightSum="1" android:focusable="true" android:focusableInTouchMode="true">
</LinearLayout>
and when I press this nothing happens
If I add, for example, some textview and assign android:background="#drawable/on_touch_" then that textview when pressed it changes the image correctly.
Where is the problem with the linear layout why it does not change the image when pressed ?
Edit:
I am sure that my drawable selector is good and working cause I put as a background to other elements and it is working.
But my problem is how to set the drawable to the root element
Add this to your layout :
android:clickable="true"
This will set the pressed state when you'll click it.
try out
save_selector.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="false"
android:drawable="#drawable/save_hover_new" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/save_hover_new" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/save_hover_new" />
<item android:drawable="#drawable/save_new" />
</selector>
and give layout's background as selector like
android:background="#drawable/save_selector
I would make sure I do not have a drawable namesake. You seem to be using on_touch_.xml as your selector. Is there perhaps also an on_touch_.png?
I would also make sure that I am not setting the background again, to something else, or making it unclickable, in code.
I got a selector in a root element that is working fine; the background switches when pressed. It is not the root element of the activity layout, but it is the root element of the XML file.
The background is assigned with a png at first:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/panel"
>
...
I assign the background to the selector only when assigning an OnClickListener:
private void setClickListener(OnClickListener ocl) {
View boxRoot = findViewById(R.id.box_root);
if (ocl != null) {
boxRoot.setOnClickListener(ocl);
boxRoot.setBackgroundDrawable(getResources().getDrawable(R.drawable.panel_arrow_right_bgstate));
setClickable(true);
...
In my XML, I used android:clickable="true" but then I also added android:focusable="true" android:focusableInTouchMode="true" to match your case. That also worked fine, with background switching for all my four states.
// panel_arrow_right_bgstate.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_pressed="false" android:drawable="#drawable/panel_arrow_right_normal"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/panel_arrow_right_pressed"/>
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/panel_arrow_right_selected"/>
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/panel_arrow_right_pressed"/>
If this is not working for you, something else is wrong with your code, somewhere else.
When I add or remove drawables, Eclipse sometimes gets fishy and mix them up. My normal measurements are:
Clean project
Refresh res folder in Eclipse
Delete gen\com.comp.proj\R.java