how to set the screen brightness and Font-Scale in android - android

iam setting the device settings into certain values. i want to adjust the screen brightness and the Font-Scale iam using the following code:
//For Font-Scale
Settings.System.putFloat(this.getContentResolver(),Settings.System.FONT_SCALE,(float) 1.3);
//For Brightness
Settings.System.putString(this.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS,255);
the value of the brightness does change but to set activate it i must go to the screen settings and open the brightness settings and press OK.
the value of the Font-Scale Does not change.
i think maybe a way to refresh the settings to get the new values. can any one help me?

This cannot be done by apps. One way to get these settings to take effect is by rebooting the phone. If you have root access you can use non-public apis. use below code to do it using reflection.
To change brightness you need to call
PowerManagerService.setBacklightBrightness(brightness);
And to change
ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig)

check the source code DisplaySettings.java, here is the method to control font size scale
public void writeFontSizePreference(Object objValue) {
try {
mCurConfig.fontScale = Float.parseFloat(objValue.toString());
ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig);
} catch (RemoteException e) {
Log.w(TAG, "Unable to save font size");
}
}
here is a solution keep the font size in different fontscale.
float textSize = 22f;
mTextView.setTextSize(textSize / getResources().getConfiguration().fontScale);
and I think #shoe rat's answer is more better for an activity or application context.

Related

How to avoid the toast when screen is on landscape mode in Android

I need to show the user a message using toast or snackbar. However, i do not want to show this message when the user tilts his/her phone into landscape mode. How can I achieve this message showing only in portrait mode and not in landscape mode?
This method will return true if the phone is in portrait and false if the phone is in Landscape it uses configurations:
private boolean isPortrait(){
return (getResources().getConfiguration().orientation==ORIENTATION_PORTRAIT);
}
Import configuration classes in Android Studio its easy. So from here you can use a simple if statement using the method given above:
if(isPortrait()){
//Show you Toast and snackbar here
}
For more information on this check the official documentation here.
You can check orientation of the phone in resources configuration object:
getResources().getConfiguration().orientation
When it equals ORIENTATION_LANDSCAPE simply don't show the toast/snackbar
For the reference see:
https://developer.android.com/reference/android/content/res/Configuration.html#orientation

How to ignore font-size setting changes for Cordova App when runs on Android 4.4+?

I've developed Cordova App with Cordova Version 3.6.3 and JQuery. The only one problem that i still can't get the best solutions is when i test my app on Android 4.4+ and there are users who love to change font size in setting > display > font-size of their device to be larger than normal. It causes my app layout displays ugly (the best display is when the font-size setting is normal only). But there is no effect of font-size setting for Android older than 4.4 (4.3,4.2...) So the app displays perfectly on older version.
The solutions that I've applied to my app is creating the custom plugin to detect configuration changed and it will detects if user uses Android 4.4+ and if they set font-size setting to anything that is not normal, I'll use JQuery to force font-size to the specified size.
Example is....
if (font_scale == huge)
{
$("div").css("font-size","20px !important");
}
This works fine but sometimes after the page loaded, the css doesn't changes as I want. And suppose if there are 30 divs+ on that page so i must insert the statement like above 30 times and it takes too much time needlessly.
I just want to know, are there another ways to solve this problem that is easier than using this plugin? Maybe using some XML configuration or CSS3 properties that can makes my app displays properly without side effect from font-size setting of Android 4.4?
Another ways I also tried and it doesn't works are
inserting fontScale on Androidmanifest.xml > activity tag
inserting webkit-text-size-adjust:none; in css
I'd love to hear any ideas that help to solve this.
So you just want to ignore the system font preferences.
Here is the solution,I use MobileAccessibilty Plugin to ignore the system font preferences.
You just have to write following code in your onDeviceReady function in index.js
if(window.MobileAccessibility){
window.MobileAccessibility.usePreferredTextZoom(false);
}
usePreferredTextZoom(false) will just ignore the system font preferences. :) :) :)
I hope this helps!
In Cordova 8.0:
Edit MainActivity.java
Add references in the file header:
import android.webkit.WebView;
import android.webkit.WebSettings;
Add the code after loadUrl method:
loadUrl(launchUrl);
WebView webView = (WebView)appView.getEngine().getView();
WebSettings settings = webView.getSettings();
settings.setTextSize(WebSettings.TextSize.NORMAL);
Only this way worked for me.
Try to remove this one from your index.html:
target-densitydpi=device-dpi
Good luck.
Here is an new solution,hope it can help you. Solution
//in base activity add this code.
public void adjustFontScale( Configuration configuration) {
configuration.fontScale = (float) 1.0;
DisplayMetrics metrics = getResources().getDisplayMetrics();
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
metrics.scaledDensity = configuration.fontScale * metrics.density;
getBaseContext().getResources().updateConfiguration(configuration, metrics);
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adjustFontScale( getResources().getConfiguration());
}
If your plugin allows you to know the font scale, change all your font sizes to em and use
$(document.body).css("font-size","70%");
just once.
Do you mind sharing your plugin?
This is quite old but since I still needed it in 2019 someone else might need it as well.
In case for some reason you do not want (or can't) integrate a whole plugin for a single functionality, here is the usePreferredTextZoom(false) functionality extracted (it's from a MobileFirst 7.1 app, based on Cordova). All credit goes to the guys developing the plugin, of course.
Within the main Java file of your Cordova Android app change the onInitWebFrameworkComplete method by adding the preventDeviceZoomChange() method call:
public void onInitWebFrameworkComplete(WLInitWebFrameworkResult result){
if (result.getStatusCode() == WLInitWebFrameworkResult.SUCCESS) {
super.loadUrl(WL.getInstance().getMainHtmlFilePath());
preventDeviceZoomChange();
} else {
handleWebFrameworkInitFailure(result);
}
}
In the same class, define the preventDeviceZoomChange() method:
private void preventDeviceZoomChange() {
try {
Method getSettings = (this.appView).getClass().getMethod("getSettings");
Object wSettings = getSettings.invoke(this.appView);
Method setTextSize = wSettings.getClass().getMethod("setTextSize", WebSettings.TextSize.class);
setTextSize.invoke(wSettings, WebSettings.TextSize.NORMAL);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}

locking screen orientation on mobile browser

I create a web page(chrome & safari) for mobiles (iphone & android), I want to lock the screen orientation in portrait mode.
Unlike mobile apps,there is no manifest file and activity as it a web page.
How to lock the orientation in mobiles using technologies (css/javascript/bootstrap/jquery) or any other?
I use a manifest file for my web app, which locks orientation for Chrome on my Android. For whatever reason, Safari gives their users the "right" to do this, but not the designers of the web app... Sort of feels like copyright infringement or something! ;) Don't get me started on Safari's disgraceful rewriting/rendering of input buttons!...
Anyways, back to the answer.
1) Include a link to your manifest within the head section of your page:
<link rel="manifest" href="http://yoursite.com/manifest.json">
2) Create your manifest file, "manifest.json"
{
"name":"A nice title for your web app",
"display":"standalone",
"orientation":"portrait"
}
3) Read more about manifests HERE
From my tests, assigning the screen.lockOrientation ( every browser versions ) to a var throw an illegal invocation error. Just use wind.screen.orientation.lock('landscape'); . It
EDIT: You can't use lock orientation on safari, cause it doesn't support fullscreen api at the moment http://caniuse.com/#feat=fullscreen . The lock orientation API NEED a fullscreen page to work. In Chrome, the window.screen.orientation.lock return a promise. So, AFTER you go fullscreen with the page, you can do something like this :
var lockFunction = window.screen.orientation.lock;
if (lockFunction.call(window.screen.orientation, 'landscape')) {
console.log('Orientation locked')
} else {
console.error('There was a problem in locking the orientation')
}
However, the lock orientation and fullscreen API are still experimental, not all browsers supports it.
The lockOrientation method locks the screen into the specified orientation.
lockedAllowed = window.screen.lockOrientation(orientation);
From the following code, you can check that orientation is locked or not.
var lockOrientation = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation;
if (lockOrientation("landscape-primary")) {
// orientation was locked
} else {
// orientation lock failed
}
see the following link, you will get idea from this.
https://developer.mozilla.org/en-US/docs/Web/API/Screen.lockOrientation
You can use:
screen.addEventListener("orientationchange", function () {
console.log("The orientation of the screen is: " + screen.orientation);
});
and
screen.lockOrientation('landscape');
Following: https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model/Managing_screen_orientation

