setCompoundDrawablePadding() to zero - android

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.

Related

Make LinearLayout clickable - Colorize background like the default for ListView

Hello my name is Fabian and at the moment I try to programming an Android App.
I have a ListView with some items and above this ListView I added a LinearLayout, which I want to make clickable, to add items to the ListView.
If I touch one entry in the ListView the item gets colorized with the default color, a light grey. I like to have this behaviour for my LinearLayout, too.
I know how to get the LinearLayout clickable.
I did this by
android:clickable="true"
android:onClick="addProject"
Also I know how to define the backgroundcolor, but I don't know how I can pass the default colors from android of the ListView (android:listSelector) to the LinearLayout.
I tested to define
android:background="?android:attr/listChoiceBackgroundIndicator"
but in that case the LinearLayout gets colorized blue and only if I touch it, it gets the right color.
I hope you can help me, to pass the default color of a ListView to a LinearLayout.
Remove below line from your LinearLayout
android:onClick="addProject"
and add an id to a LinearLayout,
android:id="#+id/linear_layout"
From JAVA add this,
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout);
layout.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Toast.makeText(this, "Layout Clicked", Toast.LENGTH_LONG).show();
}
});

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

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

android custom togglebutton border

I have a grid of 64 togglebuttons in an 8x8 form. when the app runs, it sets each togglebutton's background/drawable to a colour. this is done in the program, not the xml. my problem is that while there seems to be space between the buttons on the graphical layout of the app, when the app runs, and changes the buttons to a colour, the space disappears. this makes the buttons look like a single plate, with no definition between buttons.
What I want is to put a border on the buttons to make it clear where each button is.
the function of the buttons is to be used to indicate a musical note being pressed. when the button is pressed, the colour of the button turns from grey to light blue, and adds the note to a sequence.
So i need to be able to put a border on a togglebutton with a custom design, that changes when activated/deactivated, keeping the border in both states. i have also tried setting the max width and max height, and also setting the padding on the buttons in an attempt to seperate them.
an example of the code is as follows:
public class MainActivity extends Activity implements OnClickListener
{
ToggleButton tg1;
....
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....
tg1 = (ToggleButton) findViewById(R.id.toggleButton1);
tg1.setOnClickListener(this);
....
tg1.setBackgroundColor(0xffcccccc); //set button grey
}
public void onClick(View v)
{
if((tg1.isChecked()))
{
tg1.setBackgroundColor(0xff00ffff); //set button blue
}
else
{
tg1.setBackgroundColor(0xffcccccc); //set button grey
}
}
}
any help is greatly appreciated.
Right way: do not call isChecked manually to change background. Use selector. You can draw 9-patch with a border or create shape (use stroke to set border color and solid to set fill color).
Or create a custom togglebutton to support desired functionality.

android image button

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

Categories

Resources