Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alert dialog box having list of item

In my android application, when i click on text view, i want to display an alert dialog box which contain list of items. How is it possible. kindly guide.

i code it as:

cus_name_txt = (TextView)findViewById(R.id.cus_name_txta);
    cus_name_txt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Onclick_click1(cus_name_txt);
            // TODO Auto-generated method stub

        }
    }); 

    contact_no_txt = (TextView)findViewById(R.id.contact_no_txta);


    attend_by_txtbx = (EditText)findViewById(R.id.attend_by_txt);
    attend_by_txtbx.setText(My_Task.attend_by_txt);


    ticket_no_txt = (TextView)findViewById(R.id.ticket_no_txta);


    task_detail_txt = (TextView)findViewById(R.id.task_detail_txt);

How can i get the alert box of list of items by clicking on textView. Please guide. I'll be thankful to u

like image 618
Mona Avatar asked Dec 08 '25 06:12

Mona


2 Answers

If you want to show the progressBar Before loading of the List in alert dialog, then use AsyncTask for that.

e.g.:

private class LoadingTask extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute(){
               super.onPreExecute();
               progressDialog.show();
        }

        @Override
        protected String doInBackground(String... str) {
            String response = "";
                    // Call Web Service here and return response

            response = API.getDealsByCategory(str[0], str[1]); 
                    // e.g.: above is my WebService Function which returns response in string
            return response;
        }
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            System.out.println("result is: "+result);


            new Thread(new Runnable() {
                @Override
                public void run() {
                    progressDialog.dismiss();
                }
            }).start();

        // SHOW THE ALERT DIALOG HERE.....  
        }
    }

Call AsyncTask as like below :

LoadingTask task = new LoadingTask(); task.execute("YOUR_PARAMETER","YOUR_PARAMETER");

//==============================

Just put below code in the Post Excution of the AsyncTask and you will get what you want.

final CharSequence[] items = {"","50","100","150","200","250","300","350","400","450","500","550","600","650","700","750","800","850","900","1000"};
        AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
        builder.setTitle("Select Country");
        //builder.setI
        builder.setItems(items, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int item) {
                //Toast.makeText(getApplicationContext(), con.get(item).getCountrName(), Toast.LENGTH_SHORT).show();
                selectDistanceTV.setText(items[item]);
                System.out.println("Item is: "+items[item]);
                /*CONTRY_ID = con.get(item).getCountryId();
                stateET.requestFocus();*/
           }
        });
        AlertDialog alert = builder.create();
        alert.show();

Hope it will help you.

If you need more help on how to use AsyncTask then see here:Vogella

Comment me for any query.

Enjoy Coding... :)

like image 184
Shreyash Mahajan Avatar answered Dec 10 '25 21:12

Shreyash Mahajan


Put following code in onClick of textView:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Color Mode");

ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);

builder.setView(modeList);
final Dialog dialog = builder.create();

dialog.show();

Or

Dialog dialog = new Dialog(**Your Context**);
  dialog.setContentView(R.layout.**Your Layout File**);
  dialog.show();

In this layout file you can make your layout as per your requirements. When you want to use ListView from your Dialog Layout file then you have to write

  ListView listView = (ListView)**dialog**.findViewById(R.id.**Your ListView Id**)
like image 43
Shrikant Ballal Avatar answered Dec 10 '25 20:12

Shrikant Ballal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!