Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange behavior of inflated buttons when changing one's color
    primarykey
    data
    text
    <p>I am trying to implement a dynamic button inflation for my Android application, based on an input specified by the user in real time. When clicked, button changes its color from blue to red.</p> <p>The code responsible for this goes as follows:</p> <pre><code>LayoutInflater layoutsInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layout_for_buttons); // Let's say that now we need only 3 buttons: for (int i = 0; i &lt; 3; i++) { RelativeLayout buttonLayout = (RelativeLayout) layoutsInflater .inflate(R.layout.button_layout, linearLayout, false); Button button = (Button) buttonLayout.getChildAt(0); button.setText("Button " + i); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View buttonView) { Button button = (Button) buttonView; GradientDrawable gradientDrawable = (GradientDrawable) button .getBackground(); gradientDrawable.setColor(Color.RED); gradientDrawable.invalidateSelf(); } }); linearLayout.addView(buttonLayout); linearLayout.invalidate(); } </code></pre> <p>Layout that I append buttons to is a simple <code>LinearLayout</code>:</p> <pre><code>&lt;LinearLayout android:id="@+id/layout_for_buttons" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;/LinearLayout&gt; </code></pre> <p>Inflated <code>button_layout</code> is a <code>RelativeLayout</code> consisting of a <code>Button</code> and a <code>TextView</code>:</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10sp" &gt; &lt;Button android:id="@+id/button_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/button_bg" android:gravity="left" android:paddingBottom="7dip" android:paddingLeft="50sp" android:paddingTop="7dip" android:textColor="#FFFFFF" android:textSize="20sp" /&gt; &lt;TextView android:id="@+id/label_view" android:layout_width="24dip" android:layout_height="24dip" android:layout_marginLeft="13dip" android:layout_marginTop="8dip" android:gravity="center" android:text="Hi" android:textColor="#FFFFFF" android:textSize="20sp" android:textStyle="bold" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p><code>@drawable/button_bg</code> is a <code>shape</code> in blue color:</p> <pre><code>&lt;shape xmlns:android="http://schemas.android.com/apk/res/android" android:padding="10dp" android:shape="rectangle" &gt; &lt;solid android:color="#0000ff" /&gt; &lt;corners android:radius="10dp" /&gt; &lt;/shape&gt; </code></pre> <p>Now, when I run the application everything seems to be fine. After being clicked, first button unsurprisingly turns red:</p> <p><img src="https://i.stack.imgur.com/UgkXe.png" alt="enter image description here"></p> <p>When I close and re-run the application every single button is blue, and that is a behavior that matches up to my expectations. The problem is, when I <strong>re-run the application for the second time</strong>, every button turns red (as if it has been clicked):</p> <p><img src="https://i.stack.imgur.com/BAItG.png" alt="enter image description here"></p> <p>As it is written in a documentation, <code>inflate</code> method should <strong>inflate a new view hierarchy</strong> based on a given XML, so what could be responsible for this? I thought that the same <code>GradientDrawable</code> might be shared among every inflated button, but debugger shows that each button has its own <code>GradientDrawable</code> instance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload