james
james

Reputation: 157

trying to make a textView change with image in android studio

I have an ImageView and a TextView and in the middle of these i have a Button which when pressed will set a different image in the ImageView. What I want to achieve is when pressed, I would like the TextView to change also so that different text is displayed with each different image. Greatly appreciate some help with this. You will find the code below. Thank you.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class SecondTourActivity extends AppCompatActivity {
    private Integer images[] = {R.drawable.image5, R.drawable.image7, R.drawable.image8, R.drawable.image9,
    R.drawable.image10, R.drawable.image11, R.drawable.image12, R.drawable.image13, R.drawable.image14,
            R.drawable.image15, R.drawable.image16, R.drawable.image17, R.drawable.image18,};
    private int currImage = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_tour);


        setInitialImage();
        setImageRotateListener();
    }
    private void setImageRotateListener() {
        final Button rotatebutton = (Button) findViewById(R.id.btnRotateImage);
        rotatebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                currImage++;
                if (currImage == 12) {
                    currImage = 0;
                }
                setCurrentImage();
            }
        });
    }

    private void setInitialImage() {
        setCurrentImage();
    }

    private void setCurrentImage() {

        final ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageResource(images[currImage]);

    }

}

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="jamie2dog.com.listintent.SecondTourActivity"
    android:background="#020202">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/image5" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="This first picture is liverpool lime street station, taken in 2008, continue to next image for more details."
        android:id="@+id/textView"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:textColor="#FFFFFF"
        android:layout_marginTop="50dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Next image"
        android:id="@+id/btnRotateImage"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Upvotes: 0

Views: 1700

Answers (1)

Henry
Henry

Reputation: 17851

     @Override
     public void onClick(View arg0) {
          currImage++;
          if (currImage == 12) {
                currImage = 0;
          }
          setCurrentImage();
          final TextView textView= (TextView) findViewById(R.id.textView);
          textView.setText("What ever Text you want to set")
     }

Use the above code. The idea is to have a HashMap where for each image, you have a text (or a Model class with Image and Text as the fields). So for each image you can get the text and set the text for the TextView.

Do something like this:

    final HashMap<Integer, Integer> map = new HashMap<>();
    map.put(R.drawable.image5, R.string.text5);
    // add all other things to this map
    // obtain the map here
    int text5 = map.get(R.drawable.image5);
    String text = getResources().getString(text5);

Upvotes: 1

Related Questions