-2

Hi in my app there are 6 buttons and each has a background image,Problem is Image gets blur when setting as background.I have tried all the sizes but problem remains same.I also tried setting it for ImageView,Button and ImageButton.

What should be the size of the images if I'm setting as a background.

Here is my xml,Here is how it looks like..https://i.stack.imgur.com/GPUZF.jpg

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
    android:id="@+id/tool_bar"
    layout="@layout/tool_bar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1">

        <ImageButton
            android:id="@+id/cstatn"
            android:layout_width="0dp"
            android:scaleType="fitXY"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <ImageButton
            android:id="@+id/cclinic"
            android:layout_width="0dp"
            android:scaleType="fitXY"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:orientation="horizontal"
        android:layout_weight="1" >

        <ImageButton
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <ImageButton
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:orientation="horizontal"
        android:layout_weight="1" >

        <ImageButton
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <ImageButton
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

Sunil
  • 49
  • 1
  • 12

1 Answers1

0

The images are stretched because the size of your buttons are different from the size of the image. To use an image as background without stretching, convert the image to a 9patch file. There are many info on the internet explaining what it is can how it can be used, like this one.

Kai
  • 15,284
  • 6
  • 51
  • 82
  • so if I use the 9-patch image it will not stretch?and according to my code what should be the size of the image? – Sunil Jan 17 '16 at 14:00
  • 9patch images will still be stretched, but they will be stretched according to your design. The size of the image depends on the content of the image and the size of your button, the image should obviously be smaller than the button. – Kai Jan 17 '16 at 14:02
  • here in my above code I didn't specify any width and height for button but when I'm trying to set Image for a button its stretching and the Image should look good in all types of devices. – Sunil Jan 17 '16 at 14:04
  • Speaking of which... your layout will probably produce result that's not what you'd want, you'll probably read up tutorials on them as well. – Kai Jan 17 '16 at 14:06
  • you saved my day tq very much,after trying 9 patch image i was surprised and it was looking good in all kinds of screens. – Sunil Jan 17 '16 at 14:18
  • Glade to be able to help ;) – Kai Jan 17 '16 at 14:19
  • just one more help, what was the best way to get the location updates for every 5 minutes even app is in sleep mode. – Sunil Jan 17 '16 at 14:21
  • You can't really force the OS to provide an update every 5 min, you basically just register to LocationManager to be updated on location change. – Kai Jan 17 '16 at 14:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100893/discussion-between-sunil-and-kai). – Sunil Jan 17 '16 at 14:30