0

I'm pretty new to android dev and I'm currently exploring the view visibility, the problem is that, when I try to toggle a component's visibility, my android app crashes. Code snippet below.

TextView txt;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    txt = (TextView) findViewById(R.id.textView1);
    txt.setVisibility(View.VISIBLE);

}
public void toggleVisibility(View view){
    RelativeLayout layout = new RelativeLayout(this);
    String a = getString(txt.getVisibility());
    if(a.equals("GONE")){
        txt.setVisibility(View.VISIBLE);
        layout.addView(txt);
    }
    else{
        txt.setVisibility(View.GONE);
        layout.removeView(txt);
    }

    this.setContentView(layout);
}

XML file using RelativeLayout

 <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:text="Button"
    android:onClick="toggleVisibility" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:text="Button" />
iamLinker
  • 88
  • 1
  • 10
  • Your `toggleVisibility()` method needs a `View` parameter. http://stackoverflow.com/questions/22927123/crossfading-2-views-using-a-button-in-android – Mike M. Jul 25 '16 at 02:20
  • Plz post your crash log – cowboi-peng Jul 25 '16 at 02:24
  • @MikeM. unfortunately it still crashes when I click the button – iamLinker Jul 25 '16 at 02:38
  • Yeah, I just noticed you're switching layouts with that `this.setContentView(layout)` call. Why are you doing that? If you just want to toggle a `View`'s visibility, you just need to call its `setVisibility()` method. You don't need the new `RelativeLayout` stuff. Also, you really need to post stack traces for questions about debugging crashes. http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Mike M. Jul 25 '16 at 02:41

1 Answers1

0

I have added the answers inline to explain the procedure. See if it fits your needs.

If you need anything else to be explained, please let me know

  TextView txt;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        // 1 - Here you associated the layout with your new textview. Means that you now hold a reference 
        // to your "txt" textview and that you have set it to be VISIBLE
        txt = (TextView) findViewById(R.id.textView1);
        txt.setVisibility(View.VISIBLE);

    }

    // 2 - you pass the argument view, but you dont really use it, so you can remove the paremeter 
    public void toggleVisibility(View view){

        // 3 - You dont need the new RelativeLayout. Since you already got the reference to the textview
        // and since the TextView is already part of your layout, you can just call methods on it without having to remove and add it again
        // RelativeLayout layout = new RelativeLayout(this);

        // 4 - Dont get visibility from a String like you did
        // String a = getString(txt.getVisibility());
        // So, instead of doing :
        /*
        if(a.equals("GONE")){
            txt.setVisibility(View.VISIBLE);
            layout.addView(txt);
        }
        else{
            txt.setVisibility(View.GONE);
            layout.removeView(txt);
        }
        */
        // You can just get the Visibility of a View using getVisibility() by doing the following : 

        if(txt.getVisiblity == View.VISIBLE) {
            txt.setVisibility(View.GONE);
        } else {
            txt.setVisibility(View.VISIBLE);
        }

        // 5 - The setContentView method is generally meant to attach the Activity's view to the Activity, so you dont need/cant use it here
        // this.setContentView(layout);
    }
Ricardo Vieira
  • 1,738
  • 1
  • 18
  • 26