Demo:
package
com.venky.dialogwithcheckboxsdemo;
import android.app.Activity;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
1)MainActivity.java:
import android.app.Activity;
import
android.app.AlertDialog;
import
android.content.Context;
import
android.content.DialogInterface;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.Toast;
public class MainActivity extends Activity {
Context ctx = this;
String[] items = { "Red", "Green", "Black", "White", "Yellow", "Orange" };
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)
findViewById(R.id.btn1);
btn.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View v)
{
AlertDialog.Builder
alert = new AlertDialog.Builder(ctx);
alert.setTitle("Select
Your Options");
alert.setMultiChoiceItems(items, null,
new
DialogInterface.OnMultiChoiceClickListener() {
@Override
public void
onClick(DialogInterface dialog,
int pos, boolean isChecked) {
Toast.makeText(ctx,
"U
Selected => " + items[pos], 3000)
.show();
}
});
alert.show();
}
});
}
}
2)main.xml:
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5F04B4"
android:orientation="vertical"
>
<Button
android:id="@+id/btn1"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#ffffff"
android:text="Click
Me"
android:textColor="#000000"
android:textSize="20dp"
android:textStyle="bold"
/>
</RelativeLayout>
3)Download this Project Click Here
No comments:
Post a Comment