This tutorial will help you to understand how to use of switch-case statement in an android project. This for beginners who just started android programming with Java. Besides it also explains how to create a dialog for displaying a message on a wall. You may familiar with the basic idea of switch statement from any other language like C or C++; The switch statement works with primitive data types so you can use it with any context.
Before you start with this tutorial, you need to know why we use switch. It will help to manage multiple items through a condition. You can take appropriate decision according to the condition using switch.
Switch statement in Java
public class
SExample
{
public static void main(String[] args)
{
int num=2;
String value;
switch(num)
{
case 1: value="Iam one";
break;
case 2: value="Iam two";
break;
default : value="No Number";
}
System.out.println("value
is"+value);
}
}
package com.bigknol.swithsample; import android.app.Activity; import android.app.Dialog; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class Main extends Activity implements OnClickListener { Button b1,b2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(R.id.ShowDialog); b2=(Button)findViewById(R.id.ShowToast); b1.setOnClickListener(this); b2.setOnClickListener(this); } public void onClick(View v) { switch(v.getId()) { case R.id.ShowToast: Toast.makeText(getApplicationContext(), "Iam toast Message", Toast.LENGTH_SHORT).show(); break; case R.id.ShowDialog: Dialog d=new Dialog(this); d.setTitle("Iam Title"); TextView tv=new TextView(this); tv.setText("Oh! Iam Here!"); d.setContentView(tv); d.show(); break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Main" > <Button android:id="@+id/ShowToast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="141dp" android:text="Show Toast" /> <Button android:id="@+id/ShowDialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/button1" android:layout_marginTop="55dp" android:text="Show Dialog" /> </RelativeLayout>
Video Tutorial : Android Switch Case [Example]
Android Tutorial: Using Switch Case Statement
Reviewed by Nikin
on
09 March
Rating: