Alternative to DISPLAY.getWidth() - java

I know the Display method getWidth() has been deprecated since API 13. I have the following line of code:
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int displayWidth = display.getWidth();
What is the alternative for API Level 13 and above?

In case you're not in an activity use
context.getResources().getDisplayMetrics().heightPixels;
context.getResources().getDisplayMetrics().widthPixels;

You are looking for getSize(Point outSize) (according to official android API documentation )
Hope this helps!

Related

Why are complex emojis not merged but split up when drawn on Android canvas?

I want to implement an emoji selector for my keyboard app Keyboard Designer. To do so I want to draw emojis based on unicodes in hexadecimal format. The emoji "\u1F636\u200D\u1F32B\uFE0F" is shown correctly when I write it in the text field (two eyes behind a cloud), but when I draw it on my canvas, it looks like two separate emojis (left top corner of the keyboard):
Hint: The hearts do nothing have to do with the question, they only mark favourite emojis
My source is nested in objects and methods, but to show you how it works, I change it to linear commands:
ArrayList<char[]> utf16Chars = new ArrayList<>();
String hexCode = "\\u1F636\\u200D\\u1F32B\\uFE0F";
int distance;
if (hexCode.startsWith("\\")) {
for (int i = 0; i < hexCode.length(); i += distance) {
distance = hexCode.indexOf("\\", i + 1) - i;
if (distance < 0)
distance = hexCode.length() - i;
String utf16Code = hexCode.substring(i, i + distance);
int decimalCode = Integer.parseInt(utf16Code.length() >= 6 ? utf16Code.substring(2) : utf16Code, 16);
char[] utf8Chars = Character.toChars(decimalCode);
utf16Chars.add(utf8Chars);
}
}
StringBuilder stringBuilder = new StringBuilder();
for(char[] utf8Chars : utf16Chars)
for(char character : utf8Chars)
stringBuilder.append(character);
String emoji = stringBuilder.toString();
canvas.drawText(emoji, 0, emoji.length(), x, y, paint);
Does anyone have an idea what I do wrong?
Update
Also see this bug report.
The Face in Clouds Emoji was added in Emoji Version 13.1 and will probably be generally available in later versions of Android. My answer, below, works for API 31 but not for API 30 or before. There is a backward compatibility issue with that solution.
There is a class EmojiCompat that can supply compatibility that will display the "Face in Clouds" emoji on Android version before API 31.
I have put together a EmojiCompat project based on the EmojiCompat app in Google's user-interface-samples. A couple of notes on this demo:
The EmojiCompatApplication class has some important setup.
The dependency on version 1.1.0 of androidx.emoji:emoji-bundled was updated to version 1.2.0-alpha03. Without this update, the "Face in Clouds" emoji displays as two emojis and not one. As new emojis are released (yearly, I think), this library will need to be updated. I believe an alternative is to use downloadable emoji fonts, but I do not address downloadable fonts here.
In MainActivity, I left everything as it was in the Google project except that I added processing for "MyView" which creates a StaticLayout and displays the content using Layout.draw(canvas) as specified in my previous solution which is what the OP was requesting. Canvas.drawText() is still discouraged.
Here is the output of the demo app on an emulator running API 24:
This was more involved than I thought at first and I could not find a good tutorial online. Maybe someone knows of one and can suggest it.
I used the following simple code to create the combined emoji.
// val cloudy = "\u1F636\u200D\u1F32B\uFE0F"
val cloudyFace = intArrayOf(0x1F636, 0x200D, 0x1F32B, 0xFE0F)
val sb = StringBuilder()
for (i in 0 until cloudyFace.size) {
sb.append(getUtf16FromInt(cloudyFace[i]))
}
binding.textView.text = sb.toString()
fun getUtf16FromInt(unicode: Int) = String(Character.toChars(unicode))
Instead of using Canvas.drawText() use layout.draw(canvas) where layout is a StaticLayout. From the documentation:
This is used by widgets to control text layout. You should not need to use this class directly unless you are implementing your own widget or custom display object, or would be tempted to call Canvas.drawText() directly.
Bottom line: Don't use Canvas.drawText().
You may also use the BoringLayout class if that better suits your needs.

Controlling Brightness of tablet with Qt Android

