I'm having an imageview which will navigate to another page once it is clicked. Right now when it is clicked user will not be getting any feel that it is clicked. So what i want is to have some effect (like in normal button) at the time of click on the image....can anyone help me?
use style="?android:borderlessButtonStyle" in the xml file.
It will show android default click effect.
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
style="?android:borderlessButtonStyle"
/>
Try a selector like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<solid android:color="#151B8D" />
<stroke android:width="1dp" android:color="#151B8D" />
<corners android:bottomLeftRadius="8dp" android:bottomRightRadius="0dp" android:topLeftRadius="0dp" android:topRightRadius="8dp" />
<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
</shape></item>
<item><shape>
<gradient android:angle="270" android:endColor="#151B8D" android:startColor="#151B8D" />
<stroke android:width="1px" android:color="#000000" />
<corners android:bottomLeftRadius="8dp" android:bottomRightRadius="0dp" android:topLeftRadius="0dp" android:topRightRadius="8dp" />
<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
</shape></item>
</selector>
Speaking about xml selector, here is the link which could you help xml selector link
Assuming you are referring to physical not visible feel, use something like this to make a sound and provide haptic feedback:
iv.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
iv.playSoundEffect(SoundEffectConstants.CLICK);
where iv is your imageview.
Related
I'm trying to get my buttons to show 3 states, normal, pressed and selected. Normal and pressed show ok, but the selected state does not. I've tried using state_focused state_activated and state_active and combinations of states set to true and false but without success.
Can buttons show the selected state? and if so what am I doing wrong?
minSdkVersion = 11
targetSdkVersion = 19
Here's the xml for the selector, red_button.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape>
<solid android:color="#000000" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_pressed="true">
<shape>
<solid android:color="#ef4444" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#ef4444"
android:endColor="#992f2f"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
and here's the xml for the button, which is contained within a LinearLayout and loaded as a fragment into mainActivity
<Button
android:background="#drawable/red_button"
android:id="#+id/scene_1_btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:maxWidth="150dp"
android:minWidth="100dp"
android:text="#string/scene_1_btn_text"
android:onClick="sendMessage"
style="#style/button_text" />
Thank you.
Buttons actually behave like keyboard buttons, i.e. they have only two states viz normal and pressed.
If you want to remember/show the seleted state, i got an idea..., try using 'radio group' (if you want only one to belected from a group) or use toggle button/switch (each of them remember their state as ON or OFF) and create their custom UI so that they look like buttons as you want them to.
I want my buttons to be transparent as I have an image on the activity as a background.
If the buttons are not transparent, those buttons are covering the image and image background seems useless.
If I use android:background="#android:color/transparent", it is making my button completely invisible except the text on it. So, I need the button to be transparent provided it should be visible that there is a button. Code snippet would be appreciated.
<Button
android:id="#+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent" //this line makes the button completely invisible.
android:text="#string/label" />
If you just want white borders, and the text/icons in the button, but the button background transparent create the following drawable (I named it button_border.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<stroke
android:color="#android:color/white"
android:width="1dp" />
<solid android:color="#android:color/transparent" />
</shape>
</item>
</selector>
And use it as a background for the button:
<Button
.
.
.
android:background="#drawable/button_border"
/>
I'd guess there is a more elegant way to do it with themes, but I'm not that far yet. In the button_border drawable you can add rounded corners and whatnot...
android:background="#A0FFFFFF"
I've hard coded the colour, but you should store it in colors.xml
Modify the first two characters, A0, to adjust how much transparency you want and the last 6 characters the color you want.
Apply selector for button, like this
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<solid
android:color="#color/color_1_end" />
<stroke
android:width="1dp"
android:color="#android:color/transparent" /> // transparent patametr
<corners
android:radius="4dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape></item>
<item>
<shape>
<gradient android:angle="270"
android:endColor="#color/color_1_start"
android:startColor="#color/color_1_end" /> // maybe transparent
<stroke
android:width="1dp"
android:color="#color/color_1_start" /> //maybe transparent
<corners android:radius="4dp" />
<padding android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
Modify this for you =)
Apply border for button
<item>
<shape>
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="#FF000000" />
<corners android:radius="3dp" />
</shape>
</item>
<item>
<shape>
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp" />
<gradient
android:angle="315"
android:centerColor="#FFFFFFFF"
android:endColor="#FFB0B0B0"
android:startColor="#FFB0B0B0" />
</shape>
</item>
<Button
android:id="#+id/btnLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="7dip"
android:background="#drawable/button_border"
android:scaleType="centerInside"
/>
Just try to use below code in java
<br/>
button.getBackground().setAlpha(int);<br/>
correct range of input parameter is between 1 and 255 and the '1' is more transparent.
common pitfall is using:<br/>
button.setAlpha(float);<br/>
that make all button transparent.
I have a custom button, but for the focused,pressed,selected state I wanna have the default Android orange selected state rather than make new custom images for those states.
How am I able to achieve that? What should I write on the selector for those states?
Check this code it'll give you a lead about how to change the colors for each state.
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#F5B800"
android:endColor="#F5B800"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#ffcc00" />
<corners
android:radius="1dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#F5B800"
android:startColor="#F5B800"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#ffcc00" />
<corners
android:radius="1dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#ff9900"
android:startColor="#ffcc00"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#ffcc00" />
<corners
android:radius="1dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
I am sure you want to implement that default orange color image inside your custom selector file, if this is the case then you can know the name of all the in-built drawable files used by system, from here you can know: Android Drawables
Some of button default images names are:
btn_default_pressed.9
btn_default_selected.9
btn_default_small_pressed.9
btn_default_small_selected.9
so now copy the 9-patch images inside the res/drawable folder and use it wherever you want.
Have you tried not mentioning those states? i haven't tried but off the top of my head, i would say that if you donot mention a state, android should use the default values.
I'm trying to follow the tutorial located at http://www.dibbus.com/2011/02/gradient-buttons-for-android/, however I'm not getting the expected result. Everything compiles fine, however when I run it I get a button without any colors, gradients, or even borders (so actually all I see is just the button text). The button itself responds to events ok.
In order to be compatible with as many devices as possible I'm using android:minSdkVersion="3", could this be an issue?
This is part of my layout xml file:
<TableRow>
<Button
android:id="#+id/button_calculate"
android:background="#drawable/copy_btn"
android:layout_column="1"
android:layout_span="2"
android:padding="5dp" />
</TableRow>
This is copy_btn.xml, located inside drawables.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#70c656" />
<stroke
android:width="1dp"
android:color="#53933f" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#70c656"
android:endColor="#53933f"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#53933f" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Thanks
It works fine if u add android:layout_height="wrap_content" android:layout_width="wrap_content" to your button in Android Manifest. minSdkVersion has to do nothing with this as most of the times they are backward compatable.
What "(so actually all I see is just the button text)" means? How can u see a button text when u haven't used android:text attribute? Either there is another button in ur app or u r setting text from code. Do check this.
In my app I have set for a button the background to a certain color. I don't like how it looks like.The button is like a rectangle, with straight edges. I want that the button to have rounded corners. How is it possible?
Here is my button in xml :
<Button android:id="#+id/button" android:text="Participer au concours de photos"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="410dip" android:layout_centerHorizontal="true"
android:background="#drawable/orange1"/>
Thanks...
Use selector
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#24a1dd"
android:centerColor="#158ee4"
android:endColor="#37b8ee"
android:angle="270"
android:centerY="0.88" />
<stroke
android:width="3dp"
android:color="#00ffffff"
/>
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#59bfe6"
android:centerColor="#36aced"
android:startColor="#91def7"
android:angle="270"
android:centerY="0.88" />
<stroke
android:width="3dp"
android:color="#00ffffff"
/>
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#e1e1e1"
android:centerColor="#bdbebd"
android:startColor="#f6f2f6"
android:angle="270"
android:centerY="0.88" />
<stroke
android:width="3dp"
android:color="#00ffffff"
/>
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Something like this should do the work. Just fix the color values. Which is the same as the the answer of this question Standard Android Button with a different color
There are two ways :
Use 9 patch drawables (png image)
here is complete tutorial 9 patch drawables: background of a customized Button
Set xml drawable as background of your buttons.
Take a look here Gradient buttons for android.
And to know more about xml drawables :-
ANDROID XML DRAWABLES