I am using a swipe button from com.ebanx:swipe-button library in my application and I wish to change the state of the swipe button to enable (based on the information recieved via another Bluetooth device) when I open the button's activity. ie: Without any user input I have to change swipe button's state to enable !
You can use toggleState()
SwipeButton mSwipeButton; = findViewById(R.id.my_swipe_button);
mSwipeButton.toggleState();
if you use an older version where toggleState is not available, use collapseButton(); or expandButton(); to collapse or expand the swipe button
There are two issues with the library you're using, first is coding bug, second is wrong documentation, but that's not the case.
to make the button active:
SwipeButton swipe_btn = findViewById(R.id.swipe_btn);
swipe_btn.setEnabled(true);
now by default, the button state is closed and you can change that in the xml file i.e the layout where you created the button, you will see something like below:
<com.ebanx.swipebtn.SwipeButton
app:initial_state="disable" //change to enable will make button open by default
app:has_activate_state="true"
/>
Finally to monitor the state of the button, you will have to listen to the state changes like below:
swipe_btn.setOnStateChangeListener(new OnStateChangeListener() {
#Override
public void onStateChange(boolean active) {
Toast.makeText(MainActivity.this, "IS "+active, Toast.LENGTH_LONG).show();
}
});
if active, the button is open, else it's close.
Note: When I say open, I mean the button is toggledOn, and when I say close, it means the other way round(toggleOff).
The bug here is that when you use swipe_btn.toggleState(); The button will be deactivated, meaning it will not even respond to click event which is not right, so the way around is to use the onStateChangeListener as I use it above so that when the button is open you can do something and when it's close you can still do anything.
Note: library version: 'com.ebanx:swipe-button:0.8.3'
Related
I want to integrate an android back button functionality in appcelerator for a parent window and its subsequent child views. I have just a single window in the entire project. Other screens are the children of this parent window. Any suggestions?
There are 2 cases you might be interested in:
Over-ride the default behaviour of the back button press.
$.window.onBack = function () {
// run your code like change views or whatever you like
};
Use Window's onBack property to attach back button callback.
It will have you take control of what should happen when back button is pressed on that window.
If you just want to listen to back button press along with default behaviour of back button press, use Window's androidback event.
$.window.addEventListener('androidback', function (e){});
Note that this method will still close the window you are on, but will allow you to run some code on back button press.
I believe you are looking for method 1.
Note: Method 1 is only available after or > SDK 5.5.1.GA. It was a breaking change in 6.0.0.GA. So do read docs properly & put some tests on their functionality.
Added Code sample to simulate back button feature.
Alloy.Globals.trackingArray = [];
function addNewView(_controllerName, _args) {
var newView = Alloy.createController(_controllerName, _args).getView();
$.window.add(newView);
// add new view in tracking array.
Alloy.Globals.trackingArray.push(_controllerName);
}
// now use something like this whenever you remove any view using backpress
function onBackPress() {
// remove lastly added view
Alloy.Globals.trackingArray.pop(_controllerName);
// add last opened view to simulate back button feature
var lastAddedView = Alloy.Globals.trackingArray[Alloy.Globals.trackingArray.length - 1];
var tempView = Alloy.createController(lastAddedView).getView();
$.window.add(tempView);
}
This is the basis of how you can manage this feature. But do note a point here that you will need to take care of managing arguments passed to while creating a new view or controller. There are multiple ways to do it & it depends on your coding style. :)
I am trying to work out how to show the "up" arrow in Xamarin.Forms without a pushing a page onto the stack. I.E. I just want to perform an action when the back button is pressed. I am completely stuck on this so any help would be appreciated.
I have tried creating a custom renderer which handles a view property called DisplayHomeAsBack. Which in the renderer calls the following:
FormsAppCompatActivity context = ((FormsAppCompatActivity)Forms.Context);
Android.Support.V7.App.ActionBar actionBar = context.SupportActionBar;
if (actionBar != null)
{
actionBar.SetDisplayHomeAsUpEnabled(element.DisplayHomeAsBack);
}
Unfortunately it seems this does absolutely nothing, even though all online tutorials and stackoverflow question for android suggest this method.
The plan is that I can then use the "OnBackButtonPressed" override in MasterDetailPage, which should allow me to perform this action. Unfortunately displaying the back button has been the larger hurdle so far!
Any idea of a better way to do this or how I can get the current mechanism to work?
EDIT
I have created a project and uploaded it to this question on the Xamarin support forums, if it helps.
http://forums.xamarin.com/discussion/comment/186330#Comment_186330
Sorry to keep you waiting so long!
Warning that I did not actually run this code and changed it from my own so I would be surprised if it worked perfectly without some changes.
So below should add a back button where there was not one before (so like when there is not really a page to go back to) and then we will add a custom action to perform when it gets pressed.
I would suggest you push a new page onto the stack without using animation so it is transparent to the user and also makes all of this much simpler, but if you absolutely do not want to do that, the below method should work.
MainActivity:
//Use this to subscribe to the event which will create the back button
public override bool OnCreateOptionsMenu(IMenu menu) {
if(menu != null && App.AppMasterPage != null) { //You will need this to make sure you are on your MasterDetailPage, just store a global reference to it in the App class or where ever
Xamarin.Forms.MessagingCenter.Unsubscribe<string>(this, "CreateBackButton");
Xamarin.Forms.MessagingCenter.Subscribe<string>(this, "CreateBackButton", stringWeWillNotUse => { //Use this to subscribe to the event that creates the back button, then when you want the back button to show you just run Xamarin.Forms.MessagingCenter.Send<string>(this, "CreateBackButton")
ActionBar.DisplayOptions = ActionBarDisplayOptions.ShowTitle | ActionBarDisplayOptions.ShowHome | ActionBarDisplayOptions.UseLogo | ActionBarDisplayOptions.HomeAsUp; //You may need to play with these options to get it working but the important one is 'HomeAsUp' which should add the back button
});
} else {
Xamarin.Forms.MessagingCenter.Unsubscribe<string>(this, "CreateBackButton");
}
return base.OnCreateOptionsMenu(menu);
}
Now the next step is do do a custom action when it is pressed. I think you can either override OnBackPressed() or OnOptionsItemSelected() in MainActivity or maybe you can override the MasterDetailPage method. I am not sure.
Which ever one works for you, inside of that override, I would simply check to see if you are on your App.AppMasterPage like we did above, and if so, send a MessagingCenter message which your App.AppMasterPage has already subscribed to in order for it to handle the custom action.
If you get stuck let me know!
I know it sounds like a bit of a hack, but the best "solution" I have found so far is to add a page behind the current page (behind the root) so it is not visible. Then when the user presses the back button, handle it by removing that page.
I have an application, where you can click on the background, and it's changes from the drawables to another background, but I only want to make it clickable when the user click on a button that i call, "I want to click it".
So, how to write a code like:
If user click on button1 2 times, make layout clickable
else
not make layout clickable
So, I want to store somehow the click, and force my app to remember to it, and I also want to count the clicks.
What chapter of Android are helping me understanding this? Thanks for help, and sorry for the noobish question:)
You can have counter that increments on click and then just disable component when counter reach value that you want
int count = 0;
Button button = (Button) findViewById(R.id.i_want_to_click_it);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (++count == 2) {
// make stuff clickable here on 2nd click
}
// if you also want to make things unclickable if there
// are more than 2 clicks, add the else{} condition
else {
// make stuff unclickable here
}
}
});
You can detect when user clicks a button by registering a onClickListener on it. Inside that callback you can count how many times it has been clicked and store that information inside a variable. If your app can change from portrait to landscape mode, dont forget to store the variable in onSavedInstanceState and then retrieve it in onCreate, because changing layout mode will destroy the activity and rebuild it which will reset your variable. I highlighted keywords to search for.
I have an activity which have multiple piece of UI panel(you can think them as view in android), these panels will be invisible by default.
Now when user trigger action1, PanelA will display, when trigger action2, PanelB will display(at different location of the screen).
Both PanelA and PanelB is visible at the moment, now when user hit the back menu, the PanelB should disappear, and PanelA should disappear when hit the back menu again.
At first, I use View to hold different panels, however I found it is difficult to keep the state consist, for example, the activity will be a little different between PanelA and PanelB.
Then I found the fragment, however after I tried and tested, I found that the addTobackStack() can not apply to my us-case, since the PanelA and PanelB are at different location, android can not save their state by default.
So I wonder if there is any other solution for my requirement?
You need to manually handle this scenario inside onBackPressed() method of an Activity.
For instance -
#Override
public void onBackPressed() {
if (panelB.isOpened()) {
panelB.close()
} else if (panelA.isOpened()) {
panelA.close()
} else {
super.onBackPressed();
}
}
When panelB is opened, it will close only panelB and wont do anything else. Same goes for panelA, if its opened and when both the panel are closed then it will exit the app like normal.
I highly recommend to use DialogFragments here as you can call show() and dismiss() any point of time on that and they can handle custom views pretty well.
Hope it helps.
How can we handle the check button in the CAB menu?
Is the item associated to some internal id like android.R.id.checkbox?
I've read that it could be handled in SherlockActionBar CAB, but could it be the same with the native ActionBar CAB?
Is there any way to detect the interaction of this item? onActionModeFinished() is not sufficient since I'm calling it multiple times since the CAB needs to be present due to previous changes that happened.
Thanks.
Ok, finally found a solution after trying some things.
Place the code you want for the checkbox or onbackpressed in the method below
public void onDestroyActionMode(ActionMode mode)
{
//place the code you want for the checkbox or back icon here. If you don't want
// this code run if other selections are used, then just create a boolean value that
//you earlier on and check the value in this section before implementing the code
}
};