简体   繁体   中英

Conditional Popup message in Android app development

First of all I am very new to Android Studio and Android App Development. I want to show a conditional popup in my app where the popup comes only if the condition is not satisfied.

    Double billAmount = new Double(etBillAmount.getText().toString());
    Double TipPercentage = new Double(etTipPercentage.getText().toString());

    // The popup should be given when the Tip percentage is greater than 100 or else continue with 
       the code
    if (TipPercentage > 100)
    {
        System.out.println("Please enter Tip Percentage between 0 to 100. Thank you");
        System.exit(0);
    }
    Double totalBill;
    Double totalTip;

    totalTip = TipPercentage*billAmount/100;
    totalBill = totalTip + billAmount;

    tvTotal.setText("$ " + totalTip.toString());   
    tvTip.setText("$ " + totalBill.toString());

this is the code and I want to pop up a message when the condition in if statement is not satisfied. Kindly help me in figuring it out only in Linear Layout format if possible.

kindly help me with thanking you all in Advance :) :) :) !!!!!!!!!!!

For popup message You can use Toast

Toast.makeText(this, "Please enter Tip Percentage between 0 to 100. Thank you", Toast.LENGTH_SHORT).show();

so You need to add toast inside your if block instead of system.out.println

if (TipPercentage > 100){
Toast.makeText(this, "Please enter Tip Percentage between 0 to 100. Thank you", Toast.LENGTH_SHORT).show();
}

Or you can use Alert Dialogue Too.For Alert dialogue follow this link https://developer.android.com/guide/topics/ui/dialogs

the code
if (TipPercentage > 100){
System.out.println("Please enter Tip Percentage between 0 to 100. Thank you");
System.exit(0);}else{
dialogOpenTop(NotificationDialogActivity.this,"pass string");
}}
public void dialogOpenTop(final Context context, String toke) {
final Dialog dialog = new Dialog(NotificationDialogActivity.this);           dialog.setContentView(R.layout.alert);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialog.setCancelable(false);
            dialog.setTitle("Demo for bottom dialog");
            LinearLayout someLayout = dialog.findViewById(R.id.someLayout);
            Button btnOk = dialog.findViewById(R.id.btnOk);
}
layout
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollViewLuogo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:fitsSystemWindows="false"
    >
    <LinearLayout
        android:focusableInTouchMode="true"
        android:padding="@dimen/margin_10dp"
        android:id="@+id/someLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/dialog_background"
        android:orientation="vertical">
        <TextView
            android:padding="@dimen/margin_10dp"
            android:textStyle="bold"
            android:id="@+id/title_message"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Your message"

            android:layout_marginBottom="4dp"
            android:textSize="18sp" />

        <ScrollView android:id="@+id/scroll_view"
            android:layout_width="match_parent"
            android:fitsSystemWindows="true"
            android:fadeScrollbars="false"
            style="@style/scroll_style"
            android:layout_height="match_parent"
            tools:ignore="UselessParent">

            <LinearLayout android:id="@+id/scroll_layout"
                android:padding="@dimen/margin_10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >




                <ImageView
                    android:layout_below="@+id/title_message"
                    android:adjustViewBounds="true"
                    android:id="@+id/btnAlert"
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:visibility="visible" />


                <TextView

                    android:paddingTop="@dimen/margin_10dp"
                    android:paddingBottom="@dimen/margin_10dp"
                    android:layout_below="@+id/btnAlert"
                    android:layout_gravity="center"
                    android:id="@+id/text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="YOur message"
                    android:textSize="18sp" />

                <Button
                    android:id="@+id/btnOk"
                    android:minWidth="@dimen/margin_0dp"
                    android:minHeight="@dimen/margin_0dp"
                    android:textSize="@dimen/text_20sp"
                    android:paddingTop="2dp"
                    android:paddingBottom="2dp"
                    android:paddingLeft="@dimen/margin_height_50dp"
                    android:paddingRight="@dimen/margin_height_50dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
           android:layout_gravity="center"
                    android:layout_marginTop="2dp"
                    android:background="@drawable/dialog_button"
                    android:focusable="true"
                    android:focusedByDefault="true"
                    android:text="Ok" />

                <Button
                    android:visibility="gone"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Next actvity"
                    android:id="@+id/nxt"/>
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM