I am trying to create clickable textviews in Android. I'm a complete noob when it comes to java and the Android platform, so please take it easy on me.

Here's what I have right now:

public class Hypertension extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
    public void clickHandler(View v){    	
    	if (v.getId() == R.id.tvRed) {
    		startActivity(new Intent(getApplicationContext(), Red.class));
    	if (v.getId() == R.id.tvGreen) {
    		startActivity(new Intent(getApplicationContext(), Green.class));

And I've added the line: android:onClick="clickHandler" to the appropriate textviews in the main.xml

When I run the program on the emulator, I get the main layout to display, but when I click on the text views, it appears that nothing happens. I'm trying to get it to run an activity when I click on it.

EDIT: In an attempt to debug myself, I changed the textviews to buttons in order to simplify things. I click the button, and the app crashes, and I must force quit. I don't know if that helps at all, just thought I should mention.

Edited by dfetter88: more info

7 Years
Discussion Span
Last Post by php_noob

I solved it myself... I needed to add android:clickable="true" to the appropriate xml tags as well.

Votes + Comments
Well done

well how did you assign ids? I have a problem that I created a textview that would appear to be Listview, it's data came from the xml resource.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.