Pages

Tuesday, March 27, 2012

[Android] Create a dialog with list and EditText

You may have been able to create a dialog with list using AlertDialog,

you may also find some articles to make a dialog with a EditText.

But how if we can combine them?

I mean, isn't it better that we can create the dialog with several choices and  an "Other" choice to let the user enter their own answer in some cases?


In fact, the workaround is more simpler than I was thought, I just implement the onCreateDialog method to create a AlertDialog with several choices, and create another AlertDialog with another View contains an EditText if the user choose the "Other".

Here is my example:
I want to record the users' activity with time.
------------------------------------------------------------
public class WriteTagActivity extends Activity {
    private final String sOutFile = "XXX.txt";
    private final Context m_context = this;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //do some pre-process here
}

      
        Button tagButton = (Button) findViewById(R.id.tagButton);
        tagButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
               
                showDialog(0);
            }

        });
       
    }
   
    private void writeTag(String tag)
    {
        long curTime = System.currentTimeMillis() / 1000;

       
        String sCurTime = Long.toString(curTime);
        try {
            FileWriter fout = new FileWriter(sOutFile,true);
            fout.write(sCurTime + "," + tag + "\n");
            fout.close();
           
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
       
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        final CharSequence[] tagList = {"party","dinner","exercising","studying","others(unrecommend)"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
       
        builder.setTitle("What are you doing now?");

       
        builder.setItems(tagList, new DialogInterface.OnClickListener() {
           
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                String tag = tagList[which].toString();
                String otherTags = tagList[tagList.length -1].toString();
                if(tag.compareTo(otherTags) == 0 )//custom tags
                {                       
                    createNewView();
                }
                else
                {
                    writeTag(tag);
                    Toast.makeText(m_context, "add " + tag + " to log...", Toast.LENGTH_SHORT).show();
                }
                   
            }
        });
       
       
        return builder.create();
    }
   

    private void createNewView()
    {
       
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
       
        builder.setTitle("What are you doing?");
   
        LayoutInflater inflater = LayoutInflater.from(m_context);
        final View otherTagView = inflater.inflate(R.layout.other_tag, null);
       
        final EditText tagEditText = (EditText) otherTagView.findViewById(R.id.tagEditText);

       
       
        builder.setPositiveButton("OK",new DialogInterface.OnClickListener(){

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
       
                        String tag = tagEditText.getText().toString();
                        writeTag(tag);
                        Toast.makeText(m_context, "add " + tag + " to log...", Toast.LENGTH_SHORT).show();

            }
           
        }
        );
       
        builder.setView(otherTagView);
        builder.create();
        builder.show();
       
    }   
   
}

I just put a button in main.xml, and an EditText in other.tag.xml, if you really want them
other.tag.xml
---------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >




    <EditText
        android:id="@+id/tagEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
---------------


main.xml
-----------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/tagButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Tag" />

</LinearLayout>
---------------------------


I won't talk about the detail for this stage due to my laziness...

No comments:

Post a Comment