You are on page 1of 13

1To accept username and password display in textview

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/edtu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Username"/>
<EditText
android:id="@+id/edtp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"/>
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textStyle="bold"/>

</LinearLayout>

package com.example.p1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

EditText etu;
EditText etp;
TextView txt;
Button bt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etu=findViewById(R.id.edtu);
etp=findViewById(R.id.edtp);
txt=findViewById(R.id.txt);
bt=findViewById(R.id.bt);

bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = etu.getText().toString();
String password = etp.getText().toString();

if(!username.isEmpty() && !password.isEmpty()){


String dtxt = "Username:" + username+"\nPassword:" + password;
txt.setText(dtxt);
}else {
txt.setText("Please neter both username and password");
}
}
});
}
}

2. to accept username and password dsply in toast


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/edtu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Username"/>
<EditText
android:id="@+id/edtp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"/>
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"/>

</LinearLayout>
package com.example.p1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText etu;
EditText etp;

Button bt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etu=findViewById(R.id.edtu);
etp=findViewById(R.id.edtp);
bt=findViewById(R.id.bt);

bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = etu.getText().toString();
String password = etp.getText().toString();

if(username.isEmpty() || password.isEmpty()){
Toast.makeText(MainActivity.this, "Please enter username and password",
Toast.LENGTH_SHORT).show();
}else {
String msg ="Username:"+username+"\nPassword:"+password;
Toast.makeText(MainActivity.this,msg, Toast.LENGTH_SHORT).show();
}
}
});

}
}

3.Student Personal Info


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/edtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Name"/>

<EditText
android:id="@+id/edtRoll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Roll No:"
android:inputType="number"/>
<EditText
android:id="@+id/edtAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Age:"
android:inputType="number"/>
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"/>

</LinearLayout>

package com.example.mad1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText edtName;
EditText edtroll;

EditText edtage;

Button bt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtName=findViewById(R.id.edtName);
edtroll=findViewById(R.id.edtRoll);
edtage=findViewById(R.id.edtAge);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = edtName.getText().toString();
String roll = edtroll.getText().toString();
String age = edtage.getText().toString();

if(name.isEmpty() && roll.isEmpty() && age.isEmpty()){


Toast.makeText(MainActivity.this, "Please fill the fields", Toast.LENGTH_SHORT).show();

}else {
String msg ="Name:"+name+"\n Roll No:"+roll+"\n Age:"+age;
Toast.makeText(MainActivity.this, "msg", Toast.LENGTH_SHORT).show();

}
}
});

}
}

4.Toogle Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<ToggleButton
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON"
android:onClick="Submit"/>

<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
/>

</LinearLayout>

package com.example.toggle;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

ToggleButton bt1;
TextView txt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1=findViewById(R.id.bt1);
txt=findViewById(R.id.txt);
}
public void Submit(View v)
{
if(bt1.isChecked())
//bt1.setText("Bluetooth On");
Toast.makeText(this,"Blueetooth On",Toast.LENGTH_LONG).show();
else
//bt1.setText("Bluetooth Off");
Toast.makeText(this,"Blueetooth Off",Toast.LENGTH_LONG).show();

}
}

5.Calculator
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:hint="Enter First No"/>
<EditText
android:id="@+id/et2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:hint="Enter Second No"/>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="+"/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="-"/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"/>

</LinearLayout>

package com.example.d;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

Button bt1,bt2;
EditText et1,et2;

TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=findViewById(R.id.et1);
et2=findViewById(R.id.et2);
bt1=findViewById(R.id.bt1);
bt2=findViewById(R.id.bt2);
txt=findViewById(R.id.txt);

bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num1= Integer.parseInt(et1.getText().toString());
int num2=Integer.parseInt((et2.getText().toString()));

txt.setText("Addition is :"+(num1 + num2 ));


}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num1= Integer.parseInt(et1.getText().toString());
int num2=Integer.parseInt((et2.getText().toString()));

txt.setText("Substraction is :"+(num1 - num2 ));


}
});
}
}

6.Checkbox
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Programing Language"
android:textSize="30dp"
android:textStyle="bold"
android:layout_gravity="center"/>
<CheckBox
android:id="@+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:textSize="25sp"/>
<CheckBox
android:id="@+id/c2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C++"
android:textSize="25sp"/>
<CheckBox
android:id="@+id/c3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java"
android:textSize="25sp"/>
<CheckBox
android:id="@+id/c4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JavaScript"
android:textSize="25sp"/>
<CheckBox
android:id="@+id/c5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Python"
android:textSize="25sp"/>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_gravity="center"/>

</LinearLayout>

package com.example.pr111;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


CheckBox ch1,ch2,ch3,ch4,ch5;
Button bt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ch1=findViewById(R.id.c1);
ch2=findViewById(R.id.c2);
ch3=findViewById(R.id.c3);
ch4=findViewById(R.id.c4);
ch5=findViewById(R.id.c5);
bt=findViewById(R.id.bt1);

bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String result=" ";
if(ch1.isChecked())
result+=ch1.getText().toString();
if(ch2.isChecked())
result+=ch2.getText().toString();
if(ch3.isChecked())
result+=ch3.getText().toString();
if(ch4.isChecked())
result+=ch4.getText().toString();
if(ch5.isChecked())
result+=ch5.getText().toString();

Toast.makeText(MainActivity.this, "selected"+result, Toast.LENGTH_SHORT).show();


}
});
}
}

7.Radio Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio button inside Radio Group"
android:textSize="28dp"
android:textStyle="bold" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rd1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:textSize="25sp"/>
<RadioButton
android:id="@+id/rd2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:textSize="25sp"/>

</RadioGroup>

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected"
android:layout_gravity="center"
android:textSize="35dp"/>
</LinearLayout>

package com.example.radiobutton;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


RadioButton r1,r2;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r1=findViewById(R.id.rd1);
r2=findViewById(R.id.rd2);
btn=findViewById(R.id.bt1);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String res="";

if(r1.isChecked())
res+=r1.getText().toString();
if(r2.isChecked())
res+=r2.getText().toString();

Toast.makeText(MainActivity.this, "Selected"+res, Toast.LENGTH_SHORT).show();


}
});
}
}

9.Listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.example.d;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

ListView lv;
String[] lang = {"Android", "Java", "Php", "Python", "C++", "Ruby"};

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

ArrayAdapter<String> adapter = new ArrayAdapter<>(


this,
android.R.layout.simple_list_item_1,
lang
);

lv.setAdapter(adapter);
}
}

10.GridView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:padding="10dp"
android:gravity="center"/>

</LinearLayout>
package com.example.d;

// MainActivity.java
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

GridView gridView = findViewById(R.id.gridView);

String[] languages = {
"Java",
"Kotlin",
"Python",
"JavaScript",
"Swift",
"C++",
"Ruby",
"PHP"
};

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,


android.R.layout.simple_list_item_1, languages);

gridView.setAdapter(adapter);
}
}

You might also like