2

I tried to create a rectangle shape with corner radius and border on android. I almost made it, just a little problem that i cant solve until now :

enter image description here

The border already has some radius on its corner, but the inner rectangle does NOT (please see the red mark on the image).

This is my code :

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <corners android:radius="10dp"/>
    <stroke  android:width="3dp" android:color="#000000" />
</shape>

I use this shape by using android:src on the ImageView.

This is how i use the shape :

<ImageView
        android:src="@drawable/rectangle"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

And this is how i maintain the shape's size (on my adapter) :

holder.answer.setBackgroundColor(getItem(position).getColor());
holder.answer.getLayoutParams().width = (int) GlobalUtil.size;
holder.answer.getLayoutParams().height = (int) GlobalUtil.size;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(holder.answer.getLayoutParams());
lp.setMargins(0, (int) GlobalUtil.gapGrid, 0, (int) GlobalUtil.gapGrid);
holder.answer.setLayoutParams(lp);

Please kindly help me out, Thanks.

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129

1 Answers1

3

Use android:background instead of android:src because right now it is applying scaling according to density.

EDIT:

rectangle.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="10dp"/>
<stroke  android:width="3dp" android:color="#00FF00" />

ImageView

<ImageView
    android:background="@drawable/rectangle"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Java code:

//holder.answer.setBackgroundColor(getItem(position).getColor());
holder.answer.getLayoutParams().width = (int) GlobalUtil.size;
holder.answer.getLayoutParams().height = (int) GlobalUtil.size;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(holder.answer.getLayoutParams());
lp.setMargins(0, (int) GlobalUtil.gapGrid, 0, (int) GlobalUtil.gapGrid);
holder.answer.setLayoutParams(lp);
Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
  • Thanks for your help. If i change it to be the background, the border and radius is disappear hence it just showing a normal rectangle. Please see my edited question for more information. – Blaze Tama Aug 11 '14 at 08:02
  • @BlazeTama: it is not disappeared. zoom your layout and checkout. it is having width of 3dp so it will be very thin.If you want to maintain the width properly, I would suggest you to use 9patch image instead of shape drawable. – Mehul Joisar Aug 11 '14 at 08:05
  • I have zoomed it and change the width..its disappeared. Please kindly see my edited question, Thanks – Blaze Tama Aug 11 '14 at 09:05
  • @BlazeTama: you can not set background from both, xml and java. it will override old one. try my edited answer and if it shows you some kind of green background with rectangle having radius then I hope you will understand what was going wrong. – Mehul Joisar Aug 11 '14 at 09:23
  • Sorry for the late reply. I need to set the color manually in Java. What should i do? Thanks a lot – Blaze Tama Aug 12 '14 at 11:55
  • @BlazeTama: Use 9Patch image. – Mehul Joisar Aug 12 '14 at 12:01
  • Sorry but i need to set the random color to the rectangle. Can i accomplish that with the 9Patch? Thanks – Blaze Tama Aug 14 '14 at 09:27
  • @BlazeTama: use 9patch image for only in background. u can still use colors in java code. – Mehul Joisar Aug 14 '14 at 09:40