Dialogs Custom/Predefined

Tutorials with advanced 'difficulty' and more Lines of Code.

Dialogs Custom/Predefined

Postby sunny342 » Mon May 24, 2010 11:02 pm

Hello I am making a alarm clock app, and in need the user to input the Title of the alarm, time for alarm, days alarm needs to ring. I have a list view with the item i have listed. When you click the time for alarm, a timepicker dialog appears and you can set your time. I need to make dialogs for inputing text/title, and one for choosing multiple items from a list dialog (so i can pick mon-sun). How do you do that. Is there a uniform way to create all types of dialogs.

p.s. - i've looked at the google developers site, but could not make sense of it. Where do i need to call all this stuff

Code: Select all
public class SettingList extends ListActivity implements OnItemClickListener {
   
    
      
      static final String[] OPTIONS = new String[] {"Alarm Name",    "Set Time",    "Repeat",    "Sound" };         
      ArrayAdapter<String> options;
      static final int DIALOG_TIME_ID = 1;
       private int mHour;
       private int mMinute;
       public SharedPreferences.Editor prefsEditor;
       public SharedPreferences myPrefs;
       final String items[] = {"item1","item2","item3"};
    
    public void onCreate(Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);

       SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        prefsEditor = myPrefs.edit();

       
       //Sets up the list view
       options = new ArrayAdapter<String>(this, R.layout.list_item, OPTIONS);
       setListAdapter(options);
       
       ListView lv = getListView();
       lv.setTextFilterEnabled(true);

       lv.setOnItemClickListener(this);
       


    }
   
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
       int pos = position;
       switch(pos)
       {
       case 0:
          break;
       case 1:
          showDialog(DIALOG_TIME_ID);
       case 2:
          
       case 3:
          
       }   
    }
   
   
    protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        switch(id) {
        case DIALOG_TIME_ID:
           return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, true); //(Context context, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView)
      default:
            dialog = null;
        }
        return dialog;
    }
   
    // the callback received when the user "sets" the time in the dialog
    private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener()
    {
       public void onTimeSet(TimePicker view, int hourOfDay, int minute)
       {
          mHour = hourOfDay;
          mMinute = minute;
          prefsEditor.putInt("hour", mHour);
          prefsEditor.putInt("minute", mMinute);
          prefsEditor.commit();
          
       }
    };
   
}
sunny342
Freshman
Freshman
 
Posts: 4
Joined: Tue May 18, 2010 1:19 am

Top

Re: Dialogs Custom/Predefined

Postby strider2023 » Wed Feb 23, 2011 1:26 pm

Hi sunny342,

It is possible to make your dialog boxes with custom layout. I am attaching a code snippet as well as a xml file. Please feel free to approach me anytime if you have any questions.

Code: Select all
/**
    * This is the function to display custom dialog box with scroll-able text content.
     * @param title - Title of the the dialog box displayed as the header
     * @param body_text - Body of the dialog box displayed as the body of the dialog box with relevant details.
    * @param context
    */
    public static void createDialog(String title, String body_text, Context context) {
       //Create the dialog.
          final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.diet_app);
        dialog.setTitle(title);
        dialog.setCancelable(true);
       
        //Set up text
        final TextView text = (TextView) dialog.findViewById(R.id.diet_app_body);
        text.setText(body_text);
       
        //Set up image
        ImageView img = (ImageView) dialog.findViewById(R.id.diet_app_ImageView01);
        img.setImageResource(R.drawable.logo);
       
        //Set up button
        Button button = (Button) dialog.findViewById(R.id.diet_app_yesbtn);
        button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
              // do what ever you want to do here before the dismissing the dialog
                 dialog.dismiss();
            }
        });   
       
        Button button_1 = (Button) dialog.findViewById(R.id.diet_app_cancelbtn);
        button_1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
              // do what ever you want to do here before the dismissing the dialog
                 dialog.dismiss();
            }
        }); 
       
        dialog.show();
    }


And the xml layout

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="280px"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/diet_app_ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<ScrollView
android:id="@+id/dialog_scroll"
android:layout_width="wrap_content"
android:layout_below="@+id/diet_app_ImageView01"
android:layout_height="200px">
<TextView
android:id="@+id/diet_app_body"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</TextView>
</ScrollView>
<AbsoluteLayout
android:id="@+id/widget31"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/dialog_scroll"
>
<Button android:id="@+id/diet_app_yesbtn"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Save"
android:layout_x="30px"
android:layout_y="0px"
android:textStyle="normal|bold"
android:textColor="#343434"
/>
<Button android:id="@+id/diet_app_cancelbtn"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_x="160px"
android:layout_y="0px"
android:textStyle="normal|bold"
android:textColor="#343434"
/>
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests