Issue
Hello am having a linear layout which is the parent layout and which which is holding an Edit text and a decrementing button before the edit text and and incrementing button after the edit text. The idea is when the decrementing button is clicked the number value in the edit text reduces by one and when the incrementing button is clicked the number value in the edit text increases by one. What i would love is to set the maximum number and minimum number of the edit text such that when that maximum is reached it displays a toast showing that the maximum number has been reached and when the minimum is also reached a toast is displayed showing the that the minimum number.
Here is my XML layout holding the buttons and the edit text
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="2dp">
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_decrement"
style="@style/Widget.App.Button.OutlinedButton.IconOnly"
android:layout_width="25dp"
android:layout_height="25dp"
android:theme="@style/ButtonsTheme"
app:icon="@drawable/ic_remove_icon" />
<EditText
android:id="@+id/item_quantity_edit_text"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_gravity="end"
android:background="@drawable/edit_text_background"
android:gravity="center"
android:inputType="number"
android:text="1" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_increment"
style="@style/Widget.App.Button.OutlinedButton.IconOnly"
android:layout_width="25dp"
android:layout_height="25dp"
android:theme="@style/ButtonsTheme"
app:icon="@drawable/ic_add_icon" />
</LinearLayout>
Setting on click listeneres to the buttons
btnDecrement.setOnClickListener(this);
btnIncrement.setOnClickListener(this);
Switching cases for when the button is clicked
case R.id.btn_decrement:
decreaseQuantity();
break;
case R.id.btn_increment:
increaseQuantity();
break;
Methods for incrementing and decrementing the values in the edit text
public void increaseQuantity() {
display(Integer.parseInt(etQuantity.getText().toString()) + 1);
}
public void decreaseQuantity() {
display(Integer.parseInt(etQuantity.getText().toString()) - 1);
}
private void display(int number) {
etQuantity.setText(String.valueOf(number));
}
etQuantity is the Edit text
Solution
simply add check logic in click listener.
case R.id.btn_decrement:
if (Integer.parseInt(etQuantity.getText().toString()) + 1 < maximum) {
decreaseQuantity();
} else {
// make toast.
}
break;
case R.id.btn_increment:
if (Integer.parseInt(etQuantity.getText().toString()) - 1 > minimum) {
increaseQuantity();
} else {
// make toast.
}
break;
Answered By - S T
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.