I tried the Qt forums but with no avail. I'm trying to develop an Android application that one of the functionality is changing the brightness of the tablet backlight. I have successfully written two programs in Android Studio (in Java) using the LayoutParams and Android putInt system brightness to change brightness. The issue comes when I try to move the code to my Qt application. I have the JNI code working and it runs my functions, but when I paste the brightness code in to change brightness method the application fails.
From what I understand of Android and the error statements, my issue (I think) is I am not running the code on the UI thread. I've tried to force my Java method to be a Runnable and use runonUiThread but that doesn't support the ContentResolver or Window because it is not an Activity.
Does anyone have experience with this that can guide me? Or have any experience getting anything in the Android settings to work?
I appreciate everyone helping out,
Andrew
You could have it in a static Java method like :
package com.MyApp;
public class BrightnessChanger
{
public static int change(int n)
{
float brightness = n / (float)255;
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = brightness;
getWindow().setAttributes(lp);
}
}
You can then call this static Java function from your C++ code :
bool retVal = QAndroidJniObject::callStaticMethod<jint>
("com/MyApp/BrightnessChanger" // class name
, "change" // method name
, "(I)I" // signature
, 50);
Here you pass a value between 1 and 255 to the function.

Zxing PDF417 edit padding / quiet zone

I am generating a PDF417 barcode with the zing library. All good with that...
writer = new PDF417Writer();
bitMatrix = writer.encode(barcodeMessage.getData(),
BarcodeFormat.PDF_417, WIDTH, WIDTH / 2, // To maintain a width/height ratio
ImmutableMap.of(
EncodeHintType.PDF417_COMPACT, Boolean.TRUE,
EncodeHintType.CHARACTER_SET, messageEncoding
)
);
barcodeBg = MatrixToImageWriter.toBufferedImage(bitMatrix);
But I was wondering if there is any way to be able to edit how much quiet zone is left at each side? Something similar to this... http://www.racoindustries.com/barcodegenerator/2d/pdf417.aspx that allows you to select how much space you have on each side.
I have tried adding this EncodeHintType.MARGIN but for this type of barcode it does not work.
Any ideas?
Support for EncodeHintType.MARGIN for PDF417 was added to zxing in the newest release 3.0.0.

Add custom View as marker in Google Maps API v2

I've developed a class that inherits from a View class and I want it to serve as a marker on a MapFragment/MapView from a Google Maps API v2. As I recall, such thing was possible in API v1 and even now it is possible on iOS. I'm looking for something like this:
MapView map = (MapView)findViewById(R.id.mapview);
CircleTimerView myTimer = new CircleTimerView(this);
LayoutParams lp = new LayoutParams(iv.getWidth(), iv.getHeight(), geoPoint, LayoutParams.BOTTOM);
map.addView(myTimer,lp);
My custom View is animated and it is a key feature of my app.
My question is: Is it possible for a current state of Google Maps API? Or should I try to obtain API v1 key and work with something that was deprecated?
Thanks in advance for your replies.
should I try to obtain API v1 key and work with something that was deprecated?
You cannot obtain new keys for API v1.
My custom View is animated and it is a key feature of my app.
You cannot have animated Marker icon unless you repeatedly change the icon using Marker.setIcon.
Is it possible for a current state of Google Maps API?
You can use a View as icon if you draw it into a Bitmap first.
This library from Chris Broadfoot can greatly help you with that task:
https://github.com/googlemaps/android-maps-utils
See this video for what the lib can do: http://www.youtube.com/watch?v=nb2X9IjjZpM
Or the new website: http://googlemaps.github.io/android-maps-utils/
in order to customize / use your own custom maker on google maps api v2 you could write the code as follows:
in onCreate declare the marker:
userIcon = R.drawable.red;
and then i used it where you need it, in my case i used it in a method:
if(userMarker!=null) userMarker.remove();
userMarker = theMap.addMarker(new MarkerOptions()
.position(lastLatLng)
.title("You are here")
.icon(BitmapDescriptorFactory.fromResource(userIcon))
.snippet("Your last recorded location"));
theMap.animateCamera(CameraUpdateFactory.newLatLng(lastLatLng), 100, null);
CameraUpdate center=
CameraUpdateFactory.newLatLng(lastLatLng);
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
theMap.moveCamera(center);
theMap.animateCamera(zoom);
hope this helped. good luck
i had the same issue these days and you should do the switch for v2 as you can do all the same stuff from v1 easier + many new features.
MapView has been replaced with the new GoogleMap-Objecttype.
A very detailed and decent Tutorial site can be found HERE!

Sample DevicePolicyManager codes from Android 2.2 work?

Im looking for some sample codes to start with thanks, although i can read the api ,this will be a big help for others to help with a simple code.
Taken from the API Demo:
// Password quality spinner choices
// This list must match the list found in
// samples/ApiDemos/res/values/arrays.xml
final static int mPasswordQualityValues[] = new int[] {
DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED,
DevicePolicyManager.PASSWORD_QUALITY_SOMETHING,
DevicePolicyManager.PASSWORD_QUALITY_NUMERIC,
DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC,
DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC
};
Ref: DeviceAdminSample.java

Categories