androidandroid-layoutmdpi

Stop execution of a function if layout is mdpi/ldpi?


I am designing an Android application and I am struggling with different layouts. Basically, I am showing a Calendar and a TextView when on portrait, which updates its value each time a tile from the calendar is clicked. I don't want the TextView to show up anymore in land-mdpi. I created a layout for landscape, mdpi, in which I deleted the TextView option, but I think I have to do this programatically, as an error appears now every time I try to click a tile on a device with such specifications (the function is still active and tries to update the TextView - which does not exist anymore in this layout format). How do I do this?


Solution

  • When you get the TextView reference from your view hierarchy using findViewById(), you'll get a null if the view is not there. So just use something along the lines of

    TextView tv = (TextView)someView.findViewById(R.id.yourTextViewId);
    if (tv != null) {
      // The TextView is there and can be used
    } else {
      // The TextView isn't there
    }