MCAD
MCAD
MCAD
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
<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
<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;
@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
startActivity(i);
}
});
}
}
ResultActivity.java
package com.example.practical11;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
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
<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
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;
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;
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
<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;
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
<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;
alert.setTitle("AlertDialogExample");
alert.show();
}
});
}
}
OUTPUT:
MCAD(3360704) 196080307560
MCAD(3360704) 196080307560