I want my app to be such that when the editText1 has value (2) without brackets in it, the button press (with onClick="k") would navigate to MainActivity2. And if the text is (3), the activity MainActivity3 would open. I guess it'll use If function. How can I just implement this in my app? Sorry for silly questions, I'm just a newbie. ;-;
-
(with onClick="k") -- what is k?.. button??.. or character in keyboard?.. – uday Aug 10 '15 at 12:07
-
@uday k is the android:onClick of my button. – Dhairyadev Arora Aug 10 '15 at 12:09
-
can you post any code? – Iamat8 Aug 10 '15 at 12:14
-
String string = editText1.getText().toString();{ switch(string){ case "2": public void kden(View v) { // TODO Auto-generated method stub Intent i = new Intent(getApplicationContext(),MainActivity2.class); startActivity(i); } break; case "3": public void kden(View v) { // TODO Auto-generated method stub Intent i = new Intent(getApplicationContext(),MainActivity3.class); startActivity(i); } @Mohit here's my code till now. It isn't working. It's showing errors. – Dhairyadev Arora Aug 10 '15 at 12:17
6 Answers
I'm not sure why exactly you're doing this, but here's what you can do:
Set the
EditText
property to accept only digits/numbersonClick()
of the button, access the number entered in EditText byeditText1.getText().toString()
After you've done that, use
switch-case
statements to launch the desired activity.

- 4,776
- 2
- 26
- 48
-
Could you please elaborate it a bit more? I'm a beginner and can't really understand that. ._. i've anyway typed this: String string = editText1.getText().toString();{ switch(string){ case "2": public void k(View v) { // TODO Auto-generated method stub Intent i = new Intent(getApplicationContext(),MainActivity2.class); startActivity(i); } break; case "3": public void k(View v) { // TODO Auto-generated method stub Intent i = new Intent(getApplicationContext(),MainActivity3.class); startActivity(i); } – Dhairyadev Arora Aug 10 '15 at 12:15
Try this:-
String string = editText1.getText().toString();
if(string.equals("2"))
{
Intent i = new Intent(getApplicationContext(),MainActivity2.class);
startActivity(i);
}
else if(equals("3"))
{
Intent i = new Intent(getApplicationContext(),MainActivity3.class);
startActivity(i);
}

- 52
- 2
Remove the methods inside the switch-case
case "2":
public void kden(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(),MainActivity2.class);
startActivity(i);
}
break;
and try this
case "2":
Intent i = new Intent(getApplicationContext(),MainActivity2.class);
startActivity(i);
break;

- 1,348
- 12
- 27
First, retrieve the value from the EditText
. If you only want digits/numbers then you can set this in the properties for theEditText
. For now, we'll leave it as accepting string
values. Assuming the identifier for your EditText
is 'editText1':
String targetActivity = editText1.getText().toString();
Secondly, you need to use conditional statements to evaluate which Activity
to launch. @Sree used switch-case
in his answer, here I will use if
statements as they are much easier to read and understand.
Intent i;
if(targetActivity.equals("k")){
i = new Intent(YourCurrentActivity.this, StartSomeActivity.class);
startActivity(i);
}
else if(targetActivity.equals("3")){
i = new Intent(YourCurrentActivity.this, StartAnotherActivity.class);
startActivity(i);
}
Obviously you can change what values to compare against, I used k
and 3
as those were the examples you gave in your question. Also be sure to swap out 'YourCurrentActivity' for the current Activity name and 'StartSomeActivity' and 'StartAnotherActivity' with the names of the desires activites you want to launch.

- 1,863
- 2
- 24
- 43
-
-
Yes @scott target activity is the one to which I wanna move the screen to, right? They are two. MainActivity2 and MainActivity3. – Dhairyadev Arora Aug 12 '15 at 13:57
-
At least post the code properly in comment...
- Before using switch case with
String
read Why can't I switch on a String?
And with value you can use this on your button click event
int key = Integer.parseInt(edit.getText().toString());
Intent i;
switch (key) {
case 1:
i = new Intent(MainActivity.this,MainActivity2.class);
break;
case 2:
i = new Intent(MainActivity.this,MainActivity3.class);
break;
}
startActivity(i);