Is there a way to programmatically control the back-light for the android back button?

I'm currently making an android app and testing it on a Samsung GT-S5830.
The problem I'm having is that the back button back-light is always off when the app is running (so it's not visible), which seems to confuse the users who I have asked to test the app.
The question is whether there is a way to programmatically ensure that the back-light for the back button is always on?
I'm dubious about it, as the problem seems to be phone model dependent.
Thanks.
there is already a stackoverflow answer but I will post again:
private void setDimButtons(boolean dimButtons) {
Window window = getWindow();
LayoutParams layoutParams = window.getAttributes();
float val = dimButtons ? 0 : -1;
try {
Field buttonBrightness = layoutParams.getClass().getField(
"buttonBrightness");
buttonBrightness.set(layoutParams, val);
} catch (Exception e) {
e.printStackTrace();
}
window.setAttributes(layoutParams);
}
Or try to find something in here (the new design is horrible ...) http://developer.android.com/reference/android/os/PowerManager.html

Android: access "system"-drawables

I'm currently working on an app to display the battery status and I'd like to use Android-drawables instead of own images to reduce the app size.
I've found this page which lists available images and the availability for each SDK-version:http://www.fixedd.com/projects/android_drawables_display
My question: How can I access the "system"-drawables? If you click on the link and choose the tab "Status", there are some battery-drawables like "stat_sys_battery_0", but I can't access it, Eclipse doesn't offer intellisense for it and won't compile the app if I use one of those drawables.
As those drawables are part of all SDK-versions, I'd think I should be able to use them, or are those "special" drawables protected in a way so they can only be used by system-functions (and not apps)?
Any idea is appreciated.
Select0r
Hope this is what you were looking for:
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent intent) {
int level = intent.getIntExtra("level", 0);
int batteryIconId = intent.getIntExtra("icon-small", 0);
Button toolBarBattery = (Button) findViewById(R.id.toolBarButton);
LevelListDrawable batteryLevel = (LevelListDrawable) getResources().getDrawable(batteryIconId);
batteryLevel.setLevel(level);
toolBarBattery.setBackgroundDrawable(batteryLevel);
}
};
I've found another link with information that not all drawables are public. It doesn't say why some drawables would be private, but I guess I'll have to live with the fact and copy the needed images to my app.http://androiddrawableexplorer.appspot.com/
NOTE: Some of the images in the Android jar are not public and therefore cannot be directly used (you can copy them to you own application, but can't reference them via the "android" package namespace).
There actually seems to be a way to access the system icons, but it's not really working as stated in the documentation, but I'll add it in case somebody is interested:
intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, -1)
Will get you the resource-ID of the icon that matches the current battery-status:http://developer.android.com/reference/android/os/BatteryManager.html#EXTRA_ICON_SMALL
Extra for ACTION_BATTERY_CHANGED:
integer containing the resource ID of
a small status bar icon indicating the
current battery state.
However, it always returns the same icon, no matter what the actual battery level is. Finding the icon by just trying random numbers may work, but I don't know if the IDs are consistent throughout the SKD-levels as well as different machines, so I'd rather not rely in that.

Categories

Resources