setVisibility() doesn't refresh UI Android - android-layout

I have a SurfaceView with a LinearLayout which hides or shows if you click on the SurfaceView.
The problem is that the SurfaceView is not refreshed so if the setVisibility(View.VISIBLE) actually doesn't work.
This is the code I'm using:
sv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Log.d("MainActivity-onCreate-onClick-buttons",
buttonsVisibility.toString());
if (buttonsVisibility)
{
linearLayoutButtons.setVisibility(View.INVISIBLE);
buttonsVisibility = false;
} else
{
linearLayoutButtons.setVisibility(View.VISIBLE);
buttonsVisibility = true;
}
}
});
By default buttonVisibility is set to false and the first time I click the view it actually is so so the logic is consistent, the only problem is that the setVisibility seems to have no effect. Only if I turn on/off the screen I can see the layout displayed correctly and then working properly. Why I need to turn the screen off to refresh the UI? Shouldn't it refresh automatically after the setVisibility?
Thank you very much

I don't know why but if I change my Buttons to be ImageButtons it works but buttons appear with a wrong "z-index" wherever I put them. I need to click once on them to fix the "z-index". If I change setVisibility(View.INVISIBLE) with setVisibility(View.GONE) all works fine.
Hope this might be helpful for someone with the same problem.

Related

Can you override the Navigation Controllers 'InteractivePopGestureRecognizer' action?

I'm searching for ways to implement a swipe gesture recognizer which only triggers when you swipe from the outer left side of the screen to the right. We need the gesture to open our custom SideMenu. I tried to use a simple UISwipeGestureRecognizer with the SwipeDirection property set to right, but that gets triggered on every swipe from left to right, no matter what the startpoint of the swipe is.
Ideally, we want the animation of it to look and feel like the InteractivePopGestureRecognizer of a UINavigationController. We are already using a NavigationController, which pushed our MainView over our IntroView. Now, we disable the InteractivePopGestureRecognizer, so you aren't able to go back to the IntroView. That's our problem. If it is possible, we don't want to disable the gesture of the NavigationController, but change the action of it. So the swipe from the far left side of the screen to the right would not pop the current viewcontroller, but open our SideMenu.
Is it possible to override the InteractivePopGestureRecognizer to change the action of it? If this isn't possible, do you have another idea on how to create the exact same gesture recognizer? It has to be possible somehow because many apps only open their SideMenu if your startpoint of the gesture is the left (or right) side of the screen. (e.g. Reddit)
Thanks for any help in advance.
You could use Touch Events and UISwipeGestureRecognizer to do that.
The workaround that is override TouchesBegan method to detect the started point whether fit your needs, and if so add UISwipeGestureRecognizer for View.
SwipeGestureRecognizer rightSwipeGesture;
public override void TouchesBegan (NSSet touches, UIEvent evt)
{
base.TouchesBegan (touches, evt);
UITouch touch = touches.AnyObject as UITouch;
if (touch != null)
{
//code here to handle touch
CoreGraphics.CGPoint swipPoint = touch.LocationInView(View);
if(swipPoint.X < 0.5)
{
rightSwipeGesture = new SwipeGestureRecognizer { Direction = SwipeDirection.Right };
rightSwipeGesture.Swiped += OnSwiped;
View.AddGestureRecognizers(rightSwipeGesture);
}
}
}
public override void TouchesEnded (NSSet touches, UIEvent evt)
{
base.TouchesBegan (touches, evt);
if(null != rightSwipeGesture ){
rightSwipeGesture.Swiped -= OnSwiped;
View.RemoveGestureRecognizers(rightSwipeGesture);
}
}
=============================Update=================================
I found a workaround only use one GestureRecognizer will make it works. You could have a look at UIScreenEdgePanGestureRecognizer. Although it's a Pan gesture, however if you not deal with somethind with the added view, it will work as a swip gesture. In addition, UIScreenEdgePanGestureRecognizer only can work when on the screen edge. You could set the Left edge to handle your needs.
For example:
UIScreenEdgePanGestureRecognizer panRightGestureRecognizer = new UIScreenEdgePanGestureRecognizer();
panRightGestureRecognizer.Edges = UIRectEdge.Left;
panRightGestureRecognizer.AddTarget(() => HandleSwap(panRightGestureRecognizer));
View.AddGestureRecognizer(panRightGestureRecognizer);
private void HandleSwip(UIScreenEdgePanGestureRecognizer panRightGestureRecognizer)
{
Point point = (Point)panRightGestureRecognizer.TranslationInView(View);
if (panRightGestureRecognizer.State == UGestureRecognizerState.Began)
{
Console.WriteLine("Show slider view");
}
}

Keyboard is not shown up after came from android default contact page in my app

I have one button called "Invite Paricipants". When i tap on it, my app will goes to android default contact page. After selecting contact, i am calculating the emails in it. If i found no email then, showing edit text to enter email. There i am using following code
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
editText.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
editText.requestFocus();
but keyboard is not showing up. How can i resolve my problem?
Yes he is right, after coming back the focus to EditText has been lost and by using requestFocus for EditText allows you to have focus and obviously pops up the Keyboard
You have to request input focus for editText.
To do this go to your interface definition xml and add:
<requestFocus />
Inside the definition for editText.

SmartWatch 2 change menu icon on runtime

I would like to change the Icon of a menu item after its clicked. (I am talking about the action menu which deploys on the Action key press)
This is what I have so far:
#Override
public void onMenuItemSelected(final int menuItem) {
if (menuItem == MENU_ITEM_START) {
if(!Started){
Started=true;
**Intent intent = new Intent(Control.Intents.CONTROL_MENU_SHOW);
intent.putExtra(Control.Intents.EXTRA_MENU_ITEM_ID,MENU_ITEM_START);
intent.putExtra(Control.Intents.EXTRA_MENU_ITEM_ICON, ExtensionUtils.getUriString(mContext, R.drawable.menu_item_stop));
sendToHostApp(intent);**
mHandler.postDelayed(RunnableObject, 1000);
}
else{
Started=false;
mHandler.removeCallbacks(RunnableObject);
}
}
}
So, I added the bolded code (between ** and **), to start an intent to "change" the icon in the same way I create the menu the first time, however, that wont work.
Maybe wrong intent: CONTROL_MENU_SHOW?? That is the one I found in the Control Class when initializing the menu.
Any help is appreciated, thanks!!
Nope, there's no way to do it directly. The way you are doing it is the only way.

Is it possible to forbid selection in TextField?

I have a small problem: my text field keeps selecting itself, for example if I alt-tab from and to application.
For my application, text selection is not needed and will not be used - so I want to disallow this annoying behavior. Actually, just setting selection color to transparent or white will work fine.
Is there some way to do this?
The following css fixed the problem for me:
-fx-highlight-fill: null;
-fx-highlight-text-fill: null;
You can disable text selection for key events:
myTextField.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent inputevent) {
if (!myTextField.getSelectedText().isEmpty()) {
myTextField.deselect();
}
}
});
For mouse events you could also use:
myTextField.addEventFilter(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (!myTextField.getSelectedText().isEmpty()) {
myTextField.deselect();
}
}
});
Super late for an answer, but I have a better solution.
Instead of disabling the style for selected text or managing mouse events, it's better to directly manage the selectedTextProperty().
The advantages of this solution are:
The selection properties will always be empty selection
Double clicks are also covered, not only mouse drag events
The code...
textField.selectedTextProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue.isEmpty()) textField.deselect();
});

Extend View and Custom Dialogs

I am working on a game where I am extending the view and performing operations in the class. I need to have a popup in the game which will have 3 buttons inside it. I have managed to have the popup show-up using custom dialogs but when I configure the onClick as follows:
private void popUp() {
Context mContext = getContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_fullimage_dialog);
dialog.setTitle("Cheese Market");
Button one = (Button)findViewById(R.id.firstpack);
one.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cheeseLeft = cheeseLeft + 10;
masterMoveLock = false;
return;
}
});
}
It force closes giving a nullpointerexeption even though it is defined in the custom_fullimage_dialog layout.
Can someone help me figure out how to have the button click detected in this scenario?
Thank you.
Try calling dialog.findViewById instead.
You're setting the contentView for the dialog, but by calling findViewById you're looking for it under your activity's content view.

Resources