1

I have the following shapes : 1)quizgeneralanswershape.xml:

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

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


<gradient
    android:angle="45"
    android:centerX="35%"
    android:startColor="#E8E8E8"
    android:centerColor="#7995A8"
    android:endColor="#000000"
    android:type="linear"
    />
<padding
    android:left="0dp"
    android:top="0dp"
    android:right="0dp"
    android:bottom="0dp"
/>
<size
    android:width="270dp"
    android:height="60dp"
    />
<stroke
    android:width="3dp"
    android:color="#878787"
    />

2)quizrightanswer.xml

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

<gradient
    android:angle="45"
    android:centerX="35%"
    android:startColor="#00FF80"
    android:centerColor="#66FFB2"
    android:endColor="#000000"
    android:type="linear"
    />
<padding
    android:left="0dp"
    android:top="0dp"
    android:right="0dp"
    android:bottom="0dp"
    />
<size
    android:width="270dp"
    android:height="60dp"
    />
<stroke
    android:width="3dp"
    android:color="#878787"
    />

there are also 2 more xml files in layout folder that I have created in order to describe my wanted layout using the shape I want. Here is one: quizrightanswer.xml:

<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/quizButton"
android:text="Button"
android:textColor="#FFFFFF"
android:textSize="13sp"
android:layout_width="270dp"
android:layout_height="60dp"
android:background="@drawable/quizrightanswershape"
android:clickable="true"
/>

I have no problem inflating one specific shape and use it. However, i wanted to know if there is any way to give a new shape to a already existed object. To be more clear, I want on the OnClick event to change the shape of my button. Here is some code of my activity that works fine:

    quizTableLayout = (TableLayout) findViewById(R.id.quizTableLayout);
    view = LayoutInflater.from(this).inflate(R.layout.quizquestion, null);//works fine

I want in the view object to give a quizrightanswershape shape on click. How is this possible? A solution I thinked of is deleting old shape and inflate new one with using specific parameters of my old view (like text), but I think that's not right and not fast at all.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
Menelaos Kotsollaris
  • 5,776
  • 9
  • 54
  • 68

2 Answers2

1

It's pretty easy. You're setting the general shape as the background to the button right? Yeah so, use findViewById to find your button in your layout and then attach an OnClickListener, in which you will use setBackground or setBackgroundDrawable to the button to change the background shape.

Aashir
  • 2,621
  • 4
  • 21
  • 37
1

why you are not change background of your button simply?

in easy way you can change it like this:

mButton.setBackground(getResources().getDrawable(R.drawable.yourShape));  

if you want button shape change if user click on it and if not press again have old shape so
you can use shape like this:

<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" >

    <item android:state_pressed="true" android:state_focused="true">
        <shape>
            <gradient
                android:startColor="#f00"
                android:endColor="#f00"
                android:angle="270"/>
            <stroke
                android:width="2dp"
                android:color="#eee"/>
            <corners
                android:radius="3dp" />

        </shape>
    </item>

    <item>
        <shape>
            <gradient
                android:startColor="#ccc"
                android:endColor="#ccc"
                android:angle="270"/>
            <stroke
                android:width="2dp"
                android:color="#ccc"/>
            <corners
                android:radius="3dp" />

        </shape>
    </item>
</selector>  
MHP
  • 2,613
  • 1
  • 16
  • 26