Style AlertDialog Submit Button and Title - java

I am styling my AlertDialog. I have successfully been able to style the background (see its blue) and the single choice items (see they are white with orange text).
Is it possible to style the AlertDialog title background, the footer submit button and footer background? See the image below for these areas highlighted in red. Is it possible to make the AlertDialog to have rounded rectangle corners?
As you can see I can set the title label background colour to blue but not the whole title background?
Implementation:
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(mActivity, R.style.AlertDialogCustom));
builder.setTitle("Select");
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
getActivity(), R.layout.choices, choices);
builder.setSingleChoiceItems(adapter, -1, myOnClickListener);
// Styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AlertDialogCustom" parent="#android:style/Theme.Dialog">
<item name="android:textColor">#00FF00</item>
<item name="android:textSize">10sp</item>
<item name="android:background">#0000ff</item>
<item name="android:fontFamily">fonts/myCustomFont.ttf</item>
</style>
</resources>
// choices.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:fontFamily="fonts/myCustomFont.ttf"
android:textColor="#color/theme_orange"
android:gravity="center_vertical"
android:ellipsize="marquee"
android:text="Text"
android:background="#fff"
android:paddingLeft="16dip"
android:paddingRight="7dip"/>

You can use a custom layout (xml file) instead of this and add all your requirements there.
Then set that view to your dialog as:
builder.setView(R.layout.custom_view);
Also don't forget to remove the title as mentioned here.

In this link I showed how to Style AlertDialog Submit Button and Title. It also shows how to customize the AletDialog. For example, how to change divider colro and etc. Please visit this link:
https://stackoverflow.com/a/33439849/5475941.
I hope it helps.

Related

how to change search icon, hint and text color in Android SearchView?

So I was working with this SearchView and everything looked fine in the emulator.
when I tested it in the actual device, all of the icons turned white.
I looked up on some of the sources on how to style the SearchView but most of them are maybe outdated and nothing seems to work.
Current SearchView XML:
<SearchView
android:id="#+id/lead_search_view"
style="#style/AppTheme.SearchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#drawable/custom_background_spinner"
android:iconifiedByDefault="false"
android:queryBackground="#android:color/transparent"
android:queryHint="Enter a keyword"
android:theme="#style/AppTheme.SearchView" />
The style that I followed on some solutions:
<style name="AppTheme.SearchView" parent="Widget.AppCompat.SearchView">
<item name="android:textColor">#color/colorPrimary</item>
<item name="android:textColorHint">#color/colorHint</item>
<item name="searchIcon">#drawable/ic_search_field_icon</item>
</style>
Use this way to change the search icon:
private SearchView searchbox;
private void customizeSearchbox() {
setSearchHintIcon(R.drawable.new_search_icon);
}
private void setSearchHintIcon(int resourceId) {
ImageView searchHintIcon = (ImageView) findViewById(searchbox,
"android:id/search_mag_icon");
searchHintIcon.setImageResource(resourceId);
}
private View findViewById(View v, String id) {
return v.findViewById(v.getContext().getResources().
getIdentifier(id, null, null));
}
as stated here: https://stackoverflow.com/a/16988719/2462531
To change text and hint color try this:
Add this to the parent theme.
<item name="android:editTextColor">#android:color/white</item>
<item name="android:textColorHint">#android:color/white</item>
It will change the hint text for the SearchView.
as stated here: https://stackoverflow.com/a/39036236/2462531
You can change and customize all SearchView icons like this:
<androidx.appcompat.widget.SearchView
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:iconifiedByDefault="false"
app:queryHint="#string/search"
app:searchHintIcon="#drawable/ic_search_gray"
app:searchIcon="#drawable/ic_search_white"
app:closeIcon="#drowable/x_icon"/>
these three lines make magic
app:searchHintIcon="#drawable/ic_search_gray"
app:searchIcon="#drawable/ic_search_white"
app:closeIcon="#drowable/x_icon"/>
but first, you must provide declaration in root layout in your xml with this 2 lines of code :
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
I hope this will helps someone, since it's a late reply...
enjoy and happy coding :)

Is there a way to change the color of an icon inside preference headers?

I've added some icons (as SVGs) to my preference header file and I want to change the color of them with Java (my app is themeable and I can't find any other way to change the icon color according to the theme).
I've already tried changing the color of the icon in a similar way to buttons, etc... I can't change the color with the "app:tint" attribute either and it dosen't change with the theme no matter what I do.
Here is the preference header code. I want to change the color of "ic_round_settings".
<header
android:fragment="com.appname.settings.fragment.GeneralSettingsFragment"
android:icon="#drawable/ic_round_settings"
android:title="#string/settings_general"
android:summary="#string/settings_general_explain" />
To achieve that behaviour use the reference to the color in the XML like so
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?attr/colorAccent"
android:tint="?attr/colorAccent"
android:text="#string/chat_send_text"
android:drawableTint="?attr/colorAccent"
android:drawableRight="#drawable/ic_paper_plane"/>
</FrameLayout>
Also, when working with the activities **make sure you set the theme before using setContentView(R.layout_your_layout_file) ** or else you'll have to call recreate() when you set the theme dynamically.
Example
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(whatever_theme_you_want_to_use)
setContentView(R.layout.activity_cool)
// Further view initialization
}
The drawback is that you would have to explicitly setTheme in all of your activities since Android doesn't give developers an easier way to change the theme app-wide.
OK, I've found one way of doing it. If you add an attr attribute to the preference header like this:
<header
android:fragment="com.appname.settings.fragment.GeneralSettingsFragment"
android:icon="?attr/ic_round_settings"
android:title="#string/settings_general"
android:summary="#string/settings_general_explain" />
and add that attribute to the attr.xml file in the values folder:
<attr name="ic_round_settings" format="reference"/>
And add that into the theme classes in styles.xml with a light and dark version of the icon, the theme will change:
<style name="Theme.BaseLightTheme" parent="Theme.AppCompat">
<item name="ic_round_settings">#drawable/ic_round_settings_dark</item>
</style>
<style name="Theme.BaseDarkTheme" parent="Theme.AppCompat">
<item name="ic_round_settings">#drawable/ic_round_settings_light</item>
</style>
With this in the SVG icon file, changing the color from #000000 to #ffffff in the light SVG icon copy:
<path
android:fillColor="#000000"
android:pathData=""/>
Edit: This won't work on Android 4.4 and below - the icons won't appear at all

Android: change background color showing dialog

How do we change the background's foreground color when we show a DialogFragment, like when we click on the fab in the evernote or messenger app, I know the way to do it will probably be diffrent but that's the kind of effect I'm looking for
I have found a work around for this problem. Here is the code.
In styles.xml design a custom theme.
styles.xml
<style name="AppTheme.CustomTheme">
<item name="android:windowIsFloating">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
In manifest file use this style on the popup window.
Android Manifest
<activity android:name=".DialogDesign"
android:theme="#style/AppTheme.CustomTheme"
android:screenOrientation="portrait"></activity>
In layout.xml file use 3 different relativeLayouts, one as root, another as faded background and another as pop-up window.
activity_dialog_design.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DialogDesign">
<RelativeLayout
android:id="#+id/fadedBg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7fffffff"
android:layout_margin="0dp"/>
<RelativeLayout
android:layout_width="300dp"
android:layout_height="500dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="100dp"
android:layout_marginBottom="100dp"
android:layout_centerHorizontal="true"
android:background="#android:color/white"/>
</RelativeLayout>
In activity.java file set the height and width of the faded window.
DialogDesign.java
public class DialogDesign extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog_design);
RelativeLayout fadedBg=(RelativeLayout)findViewById(R.id.fadedBg);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = 1800;
params.width = 1100;
this.getWindow().setAttributes(params);
}
}
There, It is done! Add whatever you want to add in the inner most relative layout.
Easy answer
The amount by which the content behind a floating window (such as dialog) goes darker is determined by backgroundDimAmount attribute in your theme:
<style name="AppTheme" parent="#style/Theme.AppCompat">
<item name="android:backgroundDimAmount">0.6</item>
</style>
It is limited to black color. 0.6 is the default value.
EDIT
The above answer did not work, looks like the value needs to be applied to the dialog theme, not the activity theme.
<style name="AppTheme" parent="#style/Theme.AppCompat">
<item name="alertDialogTheme">#style/AppTheme.Dialog.Alert</item>
</style>
<style name="AppTheme.Dialog.Alert" parent="#style/Theme.AppCompat.Dialog.Alert">
<item name="android:backgroundDimAmount">0.6</item>
</style>
Difficult answer
Achieving a different color would require disabling this system dim and heavily customizing dialog background. I'll leave this to someone else.
It might be some late to answer. but i combined some solutions and gained the proper way to achieve this goal.
first create the following theme in your styles.xml:
<style name="MyCustomTheme" parent="#android:style/Theme.Panel">
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.7</item>
</style>
first enable an attribute named 'backgroundDimEnabled' to make the screen foreground darker when dialogs showing on the screen. then set a float value for amount of darkness.(0 < value < 1)
and use this theme in the onCreateDialog Method in your dialog fragment.
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyCustomTheme);
View addDialogView = getActivity().getLayoutInflater().inflate(R.layout.word_detail_fragment, null);
builder.setView(addDialogView);
return builder.create();
}

