Reputation: 7092
In an Android app, I want to handle clicks both on a background element and on foreground elements. In my test case, clicks on the foreground elements do not get sent.
I've taken a barebones Hello World project called Test, and altered it as follows.
In activity_test.xml
, I have set ids for the layout and the TextView
, and I have given them both onClick
properties:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:id="@+id/layout"
android:onClick="doStuff">
<TextView
...
android:id="@+id/hello_world"
android:textSize="52dp"
android:onClick="doStuff" />
</RelativeLayout>
In the main TestActivity
class, I have the following class:
public class TestActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
TextView view = (TextView) findViewById(R.id.hello_world);
doStuff(view);
Log.d("onCreate", "testing " + view.getId());
}
public void doStuff(TextView textView) {
Log.d("test", "text " + textView.getId() + " " + R.id.hello_world);
}
public void doStuff(View view) {
Log.d("test", "view " + view.getId() + " " + R.id.layout);
}
}
In the log window, after launching the app, I see the following:
D/test﹕ text 2131165250 2131165250
D/onCreate﹕ testing 2131165250
This shows that the TestView version of the doStuff
method is definitely triggered when a TestView parameter is sent.
However, if I now tap on the Hello World TextView, this is the line that is added to the log:
D/test﹕ view 2131165249 2131165249
In other words, tapping the Hello World text has the same effect as tapping on the background itself. What do I need to do to get the TextView element to respond to its onClick setting?
Upvotes: 0
Views: 349
Reputation: 2137
hi can you use setOnTouchListener? then you can refer below given code.. it may work i think.. as per my knowledg, the problem is , whenever you touch textview, it will call onTouchListener for both textview and background.. sorry for my bad english
private int textViesTouchStatus = 0;//public variable
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView view = (TextView) findViewById(R.id.textView1);
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
textViesTouchStatus = 1;
Log.d("touch_textview", "code that should be run on textview touch goes here");
return false;
}
});
RelativeLayout layout = (RelativeLayout) findViewById(R.id.lay);
layout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (textViesTouchStatus == 1)
textViesTouchStatus = 0;
else
Log.d("touch_background", "code that should be run on layout touch goes here");
return false;
}
});
Upvotes: 0
Reputation: 44118
What you're missing is:
android:clickable="true"
The TextView
is not clickable by default.
Upvotes: 1