java - Random button Android project -
i want make app when click button go random place on screen. want imagebutton when onclick go random place on screen. i've attempted before. no success. here of code i've attempted with. --->
package com.example.e99900004533.candycollector; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.graphics.point; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.view.display; import android.view.viewgroup.marginlayoutparams; import android.animation.objectanimator; import android.widget.relativelayout; import android.widget.edittext; import android.widget.imagebutton; import java.util.random; public class mainactivity extends actionbaractivity { public edittext collectedtextedit; public imagebutton candyedit; public int collected = 0; public int screenwidth = 300; public int screenheight = 300; random random = new random(); public boolean running = true; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); collectedtextedit = (edittext) findviewbyid(r.id.collectedtext); candyedit = (imagebutton) findviewbyid(r.id.candy); collectedtextedit.settext("collected: " + collected); display display = getwindowmanager().getdefaultdisplay(); point size = new point(); display.getsize(size); screenwidth = size.x; screenheight = size.y; addlisteneronbutton(); } public void addlisteneronbutton() { candyedit.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { collected += 1; collectedtextedit.settext("collected: " + collected); int candyx = random.nextint(screenwidth - 50); int candyy = random.nextint(screenheight - 50); system.out.println(candyx + " : " + candyy); marginlayoutparams marginparams = new marginlayoutparams(candyedit.getlayoutparams()); marginparams.setmargins(candyx, candyy, 0, 0); relativelayout.layoutparams layoutparams = new relativelayout.layoutparams(marginparams); candyedit.setlayoutparams(layoutparams); //if (candyx > screenwidth) { //screenwidth -= 50; //layoutparams.setmargins(candyx, candyy, candyx, layoutparams.wrap_content); //candyedit.setlayoutparams(layoutparams); //} //if (candyy > screenheight) { //screenheight -= 50; //layoutparams.setmargins(candyx, candyy, layoutparams.wrap_content, layoutparams.wrap_content); //candyedit.setlayoutparams(layoutparams); //} //while (running == true) { //candyy -= 1; //layoutparams.setmargins(candyx, candyy, candyx, candyy); //candyedit.setlayoutparams(layoutparams); //} } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
i hope using android studio gives beautiful way of accessing benefits of android api.
anyway, before post code, set onclicklistener within java code , while fine, can clutter code. more aesthetically pleasing solution use xml , button's android:onclick. see in code.
java:
package testinc.com.randombutton; import android.graphics.point; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.*; import android.widget.imagebutton; import android.widget.linearlayout; import android.widget.textview; import java.util.random; public class mainactivity extends actionbaractivity { // encapsulating data safe... private int collected = 0; private int screenwidth = 300; private int screenheight = 300; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); display display = getwindowmanager().getdefaultdisplay(); point size = new point(); display.getsize(size); screenwidth = size.x; screenheight = size.y; textview collectedview = (textview) findviewbyid(r.id.collectedtv); collectedview.settext("collected: " + collected); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } public void torandomposition(view view) { // based on our collection candy collection: collected += 1; textview collectedview = (textview) findviewbyid(r.id.collectedtv); collectedview.settext("collected: " + collected); // based on position of our candy: random random = new random(); // understand nextint(n) go 0 -> n-1, trying control can go? float candyx = (float) random.nextint(screenwidth - 50); float candyy = (float) random.nextint(screenheight - 50); // didn't write it, need check these float values if exceed screen width , screen length. */ // sout check coordinates system.out.println(candyx + " : " + candyy); // change margins: imagebutton imgbutton = (imagebutton) findviewbyid(r.id.changeplace); imgbutton.setx(candyx); imgbutton.sety(candyy); } }
android: note: used linear layout because dislike relative. can change without repercussions suppose.
<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: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"> <textview android:id="@+id/collectedtv" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <imagebutton android:id="@+id/changeplace" android:onclick="torandomposition" android:contentdescription="it's button, ha za" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout>
key parts:
android:onclick="torandomposition"
hooks method, public void torandomposition(view view)
setx
, sety
require floating point numbers set position. unfortunately, seems random class not have nextfloat(float n) in class. recommend casting int float now.
seeing doing now, here possible (i have not checked it) solution coordinates going beyond bounds of screen:
while ( candyx >= screenwidth || candyy >= screenheight) { candyx = (float) random.nextint(screenwidth - 50); candyy = (float) random.nextint(screenheight - 50); }
i checked if position equal because button @ corner of screen pain find , may skewed off display.
Comments
Post a Comment