android - Adding imageView on ViewGroup with setOnTouchListener -


edit: question solved!

i'm facing a, hopefully, simple problem. need it's show/add image on ontouch position when user click on image/area. layout:

activity_test_diff.xml  <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" tools:context=".testdiff" android:orientation="vertical">  <relativelayout     android:id="@+id/relative_layout"     android:layout_width="match_parent"     android:layout_height="0dp"     android:layout_weight="1">      <imageview         android:id="@+id/img_rectangle"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:scaletype="fitcenter"         android:visibility="invisible"/>      <imageview         android:id="@+id/img_test_diff1"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:scaletype="fitcenter" />      <imageview         android:id="@+id/click_check_ok"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@drawable/check_green_48dp"/>      <imageview         android:id="@+id/click_check_ko"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:visibility="gone"         android:src="@drawable/close_red_48dp"/>  </relativelayout>  <imageview     android:id="@+id/img_test_diff2"     android:layout_width="match_parent"     android:layout_weight="1"     android:layout_height="0dp"     android:scaletype="fitcenter"/> </linearlayout> 

the parent group of relative layout it's linearlayout, don't care if touch outside of relativelayout. code handle touch:

testdiff.java  protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_test_diff);      relativelayout relative = (relativelayout) findviewbyid(r.id.relative_layout);      relative.setontouchlistener(this); }  @override public boolean ontouch(view v, motionevent ev) {     final int action = ev.getaction();      final int evx = (int) ev.getx();     final int evy = (int) ev.gety();      switch (action) {         case motionevent.action_down:              int x = (int) ev.getx();             int y = (int) ev.gety();             relativelayout.layoutparams lp = new relativelayout.layoutparams(                     relativelayout.layoutparams.wrap_content,                     relativelayout.layoutparams.wrap_content);             imageview iv = new imageview(getapplicationcontext());             lp.setmargins(x, y, 0, 0);             iv.setlayoutparams(lp);             iv.setimagedrawable(getresources().getdrawable(                     r.drawable.check_green_48dp));             ((viewgroup) v).addview(iv);              return true;         case motionevent.action_up:             // on up, click action.             // hidden image (img_rectangle) has 3 different hotspots on it.             // colors red, blue, , yellow.             // use img_rectangle determine region user             return true;         default:             return false;     } } 

and coming error:

attempt invoke virtual method 'void android.widget.relativelayout.setontouchlistener(android.view.view$ontouchlistener)' on null object reference                                                                        @ android.app.activitythread.performlaunchactivity(activitythread.java:2646) 

mean can figure out what's happening!

on xml layout have :

<relativelayout     android:id="@+id/relative_layout" 

in java code have

relativelayout relative = (relativelayout) findviewbyid(r.id.layout_relative);

you reversed relative & layout words


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -