0% found this document useful (0 votes)
14 views3 pages

Answers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Answers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PRACTICAL EXAM-ANSWER SHEET

Enrollment Number: EA2332251010249 Name of student: NIKHIL SAI


NANDANAVANAM
Date: 26-06-2024 Subject Code: V20PCA202
Name of Subject: Computer Networks

Title: Button Click Event


Aim/Objective:
Create a new Android application with a button that changes
the text of a Textview when clicked.

Procedure Steps/Algorithm:
Tasks:
1. Create a new Android Studio Project.
2. Add a button and a textview to the main layout file.
3. Write an onClick event handler in the MainActivity
class that changes the Textview text when the button
is clicked.
4. Run the application and test the functionality.

Program/Code:

xml code:

<?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">

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter text"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display"
android:layout_below="@id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your text will appear here"
android:layout_below="@id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>

</RelativeLayout>

Java code:
 Open mainActivity.Java.
 Implement the button click event handler with the following code.

Packagecom.example.textinputdisplay:

import android.os Bundle;


import android.view.view;
import android.widget.Button;
import android.widget.Edit Text;
import android.widget.Textview;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity{

private Textview textview;


private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = findViewById(R.id.editText);
Button button = findViewById(R.id.button);
TextView textView = findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String inputText = editText.getText().toString();
textView.setText(input text);
}
});
}
}

Run and test the Application:


 Connect an android device or start an emulator.
 Click the “Run“ button in the Android studio.
 Verify that clicking the Button button changes the textview text to input
text.

Result:

Thus creating a new Android application with a button that changes


the text of a Textview when clicked is done sucessfully.

You might also like