• TekArt

    TekArt is an Organisation where people develop Android App through innovative ideas. App for the next Generation....

Wednesday 2 April 2014

Posted by Unknown
1 comment | 06:37
Hello guys, How are you today?? Hope, fine. Today we are going to see about how to create popup selector in Android. So , lets get started.


Download Pop up Selector

1) First of all create an Android Application Project.

2) Now go to "res/layout/activity_main.xml" and create a button. For our case we made the button "show popup". The code is here.

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/popup"
       android:text="Show pop up"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"/>

</RelativeLayout>

3) Now we have to implement OnClickListener for this button, so we implement OnClickListener in the MainActivity class. OnClicking the button popup window should be created.So for creating the popup we need to use AlertDialog.Builder . The code for building the popup is below.

private void build_popup() {
       // AlertDialog Builder for building our builder
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Demo Popup");

   
        // we provided single selected options only. For multiple selections you can use the function
// builder.setMultipleChoiceItems(syntax is same to setSingleChoiceItems)
        builder.setSingleChoiceItems(R.array.arr, selected, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                temp = which;
            }

           
           
        });
       
       // for building Ok button
        builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
           
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                selected = temp;
               
                switch(selected) {
               
                case 0:text = "Bad";break;
               
                case 1:text = "Good";break;
               
                case 2:text = "Very Good";break;
               
                case 3:text = "Average";break;
               
                }
                Toast.makeText(MainActivity.this, "You Selected " + text, Toast.LENGTH_LONG).show();
               
            }
        });
       
    //for building Cancel button
        builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
           
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
            dialog.cancel();   
            }
        });
       
        AlertDialog al =builder.create();
        al.show();
    }

I have written the comments which will help you guys to better understand the code.

4) So our full code looks like.

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
    Button popup;
    int selected =0;
    int temp;String text="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        popup = (Button) findViewById(R.id.popup);
        popup.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()) {
        case R.id.popup: build_popup();break;
        }
    }

    private void build_popup() {
        // TODO Auto-generated method stub
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Demo Popup");
       
        builder.setSingleChoiceItems(R.array.arr, selected, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                temp = which;
            }

           
           
        });
       
        builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
           
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                selected = temp;
               
                switch(selected) {
               
                case 0:text = "Bad";break;
               
                case 1:text = "Good";break;
               
                case 2:text = "Very Good";break;
               
                case 3:text = "Average";break;
               
                }
                Toast.makeText(MainActivity.this, "You Selected " + text, Toast.LENGTH_LONG).show();
               
            }
        });
       
        builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
           
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
            dialog.cancel();   
            }
        });
       
        AlertDialog al =builder.create();
        al.show();
    }

}

Now we are done.

For any queries post in the comment.

For more info visit Androidcoolstuffs.

Thank you.












1 comment: