Answers
Answers
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:
<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:
@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);
}
});
}
}
Result: