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. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    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.

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

commented: Well done +13

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.