Show / Hide Button and TextView

2

I'm making my first App in Android Studio, but I'm having a hard time doing something relatively easy. I need a Click on a Button to show the TextView and another Button (which will hide the TextView when it is clicked again). But all the alternatives I researched and tried are locking the App. Would anyone have any similar code or would you have any suggestions?

After the help of @Skywalker the code is as follows, it is already working.

XML

    <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Mostar_1"
    android:id="@+id/btnMostar_1"
    android:onClick= "clickBtnMostar_1"
    android:layout_gravity="center_horizontal" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Mostar_2"
    android:id="@+id/btnMostar_2"
    android:onClick= "clickBtnMostar_2"
    android:layout_gravity="center_horizontal" />

<LinearLayout
    android:id="@+id/LyMostar_1"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:visibility="invisible">


    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Texto 1"
    android:id="@+id/textMostar_1"
    android:layout_below="@+id/LyMostar_1"
    android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Esconder1"
    android:id="@+id/btnEsconder1"
    android:layout_below="@+id/btnMostar_1"
        android:onClick= "clickBtnEsconder1"
    android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

<LinearLayout
    android:id="@+id/LyMostar_2"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:visibility="invisible">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Texto 2"
        android:id="@+id/textMostar_2"
        android:layout_below="@+id/LyMostar_2"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Esconder2"
        android:id="@+id/btnEsconder2"
        android:layout_below="@+id/btnMostar_2"
        android:onClick= "clickBtnEsconder2"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

Java

public LinearLayout LyMostar_1;
public LinearLayout LyMostar_2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tela2);
    LyMostar_1 = (LinearLayout) findViewById(R.id.LyMostar_1);
}

public void clickBtnMostar_1(View view) {

    LyMostar_1.setVisibility(View.VISIBLE);
}

public void clickBtnEsconder1 (View esc){
    LyMostar_1.setVisibility(View.INVISIBLE);
}


public void clickBtnMostar_2(View view) {
    LyMostar_2.setVisibility(View.VISIBLE);
}

public void clickBtnEsconder2 (View esc){
    LyMostar_2.setVisibility(View.INVISIBLE);
}

}

    
asked by anonymous 16.01.2016 / 04:02

1 answer

5

Put everything you want to hide inside a LinearLayout or some Layout of your choice and place an id for that Layout in xml.

<LinearLayout
        ....Layout principal da activity>
       <Button
            ...button que faz a ação de esconder o conteudo desejado
           />

       <LinearLayout
              android:id="@+id/ll_conteudo"
              ... layout que sera escondido...>
           <TextView .... />
           <Button ..... />
       </LinearLayout>
</LinearLayout>

In the java code in the onCreate of your Activity, you make the casting of this layout

llConteudo = (LinearLayout) findViewById(R.id.ll_conteudo);

In the action button listener that should hide / show the content you implement as follows.

if(show){
    llConteudo.setVisibility(View.INVISIBLE);
    show = false;
}else{
    llConteudo.setVisibility(View.VISIBLE);
    show = true;
}

Note that you need to create a flag (a boolean) to know the state of the content, invisible or visible. Home I do not know your level of knowledge, but if you are in doubt I can be less objective and more exemplary.

Your XML should look something like this:

    
16.01.2016 / 14:29