I want to hide my title bar in my android application [duplicate]

I am trying to use a custom title to include an image button to the title bar.
I got a lot of help form this post: android: adding button to the title of the app?, but could not get it work for my ListActivity.
In a nutshell, following is what I have:
I hide the titlebar in the AndroidManifest.xml
The specify a relative layout for the custom title (workorder_list_titlebar.xml)
My Activity Class looks like the following:
public class WorkOrderListActivity extends ListActivity {
String[] orders={"WO-12022009", "WO-12302009","WO-02122010", "02152010"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.workorder_list_titlebar);
setContentView(R.layout.workorder_list);
setListAdapter(new ArrayAdapter(this,R.layout.workorder_list, R.id.label,orders));
}
}
When I ran the app, I got AndroidRuntimeException: You cannot combine custom titles with other title features.
Base on the stack trace, the exception was thrown by com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:183), that was triggered by setlistAdapter call.
Does anyone have the same problem with ListActivity?
Also once I manage to get this work, how do I attach listeners to the image button for it to do something?
Thanks in advance.
I had the same issue and I fix it deleting
<item name="android:windowNoTitle">true</item>
from my theme.xml
Make you create custom style in “values” folder. Make sure you code as below.
<style name="CustomTheme" parent="android:Theme">
Don't modify parent parameter.
This did work for me.
Instead of modifying your theme.xml you may also:
create a new XML style file my_theme.xml in values folder like this:
<style name="MyWindowTitleBackground">
<item name="android:background">#444444</item>
</style>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">#style/MyWindowTitleBackground</item>
</style>
You may define other settings as you like in this theme.
Then just use this theme in your manifest within the activity's attributes
android:theme="#style/MyTheme"
Finally set your custom title as always in your activity.java:
final Window window = getWindow();
boolean useTitleFeature = false;
// If the window has a container, then we are not free
// to request window features.
if (window.getContainer() == null) {
useTitleFeature = window
.requestFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(R.layout.screen_main);
if (useTitleFeature) {
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.custom_title);
// Set up the custom title
main_title = (TextView) findViewById(R.id.title_left_text);
main_title.setText(R.string.app_name);
main_title = (TextView) findViewById(R.id.title_right_text);
main_title.setText(R.string.Main_titleInfo);
}
Don't forget to define the custom_title.xml file in your layout folder. For example...
<?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="fill_parent"
android:gravity="center_vertical" >
<TextView
android:id="#+id/title_left_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:ellipsize="end"
android:singleLine="true" />
<TextView
android:id="#+id/title_right_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#fff" />
</RelativeLayout>
I think notenking is right, that this is a problem in activities within tabs. Since some of my activities can either be stand-alone or within a tab, I've found the following helps:
final Window window = getWindow();
boolean useTitleFeature = false;
// If the window has a container, then we are not free
// to request window features.
if(window.getContainer() == null) {
useTitleFeature = window.requestFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(layoutId);
if (useTitleFeature) {
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
}
May be you find this problem when use it in tab,for there already have a title and you can not add a custom title again.
you should add this custom title in the activity which you get the Tab
I did exactly as Sunny Dasari did but with one small change I put the # before and android in the parent attribute.
So my code looked like this.
<style name="CustomTheme" parent="#android:Theme">
To avoid crashing, you can simply add
android:theme="#style/android:Theme"
to the <Activity> tag in your AndroidManifest.xml:
<activity
android:name="test.TestActivity"
android:label="#string/app_name"
android:theme="#style/android:Theme">
This is because the styles defined in your default theme conflict with FEATURE_CUSTOM_TITLE (such as the attribute android:windowNoTitle). By using another theme, you can avoid such problems.
However, you might further need to define your own theme to change other attributes, such as android:windowTitleSize, background color, text color and font, etc. In this case, you can derive your theme from an existing theme (e.g., Theme.Light) and modify its attributes:
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#323331</item>
</style>
<style name="CustomTheme" parent="#style/android:Theme.Light">
<item name="android:windowNoTitle">false</item>
<item name="android:windowTitleSize">60dip</item>
<item name="android:windowTitleBackgroundStyle">#style/CustomWindowTitleBackground</item>
</style>
</resources>
Try swapping following lines:
setContentView(R.layout.workorder_list);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.workorder_list_titlebar);
I have run into this issue as well and it looks like it is an issue with what theme is applied to an activity in the AndroidManifest.xml file. If I use a theme like:
android:theme="#android:style/Theme.Holo
Then it will throw the error
android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
However if I use a different theme like:
android:theme="#android:style/Theme.Black"
then it will not throw the error and subsequently will not crash. However I am trying to use a theme like Theme.Holo. I'm not sure if there is a way around this.
Since, I was trying to compile my program in android 4.0, I was facing the same problem. None of these solutions helped.So, I copied my style contents from values > styles.xml and pasted it in values-v11 styles.xml file and values-v14 styles.xml file. Bingo, the trick worked.
As a beginner most of the answers didn't help me for my case. So here is my answer.
Go to res/values folder in your android project and check for strings.xml (this file may vary in your case, something like themes.xml)
Inside the file under resource tag check whether you have style tags. If you don't find it, add the code below as mentioned below as a child to resources tag
something like below
<resources>
<style name="SomeNameHere">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
if you already have style tag, just add the code below to your style tag
<item name="android:windowNoTitle">true</item>

