java - when button is clicked, i want to know how to put some images in layout -


i want put image. when image clicked, drawn in framelayout.

if press cat image , cat drawn.

here code, , images put in drawable folder.

mainactivity

import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.framelayout; import android.widget.horizontalscrollview; import android.widget.imagebutton;  public class mainactivity extends appcompatactivity implements view.onclicklistener {      imagebutton mstamp, mframe, mconfirmbtn, mcat, mmush;      horizontalscrollview horizontalscrollview;     framelayout framelayout;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          mstamp = (imagebutton)findviewbyid(r.id.stamp);         mframe = (imagebutton)findviewbyid(r.id.frame);         mconfirmbtn = (imagebutton)findviewbyid(r.id.confirm);         mcat = (imagebutton)findviewbyid(r.id.cat);         mmush = (imagebutton)findviewbyid(r.id.mush);          mstamp.setonclicklistener(this);         mframe.setonclicklistener(this);         mconfirmbtn.setonclicklistener(this);         mcat.setonclicklistener(this);         mmush.setonclicklistener(this);          horizontalscrollview = (horizontalscrollview)findviewbyid(r.id.horizontalscrollview);         framelayout = (framelayout)findviewbyid(r.id.framelayout);     }      @override     public void onclick(view view) {         switch(view.getid()){             case r.id.stamp :                 horizontalscrollview.setvisibility(view.visible);                 break;              case r.id.frame :                 horizontalscrollview.setvisibility(view.gone);                 break;              case r.id.confirm :                 // intent intent = new intent(mainactivity.this, confirmactivity.class);                 // startactivity(intent);                 break;              case r.id.cat :                 horizontalscrollview.setvisibility(view.gone);                 break;              case r.id.mush :                 horizontalscrollview.setvisibility(view.gone);                 break;         }     } } 

activity_main

<relativelayout 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:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity"     android:background="@android:color/black">      <framelayout         android:id="@+id/framelayout"         android:background="@android:color/white"         android:layout_width="match_parent"         android:layout_height="match_parent">     </framelayout>      <horizontalscrollview         android:id="@+id/horizontalscrollview"         android:layout_above="@+id/linearlayout"         android:layout_width="match_parent"         android:layout_height="wrap_content">         <linearlayout             android:layout_width="wrap_content"             android:layout_height="wrap_content">             <imagebutton                 android:background="@android:color/transparent"                 android:scaletype="fitcenter"                 android:src="@drawable/cat"                 android:id="@+id/cat"                 android:layout_width="40dp"                 android:layout_height="40dp" />              <imagebutton                 android:background="@android:color/transparent"                 android:scaletype="fitcenter"                 android:src="@drawable/mush"                 android:id="@+id/mush"                 android:layout_width="40dp"                 android:layout_height="40dp" />         </linearlayout>     </horizontalscrollview>       <linearlayout         android:id="@+id/linearlayout"         android:layout_alignparentbottom="true"         android:orientation="horizontal"         android:layout_width="match_parent"         android:layout_height="wrap_content">          <imagebutton             android:background="@android:color/transparent"             android:scaletype="fitcenter"             android:src="@drawable/stamp"             android:id="@+id/stamp"             android:layout_width="60dp"             android:layout_height="60dp" />          <imagebutton             android:background="@android:color/transparent"             android:scaletype="fitcenter"             android:src="@drawable/frame"             android:id="@+id/frame"             android:layout_width="60dp"             android:layout_height="60dp" />          <imagebutton             android:background="@android:color/transparent"             android:scaletype="fitcenter"             android:src="@drawable/confirm"             android:id="@+id/confirm"             android:layout_width="60dp"             android:layout_height="60dp" />     </linearlayout>   </relativelayout> 

drawable in res folder

cat.png confirm.png frame.png mush.png stamp.png 

please, give me tips.

if want add images in layout button click, set image views in layout. didn't described what, have code here , might help. here imagebuttons

public void onclick(view view) {     switch(view.getid()){          case r.id.stamp :             mstamp.setimageresource(r.drawable.yourimagename);             horizontalscrollview.setvisibility(view.visible);             break;      } } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -