Android: Z-Order with a screen-wide clickblocker-view - android

Edit: Solved. The problem disappeared when I made all my custom views override Button instead of View.
I'm trying to programmatically create a screen-sized invisible view which should capture all click events, thereby blocking all underlying elements. But I'm having some trouble.
The intended goal is that when the user clicks a certain button, a menu is created. While this menu is up, the other buttons that were on the page should no longer function. Clicking on the menu should make stuff happen, clicking anywhere else should make the menu disappear and make the other elements work again.
I've managed to create the screen wide view just fine, and it captures clicks and destroys the menu whenever the user clicks a point that is not already covered by a button. When the user does click on a spot that contains a button however, only the button's onclickevent is handled, and not that of my clickblocker. It should be the other way around.
This leads me to think it's a z-order problem (but not 100% sure).
Unfortunately I'm trying to target older versions, which don't have support for view.setZ(), and view.bringToFront() doesn't appear to do what I want it to do.
Here's the code for the viewblocker:
public class ClickBlockerView extends View{
public ClickBlockerView(Context context) {
super(context);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(C.GetWindowWidth(getContext()), C.GetWindowHeight(getContext()));
}
public static ClickBlockerView CreateClickBlocker(Context context){
ClickBlockerView c = new ClickBlockerView(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
c.setLayoutParams(params);
c.setId(R.id.clickBlocker);
c.setClickable(true);
return c;
}
}
And this is how I'm calling it (from inside another view):
private void createMenuAndClickblocker(){
ClickBlockerView clickblocker = ClickBlockerView.CreateClickBlocker(getContext());
clickblocker.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
destroyMenu();
}
});
((RelativeLayout) this.getParent()).addView(clickblocker);
clickblocker.bringToFront();
createMenu();
requestLayout();
}
Does anyone have some idea how to fix this? Other solutions to the problem are welcome too. I'd rather not have to use xml though (it would have to be added to every activity)
Update:
Apparently, the z-ordering goes fine with spinners and checkboxes, but not with buttons and my custom views. Odd...
Update 2:
When drawn, the shape is shown to correctly fill up the screen, but it is also drawn below the other custom views, even though it is created later and brought to the front.
Strangely enough, the click functionality has meanwhile completely stopped functioning, even though I didn't change anything in the code for this clickblocker.

Related

Android - Memory leak with popup window in Recyclerview

I'm having trouble with the following error displaying in my app:
"E/WindowManager: android.view.WindowLeaked: Activity com.awt.myapp.MyList has leaked window android.widget.PopupWindow$PopupDecorView{84fdb1f V.E...... .......D 0,0-369,120} that was originally added here..."
Basically I've got a recyclerview and in the adapter I have a bunch of textviews in each row and am binding click listeners to them, as clicking one of these textviews brings up a popup window. The problem is if I hit the Android back button while a popup is still visible the above error appears.
I understand that in my activity that holds the recyclerview I can add an 'onBackPressed()' method, but from here I'm not sure how to get a reference to any of the popup windows within the adapter (and close it at this stage) as I believe this is what I need to do.
Below is my click listener code, I've experimented with some options and having setFocusable just causes the back button to stop working so not sure if that's needed.
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View moreInfoView) {
myPopupWindow.setBackgroundDrawable(new ColorDrawable());
//myPopupWindow.setFocusable(true);
myPopupWindow.setTouchable(false); // Ignores taps
myPopupWindow.setOutsideTouchable(true); // Disappear when tapping anywhere on screen
int position = -tv.getHeight();
myPopupWindow.showAsDropDown(tv, 0, position);
((MyList) context).onToggleMoreInfo("show");
myPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
((MyList) context).onToggleMoreInfo("hide");
}
});
}
});
Hopefully this makes sense, if you need any more info let me know. Any advice would be appreciated.
If you create a listener on your adapter that implements the activity and calls it when you click on the item, you can export popup window logic to activity and override on back pressed to dimiss it.

Don't show transition if view is not visible

