Blog purpose for android basic example for android app developer. any query please my contact

Tuesday, 14 April 2020

android if else statement example

android if else statement example

In this article Today learn android if else statement example. If else Statment Using the android app. This example basic check age for a voter.


Then an android if-else statement example for the using if-else statement only follow the code of the android if else statement example.

f then allow us to control the flow of a program based on conditions, whether to execute code or not. In simple words, it gives the freedom to make decisions on what code to execute or not. The biggest advantage of if-then is that it makes the program to take action based on user input.


for the following source if else in android studio:

 public void age(){

        age= Integer.parseInt(txtage.getText().toString());

        if(age>=18){

            Toast.makeText(this, "You are Qualified Votter", Toast.LENGTH_SHORT).show();

        }else{

            Toast.makeText(this, "You are not Qualified Votter", Toast.LENGTH_SHORT).show();


        }

    }


1.MainActivity.java:

package com.akash.if_else;


import androidx.appcompat.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 {

int age;

EditText txtage;

Button btnage;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        txtage=(EditText)findViewById(R.id.txtage);

        btnage=(Button)findViewById(R.id.btnage);

        btnage.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {


                age();

            }


        });


    }


    public void age(){

        age= Integer.parseInt(txtage.getText().toString());

        if(age>=18){

            Toast.makeText(this, "You are Qualified Votter", Toast.LENGTH_SHORT).show();

        }else{

            Toast.makeText(this, "You are not Qualified Votter", Toast.LENGTH_SHORT).show();


        }

    }

}




2.activity_main.xml:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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=".MainActivity">

    <EditText
        android:id="@+id/txtage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:hint="Enter Age"
        android:padding="5dp"
        android:textSize="20sp"
        android:textStyle="bold|italic" />

    <Button
        android:id="@+id/btnage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="5dp"
        android:text="Check Age Qualified For Votter"
        android:textSize="20sp"
        android:textStyle="bold|italic" />


</RelativeLayout>



No comments:

Post a Comment