package com.example.
textviewedittextexample;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Views
TextView textView = findViewById(R.id.textView);
EditText editText = findViewById(R.id.editText);
Button button = findViewById(R.id.button);
// Button Click Event
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get text from EditText
String userInput = editText.getText().toString();
// Set text to TextView
textView.setText(userInput);
}
});
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<!-- TextView to Display Text -->
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter text below"
android:textSize="18sp"
android:textColor="@android:color/black"
android:padding="8dp"/>
<!-- EditText for User Input -->
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something..."
android:padding="8dp"/>
<!-- Button to Update TextView -->
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Text"
android:layout_marginTop="16dp"/>
</LinearLayout>