clicklisteners in a customview after onConfigurationChanged - android

I have this custom view that is being added to the WindowManager as a System Alert which should function as a fullscreen overlay to block certain parts of the phone when the app is running.
This works great when the overlay is shown it blocks the phone window fullscreen. Now I want to support rotation changes.
This part also works fine, I have 3 layout files in layout layout-land and layout-h600dp-land and when I rotate the phone it changes to the correct layout. The problem I have with this is after onConfigurationChanged(Configuration newConfig) is called and I inflate the layout again all the click listeners are gone so none of the buttons in the views react to clicks. The button's pressed state does change when I tap on them but none of the onClickListeners are being triggered. Before the orientation change all the buttons do work. I have been using Butterknife to reduce the boilerplate code for onClicklisteners and findViewById's but I already changed that to findViewById and adding a clicklistener on the view manually which doesn't make a difference.
And now for some code. The service adds the view to the windowmanager
public class OverlayService extends Service {
public void showOverlay() {
startForeground();
mUiHandler.post(new Runnable() {
#Override
public void run() {
if (!DrivingOverlay.isShowing()) {
if (mOverlay == null) {
mOverlay = new Overlay(OverlayService.this);
mOverlay.setTag(OVERLAY_TAG);
mOverlay.setId(R.id.overlay);
}
addView(mOverlay);
}
});
}
private void addView(#NonNull final View view) {
try {
final WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
windowManager.addView(view, Overlay.LAYOUT_PARAMS);
} catch (IllegalStateException error) {
Log.e(TAG, Log.getStackTraceString(error));
}
}
}
The Custom View which is the overlay.
public class Overlay extends FrameLayout {
private static boolean sIsOverlayShowing;
public static final WindowManager.LayoutParams LAYOUT_PARAMS = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
public Overlay(Context context) {
super(context);
init(context);
}
public Overlay(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public Overlay(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public Overlay(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
protected void init(Context context) {
inflate(context, R.layout.driving_overlay, this);
if (!isInEditMode()) {
ButterKnife.bind(this);
}
}
#Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d(TAG, "onConfigurationChanged");
init(getContext());
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
setIsOverlayShowing(true);
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
setIsOverlayShowing(false);
}
#Override
public void onViewAttachedToWindow(View v) {
setIsOverlayShowing(true);
}
#Override
public void onViewDetachedFromWindow(View v) {
setIsOverlayShowing(false);
}
public static boolean isShowing() {
return sIsOverlayShowing;
}
public static void setIsOverlayShowing(boolean isOverlayShowing) {
sIsOverlayShowing = isOverlayShowing;
}
}

Finally figured it out.
Calling inflate(context, R.layout.driving_overlay, this) in the init adds the view that is being inflated to the root view in this case to Overlay which is a FrameLayout so each rotation was inflating a new layout and adding it to the root view so after the a rotation the Overlay class had more than 1 child view. The OnClickListeners would bind the child views button's at position 0 to the OnClickListeners in the class and it would show the child view that was newly inflated at position 1 or higher. So after adding this a check and removing obsolete views the OnClickListeners are working again.
protected void init(Context context) {
if (getChildCount() >= 1) {
removeViewAt(0);
}
final View view = inflate(context, R.layout.driving_overlay, this);
if (!isInEditMode()) {
ButterKnife.bind(this, view);
}
}

Assuming you have not restricted the Restarting of activity due to change in
the orientation. When a device s configuration gets changed in your case
Orientation, its user interface has to be updated according to the new
configuration.
Being the primary component for interaction, it can be updated with some
attributes to handle changes. Default behavior of Activity when device gets
rotated is it gets destroyed and restarted again. This may be your issue.
To handle the rotation of activity use android:configChanges = "orientation"
attribute in manifest for your activity.

Related

android app run onPreDraw infinitely

Step 1. Create a new app with Navigation Drawer template.
Step 2. Add a customized button and override the onMeasure method.
#CoordinatorLayout.DefaultBehavior(MyButton.Behavior.class)
public class MyButton extends android.support.v7.widget.AppCompatButton {
private ViewTreeObserver.OnPreDrawListener mPreDrawListener;
public MyButton(Context context) {
super(context);
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mPreDrawListener == null) {
mPreDrawListener = new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
offsetTopAndBottom(50);
return true;
}
};
ViewParent p = getParent();
if (p instanceof View) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
((View)p).getViewTreeObserver().addOnPreDrawListener(mPreDrawListener);
}
}
}
}
public static class Behavior extends CoordinatorLayout.Behavior<MyButton> {
#Override
public boolean onLayoutChild(CoordinatorLayout parent, MyButton child, int layoutDirection) {
final List<View> dependencies = parent.getDependencies(child);
return super.onLayoutChild(parent, child, layoutDirection);
}
#Override
public void onAttachedToLayoutParams(#NonNull CoordinatorLayout.LayoutParams lp) {
if (lp.dodgeInsetEdges == Gravity.NO_GRAVITY) {
// If the developer hasn't set dodgeInsetEdges, lets set it to BOTTOM so that
// we dodge any Snackbars
lp.dodgeInsetEdges = Gravity.BOTTOM;
}
}
}
}
Step 3. Use MyButton in app_bar_main layout.
Step 4. Set a breakpoint in onPreDraw and then I could see it will be executed infinitely. If I comment the offsetTopAndBottom(50), everything goes fine.
I also trace the source code and find app receive vsync signal again and again which cause the onVsync function in Choreographer.java run infinitely. Why this happens?
Update
If I set a breakpoint as below and comment onPreDraw, this breakpoint finally will not be reached, otherwise, it can be reached always.
The callback onPreDraw() is called before each frame is drawn. Since you normally keep on drawing frames (~60fps), it is normal that it is called "infinitely".
To avoid this behavior, the usual pattern is to remove the listener as first statement in onPreDraw():
view.getViewTreeObserver().removeOnPreDrawListener(this);
where view is the downcasted parent in your case.
You can see example code in this video. The engineers are part of the Android Framework team.

TextInputLayout.setError() leaves empty space after clearing the error

I recently used TextInputLayout and it's setError() method. The problem I'm getting is, when I clear the error by calling setError(null) it leaves so much of empty space at the bottom.
Normal:
With error:
After clearing error:
After looking at the source, I found that they are making the view INVISIBLE instead of GONE
.setListener(new ViewPropertyAnimatorListenerAdapter() {
#Override
public void onAnimationEnd(View view) {
view.setVisibility(INVISIBLE); // here it is
updateLabelVisibility(true);
} }).start();
I'm wondering why is it so? How to resolve this to avoid the empty space?
Check out the docs for
public void setErrorEnabled (boolean enabled)
It says
Whether the error functionality is enabled or not in this layout.
Enabling this functionality before setting an error message via
setError(CharSequence), will mean that this layout will not change
size when an error is displayed.
Well based on this, try setting setErrorEnabled(true) before setError(), and, set setErrorEnabled(false) after setError(null).
Method setErrorEnabled(false) will clear the extra space, so call it after setError(null).
Dont use setErrorEnabled(boolean), it just doesnt show up the error from the second time.
public class MyTextInputLayout extends android.support.design.widget.TextInputLayout {
public MyTextInputLayout(Context context) {
super(context);
}
public MyTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void setError(#Nullable CharSequence error) {
super.setError(error);
View layout = getChildAt(1);
if (layout != null) {
if (error != null && !"".equals(error.toString().trim())) {
layout.setVisibility(VISIBLE);
} else {
layout.setVisibility(GONE);
}
}
}
}
Then just setError(errorMessage); or setError(null);
See this page. Google will release the fix in future support library version. It says,
If you want to fix it now you can extends the TextInputLayout and
override the setErrorEnabled() method, but I cant guarantee the
backward compatibility. Because its some danger to change state in
TextInputLayout.
public class TextInputLayout extends android.support.design.widget.TextInputLayout{
public TextInputLayout(Context context) {
super(context);
}
public TextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void setErrorEnabled(boolean enabled) {
super.setErrorEnabled(enabled);
if (enabled) {
return;
}
if (getChildCount() > 1) {
View view = getChildAt(1);
if (view != null) {
view.setVisibility(View.GONE);
}
}
}
}
I create a custom view for avoiding repeated code and override setError method.
public class UserInputView extends TextInputLayout {
public UserInputView(Context context) {
this(context, null);
}
public UserInputView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public UserInputView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void setError(#Nullable CharSequence error) {
boolean isErrorEnabled = error != null;
setErrorEnabled(isErrorEnabled);
super.setError(error);
}
}
The source code of TextInputLayout show the following:
If you need to clear the error, just use
til.setErrorEnabled(false);
This will hide the error text and stretch the bottom space to its standard size.
In case you need to set the error again, just use
til.setError("Your text");
which automatically calls til.setErrorEnabled(true) as it assumes you need the error functionality.
This is extension in kotlin solving problem:
fun TextInputLayout.clearError() {
error = null
isErrorEnabled = false
}
The following code works fine
textInputLatout.getEditText().addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() < 1) {
textInputLayout.setErrorEnabled(true);
textInputLayout.setError("Please enter a value");
}
if (s.length() > 0) {
textInputLayout.setError(null);
textInputLayout.setErrorEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
By using mTextInputLayout.setErrorEnabled(false); i have solved this problem
Then you should override it like so:
#Override
public void onAnimationEnd(View view)
{
view.setVisibility(GONE); // <-- this is where you make it GONE
updateLabelVisibility(true);
}
Or try this i.e. on a button or whatever you are using:
final Button btn = (Button) findViewById(R.id.btn);
btn.setVisibility(View.GONE); //<--- makes the button gone

VisibilityAwareImageButton Usage

I was recently working with ImageButtons and I came across this new type of ImageButton 'VisibilityAwareImageButton'. It would be really helpful if someone could tell me the usage of this ImageButton and how is it different from the regular ImageButton? Thanks in advance :)
Here's the full source for VisibilityAwareImageButton.
class VisibilityAwareImageButton extends ImageButton {
private int mUserSetVisibility;
public VisibilityAwareImageButton(Context context) {
this(context, null);
}
public VisibilityAwareImageButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public VisibilityAwareImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mUserSetVisibility = getVisibility();
}
#Override
public void setVisibility(int visibility) {
internalSetVisibility(visibility, true);
}
final void internalSetVisibility(int visibility, boolean fromUser) {
super.setVisibility(visibility);
if (fromUser) {
mUserSetVisibility = visibility;
}
}
final int getUserSetVisibility() {
return mUserSetVisibility;
}
}
It appears to be almost exactly the same as a regular ImageButton, only it keeps track of the last visibility actually set by the user. The only usage I can find is in the FloatingActionButton source. It is used to keep track of what the user wants the visibility of the view to be while it does it's own internal changes and animations. i.e.
if (child.getUserSetVisibility() != VISIBLE) {
// The view isn't set to be visible so skip changing it's visibility
return false;
}
It's in the design support library and has package visibility, so it seems like Google intends on using it internally (and seemingly only for the FAB implementation at this time).

