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);
}
}
What have you tried? Even without success, add the code of what you have already asked the question by clicking [Edit].
– user28595
Thanks for the tip... I already edited the post and put the code I’m trying.
– Will Levandoski