Android Programs
Date TimePicker:
package com.example.datepickerclg;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
Button b1;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=findViewById(R.id.btnDate);
t1=findViewById(R.id.txtDate);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar cal=Calendar.getInstance();
int mYear=cal.get(Calendar.YEAR);
int mMonth=cal.get(Calendar.MONTH);
int mDay=cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog=new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
t1.setText("Your Selected Date : \n "+String.format("%02d",day)+"/"+String.format("%02d",month+1)+"/"+year);
}
},mYear,mMonth,mDay);
dialog.show();
}
});
}
}
Calculator
package com.example.calcintent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText ed1,ed2;
Button bPlus,bMin,bMul,bDiv,bRem;
int flag=0;
Double n1,n2,tot;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=findViewById(R.id.edNo1);
ed2=findViewById(R.id.edNo2);
bPlus=findViewById(R.id.btnPlus);
bMin=findViewById(R.id.btnMinus);
bMul=findViewById(R.id.btnMul);
bDiv=findViewById(R.id.btnDiv);
bRem=findViewById(R.id.btnRmd);
//bRes=findViewById(R.id.btnResult);
bPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
n1=Double.parseDouble(ed1.getText().toString());
n2=Double.parseDouble(ed2.getText().toString());
tot=n1+n2;
Intent i1=new Intent(getApplicationContext(),MainActivity2.class);
i1.putExtra("no1",n1);
i1.putExtra("no2",n2);
i1.putExtra("head","Addition of Two Number");
i1.putExtra("total",tot);
startActivity(i1);
}
});
bMin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
n1=Double.parseDouble(ed1.getText().toString());
n2=Double.parseDouble(ed2.getText().toString());
tot=n1-n2;
Intent i1=new Intent(getApplicationContext(),MainActivity2.class);
i1.putExtra("no1",n1);
i1.putExtra("no2",n2);
i1.putExtra("head","Subtraction of Two Number");
i1.putExtra("total",tot);
startActivity(i1);
}
});
bMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
n1=Double.parseDouble(ed1.getText().toString());
n2=Double.parseDouble(ed2.getText().toString());
tot=n1*n2;
Intent i1=new Intent(getApplicationContext(),MainActivity2.class);
i1.putExtra("no1",n1);
i1.putExtra("no2",n2);
i1.putExtra("head","Multiplication of Two Number");
i1.putExtra("total",tot);
startActivity(i1);
}
});
bDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
n1=Double.parseDouble(ed1.getText().toString());
n2=Double.parseDouble(ed2.getText().toString());
tot=n1/n2;
Intent i1=new Intent(getApplicationContext(),MainActivity2.class);
i1.putExtra("no1",n1);
i1.putExtra("no2",n2);
i1.putExtra("head","Division of Two Number");
i1.putExtra("total",tot);
startActivity(i1);
}
});
bRem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
n1=Double.parseDouble(ed1.getText().toString());
n2=Double.parseDouble(ed2.getText().toString());
tot=n1%n2;
Intent i1=new Intent(getApplicationContext(),MainActivity2.class);
i1.putExtra("no1",n1);
i1.putExtra("no2",n2);
i1.putExtra("head","Reminder of Two Number");
i1.putExtra("total",tot);
startActivity(i1);
}
});
/*bRes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(flag==1)
{
Intent i1=new Intent(getApplicationContext(),MainActivity2.class);
i1.putExtra("no1",n1);
i1.putExtra("no2",n2);
i1.putExtra("head","Addition of Two Number");
i1.putExtra("total",tot);
startActivity(i1);
}
else if(flag==2)
{
Intent i1=new Intent(getApplicationContext(),MainActivity2.class);
i1.putExtra("no1",n1);
i1.putExtra("no2",n2);
i1.putExtra("head","Subtraction of Two Number");
i1.putExtra("total",tot);
startActivity(i1);
}
else if(flag==3)
{
Intent i1=new Intent(getApplicationContext(),MainActivity2.class);
i1.putExtra("no1",n1);
i1.putExtra("no2",n2);
i1.putExtra("head","Multiplication of Two Number");
i1.putExtra("total",tot);
startActivity(i1);
}
else if(flag==4)
{
Intent i1=new Intent(getApplicationContext(),MainActivity2.class);
i1.putExtra("no1",n1);
i1.putExtra("no2",n2);
i1.putExtra("head","Division of Two Number");
i1.putExtra("total",tot);
startActivity(i1);
}
}
});*/
}
}
package com.example.calcintent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
TextView tN1,tN2,tTot,tHead;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tN1=findViewById(R.id.txNum1);
tN2=findViewById(R.id.txNum2);
tTot=findViewById(R.id.txTot);
tHead=findViewById(R.id.txHead);
Intent i1=getIntent();
Double n1=i1.getDoubleExtra("no1",0);
Double n2=i1.getDoubleExtra("no2",0);
Double tot=i1.getDoubleExtra("total",0);
String s1=i1.getStringExtra("head");
tHead.setText(s1);
tN1.setText(""+n1);
tN2.setText(""+n2);
tTot.setText(""+tot);
}
}
xml1 file:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculator"
android:textSize="40dp"
android:textColor="#3949AB"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="100dp"
android:text="Enter no 1"
android:textColor="#B60BE3"
android:textSize="30dp" />
<EditText
android:id="@+id/edNo1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="100dp"
android:background="#FFFFFF"
android:hint="Enter no 1"
android:inputType="numberDecimal"
android:textColor="#D5628C"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:text="Enter no 2"
android:textColor="#B60BE3"
android:textSize="30dp" />
<EditText
android:id="@+id/edNo2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:hint="Enter no 1"
android:inputType="numberDecimal"
android:textColor="#D5628C"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/btnPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:backgroundTint="#FF0600"
android:text="+"
android:textSize="30dp" />
<Button
android:id="@+id/btnMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:backgroundTint="#FF0600"
android:text="-"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="50dp"
android:backgroundTint="#FF0600"
android:text="*"
android:textSize="30dp" />
<Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="50dp"
android:backgroundTint="#FF0600"
android:text="/"
android:textSize="30dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<Button
android:id="@+id/btnRmd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:backgroundTint="#FF0600"
android:text="%"
android:textSize="30dp" />
</LinearLayout>
</LinearLayout>
xml2 file
<?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=".MainActivity2"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:id="@+id/txHead"
android:textColor="#D81B60"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginLeft="20dp"
android:textSize="30dp"
android:text="No 1"
android:textColor="#F8BC2D"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginLeft="100dp"
android:textSize="30dp"
android:id="@+id/txNum1"
android:textColor="#D85855"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginLeft="20dp"
android:textSize="30dp"
android:text="No 2"
android:textColor="#F8BC2D"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginLeft="100dp"
android:textSize="30dp"
android:id="@+id/txNum2"
android:textColor="#D85855"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginLeft="20dp"
android:textSize="30dp"
android:text="Answer"
android:textColor="#2DC9F8"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginLeft="70dp"
android:textSize="30dp"
android:id="@+id/txTot"
android:textColor="#9288BD"/>
</LinearLayout>
</LinearLayout>
0 Comments
Post a Comment