Android XML buttons -
i trying make calculator android. have started using xml make buttons , such , going reference them in java math. having trouble making them buttons instead of lines. understand weight sum, don't know if supposed using kind of thing. code following: (code has been updated)
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/total" android:textsize="45sp" android:id="@+id/calcdisplay" /> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <button android:id="@+id/number1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="1" /> <button android:id="@+id/number2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="2" /> <button android:id="@+id/number3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" /> </linearlayout> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <button android:id="@+id/number4" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" /> <button android:id="@+id/number5" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" /> <button android:id="@+id/number6" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" /> </linearlayout> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <button android:id="@+id/number7" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" /> <button android:id="@+id/number8" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" /> <button android:id="@+id/number9" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" /> </linearlayout> <button android:id="@+id/number0" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center_horizontal" /> </linearlayout>
try this:
<button android:id="@+id/number1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" />
you want replace fill_parent
match_parent
, unless targeting api 7 or lower. if api 8 or higher use match_parent
. also, want add android:text="my string here"
each button otherwise wrap_content
nothing more line since there nothing "wrap" around.
then in java can following in oncreate()
:
button number1 = (button) findviewbyid(r.id.number1); number1.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { //do action stuff!!! } });
i use lower case in xml, on occasion uppercase cause problems.
Comments
Post a Comment