Chirag Prajapati
Chirag Prajapati

Reputation: 347

How to display the dialog from ime service in android programatically?

Can anyone help me to display the custom dialog from the ime service. I have searched lot of things on the internet but nothing got usefull to popup the dialog from the ime service.

I'm using below code to display dialog :

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(context);
View view = layoutInflaterAndroid.inflate(R.layout.my_dialog, null);
builder.setView(view);
builder.setCancelable(false);
AlertDialog alertDialog = builder.create();
alertDialog.show();

But I'm getting below error :

2021-09-05 21:35:35.574 14533-14533/com.xyz W/System.err: android.view.InflateException: Binary XML file line #12: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f040003 a=-1} 2021-09-05 21:35:35.575 14533-14533/com.xyz W/System.err: Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f040003 a=-1}

W/System.err: at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:761) W/System.err: at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:7282) W/System.err: at android.view.ViewGroup$MarginLayoutParams.(ViewGroup.java:7463) W/System.err: at android.widget.RelativeLayout$LayoutParams.(RelativeLayout.java:1253) W/System.err: at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:1086) W/System.err: at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:1085) W/System.err: at android.view.LayoutInflater.rInflate(LayoutInflater.java:860) W/System.err: at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:518) W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:426) W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:377)

Please help me to show dialog from the ime service

Below is xml file for the layout of dialog :

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="5dp">

        <ImageView
            android:id="@+id/action"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:src="@drawable/ic_baseline_translate_24" />

        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:padding="5dp"
            android:text="@string/texts"
            android:textColor="@android:color/black"
            android:textSize="16sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/toolbar"
        android:orientation="vertical">

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/ten_dp"
                android:orientation="vertical">

                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/tv_2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="@dimen/ten_dp"
                        android:text=""
                        android:textColor="@android:color/black"/>
                </ScrollView>

                <Button
                    android:id="@+id/btn_cancel"
                    android:layout_width="wrap_content"
                    android:layout_height="45dp"
                    android:layout_gravity="end"
                    android:layout_marginTop="@dimen/ten_dp"
                    android:background="@android:color/transparent"
                    android:text="@string/close" />
            </LinearLayout>
        </androidx.cardview.widget.CardView>

    </LinearLayout>
</RelativeLayout>

Please let me know if there any solution for it.

Upvotes: 0

Views: 202

Answers (1)

Chirag Prajapati
Chirag Prajapati

Reputation: 347

I found the solution. It's very simple just use below code :

View dialogView = LayoutInflater.from(context).inflate(R.layout.my_dialog, new FrameLayout(context));

PopupWindow popup = new PopupWindow(context);
popup.setContentView(dialogView);
// If you need the PopupWindow to dismiss when when touched outside
popup.setBackgroundDrawable(new ColorDrawable());

popup.showAtLocation(mInputView, Gravity.CENTER, 0, 0);

Button btnCancel = dialogView.findViewById(R.id.cancel_button);
btnCancel.setOnClickListener(v -> popup.dismiss());

Thank you. Enjoy the code. :)

Upvotes: 1

Related Questions