36

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?

3
  • 1
    i think these two statement should be somewhere else in oncreate or onstart methods all = (TextView) this.findViewById(R.id.viewall); pdf = (TextView) this.findViewById(R.id.pic); like all = (TextView)findViewById(R.id.viewall); pdf = (TextView)findViewById(R.id.pic); Commented Jan 31, 2012 at 12:05
  • i'm trying to display tabs like Pulse News app and came up with this idea but i can't get it work Commented Feb 1, 2012 at 15:43
  • i got it working now, thanks for your answers guys, i sticked with my first post code... Commented Feb 1, 2012 at 16:25

5 Answers 5

116

There is an easy way. Put this in the TextView in XML:

android:clickable="true" 
Sign up to request clarification or add additional context in comments.

4 Comments

Wow...didn't realize TextViews weren't clickable by default. Glad I ran across your answer. Thought I was going crazy there for a moment.
@Fernando JS, can you support detail for reason that why need set android:clickable="true"?
No. It doesn't work for me using SDK 21. The Sameer's answer worked fine.
BOSS! Worked great on Android 4.2.1. TextView must be not clickable by default, which makes sense. But I think later versions automatically set clickable to true when you set a onClick value.
42

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" 

4 Comments

In addition to what you mentioned above I had to call btn.setFocusableInTouchMode(false); to address the fact that the TextView would require a double-tap to actually fire the OnClick event. I don't know what might have changed in the last two years but this seems to be required in 4.4.2.
I get: The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})
Look at your import,you should import android.view.View.OnClickListener;. I guess you import click for dialog listener
thx, just adding "android clickable =true" in xml file works like charm!
7

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

Comments

0

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 } } 

2 Comments

also, is there other way to make it without implements OnClickListener at the class coz i already implements another class
First you can not implement a class, you can only implement inteface.And there is no restriction on number interface to be implement.So do not worry,implement one more interface that is onclickListener
-1

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

1 Comment

you can set only integer value in switch case not TextView directly @ramesh.Use all.getID() inplace of All

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.