I have a number of ImageButtons in a layout. The image resources are 32x32 px. They all have the same attributes:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ibButton1"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_weight="0"
android:src="#drawable/not_selected"
android:layout_gravity="center_vertical"
android:background="#null"/>
In my fragment, I'm doing:
final ImageButton button1 = (ImageButton)view.findViewById(R.id.ibButton1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
isSelected = !isSelected;
if (isSelected ) {
button1.setImageResource(R.drawable.selected);
}
else {
button1.setImageResource(R.drawable.not_selected);
}
}
});
However, most times I need to click over 5 times on the button for it to register the click.
Should I need to increase the image size or is there a better way to listen to clicks? Should I use onClick attribute instead?
Provide some padding to increase the Tap area.
android:padding="10dp"
Perhaps, the size of the image is too small(as #Shoeb Siddique said) to get touch event on the image. You can increase the padding or size of image to improve the probability to touch the image.
It seems to me , you don`t need an ImageButton, but a ToggleButton with to states.
http://developer.android.com/guide/topics/ui/controls/togglebutton.html
and please remove android:clickable="true" , it is not needed, because it is a button, which mean, it is true by default. If it would be an TextView, it would needed.
Extract Image With Touch zone I Mean Transparent Background
for Eg : the Give Image is 86 * 70 px So When Ever User touches round that Transparent background 86* 70px you can fire event
Related
i want to Put the image on text in the button.
I want to set the spacing between text and images to zero.
In addition, a button with a size of 200 dp x 200 dp
I want the images and text to be laid out in the same vertical padding.
Please help me.
Button b = findViewById(R.id.button);
Drawable d = getResources().getDrawable(R.drawable.ic_launcher_background,getTheme());
b.setCompoundDrawablesWithIntrinsicBounds(null,d,null,null);
b.setCompoundDrawablePadding(0);
b.setText("good");
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "hellow", Toast.LENGTH_SHORT).show();
}
});
There's an alternative way of doing this and it can prove easier.
Instead of using a Button and trying to use an drawable with it, have you considered using a TextView and add a drawable to it? It can be done within your XML and you can add padding. Something like,
<TextView
android:id="#+id/my_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Button"
android:textColor="#color/text_secondary_dark"
android:drawablePadding="8dp"
android:drawableTop="#drawable/my_icon"
tools:drawableTint="#000"/>
As you can see, you can add a drawable, its padding, its tint etc.
I have a tablelayout with two rows each consisting of two imageview. I want to achieve result as below. I have tried few solutions mentioned in stackoverflow but somehow I have not found a way to achieve this. Each imageview is looks as below
<ImageView
android:id="#+id/imageViewY"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/cell_border"
android:clickable="true"
android:padding="2dp"
android:src="#drawable/letter_y" />
If you have any idea or solution to achieve this , i would like to request for your help. Thanks in advance.
use transparent color #11000000 as background of imageview
Here 11 is a Transparency Value of color 000000(means Black color)
and you can set alpha programatically or through xml for setting transparency
now If Set Alpha You said deprecated and don't want to take risk by using this then just set color of image view
i just explain how to handle 1st imageview's click
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==imageview1){
//set lighter dark layer on click of image 1 and reset fully transparent layer on other image
imageview1.setBackgroundColor(Color.parseColor("#11000000"));
imageview2.setBackgroundColor(Color.parseColor("#00000000"));
imageview3.setBackgroundColor(Color.parseColor("#00000000"));
imageview4.setBackgroundColor(Color.parseColor("#00000000"));
}
}
In iOS, if I set a button's background to an image, when I press the button, the whole content of the button (including the text) will be shadowed. Can I achieve the same effect in Android, or do I have to use different images for different states? Also, even if I use different images for different states, how do I make the text also shadowed? A dirty way is to set a OnClickListener to the button and programatically shadow the text when pressed, but are there any other ways?
I´ve been trying to find a solution for this for a while now but couldn´t find anything so I came up with a pretty neat solution which works on all image buttons. Just like iOS.
Create a Black, 10% transparent image and save it as PNG. I call it button_pressed.png. You can use this image, http://img84.imageshack.us/img84/7924/buttonpressed.png
Create a drawable called something relevant, I call it "button_pressed_layout.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/button_pressed" />
</selector>
Now put your button image in the LinearLayout and then the Button inside the LinearLayout. In the Button use button_pressed_layout.xml as background.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_image">
<Button
android:id="#+id/myButtonId"
android:text="#string/Continue"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/button_pressed_layout"
android:textColor="#FFF"
android:textSize="16dp" >
</Button>
</LinearLayout>
That´s it!
To get a pressed-state without creating a 2nd button image, I created an OnTouchListener to change the button's alpha. This is not the most beautiful pressed state, but gives an effective automatic behavior.
public class PressedStateOnTouchListener implements OnTouchListener
{
PressedStateOnTouchListener( float alphaNormal )
{
mAlphaNormal = alphaNormal;
}
public boolean onTouch( View theView, MotionEvent motionEvent )
{
switch( motionEvent.getAction() ) {
case MotionEvent.ACTION_DOWN:
theView.setAlpha( mAlphaNormal / 2.0f );
break;
case MotionEvent.ACTION_UP:
theView.setAlpha( mAlphaNormal );
break;
}
// return false because I still want this to bubble off into an onClick
return false;
}
private float mAlphaNormal;
}
In your Activity, apply this listener to each button:
Button theButton = (Button)findViewById( R.id.my_button_id );
theButton.setOnTouchListener( new PressedStateOnTouchListener( theButton.getAlpha() ));
In order to give the button itself different effects, I believe you need to use custom images, as seen here and, in less detail here As for the text, this post looks like it would do the trick for the shadow
I have a button that starts out as a placeholder, but once the user authenticates, it changes to a custom image for that user.
<ImageButton android:id="#+id/button"
android:background="#null"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/button_default"/>
And then later:
ImageButton ib = (ImageButton)findViewById(R.id.button);
ib.setImageBitmap(previouslyDecodedBitmap);
But this looks terrible. I can't figure out how to style it properly so that the newly decoded bitmap is the right size and behaves like an ImageButton. I suspect there is some combination of widgets I can use other than ImageButton to achieve this? I was hoping I could just nest an ImageView on top of the ImageButton by adding it as a child to ImageButton, but that doesn't seem to be allowed (it is in Silverlight...).
Anyway, any suggestions on how to properly do this are welcome. Thanks.
One way would be to use frame layout and place buttons and image one over the other , top being imageview , keep it invisible (android:visibility = "invisible")
On clicking the button and authenticating , make it visible over the button or else even you can hide the button below and show only image on top.
How can i create a button with no text and an image centered horizontally ?
I don't want to use an ImageButton because I want to define a different backgound image
You just use an ImageButton and make the background whatever you want and set the icon as the src.
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/album_icon"
android:background="#drawable/round_button" />
just use a Button with android:drawableRight properties like this:
<Button android:id="#+id/btnNovaCompra" android:layout_width="wrap_content"
android:text="#string/btn_novaCompra"
android:gravity="center"
android:drawableRight="#drawable/shoppingcart"
android:layout_height="wrap_content"/>
You can just set the onClick of an ImageView and also set it to be clickable, Or set the drawableBottom property of a regular button.
ImageView iv = (ImageView)findViewById(R.id.ImageView01);
iv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
You can use the button :
1 - make the text empty
2 - set the background for it
+3 - you can use the selector to more useful and nice button
About the imagebutton you can set the image source and the background the same picture and it must be (*.png) when you do it you can make any design for the button
and for more beauty button use the selector //just Google it ;)