Note that there are some explanatory texts on larger screens.

plurals
  1. POImplement an Image Change when textview is changed
    text
    copied!<p>I am setting up a Click Counter android app. The maximum count is 500, and the count is displayed in a TextView field.<br> I want to be able to display one image whilst the count is between 0-100, then another image when the count is between 101-400, and then the first image again between 401 - 500. I am new to Java and have no idea how to set this up. Here is my code so far:</p> <pre><code>import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences.Editor; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.view.View.OnClickListener; import android.widget.EditText; import android.preference.PreferenceManager; public class wazeefa extends Activity { //Count Button TextView txtCount; Button btnCount; int count = 0; Button wmute; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.wazeefa); //button sound final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.countbutton); txtCount = (TextView)findViewById(R.id.wcount); txtCount.setText(String.valueOf(count)); btnCount = (Button)findViewById(R.id.wclick); btnCount.setOnClickListener(new OnClickListener() { public void onClick(View V) { count++; if (count &gt; 500) count = 0; txtCount.setText(String.valueOf(count)); mpButtonClick.start(); }}); ;} } </code></pre> <p>The new code looks like this after David's/Shreya's suggestions:</p> <pre><code>public void onClick(View V) { ImageView image = (ImageView) findViewById(R.id.imageview); count++; if (count &gt; 500) count = 0; if (count &lt; 1) image.setImageResource(R.drawable.duroodimage); if (count &gt; 0) image.setImageResource(R.drawable.duroodimage); if (count &gt; 100) image.setImageResource(R.drawable.zikrimage); if (count &gt; 400) image.setImageResource(R.drawable.duroodimage); txtCount.setText(String.valueOf(count)); mpButtonClick.start(); }}); ;} </code></pre> <p>But the images do not change when the relevant counts have been reached...</p> <p>XML code for the ImageView:</p> <pre><code>&lt;ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; </code></pre>
 

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