lundi 15 mai 2017

Show random image from ,,drawable" folder in Android

package com.example.paul_2.a5mai;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        start();


    }
    void start () {

//root layout
        LinearLayout root = new LinearLayout(this);
        root.setOrientation(LinearLayout.VERTICAL);
        root.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
        setContentView(root);

//butonul de start
        Button StartBTN = new Button(this);
        StartBTN.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
        StartBTN.setText("Click Me");
        root.addView(StartBTN);


// contine toate cele 5 layouturi
        final LinearLayout linearContainer=new LinearLayout(this);
        linearContainer.setOrientation(LinearLayout.HORIZONTAL);
        linearContainer.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
        root.addView(linearContainer);

//LINIA 1
        final LinearLayout firstRow = new LinearLayout(this);
        firstRow.setOrientation(LinearLayout.VERTICAL);
        firstRow.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT));
        linearContainer.addView(firstRow);

        //dimensiunea imaginii
        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(200,200);
        //params1.leftMargin =10;
        //params1.topMargin = 10;

        //final ImageView cherry = new ImageView(this);
        //cherry.setBackgroundResource(R.drawable.cherry1);
        //firstRow.addView(cherry,params1);

        final ImageView imgView = new ImageView(this);
        Random rand = new Random();
        int rndInt = rand.nextInt(n) + 1; // n = the number of images, that start at idx 1
        String imgName = "img" + rndInt;
        int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
        imgView.setImageResource(id);
        firstRow.addView(imgView, params1);
    }
}

I need help with understand what should I do for display "imgView" randomly from 3 different images that are in "drawable" folder. First, I don't know I should write after String imgName= ?. Second, the value of rndInt must to be 3, I mean n=3? (in case I have 3 images saved in "drawable" folder. Please someone who can help me for this because I need this for school. Thank you in advance!




Aucun commentaire:

Enregistrer un commentaire