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...

Radio Button Example Android

Radio buttons are used when you have multiple choices but you have to select only one.

In  Android,  Radio buttons are grouped together by  android.widget.RadioGroup class.If radio buttons are inside Radio group so when one radio button is selected then all others becomes unselected.

Now,I am going to make radio group "Gender " with two radio buttons "Male" &  "Female".

activity_main.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:background="#FFFFFF"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtGender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="GENDER"
        android:textColor="#0198E1"
        android:textSize="16sp"
        android:textStyle="bold" >
    </TextView>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="2dp"
        android:background="#0198E1" />

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <RadioGroup
            android:id="@+id/rgGender"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rdbMale"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:checked="true"
                android:text="Male"
                android:textColor="#0198E1"
                android:textSize="16sp"
                android:textStyle="bold" />

            <RadioButton
                android:id="@+id/rdbFemale"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="Female"
                android:textColor="#0198E1"
                android:textSize="16sp"
                android:textStyle="bold" />
        </RadioGroup>
    </LinearLayout>

    <TextView
        android:id="@+id/txtSelGender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="Selected Gender"
        android:textColor="#0198E1"
        android:textSize="16sp"
        android:textStyle="bold" >
    </TextView>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="2dp"
        android:background="#0198E1" />

    <TextView
        android:id="@+id/txtSelGenderValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:textColor="#0198E1"
        android:textSize="16sp"
        android:textStyle="bold" >
    </TextView>

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="submit" />

</LinearLayout>


layout preview





















MainActivity,java




package com.arjun.radiobuttondemo;



import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnCheckedChangeListener {

Button btnSubmit;
RadioButton rdbMale, rdbFemale;
RadioGroup rgGender;
TextView txtSelecteGender;
String gender ="Male";
Context ctx;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ctx = this;

init();

rgGender.setOnCheckedChangeListener(this);

btnSubmit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
txtSelecteGender.setText(gender);
}
});



}

public void init() {
btnSubmit =(Button)findViewById(R.id.btnSubmit);
rgGender = (RadioGroup) findViewById(R.id.rgGender);
rdbMale = (RadioButton) findViewById(R.id.rdbMale);
rdbFemale = (RadioButton) findViewById(R.id.rdbMale);
txtSelecteGender =(TextView) findViewById(R.id.txtSelGenderValue);
}

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

@Override
public void onCheckedChanged(RadioGroup group, int position) {
// TODO Auto-generated method stub
switch (position) {
case R.id.rdbMale:
gender = "Male";
System.out.println("Male");
break;
case R.id.rdbFemale:
gender = "Female";
System.out.println("FeMale");
break;

default:
break;
}
}

}


after clicking submit button it will show selected gender




Comments

Post a Comment

Popular posts from this blog

Webview data directory for Android 9 (Pie)

Tabs in Android

DatePicker in Android