Simple Notification in Android

Image
Notifications are a way to provide information to your customer which displays as a pop up on the device. It helps you to keep in touch with your customer. I am going to create a simple notification without going deep because everything is available on Google. We must understand a few concepts before going further. Notification channel:  Assume you have developed an app for an online learning center where they teach Maths and English. Students can either learn Maths, English, or Both. we must send notification related to Maths to only students who have taken Maths. In this case, we can create channels for Maths, English, and both so triggered notification will be delivered to students who have taken that subject. Each Notification channel can have a different Notification sound and different behavior that you can change from the Notification setting page. I forgot to tell you that Google has introduced the Notification channel from Android 8.0 i.e API level 26. Now yo...

DatePicker in Android


Using DatePicker user can set a specific date by setting the day,month and year of DatePIcker.I am going to show you an example,There will be two buttons btnset and btndisplay.When user will click on btnset it will display  a dialog box and user will select the date and after selecting date when user will click on btndisplay it will display the date previously selected by user.

1.) Create a Project DatePickerExample.
2.) Create a date_picker.xml in res/layout.
3.) Put the following code in date_picker.xml :

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

  >

<Button
   android:id="@+id/btnset"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Set" />

<Button
   android:id="@+id/btndisplay"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Display" />

</LinearLayout>

DatePickerExample class:

import android.app.Activity;
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.widget.DatePicker;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
     public class DatePickerExample extends Activity {
     
        private int mYear, mMonth, mDay;
        private int yr,month,day;
        static final int DATE_DIALOG_ID = 1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.date_picker);

                final Calendar c = Calendar.getInstance();
                mYear = c.get(Calendar.YEAR);
                mMonth = c.get(Calendar.MONTH);
                mDay = c.get(Calendar.DAY_OF_MONTH);
                updateDisplay();
     
             
                Button date = (Button) findViewById(R.id.btnset);
                date.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                                showDialog(DATE_DIALOG_ID);
                        }
                });

               
                Button display = (Button) findViewById(R.id.btndisplay);
                display.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                        Toast.makeText(ctx,"Date:" +day+"/"+month+"/"+ yr,Toast.LENGTH_SHORT).show();
                        }
                });
               
                }
              @Override
              protected Dialog onCreateDialog(int id) {
                switch (id) {
                case DATE_DIALOG_ID:
                        return new DatePickerDialog(this,
                                mDateSetListener,
                                mYear, mMonth, mDay);
                }
                return null;
             }
     
          private DatePickerDialog.OnDateSetListener mDateSetListener =
                new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                                int dayOfMonth) {
                        yr = year;
                        month = monthOfYear;
                        day = dayOfMonth;
                     
                }
        };
        }

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular posts from this blog

Webview data directory for Android 9 (Pie)

Tabs in Android