Monday 31 July 2017

Dialog with MultipleChoice ListView

ScreenShots:





strings.xml:

<resources>     <string name="app_name">Dialog with Multiple Choice</string>     <string-array name="ITEMS">         <item>Item1</item>         <item>Item2</item>         <item>Item3</item>         <item>Item4</item>         <item>Item5</item>     </string-array> </resources>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">
    <Button         android:id="@+id/button_dialogList"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Dialog List" />     <TextView         android:id="@+id/textview"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginLeft="10dp"         android:layout_marginTop="20dp"         android:textSize="30sp"         android:textStyle="bold" /> </LinearLayout>
MainActivity.java:
package com.ramsandroid.dialogwithmultiplechoice; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener {     ArrayList<Integer> selectedItems;     TextView textView;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         selectedItems = new ArrayList<>();         textView = (TextView) findViewById(R.id.textview); Button button_dialogList = (Button) findViewById(R.id.button_dialogList);         button_dialogList.setOnClickListener(this);     }     @Override    public void onClick(View v) {   AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);         builder.setTitle("Select Item");         builder.setMultiChoiceItems(R.array.ITEMS, null, new DialogInterface.OnMultiChoiceClickListener() {             @Override             public void onClick(DialogInterface dialog, int which, boolean isChecked) {                 if (isChecked) {                     selectedItems.add(which);                 } else
if (selectedItems.contains(which)) {           selectedItems.remove(Integer.valueOf(which));                 }             }         });         builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {             @Override             public void onClick(DialogInterface dialog, int which) {                 for (Integer i : selectedItems) {     String[] ITEMS = getResources().getStringArray(R.array.ITEMS);                     textView.append(ITEMS[i] + "\n");               }             }         });         builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {             @Override       public void onClick(DialogInterface dialog, int which) {                 selectedItems.clear();             }         });         builder.create().show();     } }

10 comments:

  1. Nice blog..! I really loved reading through this article... Thanks for sharing such an amazing post with us and keep blogging.
    android training

    ReplyDelete
  2. It's not easy to promote your app on the Google play store. You can buy Android App Review online from our site buyappreviewsandroid.com which is essential for your Android app on the Internet. Our app reviews will help you promote your app on Google play store.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Appreciate it! really helpful information.Krishna IT Training is a great platform for Online Android Training . grab a chance to enhance your technical skills with our wide range of It courses.

    ReplyDelete
  5. The Web Design Company in Dubai helps in checking the local intent query and uses the approach for ranking the website in the popular search engines like Google, Yahoo, etc. by finding the keywords. Simply by typing the keywords in the tool, one can come to know about the search volume.

    Visit Website www.wscentre.com

    ReplyDelete
  6. Nice Article, Blog theme is also very user friendly. Tech information is also good on this blog. Also checkout - Programming Languages need to learn for future success
    Thanks

    ReplyDelete
  7. Maxwell Global Software is No.1 mobile app development company in Qatar provides iPhone Apps, Android Apps, iOS Apps, Blackberry Apps, Windows Apps at affordable cost

    Best mobile app development company Qatar

    ReplyDelete
  8. App reviews are extremely important. This is why it is important to get and Buy App Reviews for your application as it is soon launched. With the help of Buy App Reviews Android, you will only get the best. In order to make sure that your business is competitive, it is important to Buy App Reviews from a reputable company. We offer services such as a review alert service that lets you know when your company's name pops up in a review or on social media. Contact now!

    ReplyDelete