I have a button which have both text and image. The image size it is taking as actual size of image. How can i change the size of image in button?
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:drawableTop="#drawable/home_icon"
android:text="Settings"
/>
Set
android:scaleType="fitXY"
Also wrap_content is the preferred Parameter for width and height so that the OS will automatically adjust depending upon the screen size.
Please refer to
Android - Image Button Scale
and
Adjusting the size of an ImageButton in Android
You should use a ImageButton and specify the image in android:src, and set android:scaletype to fitXY
Set Drawable in your code
Drawable drawable = getResources().getDrawable(R.drawable.icon);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.10),
(int)(drawable.getIntrinsicHeight()*0.10));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, sWidth,sHeight);
Button btn = findViewbyId(R.id.btnId);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null); //set drawableLeft/Right for example
Control the size deterministically with specific measurements. For example:
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:layout_height="50dp"
android:layout_width="50dp"
android:background="#drawable/home_icon"
android:text="Settings"
/>
For even more control over the button's design use a container such as a RelativeLayout to hold the image and the text. Then assign an onClickListener to the container to turn it into a clickable button. Here's an example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/snazzy_button_with_image_above_text"
android:layout_width="wrap_content"
android:layout_height="64dp"
android:layout_gravity="center"
android:layout_marginLeft="1dp"
android:background="#color/SkyBlue"
android:paddingBottom="2dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingTop="1dp" >
<ImageView
android:id="#+id/your_image"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/white"
android:contentDescription="image inside button"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/your_text"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/your_image"
android:layout_alignParentLeft="true"
android:layout_gravity="center"
android:layout_marginBottom="0dp"
android:layout_marginTop="0dp"
android:background="#color/translucent_black"
android:gravity="bottom|center_horizontal"
android:paddingTop="0dp"
android:text="Your Button Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/white"
android:textSize="10sp" />
</RelativeLayout>
In the code assign the onClickListener as such:
yourButton = (RelativeLayout) findViewById(R.id.snazzy_button_with_image_above_text);
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View clickedButton) {
//button click action here
}
Set android:background="#drawable/image_name"
and give width and height like
android:layout_height="10dp"
android:layout_height="10dp"
or you can try using ImageButton instead of Button because ImageButton has more image rendering options than Button
You can refer here
Simple way: add next line to dimens.xml
This parameter change size all icons on buttons in AlertDialog
<resources>
<dimen name="abc_alert_dialog_button_dimen">24dp</dimen>
</resources>
Related
I have a Button:
<Button
android:id="#+id/btnCap2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/btn_s9cap2"
android:background="#null"
android:layout_gravity="center_horizontal"
android:drawablePadding="10dp"
android:padding="20dp"
android:text="#string/menu_btn_s9cap2"
android:textColor="#color/black"
android:textStyle="bold" />
I need to adjust drawableTop dynamically . How can I do this ?
I know how to adjust background but this does not solve my problem.
btnCap2 = (Button) findViewById(R.id.btnCap2);
drawable = getResources().getDrawable(R.drawable.btn_s9cap2);
btnCap2.setBackground(drawable);
btnCap2.setOnClickListener(this);
Use
btnCap2.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
I am creating a calc app.
How to set padding to shrink the background image..
I want to convert to something like
My xml of TextView :
<TextView
android:id="#+id/c_tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:gravity="center"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text=""
android:background="#drawable/ic_backspace_black_24dp"
android:padding="5dp"/>
Use ImageView instead and give padding to it :
<ImageView
android:padding="20dp"
android:src="#drawable/ic_backspace_black_24dp"
android:id="#+id/product_main_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
and why you need Textview and Background image to that ?
You can simply put ImageView or ImageButton
You should use imageButton instead of TextView like this:
<ImageButton
android:id="#+id/delete_button"
android:layout_width="70dp"
android:layout_height="40dp"
android:background="#drawable/delete_icon"
android:layout_margin="16"/>
I suppose there's more than one way
First way, you have to extends TextView into new class and overwrite some #override methods. and use this class in xml... this is hard way
Second way (Recommended), use Adobe Illustrator or Photoshop to adjust a new drawable and make it easily as you like
Third way, use RelativeLayout or FrameLayout and make the background you want in a Imageview... easy way and do the job
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/iv1"
android:padding="your_padding_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_backspace_black_24dp"/>
<TextView
android:id="#+id/c_tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text=""
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_alignBottom="#+id/iv1"
android:layout_alignEnd="#+id/iv1"
android:layout_alignStart="#+id/iv1"
android:layout_alignTop="#+id/iv1"/>
</RelativeLayout>
I have a button with an image. The height of the image should be the 20% of the height of the device. So I set the height of the image programmatically.
The problem is that, if i do it, the image becomes stretched. I put the settings that it has to be scaled centerinside and I have tried also with other elements, as imagebutton or imageview, but I had always the same problem.
How can I do?
UPDATE
This is my xml code:
<LinearLayout
android:id="#+id/letterLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:scaleType="centerCrop"
android:gravity="center"
android:layout_gravity="center"
android:onClick="onClick">
<Button
android:id="#+id/letterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:fontFamily="sans-serif"
android:textStyle="bold"
android:padding="30dp"
android:textColor="#000000"
android:background="#00000000"
android:clickable="false"/>
</LinearLayout>
And my Java code where i set the height:
letterLayout.getLayoutParams().height = (int) (hQdr);
letterTest.getLayoutParams().height = (int) (hQdr);
//letterTest.getLayoutParams().width = (int) (wQdr);
Did you tried with the parameter android:scaleType="centerCrop"? This Crops your Image to enter in the layout
I am trying to have a TextView with an ImageView directly to its right. Right now the image partially overlaps the far right of the text, so if the text inputted is long, then the image is "over" the text. I have an ellipse setting on the text but I need the TextView to "stop" right at the left border of the ImageView.
TextView layout settings:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
ImageView layout settings:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
Try this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/iv"
android:singleLine="true"
android:maxLines="1"
android:ellipsize="end"
.... />
<!-- background == #null if you don't want default button background -->
<ImageButton
android:id="#id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="3dp"
android:background="#null"
.... />
</RelativeLayout>
EDIT
See the comments in regards to why this works and the posted question doesn't. Also take a look at
Android Relative Layout alignParentRight and alignParentEnd
for a reference to end/right and start/left attributes.
I want to put png image over button i tried android:background but the png takes all the button what i want exactly is to use the logo png over button like this
the info logo is my png and "about" comes from the button text
u can use this:
android:drawableTop="#drawable/SomeIcon"
so then you get this:
<Button
android:id="#+id/damage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="about"
android:drawableTop="#android:drawable/ic_menu_info_details"/>
it works for me :)
You can use a FrameLayout. In a FrameLayout, the z-index is defined by the order in which the items are added, for example
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_drawable"
android:scaleType="fitCenter"/>
</FrameLayout>
In this instance, the ImageView would be drawn on top of the Button, along the bottom center of the Button.
You can check this thread
Basically, you can use:
<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" />
As the thread says.
Take a RelativeLayout, place ImageView inside it. Now set onClickListener on RelativeLayout.
By this way you can create more complex buttons.
<RelativeLayout android:id="#+id/your_btn_id"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</RelativeLayout>
you can use
android:drawableTop
to put drawable at top of Button and use appropriate padding to make it position well.
<Buttton
android:layout_width="99dp"
android:layout_height="99dp"
android:background = "#drawable/bg" />
bg is your custom image.