android image button - android

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 ;)

Related

setCompoundDrawablePadding() to zero

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.

ImageButton not always clickable on the first click

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

how to use Imageview as Clickable?

How can I use imageview as a button(clickable) in my project? As of now i am using button object but i want to display an image and not a button.
Thanks in advance
android
ImageView do listen to click events i.e. OnClickListener. So if you plan to use them as clickable you do not need any additional set up for it.
Simply create an ImageView,
<ImageView
android:id="#+id/imgClickable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="your image"
/>
Then get its reference in your Java code,
ImageView clikableImage = (ImageView) findViewById(R.id.imgClickable);
clikableImage .setOnClickListener(this); //This make it clickable
And finally handle its onClick event by overriding
#override
public void onClick(View v){
//Your action
}
You can use android:clickable="true" attribute. And then you can use onClickListener to implement a listener.
But if you want to use an image instead of button you should consider ImageButton.
http://developer.android.com/reference/android/widget/ImageButton.html
It's pretty simple. Either in your layout when you define the ImageView you can add android:onClick="someFunction" and which calls someFunction whenever you click the image (make sure you define it in your code) or you can programmatically just add an onClickListener to the ImageView.
You can include the ImageView wherever you want on your layout and implement the OnClickListener interface to listen for click events. It would be something like:
ImageView image = (ImageView) findViewById(R.id.your_id);
imager.setOnClickListener(new View.OnClickListener() {
// Do something
});
Wll how about using ImageButton.
Uoc can find it when you open a layout xml file, and at the Grapichal layout - You would see ImageButton unter the Images&Media options menu.

Transparent overlay in imageview in a tablelayout

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"));
}
}

How to put text over an imagebutton not via xml

I'm currently developing an android app with eclipse. Basicaly, I need to put the text over an imagebutton. There are some conditional statements in order to put text over that imagebutton, that's why I can't set the text in xml
This is how I set the imagebutton in xml:
<ImageButton
android:id="#+id/question1"
android:layout_width="150dip"
android:layout_height="50dip"
android:layout_gravity="center_vertical|center"/>
This is how I set the image source and text of the button:
buttonQuestion1 = (ImageButton) findViewById(R.id.question1);
TextView listContent = (TextView)findViewById(R.id.question1);
String question1 = getQuestion.get(0);
buttonQuestion1.setImageResource(R.drawable.button);
listContent.setText(question1);
The code to set the image source works fine. I use TextView and setText in order to put text over the imagebutton but when I try to run it, the app crashes. Anyone know how to set text over an imagebutton in a right way?
This line
TextView listContent = (TextView)findViewById(R.id.question1);
gives error because R.id.question1 is not a TextView, it's an ImageButton.
You can use normal <Button/> instead for layout
<Button
android:id="#+id/question1"
android:layout_width="150dip"
android:layout_height="50dip"
android:layout_gravity="center_vertical|center"/>
When you want to set the text and image resource, you can call
buttonQuestion1.setBackgroundResource(R.drawable.button);
buttonQuestion1.setText(question1);

Categories

Resources