java - How to make a button to generate a random image from Drawable - Android Studio -
i new android studio , question this: have 8 images in drawable called ukchance1 ukchance2 ukchance3 etc. have button , imageview on 1 of layouts. have make when button clicked, random ukchance image shows...
ukchancelayout:
package com.austinthomas.monopolyactioncards.activity; import android.content.res.typedarray; import android.os.bundle; import android.support.annotation.idres; import android.support.v7.app.appcompatactivity; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.imageswitcher; import android.widget.imageview; import android.widget.viewswitcher; import com.austinthomas.monopolyactioncards.r; import java.util.arraylist; import java.util.collections; import java.util.random; public class ukchancelayout extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.uk_chance); } }
uk_chance.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="uk - chance" android:id="@+id/textview4" android:layout_alignparenttop="true" android:layout_alignparentstart="true" /> <button android:layout_width="300dp" android:layout_height="100dp" android:text="click here chance!" android:id="@+id/uk_chance_button" android:background = "#f44336" android:allowundo="false" android:textcolor="#ffffff" android:layout_marginbottom="32dp" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" /> <imageview android:layout_width="250dp" android:layout_height="143dp" android:id="@+id/uk_chance_image" android:layout_margintop="37dp" android:layout_below="@+id/textview4" android:layout_centerhorizontal="true" /> </relativelayout>
edit have tried larperdoodles method , says "unfortunately app has stopped"
here logcat:
8-14 19:33:57.277 17380-17380/com.austinthomas.monopolyactioncards d/androidruntime: shutting down vm 08-14 19:33:57.278 17380-17380/com.austinthomas.monopolyactioncards e/androidruntime: fatal exception: main process: com.austinthomas.monopolyactioncards, pid: 17380 java.lang.illegalstateexception: not find method clickhandler(view) in parent or ancestor context android:onclick attribute defined on view class android.support.v7.widget.appcompatbutton id 'uk_chance_button' @
android.support.v7.app.appcompatviewinflater$declaredonclicklistener.resolvemethod(appcompatviewinflater.java:327)
at
android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:284)
at android.view.view.performclick(view.java:5198) @ android.view.view$performclick.run(view.java:21147) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) 08-14 19:33:59.376 17380-17380/com.austinthomas.monopolyactioncards i/process: sending signal. pid: 17380 sig: 9
use method return drawable randomly based on images have.
public class ukchancelayout extends appcompatactivity { int min =1, max = 8; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.uk_chance); yourbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { myimage.setimagedrawable(getrandomdrawable(ukchancelayout.this)); } } public drawable getrandomdrawable(context context){ random random = new random(); int = random.nextint(max - min + 1) + min; drawable drawable = contextcompat.getdrawable(context, defaultresid);; switch (i) { case 1: drawable = contextcompat.getdrawable(context, resid1); break; case 2: drawable = contextcompat.getdrawable(context, resid2); break; case 3: drawable = contextcompat.getdrawable(context, resid3); break; case 4: drawable = contextcompat.getdrawable(context, resid4); break; case 5: drawable = contextcompat.getdrawable(context, resid5); break; case 6: drawable = contextcompat.getdrawable(context, resid6); break; case 7: drawable = contextcompat.getdrawable(context, resid7); break; case 8: drawable = contextcompat.getdrawable(context, resid8); break; } return drawable; } }
Comments
Post a Comment