Why do the views in my TableRow don't react to their weights?

I'm programming an Android app and I'm trying to show a table. My problem is, that the columns don't fill the screen, they just all stick to left.
I already tried using padding, but first I don't think that it's compatible with all display resolutions and second the columns were aligned differently when they had different content in them.
Then I read about weight and this was exactly what I wanted, giving every column a specific percentage of the width of the screen. But weights did lead to the cells sticking to the left again. They did react in no way to the weights I've set.
I program the view in xml templates and inflate them programmatically using the LayoutInflater. Then I add them to the corresponding TableRow. All that works perfectly fine, but they just don't react to the weights.
I would really appreciate your help.
EDIT: The templates refer to a style and look like this:
<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/scheduleClass" />
And the style looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="scheduleItem" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:paddingRight">0dp</item>
<item name="android:paddingBottom">10dp</item>
</style>
<style name="scheduleClass" parent="scheduleItem">
<item name="android:layout_weight">0.2</item>
</style>
</resources>
There are other a few other columns, but they just have another name and another weight.
And this is how I add the views to the table:
public void updateList(ArrayList<Lesson> lessons) {
setContentView(R.layout.schedule);
TableLayout table = (TableLayout) findViewById(R.id.table);
for(Lesson cancel : lessons) {
TableRow row = new TableRow(this);
TextView date = (TextView) this.getLayoutInflater().inflate(R.layout.scheduledatetemplate, null);
date.setText(cancel.date);
TextView classname = (TextView) this.getLayoutInflater().inflate(R.layout.scheduleclasstemplate, null);
classname.setText(cancel.classname);
TextView lesson = (TextView) this.getLayoutInflater().inflate(R.layout.schedulelessontemplate, null);
lesson.setText(cancel.lesson);
Button details = (Button) this.getLayoutInflater().inflate(R.layout.detailsbuttontemplate, null);
details.setText(R.string.details);
details.setOnClickListener(this);
row.addView(date);
row.addView(classname);
row.addView(lesson);
row.addView(details);
table.addView(row);
}
}
And this is the TableLayout xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:scrollbars="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
</TableLayout>
</ScrollView>
You need to declare android:layout_column and android:layout_span attributes to your TableRows
See http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html
See an example here:
http://www.mkyong.com/android/android-tablelayout-example/
UPDATE:
Ok, you'll need to set the layout_span and layout_column in each of the rows you're creating, use this addView method:
http://developer.android.com/reference/android/widget/TableLayout.html#addView(android.view.View, int, android.view.ViewGroup.LayoutParams)
To supply the layout params with the appropriate column and span, to make your row span to the entire table, and have the row gravity=center to make their content in the center.
Also, the table attribute strechColumns
http://developer.android.com/reference/android/widget/TableLayout.html#attr_android:stretchColumns
Might help you, as in:
android:strechColumns="*"
or something like that.

Categories

Resources