Reputation: 1154
I am trying to use new AppCompat 22.1
feature AppCompatDialog
, but it shows me dialog without title, but I am using method setTitle
. If I change the AppCompatDialog
to Dialog
everything works fine. It's 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();
Upvotes: 4
Views: 3838
Reputation: 1887
As @Eddwhis noted in the comments above the <item name="windowNoTitle">true</item>
in the Theme 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.
Base 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>
My Dialog style:
<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's the important bit -->
</style>
Upvotes: 6
Reputation: 27535
This code worked for me
Activity
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>
dialog layout xml
<?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>
Upvotes: 0