I have some ImageButtons being used as a segmented control each has a background set, and the forground image will be a checkmark showing which one of the 3 is currently selected. The other 2 buttons should have no forground image. The images are defined in XML (see below).
<ImageButton
android:id="#+id/style_light_button"
android:layout_width="#dimen/style_color_segment_width"
android:layout_height="#dimen/style_button_segment_height"
android:background="#drawable/button_segmented_light"
android:src="#drawable/icons_checkmark_dark" />
<ImageButton
android:id="#+id/style_sepia_button"
android:layout_width="#dimen/style_color_segment_width"
android:layout_height="#dimen/style_button_segment_height"
android:background="#drawable/button_segmented_sepia"
android:src="#drawable/icons_checkmark_dark" />
<ImageButton
android:id="#+id/style_dark_button"
android:layout_width="#dimen/style_color_segment_width"
android:layout_height="#dimen/style_button_segment_height"
android:background="#drawable/button_segmented_dark"
android:src="#drawable/icons_checkmark_light" />
In code when one is clicked I will clear the checkmark from the 2 that were not clicked and make sure its added to the one that was clicked.
ImageButton lightModeButton = (ImageButton)findViewById(R.id.style_light_button);
ImageButton sepiaModeButton = (ImageButton)findViewById(R.id.style_sepia_button);
ImageButton darkModeButton = (ImageButton)findViewById(R.id.style_dark_button);
I have tried both the setImageBitmap(null) and setImageDrawable(null) but they both crash.
lightModeButton.setImageBitmap(null);
sepiaModeButton.setImageDrawable(null);
darkModeButton.setImageResource(R.drawable.icons_checkmark_light);
How can I clear the image, or at just hide the foreground image while leaving the background image showing?
from android documentation :
public void setBackgroundResource (int resid)
Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.
i don't know if it will work with setImageResource(int resId) so give this a try :
sepiaModeButton.setImageResource(0);
instead of
sepiaModeButton.setImageDrawable(null);
You can create a Transclude drawable for example, on you Drawable directory create file
transclude_drawable.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/transparent" />
</selector>
After that just set ImageButton.setImageResource(R.drawable.transclude_drawable);
Do not forget to change the ImageButton with your references, for example lightModeButton.
Related
I would like to define a button in Android, through XML, which, below the standard Button graphics (change color when clicked, slightly rounded edges,...), shows an image of my choice. I would like the final product to be somewhat like this:
I have tried change the src and background of an ImageButton, but it does not provide the intended effect. Could you please point me some way of achieving this?
Maybe that's not exactly what you mean by standard, but when you set a background, you end up having to recreate the behavior when the button is clicked. The best way to do it in Android is by using a selector as your button's background. Create a XML drawable with the selector in it.
Example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/button_bg_pressed" />
<item android:drawable="#drawable/button_bg_default" />
</selector>
That way, when your button is clicked, it will change the background to other image that does what you want.
EDIT:
There are other modifiers such as focused, if you need.
If you want Lollipop's ripple effects, I think you can create a Layout and put your button inside it. Change the Layout background and set the button background to transparent. If that doesn't work, try adding android:background="?android:attr/selectableItemBackground" to your button.
As Paulo said, you can achieve the click effect with a selector.
See this answer (https://stackoverflow.com/a/30192562) it gives the code for a very nice and customizable ripple effect.
how to do the button with ON/Off by clicking it must varies and along with that it shows Green for ON else RED. this is possible without graphics or must need graphics?
any one tried like this plz help me?
ToggleButton does the same. And here is tutorial which will tell how to use this.
You can use ToggleButton
If you with to use Button instead, you can make field boolean m_isOn; in your class, and in OnClickListener check this field and set button color (e.g. with setColorFilter()) and text accordingly.
Edit
Small example, if you really wish to avoid ToggleButton and using drawables:
#Override
public void onClick(View v) {
m_isOn ^= true;
((Button)v).getBackground().setColorFilter(m_isOn ? 0xFF00FF00 : 0xFFFF0000, PorterDuff.Mode.MULTIPLY);
((Button)v).setText(m_isOn ? "ON" : "OFF");
}
use ImageButton instead of button, and selector as image.
this XML file you can put into drawable directory
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_prev_active" />
<item android:drawable="#drawable/button_prev_no_active" />
In layout do this:
<ImageButton android:id="#+id/btnPrev"
android:scaleType="fitXY"
android:padding="0px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="prevClickHandler"
android:src="#drawable/button_prev_selector"/>
I have like 10+ textview embedded in a scrollview to give a ListView effect(I don't want to use a ListView here for some particular reasons).
Some details about the textview is that it has a background which is an image.
So my question is when I click on a particular textview among the 10+ views taht I have, I want to animate the background like the ones in a native ListView. If this is possible can someone provide some pointers please?
If not animating the background can we atleast animate the borders of the clicked textview?
Thanks,
Sana.
Set your TextView's background to state list selector drawable such as:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/your_pressed_background" />
<item android:drawable="#drawable/your_normal_background" />
</selector>
You save the above XML in the res/drawable directory and reference it in your TextView like an other resource.
Based on the state of your TextView, android will select the background drawable. When your TextView is pressed, the pressed background will be drawn. When it is not, your normal background will be drawn.
I am using a shape defined as a drawable as background for a TextView. This allows me to add rounded corners and other other effects.
The shape is defined like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:topLeftRadius="8dp" />
</shape>
and I am using it like this:
<TextView
android:id="#+id/project"
style="#style/textView"
android:background="#drawable/project_textview_background"
/>
Now, I need to change the color of that TextView programmatically depending on some conditions. I have not been able to do that.
I tried to do setBackgroundColor but that seems to overwrite the background I previously defined so it doesn't show the rounded corners anymore.
I looked at a bunch of other API methods but got nowhere
Any help would be very much appreciated. Thank you
Any ideas?
the solution was actually to set the shape and color in code instead of using a drawable resource.
I used PaintDrawable(int color) which allows me to define whatever background color I want. Then I used the setCornerRadoii(float[]) to define the rounded corners and finally I assigned the PaintDrawable object to my textView background. Worked like a charme.
You could make other shapes that still has that corner attribute you already defined.
The way to fill the cell with bgcolor is written in next web page.(solid)
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
And if the conditions you mentioned depend on focus or press, you'd better to make selector instead of shape. Search with keyword "ColorStateList" in android reference. I want to leave the address, but I can't due to my reputation;;;
I have a linear layout in which each row is inflated programatically and I want the rows to behave like the ListView when clicked on. That is, I want the row to highlight in the exact same way/colour that the default ListView does. How would I go about doing this?
Ok I have finally figured out how to do this...basically it is done using a selector like the color selector linked by style except instead of 'color' use a drawable for the states and you can refer to the default list drawable that is used in ListView by this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#android:drawable/list_selector_background" />
</selector>
and using this xml as the background for my View.
All the public default drawables can be found here: http://developer.android.com/reference/android/R.drawable.html
I was able to do the same with a text view that I wanted to behave like a list item by using:
<Textview
....
android:background="#android:drawable/list_selector_background"
/>
This might be a good place to start looking.
Although, i would advise you to use the ListView itself, rather than implementing it again.
if you still have a problem with that then please remember that some of UI elements are not clickable (RelativeLayout), so you have to add one more line:
<RelativeLayout
....
android:clickable="true"
...
To your listview set property
android:listSelector="#color/test"
and this test color set any transparent color you like. you can create any transparent color by using hex transparent color