SetTitle for AppCompatDialog not working

I am trying to use a new function AppCompat 22.1 AppCompatDialog, but it shows me a dialog without a title, but I am using a method setTitle. If I change AppCompatDialogto Dialog, everything will be fine. Is this a bug in AppCompatDialog?

Here is my dialog code:

final AppCompatDialog dialog = new AppCompatDialog(ctx);
dialog.setTitle(R.string.choose_mode);
dialog.setContentView(R.layout.dialog_with_list);

ListView listView = (ListView) dialog.findViewById(R.id.list_view);

List<ModeItemModel> modesList = new ArrayList<ModeItemModel>();
modesList.add(new ModeItemModel(GUI_MANUAL));
modesList.add(new ModeItemModel(GUI_GPS));
listView.setAdapter(new ModeItemsAdapter(ctx, R.layout.list_item_ico_with_text, modesList));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        //Do Action

        dialog.cancel();
    }
});
dialog.setCancelable(true);
dialog.show();
+4
source share
2 answers

As noted in the comments above, <item name="windowNoTitle">true</item>the topic also hides the title of the dialog. I fixed it by adding a custom AppCompatDialgStyle in which I set <item name="windowNoTitle">false</item>to false.

Main theme:

<style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar,">
   ...
   <item name="alertDialogTheme">@style/Theme.App.AppCompatDialogStyle</item>
   <item name="dialogTheme">@style/Theme.App.AppCompatDialogStyle</item>
</style>

NoActionBar Theme:

<style name="Theme.App.NoActionBar" parent="@style/Theme.App.Base">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

The style of my dialogue:

<style name="Theme.App.AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#f00</item>
    <item name="android:textColorPrimary">@color/black</item>
    <item name="android:background">@color/red</item>
    <item name="windowNoTitle">false</item> <!-- that the important bit -->
</style>
+6
source

This code worked for me

Activities

public class MyActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        final AppCompatDialog dialog = new AppCompatDialog(this);
        dialog.setTitle("Hello For Dialog");
        dialog.setContentView(R.layout.dialog_nm);

//        ListView listView = (ListView) dialog.findViewById(R.id.list_view);
//
//        List<ModeItemModel> modesList = new ArrayList<ModeItemModel>();
//        modesList.add(new ModeItemModel(GUI_MANUAL));
//        modesList.add(new ModeItemModel(GUI_GPS));
//        listView.setAdapter(new ModeItemsAdapter(ctx, R.layout.list_item_ico_with_text, modesList));
//        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//                //Do Action
//
//                dialog.cancel();
//            }
//        });
        dialog.setCancelable(true);
        dialog.show();
    }


}

activity_my.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".My">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

xml dialog location

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:gravity="center"
              android:background="@android:color/white"
              android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Your message"
        android:id="@+id/textView" android:layout_gravity="center_horizontal"/>
</LinearLayout>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

</resources>
0

Source: https://habr.com/ru/post/1584876/


All Articles