17IT007 Practical 5 PDF
17IT007 Practical 5 PDF
PRACTICAL: 7
AIM:
Create an application that will change color of the screen, based on selected options from the menu.
THEORY:
Button: In Android, Button represents a push button. A Push buttons can be clicked, or pressed by the
user to perform an action. There are different types of buttons used in android such as
CompoundButton, ToggleButton, RadioButton.
EditText: In Android, EditText is a standard entry widget in android apps. It is an overlay over
TextView that configures itself to be editable. EditText is a subclass of TextView with text editing
operations. We often use EditText in our applications in order to provide an input or text field,
especially in forms.
Toast: In Android, Toast is used to display information for a period of time. It contains a message to
be displayed quickly and disappears after specified period of time. It does not block the user
interaction. Toast is a subclass of Object class. In this we use two constants for setting the duration
for the Toast. Toast notification in android always appears near the bottom of the screen. We can also
create our custom toast by using custom layout(xml file).
CSPIT-IT
17IT007 IT349: WCMC
CODE:
Program: 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/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:layout_marginEnd="160dp"
android:text="Login"
android:textSize="35dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="252dp"
android:layout_marginBottom="69dp"
android:focusable="true"
android:hint="Enter Name"
app:layout_constraintBottom_toTopOf="@+id/editText2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.038"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
CSPIT-IT
17IT007 IT349: WCMC
tools:ignore="MissingConstraints" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:layout_alignEnd="@+id/editText"
android:layout_alignRight="@+id/editText"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="39dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="28dp"
android:layout_marginBottom="116dp"
android:text="Attempts Left:"
android:textSize="25dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toStartOf="@+id/tx1"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/tx1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="64dp"
android:layout_marginBottom="40dp"
CSPIT-IT
17IT007 IT349: WCMC
android:layout_toEndOf="@+id/textview"
android:layout_toRightOf="@+id/textview"
android:text=""
android:textSize="25dp"
app:layout_constraintBottom_toTopOf="@+id/tx3"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginEnd="124dp"
android:layout_marginBottom="52dp"
android:layout_toStartOf="@+id/textview"
android:layout_toLeftOf="@+id/textview"
android:text="login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="0.416"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginEnd="40dp"
android:layout_marginBottom="52dp"
android:layout_toEndOf="@+id/textview"
android:layout_toRightOf="@+id/textview"
android:text="Cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/tx3"
android:layout_width="183dp"
android:layout_height="32dp"
android:layout_marginBottom="144dp"
android:text=""
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
CSPIT-IT
17IT007 IT349: WCMC
</androidx.constraintlayout.widget.ConstraintLayout>
Program: MainActivity.java
package com.example.practical5_17it007;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
Button b1,b2;
EditText ed1,ed2;
TextView tx1;
TextView tx3;
int counter = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button);
ed1 = (EditText)findViewById(R.id.editText);
ed2 = (EditText)findViewById(R.id.editText2);
b2 = (Button)findViewById(R.id.button2);
tx1 = (TextView)findViewById(R.id.tx1);
tx3 = (TextView)findViewById(R.id.tx3);
CSPIT-IT
17IT007 IT349: WCMC
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ed1.getText().toString().equals("17IT007") &&
ed2.getText().toString().equals("17IT007")) {
tx3.setText("Login Successfully");
tx3.setBackgroundColor(Color.GREEN);
}else{
counter--;
tx1.setText(Integer.toString(counter));
tx3.setText("Login Failed");
tx3.setBackgroundColor(Color.RED);
if (counter == 0) {
b1.setEnabled(false);
}
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
CSPIT-IT
17IT007 IT349: WCMC
OUTPUT:
LATEST APPLICATIONS:
LEARNING OUTCOME:
1. Displaying result through textview .
2. Display toast message .
3. Fetching value from edittext .
4. Using button for login and cancel .
CSPIT-IT