0

I am new android developer. I have problem how to this convert code into programmatically. I have changes programmatically for color.

<stroke android:width="2dp" android:color="@color/appThemeColor"/>

<solid android:color="@color/appBGColor" />

<padding
    android:bottom="1dp"
    android:left="1dp"
    android:right="1dp"
    android:top="1dp" />

<corners android:radius="1dp" />

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
nitin
  • 41
  • 3

1 Answers1

0

You can chage the color programmatically, you should put your code inside :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="2dp" android:color="@color/azul_oscuro"/>
<solid android:color="@color/azul_1" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners android:radius="1dp" />
</shape>

Then you can asing your shape to any item in a layout, for example a view:

<View

android:layout_width="56dp" android:layout_height="40dp" android:background="@drawable/yourshapefilename" android:id="@+id/view_color" android:enabled="false"/>

In your code when you inflate the layout you can change the color this way:

View myvie = findViewById(R.id.view_color);
GradientDrawable draw = (GradientDrawable)myvie.getBackground();
draw.setColor(getResources().getColor(R.color.rojo)); //--> change the solid color here
draw.setStroke(7, getResources().getColor(R.color.green)); // -->change  stroke color and size 
venimania
  • 98
  • 1
  • 10
  • i have change stroke color programmatically. – nitin Jun 11 '15 at 13:25
  • Dig you get to do it? becouse i didn't understand. Any way you can change the stroke color: draw.setStroke(7, getResources().getColor(R.color.green)); Look the link:http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#setStroke%28int,%20int%29 – venimania Jun 11 '15 at 14:10