Android : setOverlapAnchor before API 23 - android

My activity has a popup window defined like this :
this.navigationMenuPopupWindow = new PopupWindow(View.inflate(this, R.layout.popup_navigation, null), ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
this.navigationMenuPopupWindow.setWidth((int) this.getResources().getDimension(R.dimen.navigation_popup_width));
this.navigationMenuPopupWindow.setBackgroundDrawable(this.getDrawable(R.drawable.main_bg_navigation_menu));
this.navigationMenuPopupWindow.setElevation(43);
this.navigationMenuPopupWindow.setFocusable(true);
this.navigationMenuPopupWindow.setOutsideTouchable(true);
I would like to position the popup under the anchor. So I naturally tried to add this line :
this.navigationMenuPopupWindow.setOverlapAnchor(false);
Unfortunately, it requires the API 23.
How can I achieve the same result but that it is compatible with the API 21?
I think I can resolve this by modifying my app style, but I don't know exactly how. By the way, this can affect all the PopupWindows of the app, it's not a problem I only have that one. Thank you for your help!

You need to use PopupWindowCompat.
PopupWindowCompat.setOverlapAnchor(this.navigationMenuPopupWindow,true);
Be aware that the PopupWindowCompat.setOverlapAnchor has no implementation before API 21.

Related

How to show popup window over dialog Android

I need help. I want to show a popup window over dialog.
On API 23 i did it. But on API >23 i can do this. See images.
API > 23
API < 23
Thanks for help.
#TuNguyen Not sure if you're still looking for an answer but for people who're still facing this: I resolved it using the (WindowLayoutType) for the popupWindow. Adding this works:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
popupWindow.windowLayoutType = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG
//Refer to the android's doc I attached above for the complete list of WindowLayouts
}
//I am using Kotlin

Android - setLayoutDirection in API 15 and API 16

Does anyone know if ViewCompat.setLayoutDirection works in API level below 17?
I search for a solution all over the internet but I can't find a solid one.
setLayoutDirection was added to in API Level 17 (read here) which is 4.2 so the older versions do not support that. So keep two layouts for higher and below and you need to work around for the below api level XML view.
If you're using the support library, you can do the following:
if (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL) {
// view -RTL layout
} else {
//view - set made layout for below apis
}
BidiFormatter might help you depending on your requirement.Check that as well.
Every one knows about android:layout_gravity="end" right? can be helpfull in this case

Setting a button background resource with a .png from internal storage

I have a button that I want to set the background of using a png file from internal storage. For android api 16 and up, this works fine:
filePath = getActivity().getFileStreamPath(colorCodes.get(i-1));
temp.setBackground(Drawable.createFromPath(filePath.toString()));
When running on an android tablet with 4.0.4, this part crashes the app with a nosuchmethod error (setBackground). After a little research, I see that setBackground is only available for api 16+. After looking around on SO and a few other places, it looks like I need to use setBackgroundDrawable (deprecated) or setBackgroundResource. I tried this:
filePath = getActivity().getFileStreamPath(colorCodes.get(i-1));
if (android.os.Build.VERSION.SDK_INT < 16) {
temp.setBackgroundDrawable(Drawable.createFromPath(filePath.toString()));
} else {
temp.setBackground(Drawable.createFromPath(filePath.toString()));
}
When logging it out, it shows that setBackgroundDrawable is running and not setBackground, but I get the same nosuchmethod error (setBackground).
The other option is setBackgroundResource, but it accepts an int and not a drawable. Can I convert from drawable to int for this purpose?
What can I do here to set the background of the button to a file in internal storage for APIs < 16?
Thanks.
***EDIT - ok, this is working. just missed a little part elsewhere in the code that had the same problem. However, is using a deprecated method really the only way?
Deprecation is a status applied to a computer software feature,
characteristic, or practice indicating it should be avoided, typically
because of it being superseded. The term is also sometimes used for a
feature, design, or practice that is permitted but no longer
recommended in other areas, such as hardware design or compliance to
building codes. (source link)
Now we can answer your question.
Before API level 16 there is a method named setBackgroundDrawable. After API Level 16 google decided to write a new method setBackground for same purpose and recommend us to use new method. (Reason of this may be found by googling.)
You can use setBackgroundDrawable method for all api levels. There aren't any constraint for this. But using new method setBackground is recommended after API Level 16.
But you can only use setBackground method for devices which is running on API Level 16 or higher. So if you only implement setBackground method in your code, you are going to get MethodNotFoundException for devices which run below API Level 16.
To sum up; it is a best practice(for me it is a must) to use new methods then deprecated ones with supportted api version check such as;
if (android.os.Build.VERSION.SDK_INT < 16) {
temp.setBackgroundDrawable(Drawable.createFromPath(filePath.toString()));
} else {
temp.setBackground(Drawable.createFromPath(filePath.toString()));
}
I am not quite sure whether it is the only way to achieve this but in my opinion it is the correct one. Because the annotation #Deprecated defines the method to be superseded (in most cases) it automatically implies you can (I would even say should) use it to address older versions which are the targeted versions of this method.

TextView.getAlpha doesnt exist in Android

I want to get current alpha of textview i am using the following code but i am getting an error that
java.lang.NoSuchMethodError: android.widget.TextView.getAlpha
Please guide me.
This method since API Level 11. Check your API version.
View.getAlpha() only exists since API level 11. You are trying to running your code on a too-old version of Android.
If you absolutely require this functionality, then update your app's minSdkVersion in AndroidManifest.xml to prevent it running on older Android versions. If you can live without it, do a runtime check to see if the API level is high enough.
you can use Alpha method like below
Textview tv_password;
tv_password =(TextView) findviewById(R.id.tv1);
tv_password.getBackground().setAlpha(50);
you can't use getAlpha() method with Textview
View.getAlpha() available from API level 11.But if you want to use this on older api then you may use NineOldAndroids library available for using the Honeycomb (Android 3.0) animation API on all versions of the platform back to 1.0!
So use need to change for e.g.
mAlpha = mView.getAlpha();
to
mAlpha = ViewHelper.getAlpha(mView);
where mView is your view.
Note : Don't forget to import com.nineoldandroids.view.ViewHelper;

Hide time in Android notification without using custom layout

In the bottom right corner of a standard Android notification is time (eg 12:00). Can I hide it without using custom notification layout?
For API level 17 and onward, the above accepted answer by Erwan is correct.
For historical purposes, the answer below remains
It is possible to do that.
Try setting when to 0.
For the few notifications that don't do that, it looks like thats what they do.
Here's the general API reference.
Here's a link to the ADB notification and search for private void updateAdbNotification()...
From API Level 17 onwards, you can use setShowWhen(false)
public Notification.Builder setShowWhen (boolean show)
Added in API level 17 Control whether the timestamp set with setWhen
is shown in the content view.
http://developer.android.com/reference/android/app/Notification.Builder.html#setShowWhen(boolean)
For API level 17 and onward,
mBuilder.setShowWhen(boolen);
1.Use Hide Notification Time
mBuilder.setShowWhen(false);
2.Use Show Notification Time
mBuilder.setShowWhen(true);

Categories

Resources