AlertDialog style when using setView() - android

The Default AlertDialog looks nice, and using setMessage() one can set its text - easy.
But when using setView(), the dialog replaces the content by the given view, which is fine.
However, I'd like to keep the default paddings/background/style. I searched for a proper style and I had a look at alert_dialog.xml, but didn't find anything helpful.
How can I apply the default style, without mimicking it?

There is an AlertDialog style, that you can set when you create your Builder.
Example:
AlertDialog.Builder builder;
if (title != null) {
builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle);
builder.setTitle(title);
}
else {
builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyleNoTitle);
}
In your styles.xml file:
<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:textColorPrimary">#color/black</item>
</style>
<style name="AlertDialogStyleNoTitle" parent="AlertDialogStyle">
<item name="android:windowNoTitle">true</item>
</style>
My styles are just an example of some things you can do - you can just have one dialog style if you want. As long as you inherit from one of the Dialog.Alert styles, you should be good to go.
You will still have to set the padding up yourself. I looked at the material design guidelines, and the margins are 24dp.

Use :
AlertDialog.Builder b = new AlertDialog.Builder(this);
AlertDialog alert = b.create();
alert.setView(view, 0, 0, 0, 0);

To fix the margins you can use wrap your Layout in a FrameLayout and add the margins in your Layout like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/dialog_margin_title"
android:layout_marginBottom="#dimen/dialog_margin"
android:layout_marginLeft="#dimen/dialog_margin"
android:layout_marginStart="#dimen/dialog_margin"
android:layout_marginRight="#dimen/dialog_margin"
android:layout_marginEnd="#dimen/dialog_margin"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please enter the email address of the person you would like to follow, this person will be notified." />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress" />
</LinearLayout>
</FrameLayout>

Basing on the source code of AppCompat
https://chromium.googlesource.com/android_tools/+/HEAD/sdk/extras/android/support/v7/appcompat/res/layout/abc_alert_dialog_material.xml
this is an example of layout for a Dialog custom view that looks good
<?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="wrap_content"
android:orientation="horizontal"
android:paddingBottom="#dimen/abc_dialog_padding_top_material"
android:paddingLeft="?attr/dialogPreferredPadding"
android:paddingRight="?attr/dialogPreferredPadding"
android:paddingTop="#dimen/abc_dialog_padding_top_material">
<Spinner
android:id="#+id/spinner1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
Note that it is not raccomanded to use private values, such as abc_dialog_padding_top_material, but I didn't find a better solution.

Related

Theming PreferenceFragmentCompat

I'm having some troubles setting a theme to PreferenceFragmentCompat trying to achieve this:
I have these styles set:
<style name="AppThemeBase" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- 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:windowLightStatusBar">true</item>
<item name="android:statusBarColor">#color/status_bar</item>
<item name="colorControlNormal">#color/colorPrimary</item>
<item name="preferenceTheme">#style/AppThemeBase.RecordingPreferencesTheme</item>
</style>
<style name="AppThemeBase.RecordingPreferencesTheme" parent="#style/PreferenceThemeOverlay">
<item name="android:textColorPrimary">#FD0000</item>
<item name="android:textColorSecondary">#1F6F2B</item>
</style>
With this options I can see what I want to achieve in Android Studio layout preview but when it does not have effect when I execute it
The result when I execute:
The only thing it worked so far is using <item name="android:textColor">#FD0000</item> that will change the tittle colour but I can't find a way to change the summary colour.
Another question I have is if there is a way to set the theme just for that preference fragment instead of using it in all preferences fragments. Using android:theme at the root of the fragment layout isn't working for me :(
Update:
As per #Susan question this is how I try to use android:theme at the root of the fragment layout I've created this theme based in the main one
<style name="RecordingPreferenceTheme" parent="AppThemeBase">
<item name="preferenceTheme">#style/AppThemeBase.RecordingPreferencesTheme</item>
</style>
And this is the preference layout where I have a fragment which will inflate the PreferenceFragmentCompat
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="#style/RecordingPreferenceTheme">
<include
android:id="#+id/include"
layout="#layout/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="#+id/fragment_container_settings"
android:name="com.example.app.RecordSettingsFragment$RecordPreferences"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/include" />
</androidx.constraintlayout.widget.ConstraintLayout>
And at the root of the layout I'll set android:theme="#style/RecordingPreferenceTheme" but it doesn't work, just changes in the main theme set in Activity will have some effect. I've already tried customize PreferenceFragmentCompat layout with same results.
Cheers!!
See this sample project here created for your specific case the the only problem for this solution is you have to create your preference from code cant use xml file to inflate and
for those who just want the gist of this solution is that use setLayoutResource() method to set layout and use custom layout i just copied the original layout and hardcoded colors according to #Bhuntupana need
val messagesCategory = PreferenceCategory(context).apply {
layoutResource = R.layout.sample_preference
key = "messages_category"
title = getString(R.string.messages_header)
screen.addPreference(this)
}
Update
use layout tag to specify layout resource to use like this
<EditTextPreference
android:layout="#layout/sample_preference"
app:key="signature"
app:title="#string/signature_title"
app:useSimpleSummaryProvider="true" />
sample_preference.xml just copy of default layout preference_material.xml
<?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="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:baselineAligned="false"
android:clipToPadding="false"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingRight="?android:attr/listPreferredItemPaddingRight">
<include layout="#layout/image_frame" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<TextView
android:id="#android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textColor="#FD0000" />
<TextView
android:id="#android:id/summary"
style="#style/PreferenceSummaryTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:layout_alignStart="#android:id/title"
android:layout_alignLeft="#android:id/title"
android:layout_gravity="start"
android:maxLines="10"
android:textAlignment="viewStart"
android:textColor="#1F6F2B" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout
android:id="#android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="end|center_vertical"
android:orientation="vertical"
android:paddingStart="16dp"
android:paddingLeft="16dp"
android:paddingEnd="0dp"
android:paddingRight="0dp" />
</LinearLayout>

