java - How to deactivate a button in android -
i'm making simple tip calculator app in android , i've got of functionality working, i'm stuck on trying fix bug i've found. happens enter number edittext
, choose service rating spinner
. have 2 buttons, 1 says what's tip?
, 1 says what's total tip?
(each self-explanatory do). bug i've found if click either button edittext
being empty, crashes app. i've tried button.setclickable(false/true)
, button.isclickable()
, button.isenabled()
none of them have worked. maybe did use correct 1 didn't use correctly i've got no idea do. appreciated.
p.s. sorry xml code not 100% formatted it's there
java code:
public void calculatetip (view view) { string tip = et.gettext().tostring(); if(tip.isempty()) { btn_tip.setclickable(false); } else { btn_tip.setclickable(true); } double finaltip = double.parsedouble(tip); string onetipformat = string.format("%.2f", finaltip * 0.10); string fourtipformat = string.format("%.2f", finaltip * 0.13); string sixtipformat = string.format("%.2f", finaltip * 0.15); string eighttipformat = string.format("%.2f", finaltip * 0.20); string tentipformat = string.format("%.2f", finaltip * 0.25); textview.setvisibility(view.visible); string rate = string.valueof(spin.getselecteditem()); if(rate.equals("1 star") || rate.equals("2 stars") || rate.equals("3 stars")) { string text = "your rating of: " + rate + " means tip should be: $" + onetipformat; textview.settext(text); } if(rate.equals("4 stars") || rate.equals("5 stars")) { string text = "your rating: " + rate + " means tip should be: $" + fourtipformat; textview.settext(text); } if(rate.equals("6 stars") || rate.equals("7 stars")) { string text = "your rating: " + rate + " means tip should be: $" + sixtipformat; textview.settext(text); } if(rate.equals("8 stars") || rate.equals("9 stars")) { string text = "your rating: " + rate + " means tip should be: $" + eighttipformat; textview.settext(text); } if(rate.equals("10 stars")) { string text = "your rating: " + rate + " means tip should be: $" + tentipformat; textview.settext(text); } } public void calculatetotal(view v) { string value = et.gettext().tostring(); double finalvalue = double.parsedouble(value); string onepriceformat = string.format("%.2f", finalvalue + (finalvalue * 0.10)); string fourpriceformat = string.format("%.2f", finalvalue + (finalvalue * 0.13)); string sixpriceformat = string.format("%.2f", finalvalue + (finalvalue * 0.15)); string eightpriceformat = string.format("%.2f", finalvalue + (finalvalue * 0.20)); string tenpriceformat = string.format("%.2f", finalvalue + (finalvalue * 0.25)); tv.setvisibility(view.visible); string rating = string.valueof(spin.getselecteditem()); if(rating.equals("1 star") || rating.equals("2 stars") || rating.equals("3 stars")) { string text = "based on service rating of '" + rating + "', total tip should be: $" + onepriceformat; tv.settext(text); } if(rating.equals("4 stars") || rating.equals("5 stars")) { string text = "based on service rating of '" + rating + "', total tip should be: $" + fourpriceformat; tv.settext(text); } if(rating.equals("6 stars") || rating.equals("7 stars")) { string text = "based on service rating of '" + rating + "', total tip should be: $" + sixpriceformat; tv.settext(text); } if(rating.equals("8 stars") || rating.equals("9 stars")) { string text = "based on service rating of '" + rating + "', total tip should be: $" + eightpriceformat; tv.settext(text); } if(rating.equals("10 stars")) { string text = "based on service rating of '" + rating + "', total tip should be: $" + tenpriceformat; tv.settext(text); } }
my xml code:
<linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:animatelayoutchanges="true" android:gravity="center" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <textview android:id="@+id/tv_total_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/what_is_the_total_price" android:textsize="24sp" android:textcolor="@android:color/black"/> <edittext android:id="@+id/et_bill" android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/enter_your_bill_here" android:gravity="center_horizontal" android:inputtype="numberdecimal"/> <textview android:id="@+id/tv_service" android:layout_margin="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rate_your_service_using_the_drop_down_menu_below" android:textsize="24sp" android:gravity="center" android:textcolor="@android:color/black"/> <spinner android:id="@+id/spinner_rating" android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/rating" android:prompt="@string/service_prompt"> </spinner> <button android:id="@+id/btn_tip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/what_s_my_tip" android:textsize="18sp" android:layout_margin="25dp" android:onclick="calculatetip"/> <textview android:id="@+id/tv_tip" android:layout_marginbottom="20dp" android:textcolor="@android:color/black" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textsize="20sp" android:visibility="invisible"/> <button android:id="@+id/btn_total" android:textsize="18sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/what_s_my_total_with_tip" android:onclick="calculatetotal"/> <textview android:id="@+id/tv_price" android:layout_margin="10dp" android:textcolor="@android:color/black" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textsize="20sp" android:visibility="invisible"/> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/clear" android:textcolor="@android:color/black" android:textsize="20sp" android:gravity="center" android:onclick="clear"/> </linearlayout>
the problem have you're disabling click of button on click handler :) in order disable button need execute logic fail when et
empty.
if want connect et
value enabling/disabling click button, you'll have use textchangedlistener
detect current edittext value.
also, if understand correctly, you're button should start disabled since initial value et
not valid calculate tip.
another option instead of disabling button add if
condition @ beginning of calculatetip
. if et
empty, show message , return without executing other code
Comments
Post a Comment