NBC
NBC

Reputation: 1698

How to change width of rectangle shape without changing height

I'm trying to make a dynamic bar chart with 5 columns that changes based on user input. I defined a rectangle xml drawable shape like so:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">

<corners
    android:radius="2dp"
    />
<solid
    android:color="#c73434"
    />
<padding
    android:left="0dp"
    android:top="0dp"
    android:right="0dp"
    android:bottom="0dp"
    />
<size
    android:width="270dp"
    android:height="60dp"
    />

<stroke android:width="2px"
        color="#ff00ffff" />
</shape>

I've embedded the bar shape 5 times in my xml layout file like this:

                <ImageView
                    android:id="@+id/bar_1"
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"
                    android:gravity="start"
                    android:src="@drawable/bar"/>
                <ImageView
                    android:id="@+id/bar_2"
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"
                    android:gravity="start"
                    android:src="@drawable/bar"/>
                    etc...

I want to be able to change the width of the bar based on user input, but when I adjust the "width" property of the ImageView, it proportionally affects the height as well (so the entire bar gets smaller). Any suggestions? Is there a better way of doing this?

Upvotes: 0

Views: 1505

Answers (1)

daxgirl
daxgirl

Reputation: 772

I think 2 things:

  1. Make your ImageView wrap content instead of match_parent (not sure about this)
  2. Most importantly, remove size constraints from background drawable you're using. I think it preserves your aspect ratio.

Upvotes: 2

Related Questions