I have a list of products, if I click on one, the image of the product is transitioned into the detail screen.
And if I go back, the image is transitioned back to the list.
This works fine.
The problem is that when I scroll down in my detail screen, the image is no longer visible.
But when I go back to the list screen the image is still transitioned, resulting is a buggy transition.
Video example here
I want to achieve something like the Play Store
Where there is no return animation if the image is no longer visible.
Code
Starting detail activity:
Intent intent = new Intent(getActivity(), ProductDetailActivity.class);
intent.putExtra(ProductDetailActivity.EXTRA_PRODUCT, product);
Bundle options = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(),
productViewHolder.getProductCover(), productViewHolder.getProductCover().getTransitionName()).toBundle();
getActivity().startActivity(intent, options);
In DetailActivity I set the transition name:
coverImageView.setTransitionName(getString(R.string.transition_key_product_cover_with_id, product.getId()));
styles.xml:
<item name="android:windowContentTransitions">true</item>
Any idea how to implement the behaviour I want to achieve?
With the following link you can know if a view inside an scroll is visible or not: https://stackoverflow.com/a/12428154/4097924
Then you can make a simple method to know if your imageView is visible inside the scrollview, similar to this easy example:
public boolean isVisibleInsideScroll(){
Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (imageView.getLocalVisibleRect(scrollBounds)) {
// Any portion of the imageView, even a single pixel, is within the visible window
return true;
} else {
// NONE of the imageView is within the visible window
return false;
}
}
Then I see two possible options that I have not proved:
option 1: overwrite the method onBack (and every way to go back if you have another one). Inside the method, you can assign the transition when the element it is visible before leaving the screen:
#Override
public void onBackPressed(){
if(isVisibleInsideScroll()){
coverImageView.setTransitionName(getString(R.string.transition_key_product_cover_with_id, product.getId()));
}
super.onBackPressed();
}
option 2: overwrite the method onScroll (and every time the scrollview is scrolled) you can register or unregister the animation if the view is visible.
The code in this option is similar to the previous one.
Good luck! I like a lot your animation, I saw it in youtube. :)

What is the best way to use multiple layouts with selective back button?

I want to use the Android device back/return button to switch to a previous layout in my application, but only for specific layouts. All other cases should use the buttons normal close application function. This is to save space on the screen for an obvious "go back to the screen before this" button.
So far I came up with this simple solution:
int screenid=0;
public void ButtonClickN(View v)
{
setContentView(R.layout.ScreenX);
screenid=3;
}
public void onBackPressed()
{
if(screenid==2) // screen z
{
setContentView(R.layout.ScreenY);
screenid=1;
return;
}
if(screenid==3) // screen x
{
setContentView(R.layout.ScreenZ);
screenid=2;
return;
}
finish(); // all other cases button works as normal
}
But I feel that this is not the best way to do this, as there might be something inbuilt to make it even simpler. Say, comparing layouts by name to remove the integer, or sliding the layouts from left to right...
Another way that works is to use Intent to make the new screen in another activity. It draws the new layout over the older one:
public void ButtonClickN(View v)
{
Intent i3 = new Intent(view.getContext(), ActivityX.class);
startActivityForResult(i3, 0);
}
This makes the Android back button work as before, but requires making a new class for every screen
public class ActivityX extends Activity
which onCreate calls:
setContentView(R.layout.ScreenX);
and requires additional declaration in the AndroidManifest.xml
<activity android:name=".ActivityX"></activity>
Clearly too much of a hassle in comparison to the first solution. It also generates an overlapping effect in the emulator, which I want to avoid. So far the effect doesn't appear on my Samsung Galaxy Y (GT-S5360), but who knows what it will be on other devices.
So my question, is there a better/simpler way to do selective use of the Android back button? Or switch between fullscreen layouts more efficiently?
Evidently keeping ID integer of which screen is currently in use the simplest solution.

How to set the Background Color of a View in onRestoreInstanceState()

I'm currently trying to learn to work with Views and states. I'm normally able to set its color in functions like in the following:
View mColorRegion = findViewById(R.id.color_region);
mColorRegion.setBackgroundColor(Color.CYAN);
However, I can't seem to be able to set the color in an onRestoreInstanceState(), as
mColorRegion.setBackgroundColor(savedInstanceState.getInt("color"));
However, working with the same View as a TextView, I'm able to restore text as in the following:
TextView mText = (TextView)findViewById(R.id.color_region);
mText.setText(savedInstanceState.getString("text");
What's the difference, and how I can set the background color in onRestoreInstanceState()?
EDIT: Since the original post, I've noticed two things:
1) mColorRegion.setBackgroundColor(Color.CYAN) doesn't seem to work in onCreate() either.
2) Even though the following function correctly changes the View color when a button is pressed, it doesn't work if I call it directly from onRestoreInstanceState():
public void centerButton1(View clickedButton) {
mColorRegion.setBackgroundColor(Color.CYAN);
}
Hmm...
So I found a "half-solution". If you add the following line to AndroidManifest.xml, it will preserve the color during orientation changes:
android:configChanges="orientation|screenSize"
However, this still doesn't answer why I can set the text but not the color in onRetoreInstanceState() or onCreate()...
Many people frown upon using android:configChanges="orientation|screenSize", like Google's Dianne Hackborn. For one, It will make the process of switching between multiple layouts for your app very difficult (for example, if you want one layout for landscape, and one for portrait) since you'll have to do all of the work that Android normally does automatically for you, in onConfigurationChanged().
Anyway, I also had this sort of problem. I was creating a DialogPreference, and upon rotation I couldn't change the progress of a SeekBar in onRestoreInstanceState(savedInstanceBundle)...so this is what I suggest if you cannot use onCreate(savedInstanceBundle) or onActivityCreated(savedInstanceBundle) (for fragments) to restore the state of your view objects:
1) Make a private class member called "mRestoredBGColor",
private int mRestoredBGColor = -1;
2) In onRestoreInstanceState():
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
mRestoredBGColor = savedInstanceState.getInt("key_you_used_in_onSaveInstanceState");
}
3) Then in onResume(), because onRestoreInstanceState() is called after onStart(), but before onResume()
#Override
public void onResume(){
super.onResume();
if(mColorRegion != null && mRestoredBGColor != -1){
mColorRegion.setBackgroundColor(mRestoredBGColor);
mRestoredBGColor = -1; //to make sure it only sets this once per rotation.
}
}
Hope this helps somebody. I believe there is always another way (except when you want quickly rotating WebViews...) than using android:configChanges="orientation|screenSize".

