MCAD

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

MCAD(3360704) 196080307560

PRACTICAL 11
Aim: Create an application that will pass two number using TextView to the
next screen , and on the next screen display sum of that number.

 activity_main.xml

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


<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:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="Enter Value1:"
android:textSize="30dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.073"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="Enter Value2:"
android:textSize="30dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.069"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<EditText
MCAD(3360704) 196080307560

android:id="@+id/value1"
android:layout_width="143dp"
android:layout_height="38dp"
android:layout_marginTop="48dp"
android:ems="10"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/value2"
android:layout_width="143dp"
android:layout_height="38dp"
android:layout_marginTop="64dp"
android:ems="10"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.507"
app:layout_constraintStart_toEndOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/value1" />

<Button
android:id="@+id/plus"
android:layout_width="407dp"
android:layout_height="54dp"
android:layout_marginTop="260dp"
android:text="+"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="2dp"
android:textSize="25dp"/>

<Button
android:id="@+id/minus"
android:layout_width="407dp"
android:layout_height="54dp"
android:layout_marginTop="336dp"
android:text="-"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="2dp"
android:textSize="25dp"/>

<Button
android:id="@+id/into"
android:layout_width="407dp"
MCAD(3360704) 196080307560

android:layout_height="54dp"
android:layout_marginTop="412dp"
android:text="*"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="2dp"
android:textSize="25dp"/>

<Button
android:id="@+id/divide"
android:layout_width="407dp"
android:layout_height="54dp"
android:layout_marginTop="480dp"
android:text="/"
android:textSize="25dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="2dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

 activity_result.xml

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


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ResultActivity">

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result:"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.249"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textSize="40dp"
android:textStyle="bold"/>

<TextView
android:id="@+id/answer"
MCAD(3360704) 196080307560

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="40dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.392"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

 MainActivity.java

package com.example.practical11;

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 {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText v1=findViewById(R.id.value1);
EditText v2=findViewById(R.id.value2);
Button b1=findViewById(R.id.plus);
Button b2=findViewById(R.id.minus);
Button b3=findViewById(R.id.into);
Button b4=findViewById(R.id.divide);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double output = 0;
double a = Double.parseDouble(v1.getText().toString());
double b = Double.parseDouble(v2.getText().toString());
output = a + b;
String ans1 = String.valueOf(output);
MCAD(3360704) 196080307560

Intent i = new Intent(MainActivity.this, ResultActivity.class);


i.putExtra("answer", ans1);
startActivity(i);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double output = 0;
double a = Double.parseDouble(v1.getText().toString());
double b = Double.parseDouble(v2.getText().toString());
output = a - b;
String ans1 = String.valueOf(output);

Intent i = new Intent(MainActivity.this, ResultActivity.class);


i.putExtra("answer", ans1);
startActivity(i);
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double output = 0;
double a = Double.parseDouble(v1.getText().toString());
double b = Double.parseDouble(v2.getText().toString());
output = a * b;
String ans1 = String.valueOf(output);

Intent i = new Intent(MainActivity.this, ResultActivity.class);


i.putExtra("answer", ans1);
startActivity(i);
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double output = 0;
double a = Double.parseDouble(v1.getText().toString());
double b = Double.parseDouble(v2.getText().toString());
output = a / b;
String ans1 = String.valueOf(output);

Intent i = new Intent(MainActivity.this, ResultActivity.class);


i.putExtra("answer", ans1);
MCAD(3360704) 196080307560

startActivity(i);
}
});

}
}

 ResultActivity.java

package com.example.practical11;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class ResultActivity extends AppCompatActivity {

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

String answer= getIntent().getStringExtra("answer");


TextView ans=findViewById(R.id.answer);

ans.setText(answer);
}
}

OUTPUT:
MCAD(3360704) 196080307560
MCAD(3360704) 196080307560

PRACTICAL 12
Aim: Create an application that will change the color of the screen,based on
selected option from the menu.

 activity_main.xml

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


<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"
android:gravity="center"
android:id="@+id/layout">

<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center" />

</RelativeLayout>

• MainActivity.java

package com.example.practical12;

import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
MCAD(3360704) 196080307560

import android.widget.Spinner; import


android.widget.Toast;

public class MainActivity extends AppCompatActivity implements


AdapterView.OnItemSelectedListener {
Spinner spinner;
RelativeLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); layout =
findViewById(R.id.layout); spinner =
findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this,R.array.colors,
android.R.layout.simple_spinner_item );

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_it
e m); spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String text = adapterView.getItemAtPosition(i).toString();
Toast.makeText(adapterView.getContext(), text,
Toast.LENGTH_SHORT).show();
if (i==0){
layout.setBackgroundColor(Color.YELLOW);
}else if (i==1){
layout.setBackgroundColor(Color.GREEN);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
MCAD(3360704) 196080307560

}
}

