XML : Making a TextView visible then invisible and back using onClick

I am trying to make two textviews appear and disappear on the same click and then on the next click vica versa appear and disappear. I have read some posts on this site, Make Textview Visible by Pressing a Button and Changing the visibility of a textview in a listview, but the solutions in these examples does not work for me. However, I have borrowed some of their ideas.

     package com.mycompany.screenchangeapplication;        import android.app.*;      import android.graphics.drawable.ColorDrawable;      import android.os.*;      import android.view.*;      import android.widget.*;    public class ScreenActivity extends Activity {        public RelativeLayout container;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_screen);            container = (RelativeLayout) findViewById(R.id.ScreenActivity);          container.setOnClickListener(new View.OnClickListener() {                @Override              public void onClick(View v) {                  changeScreen(v);              }          });      }        public void changeScreen(View v) {          ColorDrawable cd = (ColorDrawable) this.container.getBackground();          TextView ON = (TextView) findViewById(R.id.ON);          TextView OFF = (TextView) findViewById(R.id.OFF);            if (cd != null && cd.getColor() == getResources().getColor(R.color.WHITE)) {              container.setBackgroundColor(getResources().getColor(R.color.BLACK));              OFF.setVisibility(View.VISIBLE);              ON.setVisibility(View.INVISIBLE);          } else {              container.setBackgroundColor(getResources().getColor(R.color.WHITE));              OFF.setVisibility(View.INVISIBLE);              ON.setVisibility(View.VISIBLE);          }      }      }    

and

  <RelativeLayout      xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:paddingLeft="@dimen/activity_horizontal_margin"      android:paddingRight="@dimen/activity_horizontal_margin"      android:paddingTop="@dimen/activity_vertical_margin"      android:paddingBottom="@dimen/activity_vertical_margin"      android:id="@+id/ScreenActivity"      android:clickable="true"      tools:context=".ScreenActivity">        <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:textAppearance="?android:attr/textAppearanceLarge"          android:text="ON"          android:id="@+id/ON"          android:visibility="visible"          android:layout_centerVertical="true"          android:layout_centerHorizontal="true"          android:textSize="150dp" />        <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:textAppearance="?android:attr/textAppearanceLarge"          android:text="OFF"          android:id="@+id/OFF"          android:layout_centerVertical="true"          android:layout_centerHorizontal="true"          android:textSize="150dp"          android:textColor="#ffffff"          android:visibility="invisible" />    </RelativeLayout>    

When I put the app in the emulator it crashes, so something is badly wrong.

I am not sure how View v should be passed to onClick and to changeScreen. I would hope that the View passed to onClick would also be passed to changeScreen, but I am unsure how exactly this would work.

In Android Studio, all the text seems to be fine (its not though).

No comments:

Post a Comment