Search code examples
androiddialogpreference

how to make a prompt edit text dialog when open the application and store the data to preference?


I have read many articles, but all are about to set a preference screen and click on a preference item say the edit text then it prompts the edit text dialog. Now i just want tohave a dialog that will prompt when the app is first created and store the input data to preference. could I use DialogPreference in this case?


Solution

  • Yes, of course you can... At first, in onCreate of your activity create -

    boolean firstrun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("firstrun", true); 
    

    Then check -

    if (firstrun) {
    //Call HERE your showDialog, for example...
     showDialog(DIALOG_TEXT_ENTRY1);
    } 
    

    And the last - just override onCreateDialog by ... -

     @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        //main title
        case DIALOG_TEXT_ENTRY1:
             LayoutInflater factorymain = LayoutInflater.from(this);
             final View textEntryViewMain = factorymain.inflate(R.layout.main_title_dialog, null);  
    .................
    ............ 
    

    By Layoutinflater as shown above you can show any layout with any editText objects you want....