3

I am working on app which has a titlebar with chronometer on the left and a textview centered in a RelativeLayout.

Title bar with Chronometer and TextView

RelativeLayout take height of textview and fill screen width

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titlecontainer"
    android:orientation="vertical"
    android:padding="5dip" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" android:background="@color/titlebckgrnd">

I want to hide the chornometer when user clicks on it and unhide it user clicks again.

How this can be achieved?

EDIT, further to the answers and comments:

Here is the color file code

<?xml version="1.0" encoding="utf-8"?>
   <resources>
   <color name="titlebckgrnd">#FFD55A2E</color>
   <color name="titletext">#FFFFFFFF</color>
 </resources>

I used the following code as suggested

final TextView chron = (TextView)findViewById(R.id.chronometer);
    ((Chronometer) chron).start();
 chron.setOnClickListener(new OnClickListener() {
        private boolean mToggle = true;
        @Override
        public void onClick(View v) {
                Log.d("Gaurav", "Invisible");
                if(mToggle) { 
                    Log.d("Gaurav", String.valueOf(chron.getCurrentTextColor()));
                    chron.setTextColor(R.color.titlebckgrnd);
                    mToggle = false;
                }
                else {
                                          chron.setTextColor(R.color.titletext);
                                          mToogle = true;
                                    } 


                //chronImage.setVisibility(View.VISIBLE);

                //v.setVisibility(View.INVISIBLE);

        }
    });

but the result is

enter image description here

and it does not respond to any more clicks.

LogCat Results

enter image description here enter image description here

Even the debugger breakpoints show change in Textcolor value but color change in display does not happen.

Onik
  • 19,396
  • 14
  • 68
  • 91
Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166

1 Answers1

1

If I properly understood your question you can do that way to hide the chronometer:

// We are in your Activity:

final View chronometer= findViewById(R.id.chronometer);

chronometer.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        chronometer.setVisibility(View.GONE); 
        // Use View.INVISIBLE if you would like to keep the room for this view
    }
});

But there will be no view displayed anymore. So depending of where the user should click to have it displayed again, you may have to give your view and/or its text the same color as the background instead of making them invisible:

// should chronometer be a TextView that displays the time:

final TextView chronometer = (TextView) findViewById(R.id.chronometer);

chronometer.setOnClickListener(new View.OnClickListener() {
    private boolean mToggle = true;

    public void onClick(View v) {
        if (mToggle ) {
            chronometer.setTextColor(Color.BLACK); 
            mToggle = false;
        }
        else {
            chronometer.setTextColor(Color.WHITE); 
            mToggle = true;
        }
    }
});
Shlublu
  • 10,917
  • 4
  • 51
  • 70
  • i have added code and image and problem I faced with your suggestion below. – Gaurav Agarwal Sep 14 '11 at 15:36
  • 1
    Well, I see that you cast `chron` to `TextView` and then to `Chronometer`. This is not the cause of the issue but you could have casted it directly to `Chronometer` as `Chronometer` extends `TextView`. About the issue itself, what does your LogCat say? Have you tried setting a breakpoint in the listener to see whether is is properly called? – Shlublu Sep 14 '11 at 15:49
  • I have added the LogCat results to the question. I don't know how to set the breakpoint, I reading about it and will add the comment shortly. I will implement the casting suggestion. – Gaurav Agarwal Sep 14 '11 at 16:18
  • 1
    It seems the press events are properly caught, according to your logcat. There must be something else that prevents the chronometer from resuming. Let us know when you'll have stepped in your code with the breakpoint! – Shlublu Sep 14 '11 at 16:28
  • Press events are working properly. Chronometer is also changing time value. I inserted break points and monitered the value of TextColor value and it is taking value 2130968576 and 2130968577. There is some problem with color changing. – Gaurav Agarwal Sep 14 '11 at 17:35