How can I display random activity when click button - android

I used following code in main activity
public void button(View v){
//Create an intent to start the new activity.
// Our intention is to start secondActivity
Intent intent = new Intent();
intent.setClass(this,Activity.class);
startActivity(intent);
}
How can I display random activity when click button? Please help me!

debugged some codes above because there's an error in setClass().. this one works:
Random rnd = new Random();
int x=rnd.nextInt(3)+1;
Intent myIntent = new Intent();
switch(x){
case 1:
myIntent.setClass(view.getContext(),Scrn1.class);
break;
case 2:
myIntent.setClass(view.getContext(), Scrn2.class);
break;
case 3:
myIntent.setClass(view.getContext(), Scrn1.class);
break;
}
startActivity(myIntent);

Store the name of all your activities in an array, generate random number and get activity corresponding to random generated number. Sample snippet
String[] title = new String[] { "Act1.class", "Act2.class","Act1.class","Act4.class","Act5.class"};
public String getRandomActivity(){
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(5);// pass number of elements as parameters
return title[randomInt-1];//this should return class name
}
Intent intent = new Intent(this,getRandomActivity())
startActivity(intent)

Try this, assume you have 3 activities:
public void button(View v){
Random rnd = new Random();
int x=rnd.nextInt(3)+1;
Intent intent = new Intent();
switch(x){
case 1:
intent.setClass(this,Activity1.class);
break;
case 2:
intent.setClass(this,Activity2.class);
break;
case 3:
intent.setClass(this,Activity3.class);
break;
}
startActivity(intent);
}

Related

How to send String[] imageUrl to another activity

I am having array of image URL.On button click I want to send selected image URL to another activity.
Main.java
String[] imageUrl={"https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png","https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png", "https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png"};
Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse("******");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent)
}
});
OpenImage.java
ImageView image = (ImageView)findViewById(R.id.imageview);
What to write here next
In your First Activity,
String[] data = {"Hello", "World"};
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("some_key", data);
startActivity(intent);
Then in you Sencon Activity,
// At class level
private static final String TAG = SecondActivity.class.getSimpleName();
// In onCreate
String[] data = getIntent().getExtras().getStringArray("some_key");
for (String x : data) {
Log.i(TAG, x);
// Toast to display all you values one by one
Toast.makeText(SecondActivity.this, x, Toast.LENGTH_SHORT).show();
}
Hope this helps...:)
Try This:
Bundle bundel = new Bundle();
bundel.putStringArray("key",array);
Intent intent = new Intent(this,next.class)
intent.putExtras(bundel);
startActivity(intent);
or just
intent.putExtra("strings", myStrings);
the putExtra has many overloads, pass array of primitive type is one of them :)
Use This ti send another Activity...
Intent intent1 = new Intent(Intent.ACTION_VIEW, uri);
Bundle bundle = new Bundle();
bundle.putStringArray("ArrayURL", imageUrl);
intent1.putExtras(bundle);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent1);
and For Getting
Bundle b = getArguments();
Cat_Name = b.getStringArray("ArrayURL");

I tried to pass the values from one to another page but it doesn't work?

I want to pass the values from one page to another page my code is:
public class main extends Activity implements OnClickListener {
private static final String TAG = "AskMeShowMe2";
private String choice = null;
private String fname = null;
private Menu myMenu = null;
final static int START =0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG,"Main - onCreate() ...");
setContentView(R.layout.activity_main);
View promptButton = findViewById(R.id.submit_button);
promptButton.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.greetingsRadioGroup);
int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
switch (checkedRadioButton) {
case R.id.mrButton :
choice = "Mr.";
break;
case R.id.mrsButton :
choice = "Mrs.";
break;
case R.id.msButton:
choice = "Ms.";
break;
case R.id.drButton:
choice = "Dr.";
break;
}
switch(v.getId()){
case R.id.submit_button:
Intent j = new Intent(this, Info.class);
j.putExtra("choice", choice);
startActivity(j);
fname=String.valueOf(R.id.firstname);
Intent t = new Intent(this, Info.class);
j.putExtra("fname", fname);
startActivity(t);
break;
}
}
and it other Info.Class :
public class Info extends Activity {
private static final String TAG = "Info";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
Intent myIntent = getIntent();
Bundle b = myIntent.getExtras();
String choice = b.getString("choice");
String fname = b.getString("fname");
Log.i(TAG,"selection: " + choice);
TextView textView = (TextView) findViewById(R.id.showmeText);
textView.append(": " + choice);
TextView textView1 = (TextView) findViewById(R.id.fname);
textView1.append(": " + fname);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
}
when i run this program then my Info.class page doesn't show when i click the submit button,Can you please help me how to fix it?
Change Switch block to following:
switch(v.getId()){
case R.id.submit_button:
Intent j = new Intent(this, Info.class);
j.putExtra("choice", choice);
fname=String.valueOf(R.id.firstname);
j.putExtra("fname", fname);
startActivity(j);
break;
}
As you are starting Activity Info two times, Putting extras to only one intent, which is being started earlier.
use
Intent j = new Intent(this, Info.class);
j.putExtra("choice", choice);
fname=String.valueOf(R.id.firstname);
j.putExtra("fname", fname);
startActivity(j);
instead of
Intent j = new Intent(this, Info.class);
j.putExtra("choice", choice);
startActivity(j);
fname=String.valueOf(R.id.firstname);
Intent t = new Intent(this, Info.class);
j.putExtra("fname", fname);
startActivity(t);
becuase currently you are calling startActivity two times first for intent j and second time for intent t so remove one and put both choice and fname in single intent and call startActivity only once.

