Hello, beautiful members of Hive Learners, I hope you all are well. Welcome to our 20th lecture on Android App Development. We end with the simple calculator and we learn many new things during the simple calculator development. Today we will start a new app and explore more in Android app development.
Use this GitHub project to clone into your directory. It will constantly get updated in the following lecture so you will never miss the latest code. Happy Coding!.
I create a new project HiveLearners2. We need to add a button and a textview. Here is the main_activity.xml code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/state_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hide" />
<TextView
android:id="@+id/name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hive Learners"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
Now will declare, initialize and add an on-click listener for the button in the Main_Activity.java file.
Now we will use the Visibility function setVisibility. It accepts three parameters, View.VISIBLE, View.INVISIBLEand View.GONE. with View.GONEthe target widget leave our screen and set its height and width to 0.
name_tv.setVisibility(View.INVISIBLE);
It will hide the name_tv when state_btn is clicked. Run and check is it working or not.
Now let's create a login to hide and show the text and also change the text of the button. When the name_tv is hidden button text will be shown and vice versa.
if (name_tv.getVisibility() == View.INVISIBLE) {
name_tv.setVisibility(View.VISIBLE);
state_btn.setText("HIDE");
} else {
name_tv.setVisibility(View.INVISIBLE);
state_btn.setText("SHOW");
}