Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image set as background of toggle button is stretched android

I have a Toggle Button as:

<ToggleButton
            android:id="@+id/tv_pmpSwitch"
            android:layout_width="0dp"
            android:layout_weight="0.1"
            android:layout_height="wrap_content"
            android:background="@drawable/toggle_view"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:textOn=""
            android:textOff=""
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:layout_centerVertical="true"
            android:paddingTop="16dp"
            android:layout_marginLeft="16dp"
            /> 


And my toggle_view drawable is :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/ic_list_action"
        android:state_checked="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/ic_grid_action"
        android:state_checked="false"/>
</selector>


I don't understand why the image in background is stretched ?? I've tried various size images.

like image 600
AnswerDroid Avatar asked Aug 18 '15 04:08

AnswerDroid


People also ask

How to remove background of image button in android?

setImageResource(int) method. To remove the standard button background image, define your own background image or set the background color to be transparent. Save the XML file in your project res/drawable/ folder and then reference it as a drawable for the source of your ImageButton (in the android:src attribute).

What is ImageButton in android?

An ImageButton is an AbsoluteLayout which enables you to specify the exact location of its children. This shows a button with an image (instead of text) that can be pressed or clicked by the user.

What is drawables?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.


2 Answers

You can simply set in the xml layout file:

android:minWidth="0dp"
android:minHeight="0dp"

The image will not stretch anymore

like image 68
Yonatan Nir Avatar answered Nov 24 '22 08:11

Yonatan Nir


<ToggleButton
            android:id="@+id/tv_pmpSwitch"
            android:layout_width="0dp"
            android:layout_height="28dp"
            android:layout_weight="0.1"
            android:background="@drawable/toggle_view"                
            android:textOff=""
            android:textOn="" />

Try this on your code and adjust only layout height parameter!

EDIT

The way to get a non stretched image is using a bitmap and not a drawable. use following as your xml as your background.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item >
        <bitmap android:src="@drawable/ic_list_action"
          android:gravity="center_vertical|left" />
    </item>
    <item>
        <bitmap android:src="@drawable/ic_grid_action"
          android:gravity="center_vertical|left" />
    </item>
</layer-list>
like image 42
Ansal Ali Avatar answered Nov 24 '22 07:11

Ansal Ali