• OUTPUT:
MCAD(3360704) 196080307560

PRACTICAL 13
Aim: Create an application that will get the Text Entered in Edit Text and
display that Text using toast.

 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"
tools:context=".MainActivity"
android:id="@+id/rlVar1"
>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter text"
android:ems="10"
android:inputType="text"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/showBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:layout_centerInParent="true"
android:text="Show"/>

</RelativeLayout>
MCAD(3360704) 196080307560

 MainActivity.java

package com.example.app1;
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 {


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

showBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), editText.getText().toString(),
Toast.LENGTH_LONG).show();
}
});
}
}

OUTPUT:-
MCAD(3360704) 196080307560
MCAD(3360704) 196080307560

PRACTICAL 14
Aim: Create an application that will Demonstrate Button onClick() Event and
change the TextView Color based on button Clicked.

 activity_main.xml
<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"
>
<TextView
android:layout_margin="10dp"
android:id="@+id/text_view"
android:text="A sample text view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
/>
<Button
android:id ="@+id/btnClickMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Click Me"
/>
MCAD(3360704) 196080307560

</LinearLayout>

 MainActivity.java

package com.example.app;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.graphics.Color;

public class MainActivity extends AppCompatActivity {


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

Button btn = findViewById(R.id.btnClickMe);


TextView tv = (TextView) findViewById(R.id.text_view);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv.setTextColor(Color.RED);
tv.setText("I'm priyanshi...");
}
});
}
}
MCAD(3360704) 196080307560

OUTPUT:
MCAD(3360704) 196080307560

PRACTICAL 15
Aim: Create an UI such that, one screen have list of all the types of cars. On
selecting of any car name, next screen should show Car details like:
name,launched date, company name.

 activity_main.xml

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


<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:id="@+id/textView2"
android:layout_width="394dp"
android:layout_height="69dp"
android:fontFamily="monospace" a
ndroid:text="List of available Cars"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.402"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.217"
android:textAlignment="center"/>

<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="84dp"
android:fontFamily="casual"
android:text="2017 Maruti Suzuki Vitara Brezza"

android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
MCAD(3360704) 196080307560

app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:fontFamily="casual"
android:text="2019 Honda Civic"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/t1" />

<TextView
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="84dp"
android:fontFamily="casual"
android:text="2020 Hyundai Elite i20"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/t1" />

<TextView
android:id="@+id/t4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="140dp"
android:fontFamily="casual"
android:text="2017 Ford Ecosport"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/t1" />

</androidx.constraintlayout.widget.ConstraintLayout>

 MainActivity.java
MCAD(3360704) 196080307560

package com.example.practical15;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


TextView t1;
TextView t2;
TextView t3;
TextView t4;

protected void onCreate(Bundle savedInstanceState)


{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = findViewById(R.id.t1);
t2 = findViewById(R.id.t2);
t3 = findViewById(R.id.t3);
t4 = findViewById(R.id.t4);
Intent intent = new Intent(this, outputActivity.class);

t1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{ intent.putExtra("car_selected","t1");
startActivity(intent);
}
});
t2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{ intent.putExtra("car_selected","t2");
startActivity(intent);
}
});
t3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{ intent.putExtra("car_selected","t3");
startActivity(intent);
}
});
MCAD(3360704) 196080307560

t4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{ intent.putExtra(“car_selected”,”t4”);
startActivity(intent);
}
});

}
}

 OUTPUT:

;
MCAD(3360704) 196080307560

PRACTICAL 16
Aim: Create an application that will Demonstrate Dialog Box Control in
Android.

 activity_main.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_centerInParent="true"
android:text="Close app" />

</RelativeLayout>

 MainActivity.java

pakage com.example.app;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


public static Button closeButton;
AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MCAD(3360704) 196080307560

closeButton = (Button) findViewById(R.id.button);


builder = new AlertDialog.Builder(this);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View v) {


builder.setMessage("Welcome to Alert Dialog") .setTitle("Dialog Demo");

setPositiveButton("Yes", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {


finish();
Toast.makeText(getApplicationContext(),"you choose yes action for alertbox",
Toast.LENGTH_SHORT).show();
}
})

.setNegativeButton("No", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {


dialog.cancel();
Toast.makeText(getApplicationContext(), "you choose no action for alertbox",
Toast.LENGTH_SHORT).show();
}
});

AlertDialog alert = builder.create();

alert.setTitle("AlertDialogExample");
alert.show();
}
});
}
}

 OUTPUT:
MCAD(3360704) 196080307560
MCAD(3360704) 196080307560

You might also like