Andrioid Programming
Andrioid Programming
Dilshad
ROLL NO- 238
ASSIGNMENT- 1 and 2
SET-A
JAVA:-
MainActivity.java
package com.example.activitylifecycle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
super.onStart();
@Override
protected void onResume() {
super.onResume();
@Override
super.onPause();
@Override
super.onStop();
@Override
super.onRestart();
@Override
super.onDestroy();
}
}
XML:-
<androidx.constraintlayout.widget.ConstraintLayout
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Q2-Create a Simple application that shows factorial of a number and
display on second Activity.
XML:-
<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"
tools:context=".MainActivity" >
<EditText
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Go" />
</LinearLayout>
JAVA:-
package com.example.fact;
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;
EditText t1;
Button b;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=findViewById(R.id.text1);
b=findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
int a=Integer.parseInt(t1.getText().toString());
int i,fact=1;
for(i=1;i<=a;i++){
fact=fact*i;
}
intent.putExtra("Number",fact);
startActivity(intent);
});
SecondXML:-
<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"
tools:context=".second">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Second JAVA:-
package com.example.fact;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
TextView r;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
r=findViewById(R.id.textView);
Intent intent=getIntent();
Bundle b=intent.getExtras();
int a=b.getInt("Number");
r.setText("Factorial is:="+a);
}
Q3-Create a Simple application that accepts two number and calculate
power and average of a number and display on second Activity.
XML:-
<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"
tools:context=".second">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
JAVA:-
package com.example.powerandaverage;
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;
EditText n1,n2;
Button b;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
n1=findViewById(R.id.text1);
n2=findViewById(R.id.text2);
b=findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
int a,b,i,pow=1;
a=Integer.parseInt(n1.getText().toString());
b=Integer.parseInt(n2.getText().toString());
for(i=1;i<=b;i++){
pow=pow*a;
int c,d,avg;
c=Integer.parseInt(n1.getText().toString());
d=Integer.parseInt(n2.getText().toString());
avg=(c+d)/2;
intent.putExtra("power",pow);
intent.putExtra("average",avg);
startActivity(intent);
});
SecondXML:-
package com.example.powerandaverage;
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;
EditText n1,n2;
Button b;
@Override
setContentView(R.layout.activity_main);
n1=findViewById(R.id.text1);
n2=findViewById(R.id.text2);
b=findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
int a,b,i,pow=1;
a=Integer.parseInt(n1.getText().toString());
b=Integer.parseInt(n2.getText().toString());
for(i=1;i<=b;i++){
pow=pow*a;
int c,d,avg;
c=Integer.parseInt(n1.getText().toString());
d=Integer.parseInt(n2.getText().toString());
avg=(c+d)/2;
intent.putExtra("power",pow);
intent.putExtra("average",avg);
startActivity(intent);
});
}
Second JAVA:-
package com.example.powerandaverage;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
TextView t1,t2;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
t1=findViewById(R.id.textView1);
t2=findViewById(R.id.textView2);
Intent intent=getIntent();
Bundle b=intent.getExtras();
int power=b.getInt("power");
t1.setText("Power is:"+power);
int average=b.getInt("average");
t2.setText("Average is:"+average);
}
Q4-Create a Simple application that performs Arithmetic operation
and display result on second Activity.
JAVA:-
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
val intent = Intent(this, ResultActivity::class.java)
intent.putExtra("RESULT", result)
startActivity(intent)
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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextNumber1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:inputType="number"/>
<EditText
android:id="@+id/editTextNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextNumber1"
android:layout_centerHorizontal="true"
android:inputType="number"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextNumber2"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:entries="@array/operators" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/spinner"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Calculate"
android:onClick="calculate"/>
</RelativeLayout>}
JAVA:-
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_result.*
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_result)
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"
tools:context=".ResultActivity">
<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="24sp"
android:textStyle="bold"/>
</RelativeLayout>
XML:-
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="107dp"
android:layout_marginBottom="564dp"
android:ems="10"
android:inputType="text" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="171dp"
android:layout_marginBottom="463dp"
android:text="Button" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="171dp"
android:layout_marginBottom="371dp" />
</RelativeLayout>
JAVA:-
package com.example.evenodd;
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;
Button b;
TextView t1;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
n1=findViewById(R.id.text1);
b=findViewById(R.id.button);
t1=findViewById(R.id.textView);
b.setOnClickListener(new View.OnClickListener() {
@Override
int a=Integer.parseInt(n1.getText().toString());
if(a%2==0){
t1.setText("Even number");
}else {
t1.setText("Odd number");
});
}
SET-B
XML:-
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="102dp"
android:layout_marginBottom="499dp"
android:ems="10"
android:inputType="text" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="103dp"
android:layout_marginBottom="411dp"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="153dp"
android:layout_marginBottom="309dp"
android:text="submit" />
</RelativeLayout>
JAVA:-
package com.example.login;
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;
import android.widget.Toast;
EditText user,pass;
Button b;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user=findViewById(R.id.text1);
pass=findViewById(R.id.password);
b=findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
if(user.getText().toString().equals("Sana") &&
pass.getText().toString().equals("Sana")){
startActivity(intent);
}else{
Toast.makeText(MainActivity.this,"Login
Failed",Toast.LENGTH_LONG).show();
}
}
});
SecondXML:-
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".second">
</RelativeLayout>
Second JAVA:-
package com.example.login;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Toast.makeText(second.this,"Login
successfull",Toast.LENGTH_LONG).show();
JAVA:-
<androidx.constraintlayout.widget.ConstraintLayout
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">
<EditText
android:id="@+id/editTextText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
tools:layout_editor_absoluteX="82dp"
tools:layout_editor_absoluteY="32dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="180dp"
android:text="Click"
app:layout_constraintEnd_toEndOf="parent"
tools:layout_editor_absoluteY="173dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
JAVA:-
package com.example.twoactivity;
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;
EditText send;
Button b1;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send=findViewById(R.id.editTextText);
b1=findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
String str=send.getText().toString();
intent.putExtra("msg",str);
startActivity(intent);
});
SecondXML:-
<androidx.constraintlayout.widget.ConstraintLayout
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=".Activitysecond">
<TextView
android:id="@+id/textView"
android:layout_width="245dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:layout_editor_absoluteX="83dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Second JAVA:-
package com.example.twoactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
TextView receive;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activitysecond);
receive=findViewById(R.id.textView);
Intent intent=getIntent();
String str=intent.getStringExtra("msg");
receive.setText(str);
}
Q3- Create a simple application that will change the colour of college
name on click of push Button and change the font size ,font style of
text view using XML.
XML:-
<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"
tools:context=".MainActivity">
<EditText
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
<Button
android:id="@+id/bn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="blue" />
<Button
android:id="@+id/bn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="green" />
<Button
android:id="@+id/bn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<Button
android:id="@+id/bn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="serif" />
<Button
android:id="@+id/bn5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<Button
android:id="@+id/bn6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bn7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
</LinearLayout>
JAVA:-
package com.example.style;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
EditText t;
Button b1,b2,b3,b4,b5,b6,b7;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=findViewById(R.id.text1);
b1=findViewById(R.id.bn1);
b2=findViewById(R.id.bn2);
b3=findViewById(R.id.bn3);
b4=findViewById(R.id.bn4);
b5=findViewById(R.id.bn5);
b6=findViewById(R.id.bn6);
b7=findViewById(R.id.bn7);
b1.setOnClickListener(new View.OnClickListener() {
@Override
t.setTextColor(Color.BLUE);
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
t.setTextColor(Color.GREEN);
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
t.setTypeface(Typeface.SANS_SERIF);
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
t.setTypeface(Typeface.SERIF);
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
t.setTextSize(10);
});
b6.setOnClickListener(new View.OnClickListener() {
@Override
t.setTextSize(15);
});
b7.setOnClickListener(new View.OnClickListener() {
@Override
t.setTextSize(20);
});
}
MainActivity:
package com.example.student;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fname=findViewById(R.id.t1);
mname=findViewById(R.id.t2);
lname=findViewById(R.id.t3);
dob=findViewById(R.id.t4);
email=findViewById(R.id.t5);
b=findViewById(R.id.btn);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String t1=fname.getText().toString();
String t2=mname.getText().toString();
String t3=lname.getText().toString();
Integer t4=Integer.parseInt(dob.getText().toString());
String t5=email.getText().toString();
Intent intent=new Intent(MainActivity.this,page2.class);
intent.putExtra("Firstname:",t1);
intent.putExtra("Middlename:",t2);
intent.putExtra("Lastname:",t3);
intent.putExtra("Date of Birth:",t4);
intent.putExtra("Email:",t5);
startActivity(intent);
}
});
}
}
XML 1:
<EditText
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="Name" />
<EditText
android:id="@+id/t2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="Middle Name:" />
<EditText
android:id="@+id/t3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="Last Name:" />
<EditText
android:id="@+id/t4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date" />
<EditText
android:id="@+id/t5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
SecondActivity:
package com.example.student;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page2);
view1=findViewById(R.id.tv1);
view2=findViewById(R.id.tv2);
view3=findViewById(R.id.tv3);
view4=findViewById(R.id.tv4);
view5=findViewById(R.id.tv5);
Intent intent=getIntent();
XML 2:
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="77dp"
android:text="TextView" />
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="84dp"
android:text="TextView" />
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="74dp"
android:text="TextView" />
<TextView
android:id="@+id/tv4"
android:layout_width="match_parent"
android:layout_height="84dp"
android:text="TextView" />
<TextView
android:id="@+id/tv5"
android:layout_width="match_parent"
android:layout_height="62dp"
android:text="TextView" />
</LinearLayout>
Q5- Create a simple application that accept number from user that
find square of number and display result using Toast.(use constraint
layout).
XML:-
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="89dp"
android:layout_marginBottom="470dp"
android:ems="10"
android:inputType="text" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="158dp"
android:layout_marginBottom="304dp"
android:text="Button" />
</RelativeLayout>
JAVA:-
package com.example.square;
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;
EditText n1;
Button b;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
n1 = findViewById(R.id.text1);
b = findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
int a = Integer.parseInt(n1.getText().toString());
int square = a * a;
Toast.LENGTH_LONG).show();
});