Autocompletetextview dropdown behind soft keyboard in dialog fragment

I have a dialog fragment in my app with an autocompletetextview in it, but the drop-down list instead of align with the soft keyboard's top, is placed behind, not giving access to some of the items.
Here is the layout:
<?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="wrap_content"
android:gravity="center"
android:padding="10dp"
android:orientation="horizontal"
android:windowSoftInputMode="adjustPan|adjustResize">
<android.widget.AutoCompleteTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/customDialogAtocompleteTextview"
android:layout_weight=".7"
android:layout_gravity="top" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".3">
<Button
android:id="#+id/customDialogBtOk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Aceptar"/>
<Button
android:id="#+id/customDialogBtSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Buscar"/>
<Button
android:id="#+id/customDialogBtMore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mas"/>
</LinearLayout>
</LinearLayout>
So how can I make it to align with the keyboard?
Theory says that android:windowSoftInputMode="adjustPan|adjustResize"should do this but for some reason it doesn't, so you have to do the same programmatically:
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Aand the magic happens!!!
[update]
in my case.. cause i use bottomShetDialogFragment with autocomplete inside. drop down not show cause i not set dropdownanchor for autocomplete.
just add android:dropDownAnchor="#id/layout_above_this_autocomplete" and work perfect
ad news style in your style.xml
<style name="AppBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">#style/AppModalStyle</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowSoftInputMode">adjustResize</item>
</style>
<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="behavior_peekHeight">400dp</item>
</style>
in your bottomshetdialog framgnet.
oncreate -> setStyle(DialogFragment.STYLE_NORMAL, R.style.AppBottomSheetDialogTheme)
override fun getTheme(): Int {
return R.style.AppBottomSheetDialogTheme
}

Android - how do I change the color of the spinner itself?