Android Code - Radom number generation for calling activity [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Generating random number in a range with Java
How can I generate random number in specific range in Android?
Here is my scenario
From the Mainactivity ; On click of a Button i want to generate a random number from 1 to 4
Based on the output, i want to write an if-else that will call 4 different activities
So on click, if 4 is generated, then call activity 4
Next time 1 can be generated and should call activity 1
and so on ....
Can someone please help me with this code?
myBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Random r = new Random();
int index = r.nextInt(4)+1;
Intent intent;
if (index == 4) {
intent = new Intent(Activity.this, Activity4.class);
}
else if (index == 3) {
intent = new Intent(Activity.this, Activity3.class);
}
else if (index == 2) {
intent = new Intent(Activity.this, Activity2.class);
}
else
intent = new Intent(Activity.this, Activity1.class);
startActivity(intent);
}
});
public void test1(){
Random r = new Random();
int index = r.nextInt(4)+1;
Intent intent = null;
switch(index){
case 1: intent = new Intent(this, Activity1.class);
break;
case 2: intent = new Intent(this, Activity2.class);
break;
case 3: intent = new Intent(this, Activity3.class);
break;
case 4: intent = new Intent(this, Activity4.class);
break;
default:
Log.e("ERROR", "");
return;
}
if(intent != null){
this.startActivity(intent);
}
}

How to pass integer from one Activity to another?

I would like to pass a new value for an integer from one Activity to another.
i.e.:
Activity B contains an
integer[] pics = { R.drawable.1, R.drawable.2, R.drawable.3}
I would like activity A to pass a new value to activity B:
integer[] pics = { R.drawable.a, R.drawable.b, R.drawable.c}
So that somehow through
private void startSwitcher() {
Intent myIntent = new Intent(A.this, B.class);
startActivity(myIntent);
}
I can set this integer value.
I know this can be done somehow with a bundle, but I am not sure how I could get these values passed from Activity A to Activity B.
It's simple. On the sender side, use Intent.putExtra:
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);
On the receiver side, use Intent.getIntExtra:
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
Their are two methods you can use to pass an integer. One is as shown below.
A.class
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);
B.class
Intent intent = getIntent();
int intValue = intent.getIntExtra("intVariableName", 0);
The other method converts the integer to a string and uses the following code.
A.class
Intent intent = new Intent(A.this, B.class);
Bundle extras = new Bundle();
extras.putString("StringVariableName", intValue + "");
intent.putExtras(extras);
startActivity(intent);
The code above will pass your integer value as a string to class B. On class B, get the string value and convert again as an integer as shown below.
B.class
Bundle extras = getIntent().getExtras();
String stringVariableName = extras.getString("StringVariableName");
int intVariableName = Integer.parseInt(stringVariableName);
In Activity A
private void startSwitcher() {
int yourInt = 200;
Intent myIntent = new Intent(A.this, B.class);
intent.putExtra("yourIntName", yourInt);
startActivity(myIntent);
}
in Activity B
int score = getIntent().getIntExtra("yourIntName", 0);
In Sender Activity Side:
Intent passIntent = new Intent(getApplicationContext(), "ActivityName".class);
passIntent.putExtra("value", integerValue);
startActivity(passIntent);
In Receiver Activity Side:
int receiveValue = getIntent().getIntExtra("value", 0);

Make randomly generated ImageButtons clickable