There is a way to start contentScrim animation sooner on CollapsingToolbarLayout?

In this example the contentScrim attribute is set with a color, but I can't figure out how to control when it starts. I woud like to start the color transition sooner.
Can you give me a hint? Thanks in advance.
You'd have to create a class that extends CollapsingToolbarLayout. Something like this (you might need to adjust that so it exactly fits your needs):
public class CustomCollapsingToolbarLayout extends CollapsingToolbarLayout {
public static interface Listener {
public void onContentScrimAnimationStarted(boolean showing);
}
private Listener mListener;
public CustomCollapsingToolbarLayout(Context context) {
super(context);
}
public CustomCollapsingToolbarLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomCollapsingToolbarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void setScrimsShown(boolean shown, boolean animate) {
super.setScrimsShown(shown, animate);
if (animate && mListener != null) {
mListener.onContentScrimAnimationStarted(shown);
}
}
public void setListener(Listener listener) {
mListener = listener;
}
}
And just call setListener on your CustomCollapsingToolbarLayout instance.
CustomCollapsingToolbarLayout mToolbarLayout =
(CustomCollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
mToolbarLayout.setListener(new Listener() {
#Override
public void onContentScrimAnimationStarted(boolean showing) {
//do what you want
}
});
EDIT (actually answering the question):
Modify the scrimVisibleHeightTrigger value (with the setScrimVisibleHeightTrigger method of the CollapsingToolbarLayout) to change the starting point of the animation.

Animate adding child views from addView() method

I'm trying to animate adding child views to a LinearLayout. Just wanted to know if there is a better way to go about this or if this approach have any drawbacks.
And if this post helped don't forget to up vote :)
Here is what I did:
public class CardLayout extends LinearLayout {
public CardLayout(Context context) {
super(context);
initLayoutObserver();
}
public CardLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initLayoutObserver();
}
#Override
public void addView(View child) {
super.addView(child);
Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_up_in);
animation.setStartOffset(getChildCount() * 100 + getResources().getInteger(R.integer.screen_transition_time_fade_in));
child.startAnimation(animation);
}
}

Categories

Resources