Question

I found all things working with alert box,dialog box but when i try creating things with my own custom dialog box it gives me problems. Though i followed the instructions as per the dev guide: http://developer.android.com/intl/de/guide/topics/ui/dialogs.html i could'nt reach with my results just it displays a force close with the following error message.

03-04 11:37:08.780: ERROR/AndroidRuntime(726): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

I have been trying to make my custom dialog box for many days but i couldnt bring it up. I even tried with the solutions that i got on forums but that too doesnt seems of working. Give me some piece of good code or some suggestion to work with... Any suggestions on this is appreciable.

Was it helpful?

Solution

Android Dialog - confused take a look on the question, looks similar to yours. Also you have to share a code which results in the error otherwise it is hard to help.

OTHER TIPS

create custom_dialog xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:id="@+id/root"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom Dialog"
        android:textColor="#000"
        android:textSize="25dp"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ok"
        android:textColor="#000"
        android:textSize="19dp"/>
        <Button
            android:id="@+id/cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel"
            android:textColor="#000"
            android:textSize="19dp"/>
    </LinearLayout>
</RelativeLayout>

Add custom dialog View in MainActivity.java

package techamongus.com.testapplication;

import android.app.Activity;
import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {
Dialog customDialog;
    Button ok,cancel;
    Button showDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showDialog=(Button)findViewById(R.id.show);
        showDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customDialog.show();
            }
        });
        customDialog=new Dialog(this);
        LayoutInflater customInflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View customLayout=customInflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.root));
        customDialog.setContentView(customLayout);
        ViewGroup.LayoutParams layoutParams2= customLayout.getLayoutParams();
        layoutParams2.height=400;
        ok=(Button)customLayout.findViewById(R.id.ok);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //done what do you want to do
                customDialog.dismiss();
            }
        });
        cancel=(Button)customLayout.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //done what do you want to do
                customDialog.dismiss();
            }
        });
    }

}

here is your main_activity.xml

<?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/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="techamongus.com.testapplication.MainActivity">
    <Button
        android:id="@+id/show"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:text="Show dialog"
        android:layout_gravity="center"
        android:textColor="#000"/>
</LinearLayout>

http://www.techamongus.com/2017/03/android-create-custom-dialog-program.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top