Friday, 27 April 2018

Android switch statement example

Android switch statement example

  • Definition : Based on the name switch provide many options you select one of them.
  • switch statement is same as if-else condition.
  • default : (optional) It is used for if all options are wrong than default statement is call.
  • break : Break switch statement (optional)

Example of Android switch statement :

int position=2;     
//you can used string,intent,object id based on your requirements 
//here select position 
switch (position)
{
    case 0:
        Toast.makeText(getApplicationContext(),"0",Toast.LENGTH_SHORT).show();
        break;
    case 1:
        Toast.makeText(getApplicationContext(),"1",Toast.LENGTH_SHORT).show();
        break;
    case 2:
        Toast.makeText(getApplicationContext(),"2",Toast.LENGTH_SHORT).show();
        break;
    default://
        Toast.makeText(getApplicationContext(),"default",Toast.LENGTH_SHORT).show();
        break;
}

Output : 

2

World News Hub provide 100+ different type of newspapers and different kind of news in various languages.

Share This

1 comment: