Sunday, July 6, 2025

Android Tutorial with Examples (Projects)

 

Learn Android with Examples: The best way to learn Android development is by practicing (or developing) some basic projects. Our Android Tutorial is designed for students as well as developers having projects from basic to advanced levels.

Android Tutorial: Index

  1. How to Install and Set up Android Studio on Windows?
  2. How to Install Android Virtual Device (AVD)?
  3. How to Create Virtual Device (Emulator) in Android Studio?
  4. How to Start/Create a New Project in Android Studio?
  5. How to Create a New Activity in Android Studio?
  6. How to Use Full Screen Activity in Android Studio?
  7. How to use bundle class for passing data of any type in Android?
  8. Activity Lifecycle in Android with an Example App
  9. How to Build Your First Android Application?
  10. Android code to make call from your own application
  11. Android - setOnClickListener Method, how does it work?
  12. Android - How to get value of EditText?
  13. Android - setOnCheckedChangeListener for checkbox
  14. Android - setOnCheckedChangeListener for Radio Buttons
  15. Android - Signup and Sign in (login) and Sign out (logout) with codes
  16. Initiate/Display an Analog Clock in Android (With Codes)
  17. Android String Resources: Define string for whole project?
  18. Android Dialogs with Examples
  19. Create Simple User Form in Android (Example with Codes)
  20. GridView in Android with Example
  21. ImageButton in Android with Example
  22. ListView in Android with Example
  23. Intent in Android with Example
  24. Linear Layout in Android with Example
  25. How to Create Linear Orientation Layout of an Android App?
  26. Grid Layout in Android with Example
  27. Relative Layout in Android with Example
  28. ImageView in Android with Example
  29. How to fill a ListView with Class Objects in Android?
  30. Tabbed Activity in Android with Example
  31. Android Implicit Intent Example with Code
  32. Android Explicit Intent (Move from One Activity to Another)
  33. How to Connect Android App to Web Server?
  34. How to Send Data to Server through an Android Application?
  35. Save/Read Data to/from Internal Storage Example in Android
  36. Android code to Read and Save Data from/to External Storage
  37. An example of Simple Toast in Android
  38. Toast the result of dialog in Android
  39. Pre-defined Login activity in Android Studio
  40. Android - StartActivityForResult Method
  41. Android - Independent view (1) setOnClickListener
  42. Android - List view - setOnItemClickListener
  43. Android - Set Listener on item of a list setOnClickListener
  44. Android ACTION_SEND event Example with Source Codes
  45. How to zoom the image in Android (animation)
  46. How to know Charging mode in Android?
  47. Showing list of all paired devices in Android
  48. BatteryManager and its properties in Android?
  49. How to use ClipboardManager to ClipData in Android?
  50. How to use Custom made fonts in Android?
  51. Implementing gestures in Android
  52. Rotate the image in both clockwise and anti-clockwise direction in Android (animation)
  53. Fade image for a particular duration in Android (animation)
  54. Blink image for an infinite count in Android (animation)
  55. Move image on a particular axis in Android (animation)
  56. Slide image to XScale in Android (animation)
  57. Android Popup Menu Example
  58. How to show audio mode of device in Android?
  59. How to set your device on Ring/Normal mode in Android?
  60. How to set your device on Vibrate mode in Android?
  61. Change the device mode on Silent in Android?
  62. Implement auto complete in Android
  63. Generate backup on Google backup services in Android
  64. How to check and turn on the device Bluetooth in Android?
  65. How to get co-ordinates of touch points on screen in Android?
  66. Android - Context Menu Example
  67. How to get motion touch co-ordinates in Android?
  68. How to change image view color using bitmap in Android?
  69. Write file using FileOutputStream class in Android
  70. Implement process circle in Android
  71. How to generate a notification in an Android device?
  72. Progress Bar in Android Application
  73. How to share something on social media from your Android Device?
  74. Switching between two images in Android
  75. Create Webview inside Android Application
  76. Make a call by using Intent.ACTION_CALL in Android
  77. Compose mail from your Android Device
  78. How to parse XML file in Android?
  79. How to Switch on WIFI service in Android?
  80. Save data using SharedPreferences in Android
  81. How to send SMS in android using Intent?
  82. How to use Services in Android?
  83. Android - Option Menu Example
  84. How to work with scrolling activity in Android?
  85. What is Navigation Drawer in Android?
  86. Map activity in Android Studio
  87. Settings Activity in Android Studio
  88. Fade In Animation Example in Android
  89. Android Services Example
  90. Android Button Example Code
  91. Android ImageButton Example Code
  92. Android Button Customization Example
  93. Android Example to Click Photo or Select Image from Gallery
  94. Android | How to declare string in Java with Android?
  95. Android | Application to Display All Available WiFi Network and Connect with Specific Network
  96. Android - Detect on which Network (WiFi or Mobile Data) Mobile is connected