android button not clickable while playing animations

I have a button and while this button is playing an animation, I'm not able to click on button. I've set click listener and touch listener but in debug mode it's not entering in OnClick and in onTouch methods. Do you know why? Thanks
edit: I've tried something like:
AsyncTask task = new AsyncTask() {
#Override
protected Object doInBackground(Object... objects) {
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast toast = Toast.makeText(MyActivity.this, button1.getText(), Toast.LENGTH_SHORT);
toast.show();
}
});
return null;
}
;
};
task.execute(button1);
but it's not working
Edit: here is the full source code
Before Android 3.0, using any of the animation classes only changes where the view is drawn - it won't adjust its touchable-bounds during (or after) the animation:
http://android-developers.blogspot.com/2011/02/animation-in-honeycomb.html
You could:
Make a ViewGroup which moves its children every onDraw
Override your View's onDraw to move itself - Could use margin or padding, or position (if view is in a FrameLayout or something similar).
Only use 3.0+ devices
Override the parent's onTouch (or onInterceptTouchEvent), calculate where the View is being drawn (you can get how far into and the offset from real position from the Animation *) and handle accordingly... * Looking at your code (since you generate a random direction each time it finishes), this might not be possible without tracking which directions you've previously take..
you are facing same issue that i was recently ... when you apply animation on button or any other view and use setFillAfter(True) it means that the image of view is moved not the actual view thats why its not listening to your click listener because its just image of view not your actual view you have to do something like that i explained in answer to my own question according to your situation... means you have to also move the actual view on the end place of animation and use setFillAfter(false) so that when you click after anmation then it should be an actual view not just image used for animation purpose by android
check this link....
EditText stucks after animation and alive back on scrolling......?
In your code use setFillafter(false) and actually place your button at end position of animation by somehow like setting margin or according to your layout use appropriate properties for placement. By Applying these changes your click listener will work perfectly.
==> if you are trying that your button's click listener work while its moving (being animate) then as far as i know its not possible because android uses just image of your view to perform animation not the actual.
This might be a threading problem. You should read Event dispatch thread and how to do thing in android async by reading painless threading and take a look at AsyncTask JavaDoc.
In short: the main thread should not be blocked, since it is used for responing to UI events, such as button presses. Therefore, whenever you do something that take more than some milliseconds, you should do it asynchroniously in another thread.
I think there are two things
1)You can handle one event at one time for one object.Like animation is playing on you button that means you can not click on that untill one work get completed(As i understand you animation is on button not on other part of screen)
2)Second if you have playing on rest part of screen and you want to stop it on click of button.Then i think its a problem of focus.once set onfoucslistener to button and check that when you are clicking it Is it getting focus?
I would put the animation into the async task and then the button click should be handled normally on the main thread I think. Because the way you do it at the moment is: The animation starts and blocks the main thread. This means that the click event can't be excecuted in the async task
You must try to use AsyncTask . Programming Android without it would be and horrible usability serious problem.
See this link for how use an asynctask.
Here an example:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Initialisation of the Activity
MyLongTask task = new MyLongTask();
task.execute("http://blog.fr4gus.com/api/test.json");
}
#Override
protected void onPause() {
// If you wanna make something in the pause of the Asynctask
}
class MyLongTask extends AsyncTask<String, Void, Void>{
#Override
protected void onPreExecute() {
// Before executing what you want to execute
}
#Override
protected Void doInBackground(String... params) {
// what you want to execute come here :D
return null; // Or whatever you want pass to onPostExecute
}
#Override
protected void onPostExecute(Void result) {
// Here we can update for example the UI with something (who knows :? )
}
}
}
MODIFIED
You must always extend the Asynctask Activity , since you are trying to pass params through it , you must define them in the head of your extended class :
class MyLongTask extends AsyncTask<String, Void, Void>{ ...
Then first is the doInBAckground param , next is the onPreExecute param , and the last is the onPostExecute param . That way you can send the params like a Button.
You can learn more about here or in my first link.

Categories

Resources