I am creating a custom dialog and I want to know how to change the background of the title bar.
I've tried two approaches:
1 - I've tried the AlertDialog.Builder method 'setCustomTitle'. I created a simple layout view comprising of a textview with layout width and height 'match_parent' and background color. When I run the app, only the top half of the title bar is showing the background color. The bottom half is still showing the default theme background color. Does anyone know why?
2 - I've created my own dialog theme. Ive created a style with parent inheritance to '#android:style/Theme.Holo.Light.Dialog'. I've then passed that in the AlertDialog.Builder constructor - new AlertDialog.Builder(this, R.style.test_dialog). It seems good but somehow the dialog is wrapped within a dialog. A square box is surrounding the dialog.
Does anyone know why?
You can create a style like,
<style name="cust_dialog" parent="#android:style/Theme.Dialog">
<item name="android:windowTitleStyle">#style/dialog_title_style</item>
</style>
<style name="dialog_title_style" parent="android:Widget.TextView">
<item name="android:background">#android:color/black</item>
<item name="android:padding">10dp</item>
</style>
And you can instantiate dialog:
Dialog dialog=new Dialog(this,R.style.cust_dialog);
dialog.setContentView(R.layout.fragment_features_dialog);
dialog.setTitle(R.string.features);
Now the dialog shows up with black title background color.
The dialog-wrapped-within-a-dialog appearance is caused by the dialog's window background. Every dialog has this, but the default Android dialogs have the window background set to transparent. To do this, add this item in your custom dialog theme:
<style name="CustomDialog" parent="#android:style/Theme.Holo.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
</style>
I am using Mono Android(Xamarin); am showing you another alternative that am using in my app to create a dialog in a fragment:
Dialog itemDialog = new Dialog(this.Activity);
TextView alertTitle=(TextView)itemDialog.Window.DecorView.FindViewById(Android.Resource.Id.Title);
alertTitle.SetTextColor(Android.Graphics.Color.Blue);
alertTitle.SetBackgroundColor(Android.Graphics.Color.Orange);
itemDialog.SetContentView(Resource.Layout.listview_custom_dialog);
string[] options = new string[] { "Open", "Mark as Unread","Mute","View
Profile","Block Connection","Delete Conversation" };
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this.Activity,
Resource.Layout.listitem_custom_dialog,Resource.Id.textViewDialogDescription,
options);
Resource.Layout.listitem_custom_dialog: this is custom listview layout, here is the xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" >
<TextView
android:id="#+id/textViewDialogDescription"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="#ffffff"
android:textColor="#386B96"
android:paddingLeft="4dp"
android:textSize="14dp" />
</RelativeLayout>
ListView lv = itemDialog.FindViewById<ListView>
(Resource.Id.listViewDialogItems);
lv.Adapter = adapter;
adapter.NotifyDataSetChanged();
itemDialog.SetCancelable(true);
itemDialog.SetTitle("Conversation");
itemDialog.Show();
Android.Resource.Id.Title: this is the id of the textview containing the dialog title. and it is predefined by android.
this way you will get a dialog that you can style in way you want.
Create an xml layout file for your title and inflate it and set to to the AlertDialog as
View view = getLayoutInflater().inflate(R.layout.cust_dialog_title, null);
alertDialog.setCustomTitle(view);
You can just set custom title like this
LayoutInflater inflater = this.getLayoutInflater();
View titleView = inflater.inflate(R.layout.custom_title, null);
new AlertDialog.Builder(SubCategoryActivity.this)
.setCustomTitle(titleView);
and in custom_title layout you can create custom title like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:id="#+id/llsubhead"
android:background="#color/colorPrimary">
<TextView
android:id="#+id/exemptionSubHeading4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:text="Exemption Sub Head"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>
Related
I am implementing a popup menu for my android application. I want to be able to customize the items of the menu and change the default values (for the item padding, margin, padding between item icons etch.). Currently i am able to change only the text color and size of each item with the properties:
<item name="android:textColor">#color/white</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
which are set within :
<style name="PopupMenu" parent="#android:style/Widget.PopupMenu">
How is this possible, which attributes i need to set in the styling of the menu?
Hello, You can create customized layout specifically for the popup and you can give each items different margin padding..
Check out below code.
private fun createPopUp() {
val popUpView: View = layoutInflater.inflate(R.layout.popup, null)
val mpopup = PopupWindow(
popUpView, ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT, true
)
popUpView.close.setOnClickListener{
mpopup.dismiss()
}
}
Here is the code for the popup layout you can create a separate XML for that and add layout and items into that with each attributes available within.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical"
android:padding="#dimen/_16sdp">
<Button
android:id="#+id/close"
android:layout_width="160dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:text="#string/date"
android:textColor="#color/white" />
</RelativeLayout>
I'm trying to change the background color of an Android AlertDialog.
Every post I've seen is about changing the color of the AlertDialog, but not the background behind.
Any help would be appreciated.
<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">#color/myColor</item>
and then apply it in the Builder constructor:
AlertDialog alertDialog = new AlertDialog.Builder(getContext(), R.style.MyDialogTheme)
...
.create();
for more info see see this post
you can reduce the dim Amount
dialog.getWindow().setDimAmount(0.5f);
To get a background color you need to make a custom Alert Dialog.
There is one way to do what you asked for but its not that much of a good solution
First, set custom dialog theme like this.
styles.xml
<style name="CustomDialogTheme" parent="android:Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
CustomDialog.java
Apply the theme and construct your custom_dialog view as follows.
public HTCustomDialog(Context context) {
super(context, R.style.CustomDialogTheme);
setContentView(R.layout.custom_dialog);
}
custom_dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/main_solid_80">
<RelativeLayout
android:id="#+id/dialog_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="#drawable/bg_popup"
android:padding="16dp">
</RelativeLayout>
Now that CustomDialog view is a full-screen view, set background of your root layout to whatever color you'd like.
I got this answer from here take a look .
I have a custom AlertDialog style that makes the AlertDialog box transparent. It works fine except that when I inflate my desired transparent layout into the alert dialog window, it shows up with a black background. My goal is to have a completely transparent AlertDialog to where it seems as if there are only 4 buttons floating rather than a frame.
Image one is what the custom dialog is giving me, image two is my desired or what I am aiming for.
Here is the code to the custom Dialog
<style name="CustomDialog" parent="android:Theme.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:background">#android:color/transparent</item>
</style>
Here is what I am calling in my onCreate()
AlertDialog.Builder imageDialog = new AlertDialog.Builder(TimeLine.this, R.style.CustomDialog);
inflater = getLayoutInflater();
View view=inflater.inflate(R.layout.tabs, null);
AlertDialog a = imageDialog.create();
a.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
a.setView(view, 0, 0, 0, 0);
a.show();
Edit*
The code for the tabs layout xml is here
`
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/tabLayout"
android:background="#000000ff">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2"
android:layout_marginTop="206dp"
android:layout_below="#+id/button4"
android:layout_alignStart="#+id/button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3"
android:layout_alignTop="#+id/button2"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button4"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="100dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="100dp" />
</RelativeLayout>
`
From testing to see what the source of the problem is, I have found out that it is true that the layout is transparent because when I change the background color of the layout, so does the alert dialog change. However when the layout is set to transparent, it seems that what is behind the inflated layout is black. And because of that I am unsure of what to do or whether it is the AlertDialog settings or my layout code.
The problem is that AlertDialog builder is actually not good for designing transparent dialog and will and always have this black background which is actually a Theme for it, instead use the Dialog to create a transparent theme instead.
sample:
Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.tabs);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
Using Dialog does not require any theme manipulation for transparent background so it is basically easy.
Have you tried using something like this:
<style name="CustomDialog" parent="#android:style/Theme.Translucent">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
...
</style>
EDIT
Try this in java code
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Those who are having still problem creating custom transparent dialog or alert dialog, can use this combination. I also wanted to show custom background this is what worked for me.
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
example:-
/**
* alert dialog
*/
public class ToolTipView extends AlertDialog {
public ToolTipView(#NonNull Context context) {
super(context);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_tool_tip_view);
}
}
In your R.layout.tabs,
Replace
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/tabLayout"
android:background="#000000ff">
with
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/tabLayout"
android:background="#android:color/transparent">
I want to customize all dialogs, alertDialogs and spinner. I want to change the color of text and the background of the title bar.
I try to explain better:
I want the text (JOLLY BAR DI ...) to be white and the background of the title (from the top of the dialog to the soft-blue line below the title included) to be blue (#044592).
How can I do it?
Ok, I've almost solved the problem (I'm answering my own question because it may help others with the same problem as mine).
The problem remains for spinners, DatePickers etc.
For the AlertDialogs I do something like this:
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this, AlertDialog.THEME_HOLO_LIGHT);
//Then I do what I need with the builder (except for setTitle();
LayoutInflater inflater = getLayoutInflater();
View view=inflater.inflate(R.layout.dialog_custom_title, null);
TextView title = (TextView)view.findViewById(R.id.myTitle);
title.setText("Title I want to show");
builder.setCustomTitle(view);
AlertDialog alert = builder.create();
alert.show();
dialog_custom_titile.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:background="#044592" >
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTitle"
android:layout_width="fill_parent"
android:textSize="20dp"
android:layout_height="70dp"
android:layout_marginLeft="25dp"
android:paddingTop="22dp"
android:textColor="#android:color/white"
android:textStyle="bold" />
</RelativeLayout>
for the Activities that I want to look like Dialogs I do like this:
I declare them in the Manifest.xml as follows:
<activity
android:name="com.aveschini.AnotherActivity"
android:screenOrientation="portrait"
android:label="My Dialog"
android:theme="#style/MyDialog"
android:windowSoftInputMode="adjustPan" />
then, in the res/values/styles.xml file I do like this:
<style name="AppTheme" parent="android:Theme.Holo.Light" />
<style name="MyDialog" parent="android:Theme.Holo.Light.Dialog">
<item name="android:windowTitleStyle">#style/DialogWindowTitle</item>
</style>
<style name="DialogWindowTitle">
<item name="android:textAppearance">#style/TextAppearance.DialogWindowTitle</item>
<item name="android:background">#044592</item>
</style>
<style name="TextAppearance.DialogWindowTitle" parent="android:TextAppearance">
<item name="android:textColor">#android:color/white</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">20sp</item>
</style>
I still get the light blue divider between the title and the body... still working on it.
As already said, I still don't know how to deal with Spinners, DatePicker, etc...
That's the result:
AlertDialog:
And an Activity with the look of a dialog:
I have a listview set to use singleChoice. All I want to do is change the default background color to white and the text color to black. I cannot figure out how to do this. Here is my xml layout:
<ListView
android:id="#+id/lvSpeeds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/llToolbar"
android:layout_below="#id/rgSpeedUnits"
android:textColor="#android:color/black"
android:choiceMode="singleChoice"
android:background="#ffffff"
android:cacheColorHint="#00ffffff"
android:clickable="true"
android:divider="#ff000000"
android:dividerHeight="1dp"
android:focusable="true"
android:scrollingCache="true" />
EDIT: I should have pointed out that I want to change this only using xml layout files and NOT in code. I already know how to do this in code. Using a custom layout other than android.R.layout.simple_list_item_single_choice forces you to implement an adapter, bind, write more code, and so on. From viewing a lot more posts, it does not appear possible to change the text color using only xml. In fact, it doesn't seem possible to change anything on a row as the underlying layout android.R.layout.simple_list_item_single_choice is not accessible.
for a list view Use android selector
like this
and save it with anyname.xml and giveits reference to background of your list
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/about_btn_hover"></item>
<item android:drawable="#drawable/about_btn"></item>
</selector>
and for changing text color add color folder in your res directory
create an xml save it textchangecolor.xml
and add the following lines to it
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/whiteColor"></item>
<item android:color="#color/bluetxt"></item>
</selector>
and give its refernce to the textcolor
try to this:
insert to this code in adapter in getview mathod:
LinearLayout mRowLayout = (LinearLayout) vi
.findViewById(R.id.list_item_layout);
final TextView title = (TextView) vi
.findViewById(R.id.list_item);
title.setText(Massage[position]);
mRowLayout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
title.setTextColor(Color.RED);
});
here list_item code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list_item_layout"
android:orientation="vertical" >
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/row_single"
android:layout_margin="5dp">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#android:color/black"
android:padding="10dp"
android:textSize="16sp"
android:layout_toLeftOf="#+id/arrow"
android:id="#+id/list_item"/>
</RelativeLayout>
</LinearLayout>
The best solution I could find was to use styles.xml and set a theme for my dialogs.
styles.xml
<style name="DialogTheme" parent="AppTheme">
<item name="android:textColor">#color/text</item>
<item name="android:textColorAlertDialogListItem">#color/text</item>
+ other styles
</style>
In Java Built Dialogs:
ContextThemeWrapper theme;
theme = ContextThemeWrapper(view.getContext(), R.style.DialogTheme);
AlertDialog.Builder builder = new AlertDialog.Builder(theme);
In XML built Dialogs:
<myLayout
....
android:theme="#style/DialogTheme" />