Sunday, February 16, 2025

10. Login & Logout Features in Android Application

 

Login & Logout Features in Android Application


File Name: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.abbu.login_logout.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="187dp"
        android:id="@+id/editTextPass"
        android:hint="password" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_alignBottom="@+id/editTextPass"
        android:layout_alignEnd="@+id/editTextPass"
        android:layout_marginBottom="80dp"
        android:id="@+id/editTextUser"
        android:hint="username" />

    <Button
        android:text="Login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextPass"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="72dp"
        android:id="@+id/button" />
</RelativeLayout>

File Name: MainActivity.java

package com.example.abbu.login_logout;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import static android.R.attr.value;

public class MainActivity extends AppCompatActivity {

    EditText username, password;
    Button login;

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

        username = (EditText) findViewById(R.id.editTextUser);
        password = (EditText) findViewById(R.id.editTextPass);
        login = (Button) findViewById(R.id.button);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(username.getText().toString().equals("admin") &&
                        password.getText().toString().equals("admin")){
                    Intent i = new Intent(MainActivity.this.getApplicationContext(),
                            Logout.class);
                    i.putExtra("message", value);
                    startActivity(i);
                }
                else{
                    Toast.makeText(MainActivity.this, "Invalid Details",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

File Name: logout.xml

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

    <Button
        android:text="Logout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="125dp"
        android:id="@+id/buttonLogout" />
</RelativeLayout>

File Name: Logout.java

package com.example.abbu.login_logout;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import static android.content.ContentValues.TAG;

/**
 * Created by Abbu on 4/15/2017.
 */

public class Logout extends MainActivity {

    Button Logout;



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logout);
        Logout = (Button) findViewById(R.id.buttonLogout);

        Intent in = getIntent();
        String string = in.getStringExtra("message");
        Logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Logout.this);
                builder.setTitle("Confirmation PopUp!").
                        setMessage("You sure, that you want to logout?");
                builder.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Intent i = new Intent(getApplicationContext(),
                                        MainActivity.class);
                                startActivity(i);
                            }
                        });
                builder.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                AlertDialog alert11 = builder.create();
                alert11.show();
            }
        });
    }
}

Output:

Login & Logout Features in Android Application
activity_main.xml
After clicking on LOGIN Button, logout.xml will apear.
Login & Logout Features in Android Application
After_Successful_Login (logout.xml)
Now, when you click on LOGOUT Button, a confirmation popup (dialog box) will appear.
Login & Logout Features in Android Application
Logout_Confirmation
After clicking on YES in the confirmation popup, we'll come back to activity_main.xml
Login & Logout Features in Android Application
After_Logout

9. Login Application in Android

 

Login Application in Android

FileName: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.abbu.login_page.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="187dp"
        android:id="@+id/editTextPass"
        android:hint="password" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_alignBottom="@+id/editTextPass"
        android:layout_alignEnd="@+id/editTextPass"
        android:layout_marginBottom="80dp"
        android:id="@+id/editTextUser"
        android:hint="username" />

    <Button
        android:text="Login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextPass"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="72dp"
        android:id="@+id/button" />
</RelativeLayout>

FileName: MainActivity.java

package com.example.abbu.login_page;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText username, password;
    Button login;

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

        username = (EditText) findViewById(R.id.editTextUser);
        password = (EditText) findViewById(R.id.editTextPass);
        login = (Button) findViewById(R.id.button);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(username.getText().toString().equals("admin") &&
                        password.getText().toString().equals("admin")){
                    Toast.makeText(MainActivity.this, "Welcome " +
                            username.getText().toString(),
                            Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(MainActivity.this, "Invalid Details",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

Output:

Login Application in Android
activity_main.xml
Login Application in Android
After Successful Login
Login Application in Android
After unsuccessful Login

8. Three Buttons Vertically Aligned & Background Color Usage in Android

 

Three Buttons Vertically Aligned & Background Color Usage in Android

FileName: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.abbu.button_color.MainActivity">

    <Button
        android:text="GAMBOGE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/buttonPatina"
        android:layout_alignStart="@+id/buttonPatina"
        android:layout_marginTop="30dp"
        android:id="@+id/buttonGamboge" />

    <Button
        android:text="AMBER"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/buttonGamboge"
        android:layout_alignStart="@+id/buttonGamboge"
        android:layout_marginTop="30dp"
        android:id="@+id/buttonAmber" />

    <Button
        android:text="PATINA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/buttonPatina"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

FileName: MainActivity.java

package com.example.abbu.button_color;

import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;


public class MainActivity extends AppCompatActivity {

    Button btn1,btn2,btn3;

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

        btn1 = (Button) findViewById(R.id.buttonPatina);
        btn2 = (Button) findViewById(R.id.buttonGamboge);
        btn3 = (Button) findViewById(R.id.buttonAmber);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RelativeLayout rl = (RelativeLayout)findViewById(R.id.activity_main);
                    rl.setBackgroundColor(Color.parseColor("#407A52"));
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RelativeLayout rl = (RelativeLayout)findViewById(R.id.activity_main);
                rl.setBackgroundColor(Color.parseColor("#FFB61E"));
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RelativeLayout rl = (RelativeLayout)findViewById(R.id.activity_main);
                rl.setBackgroundColor(Color.parseColor("#CA6924"));
            }
        });
    }
}

