TextView onClick() not working

here’s my code for main.xml

 <merge xmlns:android="http://schemas.android.com/apk/res/android">
 <RelativeLayout 
    android:id="@id/container" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">


<include layout="@layout/tabs" />

<ScrollView
    android:fillViewport="true" 
    android:scrollbars="@null"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

    <LinearLayout 
        android:paddingTop="10dp"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <!-- first text_view -->
        <TextView 
            android:background="@color/grey"
            android:textColor="@color/white"
            android:text="@string/category" 
            android:id="@+id/category1" 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent"
            android:layout_marginTop="65dp" 
            android:textSize="17dp"
            android:typeface="serif"/>

        <!-- first horizontal_scrollview -->
        <HorizontalScrollView 
            android:scrollbars="@null"
            android:id="@+id/horizontalScrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <LinearLayout android:id="@+id/linearLayout1"
                android:orientation="horizontal" 
                android:visibility="visible"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content">

                <!-- image_view should be here -->

            </LinearLayout>
        </HorizontalScrollView>
    </LinearLayout>
</ScrollView>
</RelativeLayout>
</merge>

here’s my code for tabs.xml

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" 
android:background="#333333">

<TextView
    android:textColor="@color/gradient_green"
    android:id="@+id/viewall"
        android:layout_width="85dp"
        android:layout_height="25dp"
        android:layout_marginLeft="10dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textSize="17dp"
        android:textStyle="bold" 
        android:text="@string/view_all"
        android:onClick="onClick"
        android:focusable="false"
        android:clickable="true" />

    <TextView
        android:textColor="@color/white"
        android:id="@+id/pic"
        android:layout_width="45dp"
        android:layout_height="25dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/viewall"
        android:textSize="17dp"
        android:textStyle="bold" 
        android:text="@string/pic"
        android:onClick="onClick"
        android:focusable="false"
        android:clickable="true" />
</RelativeLayout>

and here’s the code inside the Main.java:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView all = (TextView) this.findViewById(R.id.viewall);
    TextView pic = (TextView) this.findViewById(R.id.pic);

    all.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            TextView all = (TextView) findViewById(R.id.viewall);
            TextView pic = (TextView) findViewById(R.id.pic);

            Toast.makeText(Main.this, "VIEW ALL", Toast.LENGTH_SHORT).show();

            all.setTextColor(getResources().getColorStateList(R.color.gradient_green));
            pic.setTextColor(getResources().getColorStateList(R.color.white));
        }
    });

    pdf.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            TextView all = (TextView) findViewById(R.id.viewall);
            TextView pic = (TextView) findViewById(R.id.pic);

            Toast.makeText(Main.this, "VIEW ALL", Toast.LENGTH_SHORT).show();

            all.setTextColor(getResources().getColorStateList(R.color.white));
            pic.setTextColor(getResources().getColorStateList(R.color.gradient_green));
        }
    });

  }

so, if i set the setContentView() in the Main.class or Main.java as setContentView(R.layout.tabs) instead of setContentView(R.layout.main), the onClick() works, what should i do or what’s wrong with my code that hinders onClick() not to work?

TextView onclick event not working

In my android code, I have implemented onclick event on 3 textviews using following code. But nothing happens when they are clicked. What is wrong ? <TextView android:[email protected]+id/tv1 android:layout_

ontouch and onclick on a textview not working

have a look at the code below… i am setting the onclick listener on the textview and the ontouch listener but none of them is working… what seems to be the problem>….? AlertDialog.Builder bu

Android, textview onclick crash

i have my class which extends baseActivity, all works well except a simple onClick on a textview šŸ˜ TextView labelName_view = (TextView) findViewById(R.id.labelName); labelName_view.setOnClickListene

TextView – OnClick – Android

Is it possible to add onclick to some portion of a textview? For instance my code goes like this, String content = Hello this a test.. For more details contact @Peter; someTextView.setText(content);

onClick and onTouch on TextView?

I want to do something when the user touch (or click) on a TextView but I can’t seems to make it work. I simply place a TextView in Layout and I tryied to set an onclick listener. touchTV = (TextView

TextView in Listview onclick conflict

I have made an Listview populated with list_row_layout.xml(which is populated with json serializable class), i have clickable textview and onclick changing text from Accept to Accepted. But when i

OnClick() method doesn’t work in TextView

I have a textView with OnClick method to navigate another activity. But, If i pressed that text it doesn’t navigate. But If i used Button instead TextView, it works perfectly. Can’t use OnClick method

Adding OnClick(View v) in a textview

i have added onClick in my mainactivity for my textview and i had no errors but when i run the app it crashes saying app has stopped working even though i have no errors in my codes i dont know what’s

Clickable textview not working

I’ve looked around SO for how to make a textview clickable, and I am doing everything they say but the onClick method is still not being called. Here’s my code from the activity: public void onClick(V

Textview onclick bg color change in android

If we click any of the Textview within a Listview, background color should have to change and onclick release, it should have to change to transparent color in android.Is there anyway?

Answers

please make changes like this :

 <include  android:id = "@+id/lyttab" layout="@layout/tabs" />

in your Java class

 public void onClick(View v){   

  View view = findViewById(R.id.lyttab);

 all = (TextView) view.findViewById(R.id.viewall);
 pif = (TextView) view.findViewById(R.id.pic);


switch (v){

case  all :
    Toast.makeText(Main.this, "VIEW ALL", Toast.LENGTH_SHORT).show();

    all.setTextColor(getResources().getColorStateList(R.color.gradient_green));
    pic.setTextColor(getResources().getColorStateList(R.color.white));

    break;

case pic:
    Toast.makeText(Main.this, "PDF", Toast.LENGTH_SHORT).show();

    all.setTextColor(getResources().getColorStateList(R.color.white));
    pic.setTextColor(getResources().getColorStateList(R.color.gradient_green));
    break;

 }
 }

i think this will help you

Use these

 all = (TextView) this.findViewById(R.id.viewall);
 pdf = (TextView) this.findViewById(R.id.pic);

in on create and then set

all.setOnclickListener(this) in oncreate() method too.Implement onClicklistener when it will show error. it will work like a charm.

Edited

TextView btn=(TextView) findViewById(R.id.accInfo);
btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //DO you work here
        }
    });

Setting Clicklistenner to TextView will automatically make it clickable so no need of

android:clickable="true"

In your onCreate method you need to:

  1. Create variables for your TextViews
  2. Assign the onClickListener for them

And your class also needs to implement OnClickListener.

public class SqliteTestsActivity extends Activity implements OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView all = (TextView) R.findViewById(R.id.viewall);
        all.setOnClickListener(this);
    }

    public void onClick(View v) {
        // Fill in this with your switch statement
    }
}

There is a easy way, put this in xml TextView:

android:clickable=”true”

add android:clickable=”true” in <TextView>