Reputation: 14108
Goal:
When I click on the button throw, a picture should be exchanged into another picture in real time.
Problem:
What is wrong with the code? I have to press twice in order to change the picture.
Info:
*Using Android API 25 and using the tool android studio
package com.jfdimarzio.dice;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity
{
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void throwDice(View v)
{
image = (ImageView) findViewById(R.id.dice_grey_3);
Button button = (Button) findViewById(R.id.btnThrow);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0)
{
image.setImageResource(R.drawable.red1);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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"
tools:context="com.jfdimarzio.dice.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:text="Throw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnThrow"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:onClick="throwDice"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/grey1"
android:id="@+id/dice_grey_1"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/grey2"
android:id="@+id/dice_grey_2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/grey3"
android:id="@+id/dice_grey_3"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Upvotes: 0
Views: 1864
Reputation: 6114
QUICK FIX:
public void throwDice(View v)
{
image = (ImageView) findViewById(R.id.dice_grey_3);
Button button = (Button) findViewById(R.id.btnThrow);
image.setImageResource(R.drawable.red1);
}
Why do this work?
There are two ways to listen to a click in Android, either programmatically using Java or using XML . What you've done is you've implemented both these nested. In the end it takes two clicks to get the end result. Either you can use the onClickListener()
or the throwDice()
method which you've defined in the XML : android:onClick="throwDice"
Upvotes: 0
Reputation: 26198
Since you specify the onclick for the button here: android:onClick="throwDice"
it will register it. But when it is clicked again the method throwDice
will register another onclick listener thus change the old listener with the new one. And when click again it will trigger the new listener to change the image which will execute this:
image.setImageResource(R.drawable.red1);
Now you need to only register the listener once only. Ether by xml or programatically.
Upvotes: 2