Output:

Three Buttons Vertically Aligned & Background Color Usage in Android
Three Buttons Vertically Aligned & Background Color Usage in Android
After clicking on PATINA Button, the background color changes to:
Three Buttons Vertically Aligned & Background Color Usage in Android
PATINA (#407A52)
After clicking on GAMBOGE Button, the background color changes to:
Three Buttons Vertically Aligned & Background Color Usage in Android
GAMBOGE (#FFB61E)
After clicking on AMBER Button, the background color changes to:
AMBER (#CA6924)

7. Radio Button, ImageView and Toast Usage in Android

 

Radio Button, ImageView and Toast Usage in Android


File Name: activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.abbu.radio_power.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enquire Teacher-in-Charge"
        android:id="@+id/textView" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/choices"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true">

    <RadioButton
        android:text="Computer Science"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_marginStart="5dp"
        android:layout_marginTop="10dp"
        android:id="@+id/radioButtonCS" />

    <RadioButton
        android:text="Commerce"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/radioButtonCS"
        android:layout_toEndOf="@+id/textView"
        android:layout_alignParentStart="false"
        android:layout_marginStart="5dp"
        android:layout_marginTop="10dp"
        android:id="@+id/radioButtonCO" />

    <RadioButton
        android:text="Chemistry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/radioButtonEL"
        android:layout_alignStart="@+id/radioButtonCO"
        android:layout_alignParentStart="false"
        android:layout_marginStart="5dp"
        android:layout_marginTop="10dp"
        android:id="@+id/radioButtonCH" />

    <RadioButton
        android:text="Electronics"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioButtonEL"
        android:layout_alignParentStart="false"
        android:layout_marginStart="5dp"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/radioButtonCS"
        android:layout_alignStart="@+id/radioButtonCS" />
    
    </RadioGroup>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/imageView"
        android:layout_marginTop="41dp"
        android:layout_below="@+id/choices"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

File Name: MainActivity.java

package com.example.abbu.radio_power;

import android.content.res.TypedArray;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private TypedArray images;
    private ImageView itemImage;
    RadioButton rch = null;
    RadioButton rco = null;
    RadioButton rcs = null;
    RadioButton rel = null;
    RadioGroup rg = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        images = getResources().obtainTypedArray(R.array.radiobtn_image);
        itemImage = (ImageView) findViewById(R.id.imageView);
        rch = (RadioButton) findViewById(R.id.radioButtonCH);
        rco = (RadioButton) findViewById(R.id.radioButtonCO);
        rcs = (RadioButton) findViewById(R.id.radioButtonCS);
        rel = (RadioButton) findViewById(R.id.radioButtonEL);
        rg = (RadioGroup) findViewById(R.id.choices);

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == rch.getId()) {
                    itemImage.setImageResource(R.drawable.pic2);
                    Toast.makeText(MainActivity.this, "Dr. Neelu Dhir",
                            Toast.LENGTH_SHORT).show();
                }else if (checkedId == rco.getId()) {
                    itemImage.setImageResource(R.drawable.pic4);
                    Toast.makeText(MainActivity.this, "Ms. Sangeeta Relan",
                            Toast.LENGTH_SHORT).show();
                } else if (checkedId == rcs.getId()) {
                    itemImage.setImageResource(R.drawable.pic1);
                    Toast.makeText(MainActivity.this, "Dr. Vibha Gaur",
                            Toast.LENGTH_SHORT).show();
                } else {
                    itemImage.setImageResource(R.drawable.pic3);
                    Toast.makeText(MainActivity.this, "Dr. Ravneet Kaur",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
}
}

File Name: strings.xml

<resources>
    <string name="app_name">Radio_Power</string>

    <string-array name="radiobtn_image">
        <item>@drawable/pic1</item>
        <item>@drawable/pic2</item>
        <item>@drawable/pic3</item>
        <item>@drawable/pic4</item>
    </string-array>
</resources>

Output:

Radio Button, ImageView and Toast Usage in Android
Radio Button, ImageView and Toast Usage in Android
Radio Button, ImageView and Toast Usage in Android
Radio Button, ImageView and Toast Usage in Android

RecyclerView in Android with Example

  RecyclerView is a ViewGroup added to the android studio as a successor of the GridView and ListView. It is an improvement on both of them ...