Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6834433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:06:41+00:00 2026-05-26T23:06:41+00:00

I have a button in android which on clicking produces an alert box. But

  • 0

I have a button in android which on clicking produces an alert box. But i don’t know how to capture the selected value from the alert box. My code is

btn=(Button)findViewById(R.id.btn);

String[] items = new String[] {"name", "doc", "age"};
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, items);

btn.setText("Select One");

 btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            new AlertDialog.Builder(context)
              .setTitle("Select Airport")
              .setAdapter(adapter, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {



                  dialog.dismiss();
                }
              }).create().show();

        }

    });

Please help. Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:06:41+00:00Added an answer on May 26, 2026 at 11:06 pm

    In your code

    int which
    

    argument of onClick event of AlertDialog returns the number of position of the item that is selected.

    For checking you just Toast a message when you select any item from AlertDialog

    Code :

    btn.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
            new AlertDialog.Builder(context)
              .setTitle("Select Airport")
              .setAdapter(adapter, new DialogInterface.OnClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which) {
    
                  Toast.makeText(YourActivity.this,"You Selected : "+items[which],Toast.LENGTH_SHORT).show();
    
                  dialog.dismiss();
                }
              }).create().show();
    
        }
    
    });
    

    You can refer Android’s Official Guide On Dialogs for more information about Dialogs in Android

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an activity which have an Update button as menu for android.
I am writing a simple android app which have a edittext and a button.
I am developing an android application in which I have passed a string from
I have an activity in android which I set up a button under a
Android 4.0 phones only have virtual buttons, which actually go invisible when playing youtube/video
I have a button like this: <Button android:id=@+id/logout_button android:layout_width=fill_parent android:layout_height=wrap_content android:text=Log out /> and
I have a button that basically looks like this: <Button android:id=@+id/admin_new_questions android:layout_width=fill_parent android:layout_height=wrap_content android:text=See
I have a radio button group in Android. I receive events when the items
I have created a button in my Android application & I tried to set
I have to create a custom compound button bar control in Android like the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.