Mustafa
Mustafa

Reputation: 775

Implement an Image Change when textview is changed

I am setting up a Click Counter android app. The maximum count is 500, and the count is displayed in a TextView field.
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:

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 > 500) 
               count = 0;
           txtCount.setText(String.valueOf(count));
           mpButtonClick.start(); 
        }});
    ;}
    }

The new code looks like this after David's/Shreya's suggestions:

public void onClick(View V) {
        ImageView image = (ImageView) findViewById(R.id.imageview);
           count++; 
           if (count > 500) count = 0;
           if (count < 1) image.setImageResource(R.drawable.duroodimage); 
           if (count > 0) image.setImageResource(R.drawable.duroodimage);
           if (count > 100) image.setImageResource(R.drawable.zikrimage); 
           if (count > 400) image.setImageResource(R.drawable.duroodimage);
           txtCount.setText(String.valueOf(count));
           mpButtonClick.start(); 
        }});
    ;}

But the images do not change when the relevant counts have been reached...

XML code for the ImageView:

<ImageView
    android:id="@+id/imageview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Upvotes: 1

Views: 188

Answers (1)

David Fang
David Fang

Reputation: 1787

You can put the code to change image in the btnCount's onClickListener.

btnCount.setOnClickListener(new OnClickListener() {
    public void onClick(View V) {
        ImageView image = findViewById(...); // ... is the id of ImageView
        count++; 
        if (count > 500) count = 0;
        if (count > 100) image.setDrawable(...); // 100~200
        if (count > 200) image.setDrawable(...); // 200~300
        // ... and then 300~400 and 400~500
        txtCount.setText(String.valueOf(count));
        mpButtonClick.start(); 
    }
});

Upvotes: 1

Related Questions