How to set buttonstyle for programmatically added buttons? - java

We use this function to add buttons to our activity:
private void createButtonListInLayout(Cursor c) {
if (c.moveToFirst()) {
do {
Button button = new Button(this);
button.setText(c.getString(DBAdapter.COL_TABLE_NAME_MAIN));
linearLayoutList.addView(button);
} while(c.moveToNext());
}
}
I am trying to give these buttons a custom style. I've addded a buttonstyle to my apptheme, as you can see below, but the buttons created using the function above don't have this style.
This is the style I am trying to give to my buttons using my styles.xml file:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- 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:textColor">#color/colortext</item>
<item name="android:titleTextStyle">#color/colortext</item>
<item name="buttonStyle">#style/AppTheme.button</item>
</style>
<style name="AppTheme.button" parent="Widget.AppCompat.Button.Borderless" >
<item name="android:maxHeight">50dp</item>
<item name="android:maxWidth">200dp</item>
</style>
The buttons which are created by the first function don't have the style I want them to have. The other buttons (which are defined in the activity.xml files) do have the style.
How can I make the buttons, which are created using the function above, the style I defined in the styles.xml file? Or is there another way to change the style of those buttons?

Related

App styling in Android Studio

I cannot seem to find the right solution to increase and decrease the text size of the whole android app project.I want to give user an option so the user can change the font size of the whole app. I tried extend my Base-activity to Text-view but didn't do the trick i hope anyone here can help me.
You'll have to update textSize of TextViews from Design tab in
.xml files that you can find at res > layout like activity_main.xml file.
If you want to change size for whole app, Try custom themes and set it dynamically.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:textSize">16sp</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme2" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:textSize">15sp</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme3" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:textSize">17sp</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Create separate themes for different sizes, and use this class for setting it.
public class themeUtils
{
public static int cTheme;
public static void changeToTheme(Activity activity, int theme)
{
cTheme = theme;
activity.finish();
activity.startActivity(new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
}
public static void onActivityCreateSetTheme(Activity activity)
{
try
{
activity.setTheme(cTheme);
}
catch (Exception e)
{
activity.setTheme(R.style.AppTheme); //default
}
}
}
In your activity, call this method before super.oncreate() method,
#Override
protected void onCreate(Bundle savedInstanceState) {
themeUtils.onActivityCreateSetTheme(this);
super.onCreate(savedInstanceState);
...}
Finally change theme in your code where ever you want,
themeUtils.changeToTheme(this, themeselected);//your theme name

how to remove actionbar from dialog activity android

i have a dialog activity and want to remove the action bar from it , and anther dialog activity but just want to change the color of its action bar >>
i tried to change the theme and style but nothing change also.
<style name ="Dialog" parent="Theme.AppCompat.Dialog">
<item name="windowActionBar">false</item>
</style>
and the other style is
<style name ="coloredDialog" parent="Theme.AppCompat.Dialog">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
</style>
also i have tried to use
getSupportActionBar().hide()
but their some is error then and the app is closed suddenly

How to make alert dialog match theme color?

So in the app I am making, I have both a dark and light theme. The light theme works fine, but the alert dialogs I have still show the background the same as the light theme. Since it is dark, the text changes to light and the user can't see anything. Is there a certain attribute I need to change?
Here is the styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#E53935</item>
<item name="colorPrimaryDark">#C62828</item>
<item name="android:navigationBarColor">#E53935</item>
<item name="android:colorAccent">#1565C0</item>
<item name="android:colorForeground">#color/foreground_material_light</item>
<item name="android:colorBackground">#color/background_material_light</item>
<!-- Other attributes -->
</style>
<style name="Dark" parent="#style/AppTheme" >
<item name="android:colorForeground">#color/foreground_material_dark</item>
<item name="android:colorPrimary">#color/primary_material_dark</item>
<item name="android:colorPrimaryDark">#color/primary_dark_material_dark</item>
<item name="android:colorAccent">#color/accent_material_dark</item>
<item name="android:navigationBarColor">#color/primary_material_dark</item>
<item name="android:colorBackground">#color/background_material_dark</item>
<item name="android:textColorPrimary">#android:color/primary_text_dark</item>
<item name="android:textColorPrimaryInverse">#android:color/primary_text_light</item>
<item name="android:textColorSecondary">#android:color/secondary_text_dark</item>
<item name="android:textColorSecondaryInverse">#android:color/secondary_text_light</item>
<item name="android:textColorTertiary">#android:color/tertiary_text_dark</item>
</style>
Here is what it shows up as:
Solved: You need to create your own xml like this:
<item name="alertDialogTheme">#style/DarkAlertDialog</item>
With this being your custom dialog:
<style name="DarkAlertDialog" parent="Theme.AppCompat.Dialog">
<item name="background">#color/background_material_dark</item>
</style>
Use the parent AppCompat and then use colorPrimary and colorAccent properties (without an Android prefix) to theme things.
Right now the styles are coming from a dialog subtheme and not being affected by these style definitions.

Change Google Place Picker Appbar Text Color

I would really like to know if I am able to change the text color of the Appbar text in the PlacePicker API for Android. It should inherit the styling from my application, but it does not do that.
my styles.xml is as follows:
<style name="MaterialParent" parent="Theme.AppCompat.Light.NoActionBar">
<!-- ...and here we setting appcompat’s color theming attrs -->
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textColorPrimary">#android:color/white</item>
<item name="android:textColorSecondary">#color/secondary_text</item>
<item name="icon">#color/icons</item>
<item name="divider">#color/divider</item>
<item name="colorControlHighlight">#color/accent_alpha26</item>
</style>
<style name="Material" parent="MaterialParent"/>
I've already tried changing android:textColorPrimary, android:textColorSecondary and textColor to white with no luck.
Please use Theme.AppCompat.Light.NoActionBar instade of Theme.AppCompat.Light.DarkActionBar
Just change the colorPrimary value of color file with the color hex code, that you want and it works fine.

Hide Calendarview from DatePicker programmatically in API 21

I'm having the same issue as this thread: Android Material Design Inline Datepicker issue, but I am not using an XML layout, instead I'm building the DatePicker programmatically.
This is the code I am using but is not working
DatePicker dpView = new DatePicker(ctx);
dpView.setCalendarViewShown(false);
dpView.setSpinnersShown(true);
how can I make it work?
If you have to set it up programmatically in a DatePickerFragment just set this:
DatePickerDialog dialog = new DatePickerDialog(getActivity(),android.R.style.Theme_Holo_Dialog_MinWidth, this, year, month, day);
with whatever style you like.
The problem in Android 5.0 is the "mode" which determines whether to use a calendar or not is read at construct time, and in code you can't set the mode until after it has been constructed, thus it's too late. (Source is here: DatePicker Source Code)
My solution was to create my own reusable DatePicker layout that specifies the "no calendar" mode, and construct my DateTimes programmatically with that layout instead of Android's default.
Bottom line is, create a "DatePicker.axml" file, put it in the resources folder, and paste the following as its contents:
<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:calendarViewShown="false"
android:datePickerMode="spinner"/>
and declare it wherever you need in code like this:
LayoutInflater inflater = LayoutInflater.From( Activity );
DatePicker datePicker = (DatePicker)inflater.Inflate( Resource.Layout.DatePicker, null );
Adding to the answer from user Luismi:
The theme Theme.Holo.Dialog or Theme.Holo.Dialog.MinWidth might cause problems like two boxes drawn around the date picker.
Instead you should the theme Theme.Holo.Dialog.NoFrame to prevent this. The theme might not be accessable from your code, so just create your own theme:
<style name="DatePickerDialogTheme" parent="android:Theme.Holo.Light.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
Edit: The better way to fix this problem is to use the SpinnerDatePicker library that recreates the old design.
I simply changed my AppTheme(v21) style and worked:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorControlActivated">#color/colorPrimary</item>
<item name="android:timePickerDialogTheme">#style/PickerDialogCustom</item>
<item name="android:datePickerDialogTheme">#style/PickerDialogCustom</item>
<item name="alertDialogTheme">#style/AlertDialogCustom</item>
</style>
<style name="PickerDialogCustom" parent="AlertDialogCustom">
<item name="android:textColor">#color/colorPrimary</item>
<item name="android:textColorPrimary">#color/colorPrimaryDark</item>
<item name="colorControlNormal">#color/greyLight500</item>
<item name="android:layout_margin">2dp</item>
<item name="android:datePickerMode">spinner</item>
</style>
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorAccent">#color/colorPrimary</item>
<item name="android:positiveButtonText">#color/colorPrimary</item>
<item name="android:negativeButtonText">#color/greyDark200</item>
<item name="buttonBarNegativeButtonStyle">#style/negativeButton</item>
<item name="android:datePickerStyle">#style/PickerDialogCustom</item>
</style>

Categories