java - The following android code is not giving desired result -


i wanted make clickable button such when click it, display text message. wrote following code:

activity_main.xml   <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.android.quiztime.mainactivity">  <linearlayout     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_weight="1"> <imagebutton     android:src="@drawable/football"     android:layout_width="wrap_content"     android:layout_height="250dp"     android:clickable="true"     android:id="@+id/football"     />     <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/texty"/> </linearlayout> 

java file:

package com.example.android.quiztime;  import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.imagebutton; import android.widget.textview;  public class mainactivity extends appcompatactivity {  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     imagebutton imagebutton = (imagebutton) findviewbyid(r.id.football);     imagebutton.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             final textview textview = (textview) findviewbyid(r.id.texty);             textview.settext("click success");         }     });  }  } 

i tried change imagebutton imageview , try setonclicklistener that. in vain. please show me going wrong.

you can add android:onclick xml file

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.android.quiztime.mainactivity">  <linearlayout     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_weight="1"> <imagebutton     android:src="@drawable/football"     android:layout_width="wrap_content"     android:layout_height="250dp"     android:clickable="true"     android:onclick="pritntext"     android:id="@+id/football"     />     <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/texty"/> </linearlayout> 

mainactivity.java

public class mainactivity extends appcompatactivity {     imagebutton imagebutton;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         imagebutton = (imagebutton) findviewbyid(r.id.football);     }      public void printtext(view v) {         log.d("debug","works");     } } 

Comments