Note that there are some explanatory texts on larger screens.

plurals
  1. POI can't change the color of the background with a click button
    primarykey
    data
    text
    <p>I'm trying to change the color of the background by clicking button. For example: clicking “Y” should make it yellow, “G” green, and so on, but clicking the buttons doesn’t change anything. I’ve only implemented two buttons, but they didn’t work. Can anybody tell me where I’m going wrong?</p> <p>This my activity:</p> <pre class="lang-java prettyprint-override"><code>import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity implements OnClickListener { // Declare UI elements private Button firstButton; private Button secondButton; private ImageView changeBackground; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Our only layout for this app is main.xml // Initialize the UI components changeBackground = (ImageView) findViewById(R.id.backGround); firstButton = (Button) findViewById(R.id.button1); // When we creating a button and if we expect that to use for event handling we have to set the listener firstButton.setOnClickListener(this); secondButton = (Button) findViewById(R.id.button2); secondButton.setOnClickListener(this); } // Have to implement with the OnClickListner // onClick is called when a view has been clicked. @Override public void onClick(View v) { // Parameter v stands for the view that was clicked. if(v.getId() == R.id.button1){ // setText() sets the string value of the TextView changeBackground.setBackgroundColor(Color.RED); }else if(v.getId() == R.id.button2){ changeBackground.setBackgroundColor(Color.BLACK); } } } </code></pre> <p>and my XML: </p> <pre class="lang-xml prettyprint-override"><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="#CCEEFF" tools:context=".MainActivity" &gt; &lt;ImageView android:id="@+id/backGround" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /&gt; &lt;Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/backGround" android:layout_alignParentBottom="true" android:text="@string/white" /&gt; &lt;Button android:id="@+id/button2" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_toRightOf="@+id/button1" android:text="@string/black" /&gt; &lt;Button android:id="@+id/button3" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_toRightOf="@+id/button2" android:text="@string/red" /&gt; &lt;Button android:id="@+id/button4" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button3" android:layout_alignBottom="@+id/button3" android:layout_toRightOf="@+id/button3" android:text="@string/yellow" /&gt; &lt;Button android:id="@+id/button5" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button4" android:layout_alignBottom="@+id/button4" android:layout_toRightOf="@+id/button4" android:text="@string/green"/&gt; &lt;/RelativeLayout&gt; </code></pre>
    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