Rohit Bandil
Rohit Bandil

Reputation: 232

Can we write onClickListner on invisible TextView - Android?

This is my xml file in that on TextView is there

<TextView
    android:id="@+id/hello"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    android:textColor="#ffffff"
    android:background="#000000"
    android:clickable="true"
    android:visibility="invisible" />

and this is my activity code here i am writing the event on text view. i want to visible/invisible the text view when we click on it.

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_visibility_test);
        view=(TextView) findViewById(R.id.hello);
        view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(view.getVisibility()==View.VISIBLE)
            {
                Log.d("visibility test", "Visible");
                view.setVisibility(View.INVISIBLE);
            }
            else
            {
                Log.d("visibility test", "inVisible");
                view.setVisibility(View.VISIBLE);
            }
        }
    });
}

Upvotes: 0

Views: 315

Answers (4)

yushulx
yushulx

Reputation: 12160

Definitely you can, but the onClick event won't be triggered if the view is invisible. To make it visible, you can use onTouchEvent:

@Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        view.setVisibility(View.VISIBLE);
        return super.onTouchEvent(event);
    }

view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(view.getVisibility()==View.VISIBLE)
            {
                Log.d("visibility test", "Visible");
                view.setVisibility(View.INVISIBLE);
            }
         /*   else
            {
                Log.d("visibility test", "inVisible");
                view.setVisibility(View.VISIBLE);
            }
        } */
    });

Upvotes: 0

balaji koduri
balaji koduri

Reputation: 1321

yes, we can set ClickListener for invisible views check this link , The view is invisible, but it still takes up space for layout purposes.

Upvotes: 0

Vignesh Shiv
Vignesh Shiv

Reputation: 1157

XML:

<TextView android:id="@+id/hello"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:hint="@string/A2"
    android:visibility="invisible"/>

Code:

Button abutton = (Button) findViewById(R.id.AButton);
abutton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        TextView tv = AndroidAssignment2_1.this.findViewById(R.id.Answers);
        tv.setVisibility(View.VISIBLE);
    }
}); 

Upvotes: 1

Rajesh
Rajesh

Reputation: 15774

Yes, you can have event listeners on invisible views.

Upvotes: 1

Related Questions