How to programmatically get an Image Button's width and height in Android -
im trying programmatically integer variable of image button's width , height in android. want because size of image vary depending on device. how can this? i've tried.
width = image.getwidth(); height = image.getheight();
but returns zero?? bitmap deprecated in api 22+. getting size of actual image not work because distort fit needs. drawable.getintrinsicheight();
off table (yes know must declare drawable befor saying drawable.getintrinsicheight
idc). i'm confused. please help!
full code:
package com.example.e99900004533.candycollector; //imports android, , random 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.widget.edittext; import android.widget.imagebutton; import android.os.countdowntimer; import android.graphics.drawable.drawable; import java.util.random; public class mainactivity extends actionbaractivity { //original global variables. non-static. public edittext collectedtextedit; public edittext timertextedit; public imagebutton candyedit; public int screenwidth; public int screenheight; public int candyx; public int candyy; public int collected = 0; public long time; public boolean candybag = false; public string currentcandy = "candy"; public boolean running = true; public int candywidth; public int candyheight; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); collectedtextedit = (edittext) findviewbyid(r.id.collectedtext); timertextedit = (edittext) findviewbyid(r.id.timertext); candyedit = (imagebutton) findviewbyid(r.id.candy); candyedit.setbackgroundresource(r.drawable.candy); collectedtextedit.settext("collected: " + collected); timertextedit.settext("time: "); //sets timer 30 seconds. displays time. ends game when time runs out. new countdowntimer(30000, 1000) { public void ontick(long millisuntilfinished) { time = millisuntilfinished / 1000; timertextedit.settext("time: " + time); } public void onfinish() { //when game timer complete. timertextedit.settext("game over!!!"); collectedtextedit.settext("score: " + collected); timertextedit.setx(screenwidth / 2); //sets middle of screen. directly above score. timertextedit.sety(screenheight / 2); collectedtextedit.setx(screenwidth / 2); //sets middle of screen. directly below score. collectedtextedit.sety(screenheight / 3); running = false; candyedit.setvisibility(view.invisible); //makes candy image invisible. } }.start(); //gets screen width , height. sets 2 created variables. display display = getwindowmanager().getdefaultdisplay(); point size = new point(); display.getsize(size); screenwidth = size.x; screenheight = size.y; candywidth = candyedit.getmeasuredwidthandstate(); //problem here!! candyheight = candyedit.getmeasuredheightandstate(); //problem here!! addlisteneronbutton(); } //onclick imagebutton candy. public void addlisteneronbutton() { candyedit.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (running) { //adds collected , changes text. collected += 1; collectedtextedit.settext("collected: " + collected); //checks if candybag. resets. if (candybag) { candybag = false; if (currentcandy.equals("candy")) { candyedit.setbackgroundresource(r.drawable.candy); } if (currentcandy.equals("candytwo")) { candyedit.setbackgroundresource(r.drawable.candy2); } if (currentcandy.equals("candythree")) { candyedit.setbackgroundresource(r.drawable.candy3); } } //gets new candy images @ score. if (collected >= 15 && collected < 30) { candyedit.setbackgroundresource(r.drawable.candy2); currentcandy = "candytwo"; } if (collected >= 30) { candyedit.setbackgroundresource(r.drawable.candy3); currentcandy = "candythree"; } //sets candy x , y variables. random random = new random(); candyx = random.nextint(screenwidth - candywidth * 2); //minus height , width of image. candyy = random.nextint(screenheight - candyheight * 2); candyx += candywidth; candyy += candyheight; //plus keep on screen. //sets candybag if random = 1. 1 / x chance. int candyrandom = random.nextint(10); if (candyrandom == 1) { candyedit.setbackgroundresource(r.drawable.candybag); candybag = true; collected += 2; } //makes sure candy not off screen. replaces if so. while (candyx >= screenwidth - candywidth || candyy >= screenheight - candyheight) { if (candyx >= screenwidth || candyy >= screenheight) { candyx = random.nextint(screenwidth - candywidth * 2); candyy = random.nextint(screenheight - candyheight * 2); candyx += candywidth; candyy += candyheight; } } //sets candy x , y. system.out.println(candyx + " : " + candyy); system.out.println((screenwidth - candywidth * 2) + " : " + (screenheight - candyheight * 2)); system.out.println(candywidth + " : " + candyheight); candyedit.setx(candyx); candyedit.sety(candyy); } } }); } @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); } }
.
<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="@drawable/background"> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/collectedtext" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:inputtype="none" android:text="@string/collectedtext" android:textcolor="#ffffff" android:textsize="25sp" android:textstyle="bold" android:singleline="true" android:clickable="false" android:textisselectable="false" android:editable="false" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/timertext" android:layout_aligntop="@+id/collectedtext" android:layout_alignparentright="true" android:layout_alignparentend="true" android:inputtype="none" android:text="@string/collectedtext" android:textcolor="#ffffff" android:textsize="25sp" android:textstyle="bold" android:singleline="true" android:clickable="false" android:textisselectable="false" android:editable="false" /> <imagebutton android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/candy" android:background="@drawable/candy" android:contentdescription="@string/candyimg" android:clickable="true" /> </relativelayout>
it may be, calling these methods early. use imagebutton.post(new runnable..)
getwidth , getheight in overriden run()
method.
had same problem time ago. caused because of calling width , height methods cause them 0, because view isn't initialized / measured.
Comments
Post a Comment