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
Related
I'm trying to implement a dark theme to my app. The user can easily change between normal and dark in an options menu – which works fine. But when the theme is changed at runtime, only the text color changes and I don't know why.
My dark theme in styles.xml:
<style name="Dark" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/dark_background</item>
<item name="colorPrimaryDark">#color/dark_top</item>
<item name="colorAccent">#color/dark_button</item>
<item name="colorButtonNormal">#color/dark_button</item>
<item name="android:colorBackground">#color/dark_background</item>
<item name="android:itemBackground">#color/dark_background</item>
<item name="android:textColor">#color/white</item>
<item name="android:textColorHint">#EAEAEA</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="android:textColorSecondary">#color/white</item>
<item name="android:textColorTertiary">#color/white</item>
</style>
My way of setting the style:
setTheme(R.style.Dark);
Before changing theme:
before
After changing theme:
after
I don't really know why. Is it because of the NavigationView?
Make sure you are calling setTheme() before setContentView() or inflating a view. According to the documentation you must use setTheme() before any views are instantiated in the Context. Use recreate() to create a new instance of your activity so you can apply the changed theme in the onCreate() method.
You can find several examples of theme switching if you search around a little bit. This is a link to one such example:
https://gist.github.com/alphamu/f2469c28e17b24114fe5
I use PreferenceManager to store settings like this for easy access if I have more than one activity that will need to use the setting. Unless you already have a better way to store your users' theme choice, I would suggest something like the following examples.
Example MyAppPreferences class:
public class MyAppPreferences {
private static SharedPreferences getPrefs(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}
public static int getThemeId(Context context, int defaultThemeId) {
return getPrefs(context).getInt("CurrentThemeId", defaultThemeId);
}
public static void setThemeId(Context context, int value) {
getPrefs(context).edit().putInt("CurrentThemeId", value).commit();
}
}
Example Activity class using the MyAppPreferences class:
public class MyActivity extends AppCompatActivity implements OnClickListener {
private Button btnDark;
private Button btnLight;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the theme
// If there is nothing set, the light theme will be used by default
setTheme(MyAppPreferences.getThemeId(this, R.style.Light));
setContentView(R.layout.myLayout);
btnDark = (Button) this.findViewById(R.id.viewbtnDark);
btnDark.setOnClickListener(this);
btnLight = (Button) this.findViewById(R.id.viewbtnLight);
btnLight.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// 1. Set the theme preference
// 2. Recreate the activity to "apply" the theme
if (v.equals(btnDark)) {
MyAppPreferences.setThemeId(this, R.style.Dark);
this.recreate();
} else if (v.equals(btnLight)) {
MyAppPreferences.setThemeId(this, R.style.Light);
this.recreate();
}
}
}
Your example theme does not show the windowActionBar or windowNoTitle settings so if you happen to be using a default theme and you do not set these options the same way in your dark theme, you may still encounter crashes. Check Logcat for an error like this: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor..
Example Dark theme
<style name="Dark" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/dark_background</item>
<item name="colorPrimaryDark">#color/dark_top</item>
<item name="colorAccent">#color/dark_button</item>
<item name="colorButtonNormal">#color/dark_button</item>
<item name="android:colorBackground">#color/dark_background</item>
<item name="android:itemBackground">#color/dark_background</item>
<item name="android:textColor">#color/white</item>
<item name="android:textColorHint">#EAEAEA</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="android:textColorSecondary">#color/white</item>
<item name="android:textColorTertiary">#color/white</item>
</style>
I try to open a new activity from another one after some seconds, I used this code,
but it's not working (first activity runs but after some seconds I have an error)
public class WelcomeActivity extends AppCompatActivity {
private static int TIME_OUT = 4000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
//finish();
}
}, TIME_OUT);
}
}
This is a stacktrace:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{________}: java.lang.IllegalStateException: This
Activity already has an action bar supplied by the window decor. Do
not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar
to false in your theme to use a Toolbar instead
The problem is not with this code. According to your exception the problem is in the MainActivity class. I think your are trying to create custom action bar. But the error is occurring due to your current theme already have an action bar. So you need customize your theme for that class.
In styles.xml create a new theme.
<style name="Theme.FullScreen" parent="AppTheme.NoActionBar">
<item name="android:windowNoTitle">false</item>
<item name="android:windowFullscreen">false</item>
<item name="android:windowActionBar">false</item>
</style>
In the AndroidManifest.xml change the theme of your view to your customized theme.
<activity
android:name=".MainActivity "
android:theme="#style/Theme.FullScreen" />
This link will help you.
make
<item name="windowActionBar">false</item> in your style.xml
Example:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.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:windowAnimationStyle">#style/AnimationActivity</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
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?
I have an activity with a theme associated (in AndroidManifest.xml)
<activity
android:name=".BenchTestActivity"
android:parentActivityName=".HomeActivity"
android:theme = "#style/AppTheme.CaeTheme">
The AppTheme.CaeTheme contains nothing (at the moment) but AppTheme is:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:background">#color/colorPrimary</item>
</style>
(colorPrimary is dark blue)
Now I'm defining a progressDialog in above mentioned activity.
progressDialog = new ProgressDialog(BenchTestActivity.this);
progressDialog.setProgressStyle(R.style.ProgressDialog);
and here the ProgressDialog style:
<style name="ProgressDialog">
<item name="android:alertDialogStyle">#style/CustomAlertDialogStyle</item>
<item name="android:layout_centerHorizontal">true</item>
<item name="android:layout_centerVertical">true</item>
<item name="android:visibility">gone</item>
</style>
<style name="CustomAlertDialogStyle">
<item name="android:background">#color/colorBackgroundProgressDialog</item>
<item name="android:textColorPrimary">#e6e6e6</item>
</style>
colorBackgroundProgressDialog is a light gray. I was expecting to obtain that color as background for my Progress Dialog but that's not what it's happening (the background color is a dark blue, colorPrimary). So the background on the activity theme is winning. So, how can I set a custom color for the Progress Dialog?
Thanks in advance.
Use the constructor that accepts theme as argument and pass appropriate theme.
ProgressDialog progressDialog = new ProgressDialog(context, R.style.MyProgressDialogTheme);
progressDialog.show();
In styles.xml:
<style name="MyProgressDialogTheme" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<!-- override attributes here -->
</style>
in your styles.xml
<style name="Custom" parent="android:Theme.DeviceDefault.Dialog">
<item name="DialogTitleAppearance">#android:style/TextAppearance.Medium</item>
<item name="DialogTitleText">Loading……</item>
<item name="DialogSpotColor">#android:color/holo_orange_dark</item>
<item name="DialogSpotCount">4</item>
</style>
and in your activity,java:
SpotsDialog spotsDialog = new SpotsDialog(Context,R.style.Custom);
spotsDialog.show(); //where you want
spotsDialog.dismiss(); //where you want
and add Dependency
compile 'com.github.d-max:spots-dialog:0.7#aar'
Hi I'm using Lollipop theme for all other API version, but i don't use Lollipop toolbar and style actions. I have created a ActionBar like custom ActionBar layout like left and right corner Buttons and title is in center.
Here the my the theme but it doesnot work. can you update this issue. also tell me the style for "Theme.AppCompat.Light"
// Style code
res/values/theme.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimary</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
</resources>
values-v21/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
</style>
</resources>
// Activity code like.
public class CreateAccountActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// To set the custom title with Button
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.create_account_screen_1);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.title_layout_with_two_button);
((TextView) findViewById(R.id.myTitle)).setText(R.string.create_acc);
// ((Button) findViewById(R.id.back_button)).setText(R.string.login);
((Button) findViewById(R.id.right_button))
.setVisibility(View.INVISIBLE);
}