All of my searches has led me to guides on how to change the text color of the spinner choices, but how do I change the color of the text and arrow on the spinner itself?
Here's a snip of my spinner. It's supposed to say "Mercury".
Here's my attempt at the style. It did change the arrow to white, but not the text. I only applied it to the spinner since it will mess up the rest of the activity.
<style name="Theme.LightText2" parent="AppTheme">
<item name="android:editTextColor">#ffffff</item>
<item name="android:textColorPrimary">#ffffff</item>
<item name="android:textColorSecondary">#ffffff</item>
<item name="android:textColorTertiary">#ffffff</item>
<item name="android:textColorPrimaryInverse">#ffffff</item>
<item name="android:textColorSecondaryInverse">#ffffff</item>
<item name="android:textColorTertiaryInverse">#ffffff</item>
</style>
UPDATE: I tried to apply it to the activity itself via the manifest and it worked, but it's messed up the rest of my activity.
If you want to achieve this do one thing , Take one textview and set arrow icon on right(Drawableright) customize your text text view as you wont and on click open dialog with single selection and on item selected set text to textview
Try like this and use spnrSelect.performClick(); on click of rlSpinner to open spinner
<RelativeLayout
android:id="#+id/rlSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/scale_20dp"
android:layout_marginRight="#dimen/scale_20dp"
android:layout_marginTop="#dimen/scale_1dp"
android:background="#null"
android:clickable="true">
<Spinner
android:id="#+id/spnrSelect"
android:layout_width="match_parent"
android:layout_height="0dp"
android:clickable="true"
android:dropDownWidth="#dimen/scale_300dp"
android:scrollbars="none"
android:spinnerMode="dropdown" />
<TextView
android:id="#+id/txtSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingBottom="#dimen/scale_1dp"
android:paddingRight="#dimen/scale_1dp"
android:paddingTop="#dimen/scale_1dp"
android:text="#string/txtSelect"
android:textColor="#color/edt_hint"
android:textColorHint="#color/edt_hint"
android:textSize="#dimen/txt_12" />
<ImageView
android:id="#+id/spnrDownArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/scale_10dp"
android:background="#drawable/arrow_icon"
android:rotation="90" />
</RelativeLayout>
Create array adapter which will inflate TextView from Layout_resource
then set your color to inflated TextView
your_spinnersp.setAdapter(new ArrayAdapter<>(context,R.layout.txt_spinner,lst_data));
in layout add TextView
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="8dp" android:textSize="16sp"
android:background="#ff0000"
android :textColor="#ffff00"
android:layout_width="match_parent" android:layout_height="wrap_content"/>

How to change text size of dialog preference message?

I have a class that extends EditTextPreference and I use it in my own layout, such as:
<com.app.MyEditTextPreference
android:key="#string/key"
android:title="#string/title"
android:summary="#string/summary"
android:dialogTitle="#string/title"
android:dialogMessage="#string/message"
android:layout="#layout/preference_edittext"/>
My app theme inherits from Theme.AppCompat.Light.DarkActionBar and I have changed the values of the text sizes:
<style name="TextAppearance.Small">
<item name="android:textSize">18sp</item> <!-- was 14sp -->
</style>
However, when the preference dialog is displayed, the size of the message is different than the one I have defined in my style.
So how do I set the text size of the dialog message correctly?
EDIT
I copied the dialog layout from the sdk:
<LinearLayout
android:id="#+android:id/edittext_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:id="#android:id/message"
android:layout_marginBottom="48dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:textColor="?android:attr/textColorSecondary"
style="#style/TextAppearance.Small" />
</LinearLayout>
However, I get a build error Resource is not public on the line android:id="#+android:id/edittext_container".
And if I change it to "#*android:id/edittext_container", the edit text is not visible anymore.
Is there a simpler way to just change the textsize of the message?
First create new xml file name cus.xml in layout folder.
it need for set the view for dialog at runtime .
<?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">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Name :"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="18dp" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:textSize="18dp" />
As you need dialog:
here's dialog code:
Dialog d=new Dialog(getActivity());
d.setTitle("Image Title");
d.setContentView(R.layout.cus);
d.show();
I finally managed to change the message text size by:
a) Changing the id of the LinearLayout to: #+id/edittext_container, and
b) Adding the following code in my class extending EditTextPreference:
#Override
protected void onAddEditTextToDialogView(View dialogView, EditText editText) {
ViewGroup container = (ViewGroup) dialogView
.findViewById(R.id.edittext_container);
if (container != null) {
container.addView(editText, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
This works fine and is clearly a valid answer, however a simpler solution would be to just change the message text size through themeing.

Android Change Spinner theme/style with bottom border

I have a spinner after i loaded data into it. the style is not as my expected.
Spinner code
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinnerGender"
android:spinnerMode="dropdown"
android:layout_marginLeft="-5dp"
android:layout_marginTop="5dp"/>
Current style.
My expected
How can i change theme/style the spinner
try this,
<Spinner
android:id="#+id/spinnerGender"
style="#android:style/Widget.Holo.Light.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="-5dp"
android:layout_marginTop="5dp"
android:spinnerMode="dropdown" />
otherwise you can create custome style and use
Example at following link:
http://androidopentutorials.com/android-how-to-get-holo-spinner-theme-in-android-2-x/
Another approach is using the styles.xml file
<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:spinnerStyle">#android:style/Widget.Holo.Light.Spinner</item>
</style>

Categories

Resources