Close floating action button Android - android

I used the following library https://github.com/futuresimple/android-floating-action-button and everything works fine.
How do I close the menu automatically after clicking on one of the buttons on the same menu?

Its open-source lib, You can read the source code and find that by your self.
FloatingActionsMenu.collapse(); // close the menu
FloatingActionsMenu.toggle(); // toggle the menu
FloatingActionsMenu.expand(); // open the mneu
In the Click listener of the Menu Item call .collapse()

FloatingActionMenu fAM = (FloatingActionMenu) findViewById(R.id.fAM_ID);
FloatingActionButton fAB1 = (FloatingActionButton)findViewById(R.id.fAB_ID);
floatingActionButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Action
Toast.makeText(MainActivity.this, "FAB Click", Toast.LENGTH_SHORT).show();
fAM.collapse();
}
});

Related

Opening a spinner dropdown menu from an ImageButton on Android

My layout contains a spinner and an ImageButton.
The spinner is setup properly. Pressing on it displays the drop down menu.
Now I'm trying to add an onClick action on my button so it can open the Spinner
The following code:
this.imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
spinner.performClick();
}
});
works fine on the genymotion emulator, but not on my Nexus 4 & 6 devices.
On those devices the dropdown menu opens then closes automatically. Every 10-20 attempts it might remain open but that's it.
Any idea what's going on ?
How can I prevent this ?
Edit if I enable android:spinnerMode="dialog" instead of thde default 'dropdown' it works fine, as if something was taking the focus from the spinner... What's strange is that opening the dropdown menu by pressing on the spinner works fine
Here is Source Hope so It works,
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
Button okButton = (Button) findViewById(R.id.yesButton);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(spinner.getSelectedItem() == null) { // user selected nothing...
spinner.performClick();
}
}
});

Show menu on button click in android

I just want the normal menu which gets displayed on clicking the settings button in android phone instead with Virtual Button Click. I tried openOptionsMenu(); but it didn't help. I hope you understand the problem here: I want for example default "setting" option displayed which we see when I click on button in actionbar but this time I have action bar hidden so I have a button which I want to display the menu on Clicking that button.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openOptionsMenu();
}
});
You can set its property in xml file to invisible or gone & in the .java file set it to visible see code below:
Note: In my case I'm using Buttons you can use according to your own
.java file:
Button next,btn;
public void newBtnL(View view){
next = (Button) findViewById(R.id.ListenBtn);
btn = (Button) findViewById(R.id.newBtn);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setVisibility(View.VISIBLE);
}
});
}
.xml file:
<Button
android:id="#+id/ListenBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="this"
android:layout_below="#+id/btn_stop_service"
android:onClick="newBtnL"/>
<Button
android:id="#+id/newBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/ListenBtn"
android:text="that"
android:visibility="invisible"/>

FAB transparent layer does not block the parent layer

I have a problem using Clans FloatingActionButton. I need to add a transparent layer that blocks the parent layer. The effect that I need is similar to Google Inbox.
So in my example I have this sequence:
1) Click item 3
2) Click on fab
3) When I click over transparent layer, the item from ListView is clicked.
I try to block the click event transparent layer using:
final FloatingActionMenu menu1 = (FloatingActionMenu) findViewById(R.id.menu1);
menu1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
menu1.close(true);
}
});
So with this code, when I click transparent layer the fab menu is closed (this is ok), but the same click listener blocking the click event over listView.
I have tried different ways to solve this problem but the only way I found was to resize the background programmatically . This is not a good solution because the effect is not good.
In a few hours I'll upload a demo and source code to github , but even as hope someone can help me with some idea. Thank You
EDITED
Here is the LINK to my repo.
Here is the LINK to download this apk
Here is the LINK to video demo
I have download your project from Github and tried the code myself and have solved the issue.
It is working perfectly for me as demonstrated in the Inbox by Google app. You just need to change a very small portion of your code.
menu1.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() {
#Override
public void onMenuToggle(boolean opened) {
if (opened) {
menu1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
menu1.close(true);
menu1.setClickable(false);
}
});
} else {
Toast.makeText(getApplicationContext(), "Close", Toast.LENGTH_SHORT).show();
}
}
});
Hope I could help you.

How to hide one item(Button) and show another item(Button) from menu (Buttons in ActionBar) in single Activity

As attached in screenshot when I click on edit button then i want to hide edit button and show save button. How can i do that?
My menu file is as below:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/edit_button"
android:icon="#drawable/edit_button"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/edit"/>
</menu>
Is it possible this with image as edit to another image as save when button is clicked once and as save to edit when button is clicked again.
Try this
#Override
public boolean onOptionsItemSelected(MenuItem item){
{
if(editing){
item.setIcon(R.drawable.ic_save);
}else{
item.setIcon(R.drawable.ic_edit);
}
return super.onOptionsItemSelected(menu);
}
final DatePicker dp2 = (DatePicker) findViewById(R.id.datePick2);
final Button btn2 = (Button) findViewById(R.id.btnDate2);
dp2.setVisibility(View.GONE);
or
dp2.setVisibility(View.INVISIBLE);
btn2.setVisibility(View.GONE);
or
btn2.setVisibility(View.INVISIBLE);
when need visible:
btn2.setVisibility(View.VISIBLE);
or you use Invisible or Gone, but not both!
Based on your comments, you need other than asked.
Make not relevant, but change the
<item
android:id="#+id/edit_button"
to
<item
android:id="#+id/edit_or_save_button"
From the name you should know what will be the next:
if you press the button when it was an Edit, than you will do stuff in the activity which allows editing your data, but this button will change his text ( and action) to Save!
Of course you will assign different action listener, which will do the validation and save action.
If you really want to stick with 2 button idea (highly disagree with that) :
Button edit_button = (Button)findViewById(R.id.edit_button);
Button save_button = (Button)findViewById(R.id.save_button);
...
edit_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edit_button.setVisibility(View.INVISIBLE);
save_button.setVisibility(View.INVISIBLE);
DoEdit(v);
}
});
save_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
save_button.setVisibility(View.INVISIBLE);
edit_button.setVisibility(View.INVISIBLE);
DoValidationAndSave(v);
}
});
I hope it solve your problem!
Try this,
public boolean checkHide = false
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case EDIT:
if(checkHide){
checkHide=false;
item.setTitle("edit");
// ToDo your function
}
else{
checkHide=true;
item.setTitle("save");
// ToDo your function
}
}

Button in slidingdrawer!

I'm having a button in a sliding drawer in a Android Application. The problem is it does not seem to react to any clicks as normal buttons do.
I'm guessing the problem is that it's a different view than buttons on the normal view.
If I implement a button the normal way like this
myAgenda = (Button)findViewById(R.id.BtnMyAgenda);
myAgenda.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()){
case R.id.BtnMyAgenda:
test.setAnimation(leftLeft);
test.startAnimation(leftLeft);
break;
}
I'm guessing there is something wrong with the above code since the button is in a SlidingDrawer and not in the "normal" view.
Any ideas how to fix the problem?
Here is the code
Register with event listner like below code
button.setOnClickListener(clickButtonListener);
and create this listner for button
private OnClickListener clickButtonListener= new OnClickListener()
{
#Override
public void onClick(View v)
{
if(v == button)
{
}
}
}
I actually found the solution to the problem, I simply created a new view.onclicklistener specific to that button.
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});

Categories

Resources