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();
}
}
}
No comments:
Post a Comment