I'm having trouble with making my randomly generated image, which is interpreted as a button, become clickable. Each leads to a different activity.
The random images work perfect actually, the only problem it's not clickable.
Here's my Main.java:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final List<String> images = new ArrayList<String>();
for (int i=1; i<=13; i++) {
images.add("img"+i);
}
final Button imgView = (Button)findViewById(R.id.top1);
String imgName = null;
int id = 0;
Collections.shuffle(images, new Random());
imgName = images.remove(0);
imageRandomizer(imgName, id, imgView);
}
public void imageRandomizer(String imgName, int id, final Button imgView) {
id = getResources().getIdentifier(imgName,
"drawable",
getPackageName());
imgView.setBackgroundResource(id);
}
}
On my layout, I specified the id top1 as a Button. So the above code will look up to my drawable images, which have the names img1.jpg, img2.jpg, img3.jpg , until img13.jpg.
Making an ImageButton clickable to one activity without being dependent on the shown random image is easy, I can do it without problem.
But what I wanna make is something like, when img1.jpg is generated, it becomes clickable and leads to Activity1.java, for img2.jpg the intent goes to Activity2.java, etc.
EDIT
#Roflcoptr
Here's my OnClickListener:
private OnClickListener top_listener = new OnClickListener() {
public void onClick(View v) {
switch((Integer) v.getTag()) {
case 1:
Intent aid = new Intent(Main.this, ProjektAID.class);
startActivity(aid);
case 2:
Intent adh = new Intent(Main.this, ProjektADH.class);
startActivity(adh);
case 3:
Intent bos = new Intent(Main.this, ProjektBOS.class);
startActivity(bos);
case 4:
Intent brot = new Intent(Main.this, ProjektBROT.class);
startActivity(brot);
case 5:
Intent care = new Intent(Main.this, ProjektCARE.class);
startActivity(care);
case 6:
Intent caritas = new Intent(Main.this, ProjektCARITAS.class);
startActivity(caritas);
case 7:
Intent doc = new Intent(Main.this, ProjektDOC.class);
startActivity(doc);
case 8:
Intent drk = new Intent(Main.this, ProjektDRK.class);
startActivity(drk);
case 9:
Intent give = new Intent(Main.this, ProjektGIVE.class);
startActivity(give);
case 10:
Intent hive = new Intent(Main.this, ProjektHIV.class);
startActivity(hive);
case 11:
Intent jo = new Intent(Main.this, ProjektJOHANNITER.class);
startActivity(jo);
case 12:
Intent kind = new Intent(Main.this, ProjektKINDERHERZ.class);
startActivity(kind);
case 13:
Intent kult = new Intent(Main.this, ProjektKULTURGUT.class);
startActivity(kult);
}
}
};
and here's the randomizer method:
public void imageRandomizer(String imgName, int id, final Button imgView) {
id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setBackgroundResource(id);
imgView.setTag(new Integer(1)); //example for image 1
if (imgName.equals("img1")) {
imgView.setTag(new Integer(1)); //example for image 1
} else if (imgName.equals("img2")) {
imgView.setTag(new Integer(2));
} else if (imgName.equals("img3")) {
imgView.setTag(new Integer(3));
} else if (imgName.equals("img4")) {
imgView.setTag(new Integer(4));
} else if (imgName.equals("img5")) {
imgView.setTag(new Integer(5));
} else if (imgName.equals("img6")) {
imgView.setTag(new Integer(6));
} else if (imgName.equals("img7")) {
imgView.setTag(new Integer(7));
} else if (imgName.equals("img8")) {
imgView.setTag(new Integer(8));
} else if (imgName.equals("img9")) {
imgView.setTag(new Integer(9));
} else if (imgName.equals("img10")) {
imgView.setTag(new Integer(10));
} else if (imgName.equals("img11")) {
imgView.setTag(new Integer(11));
} else if (imgName.equals("img12")) {
imgView.setTag(new Integer(12));
}
else if (imgName.equals("img13")) {
imgView.setTag(new Integer(13));
}
}
I would use a tag to identify the button. So in your imateRandomizer add a unique ID for each possible Image. I don't know how you can identify the images uniquely, but I'll show the example here for the name:
public void imageRandomizer(String imgName, int id, final Button imgView)
{
id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setBackgroundResource(id);
if (imgName.equals("Name of the Image for your first activity") {
imgView.setTag(new Integer(1)); //example for image 1
} else if (imgName.equals("Name of the Image for your second activity") {
imgView.setTag(new Integer(2));
}
}
And then In your ClickListener you can check which tag the button has:
imgView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch((Integer) v.getTag()) {
case 1: //start Activity 1;
break;
case 2: //start Activity 2;
break;
}
}
});

Categories

Resources