0

The first image is what I want. The second is what I get

enter image description here

I've this class to create a dialog

import android.app.AlertDialog;
import android.app.Application;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.Toast;

public class AlertFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
                // Set Dialog Icon
                .setIcon(R.drawable.androidhappy)
                // Set Dialog Title
                .setTitle("Alert DialogFragment")
                // Set Dialog Message
                .setMessage("Alert DialogFragment Tutorial")

                // Positive button
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                })

                // Negative Button
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something else
                    }
                }).create();
    }
}

In my view

import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.ProgressDialog;

import android.content.DialogInterface;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

.......

final FragmentManager fm = getSupportFragmentManager();

.......

AlertFragment alertdFragment = new AlertFragment();
// Show Alert DialogFragment
alertdFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialogFragmentStyle);
alertdFragment.show(fm, "Alert Dialog Fragment");

This is my style file

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="android:spinnerItemStyle">
            @style/spinnerItemStyle
        </item>

        <!-- For each individual Spinner list item once clicked on -->
        <item name="android:spinnerDropDownItemStyle">
            @style/spinnerDropDownItemStyle
        </item>

    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <style name="spinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
        <item name="android:padding">0dp</item>
        <item name="android:textSize">@dimen/fld_txt_size</item>
    </style>

    <style name="spinnerDropDownItemStyle">
        <item name="android:padding">0dp</item>
        <item name="android:textSize">@dimen/fld_txt_size</item>
    </style>

    <style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
        <item name="android:textColor">@color/btn_text</item>
    </style>

    <style name="MyDialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="windowActionBar">true</item>
        <item name="windowNoTitle">false</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">false</item>
        <item name="android:headerDividersEnabled">true</item>
    </style>


</resources>

I've posted all the style to see if there's anything going wrong. Regardless that I would like the separator between the header and the body of the dialog I do not understand why the buttons do not appear.

Edit:

I read this

Missing buttons on AlertDialog | Android 7.0 (Nexus 5x)

and I realized that i need to define a style for my dialog.

Infact if i set this

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">true</item>
    <item name="windowNoTitle">false</item>
    <item name="android:buttonBarStyle">@style/Widget.AppCompat.ActionButton</item>
</style>

and then initialize dialog in this way

AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this,R.style.dialog_theme);

I can see change of color and text. But wich is property to show title, separator and buttons?

Community
  • 1
  • 1
ciro
  • 771
  • 1
  • 8
  • 30

1 Answers1

0

if you want a customized layout. You need to create an instance of Dialog. i.e. Dialog dialog = new Dialog; and set the contentview for that dialog: dialog.setContentView(R.layout.my_customized_dialog_layout); you can then create buttons inside that layout and initialize them.

HaroldSer
  • 2,025
  • 2
  • 12
  • 23
  • I followed this tutorial http://www.androidbegin.com/tutorial/android-dialogfragment-tutorial/ and He not use setContentView. – ciro Apr 26 '17 at 20:28
  • where is your alertdfragment? you followed example for the first(left image) not for the right image which is the result of alertdfragment class. – HaroldSer Apr 26 '17 at 20:31
  • My AlertFragment is his AlertDFragment class. Infact I can see icon. – ciro Apr 26 '17 at 20:37
  • where is your activity_main.xml file? – HaroldSer Apr 26 '17 at 20:52
  • and your mainactivity? you need the activity_main.xml file inflated in mainactivity to show those buttons. – HaroldSer Apr 26 '17 at 20:55
  • MainActivity.java contains only code to display dialog. And in xml there are only button to raise event that show two different dialog. – ciro Apr 27 '17 at 07:24