0

I have a layout with 1 textview, 1 imageview and 1 button. And I have 5 pictures in drawable. Their names like image1, image2, image3... When I click button, button change random picture in drawable("image" + rand. numbers method) But I want to print changed image name to textview. How can I do it? *Briefly; Button Funcion: First print existing image > then change new random image. **I'm sorry for bad grammer..

public void onClick(View v) {
    ImageView img=(ImageView)findViewById(R.id.imageView1);
    Random rand = new Random();
    int rndInt = rand.nextInt(5) + 1;
    String drawableName = "image"+ rndInt;
    int resID = getResources().getIdentifier(drawableName, "drawable", getPackageName());
    img.setImageResource(resID);
};
Gavriel
  • 18,880
  • 12
  • 68
  • 105
Eren
  • 75
  • 8
  • No it's different? My question is, how can I print existing random picture name to textview after then I want to change new random image all of it in 1 button.. – Eren Feb 03 '16 at 22:45
  • Eren, maybe if you show your code we'll understand better what you want – Gavriel Feb 03 '16 at 22:46
  • Can we talk with another platform? Maybe skype or fb? – Eren Feb 03 '16 at 22:53
  • https://chat.stackoverflow.com/rooms/102527/eren – Gavriel Feb 03 '16 at 22:58
  • public void onClick(View v) { ImageView img=(ImageView)findViewById(R.id.imageView1); Random rand = new Random(); int rndInt = rand.nextInt(5) + 1; String drawableName = "image"+ rndInt; int resID = getResources().getIdentifier(drawableName, "drawable", getPackageName()); img.setImageResource(resID); }; This code in my button. When I click button system change random image in my drawable folder. I want to print image name in the textview for example system change image2, image2 in the imageview now, I want to print textview to image2 i guess i can tell now – Eren Feb 03 '16 at 23:02
  • I don't have enough reputation.. – Eren Feb 03 '16 at 23:02

1 Answers1

0

With the following code you'll always see the currently displayed image's name:

String  lastImageName = "";

public void onClick(View v) {
    ImageView img=(ImageView)findViewById(R.id.imageView1);
    Random rand = new Random();
    int rndInt = rand.nextInt(5) + 1;
    String drawableName = "image"+ rndInt;
    int resID = getResources().getIdentifier(drawableName, "drawable", getPackageName());
    img.setImageResource(resID);

    // just set the text in the textview. You need to have it in your layout xml of course
    TextView textView = (TextView)findViewById(R.id.logoismi);
    textView.setText(lastImageName);

    lastImageName = drawableName;
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/bos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/dogru"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="BOŞ" />

    <ImageView
        android:id="@+id/logolar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bos"
        android:layout_marginBottom="70dp"
        android:maxHeight="800dp"
        android:maxWidth="800dp"      
        android:layout_centerInParent="true"
        android:scaleType="centerInside"
        android:src="@drawable/image1" />

    <TextView
        android:id="@+id/logoismi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dogru"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="52dp"
        android:text="image1" />

</RelativeLayout>
Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • Thanks Gavriel, but this code first change image after then print new image name. I need first existing image name after then change new image and loop... – Eren Feb 03 '16 at 23:10
  • Show your layout xml – Gavriel Feb 03 '16 at 23:13
  • http://pastebin.com/bHMQKtti – Eren Feb 03 '16 at 23:17
  • Actually I show you my project via teamviewer and we should talk live about it, it will be better i think. – Eren Feb 03 '16 at 23:18
  • Gavriel, imagine it, my button name is "Correct!" we have an random image and I should print last image name to textview. We don't need new image name. We need before image name because it may be true, we can not say anything about the new image.. Maybe it will no correct.. – Eren Feb 04 '16 at 17:05
  • Eren, see my edit. This will work as long as your activity is visible. If you want it to "remember" the lastImageName even if you click home or back button. then later open the app again, then you'll need to save lastImageName to SharedPreferences – Gavriel Feb 04 '16 at 17:13