Mostar / Hide Button and Textview

Asked

Viewed 11,112 times

2

I’m making my first App on Android Studio, but I’m having trouble doing something relatively easy. I need Click a Button to show Textview and another Button (which will hide Textview when clicked again). But all the alternatives I’ve researched and tried are crashing the app. Would anyone have any similar code or would have any suggestions?


After help from @Skywalker the code was as follows, 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);
}

}

  • 2

    What have you tried? Even without success, add the code of what you have already asked the question by clicking [Edit].

  • Thanks for the tip... I already edited the post and put the code I’m trying.

1 answer

5


Put everything you want to hide inside a Linearlayout or some Layout of your preference and put 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 on your Activity’s onCreate, you do the casting of this layout

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

In the action button Listener you must 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.

I don’t know your level of knowledge, but if in doubt I can be less objective and exemplify more.

Your XML should look more or less like this:

  • Thank you @Skywalker for the tip. About my level of knowledge I presume to be little, but I think the only part I was doubtful about would be about "casting layout"... Could you explain how I do it? .

  • In this case casting is you take the reference of the layout you declared in xml to work on java code. Casting involves heretic and polymorphism, da a good researched on polymorphism that you will understand.

  • I added a correction to the code, view.findViewById, remove the view. findViewById methods is from Activity itself, same as this.findViewById. I had used the view. because I mistook that you were working with Fragments.

  • Give a look at the xml I modified, I think you n understood yet.

  • Thanks again @Skywalker, overall I’ve managed to make the code work as I wanted. The only thing is, and that my screen is not "self-organizing", for example, I would like Layout1 to come between Button 1 and Button 2, and after Layout is hidden, the buttons go back to one at the bottom of the other. I believe it is something easy, I just lack knowledge. I need to open a new question for this? Another question would be, I need to delete the question after solved?

  • Change where you have View.INVISIBLE by View.GONE, then it won’t take up space. Don’t need to delete the question, it can help others.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.