I have a cascate menu with inner elements. You can navigate on 2nd level and you should be able to return back to main one. The problem is that, notwithstanding the button animation hide the back button, it remains active on the background.
View.Gone is not working. Note: i use linear layouts.
/** CLICK SUL PULSANTE INDIETRO DA DASHBOARD */
dash_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Toast.makeText(getApplicationContext(), "back", Toast.LENGTH_LONG).show();
dd.setVisibility(View.GONE);
exitRight(dd);
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
ll.setVisibility(View.VISIBLE);
navigateRight(ll);
section = "main";
Toast.makeText(getApplicationContext(), "back", Toast.LENGTH_LONG).show();
dd.setVisibility(View.GONE);
}
},
200);
}
});
Related
I wanted to make a snackbar dialog on double press to exit...(java)
Requested with these
On 1st time back pressed show dialogue " press back again to exit " for 2 seconds
On pressing back again show "do you want to exit ? " with the
confirmation button for 2 seconds
As like below -
Create id for your layout in activity_main
CoordinatorLayout coordinatorLayout;
#Override
public void onBackPressed() {
coordinatorLayout= (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
if (!doubleBackToExitPressedOnce) {
this.doubleBackToExitPressedOnce = true;
Snackbar.make(coordinatorLayout, "Do you really want to exit?", Snackbar.LENGTH_LONG)
.setAction("YES", new View.OnClickListener() {
#Override
public void onClick(View view) {
//button action here
System.exit(0);
}
}).setActionTextColor(Color.YELLOW)
.show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
Hope this helps
Here is the solution for you -
private final long DOUBLE_PRESS_BACK_TO_EXIT_TIME = 2000;
boolean doubleBackPressed = false;
#Override
public void onBackPressed() {
// todo: show the snackbar here.
this.doubleBackPressed = true;
utils.showToastLong(getString(R.string.press_again_to_exit));
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackPressed = false;
// todo: hide the snackbar here.
}
}, DOUBLE_PRESS_BACK_TO_EXIT_TIME);
}
To customize the Snackbar you may follow this link
The animation does not work after I set visibility to Invisible, I tried clear animation but not work. I have a button when I click the button it opens a linear layout with animation when I press back button I set the linear layout visibility to invisible again I click the button linear layout appear but no animation please help me.
l1 = (LinearLayout) findViewById(R.id.lnrlgn);
l2 = (LinearLayout) findViewById(R.id.lnrlgn1);
l2.setVisibility(View.INVISIBLE);
Animation uptodown = AnimationUtils.loadAnimation(this,R.anim.uptodown);
viewcrrd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
l2.setAnimation(downtoup);
l2.clearanimation(); // is it right ?
l2.setVisibility(View.VISIBLE);
}
});
public void onBackPressed() {
// super.onBackPressed();
if (back_pressed + TIME_DELAY > System.currentTimeMillis()) {
// super.onBackPressed();
Exitdlg alert = new Exitdlg();
alert.showDialog(LoginActivity.this, "Are You Sure ");
l2.clearAnimation();
} else {
l2.clearAnimation();
l2.setVisibility(View.INVISIBLE);
}
back_pressed = System.currentTimeMillis();
}
Use startAnimation instead of setAnimation
viewcrrd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
l2.setVisibility(View.VISIBLE);
l2.clearanimation();
l2.startAnimation(downtoup);
}
});
I'm trying to perform an action in one fragment, then move to the previous fragment and show a snackbar with a message, confirming the action from the first fragment. However, I'm creating and showing the snackbar in the first fragment (the one I'm moving from), and the snackbar does not appear in the fragment I'm changing to, probably because it's shown in the fragment I'm moving from.
I'm executing the code inside an alertdialog:
builder.setPositiveButton(positiveText, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dbHandler.deleteExercise(exercise.getId());
// Making the snackbar here did not work, either.
getFragmentManager().popBackStack();
Snackbar snack = Snackbar.make(mainLayout, "Exercise deleted", Snackbar.LENGTH_SHORT);
snack.show();
}
});
Any idea how I could go about achieving this?
Thanks!
EDIT:
I made this incredibly crude drawing of the flow to make it clearer what I'm trying to achieve.
I ended up implementing messaging between Fragments via the main Activity and checking for messages in the fragment's onResume() method:
MainActivity:
private String fragmentTransactionMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
// Snip
// Initialize fragment transaction message
fragmentTransactionMessage = null;
// Snip
}
// Three methods used to send information (text) between fragments
public void setFragmentTransactionMessage(String message) {
this.fragmentTransactionMessage = message;
}
public String getFragmentTransactionMessage() {
return fragmentTransactionMessage;
}
public void resetFragmentTransactionMessage() {
this.fragmentTransactionMessage = null;
}
Fragment 2:
// Remove exercise from database
dbHandler.deleteExercise(exercise.getId());
// Update message in main activity
((MainActivity)getActivity()).setFragmentTransactionMessage("Item deleted");
// Move to previous fragment
getFragmentManager().popBackStack();
Fragment 1:
#Override
public void onResume() {
super.onResume();
// Check for messages in main activity
String message = ((MainActivity)getActivity()).getFragmentTransactionMessage();
// If any, display as snackbar
if(message != null) {
Snackbar snack = Snackbar.make(mainLayout, message, Snackbar.LENGTH_SHORT);
snack.show();
// Reset message in activity
((MainActivity)getActivity()).resetFragmentTransactionMessage();
}
}
Another suggestion is to wrap getFragmentManager().popBackStack(); in a method which takes a Runnable Obj which will run after a chosen delay
For Example:
public void goBack(Runnable runAfterBack, long delay) {
mFragmentActivity.onBackPressed();
if (runAfterBack != null) {
(new Handler(Looper.getMainLooper())).postDelayed(runAfterBack, delay);
}
}
Usage:
goBack(new Runnable() {
#Override
public void run() {
Toast.makeText(mActivity, "STRING", Toast.LENGTH_SHORT).show();}
}, 1500);
I am using this library this library.I used the code described in its description on github. looks like this I have event for the menu buttons, not its main Button. i want to get its main button's click event for make rest layout blur when its clicked/expended. even want to hide or set its default state when user click or touch somewhere else on screen.
FloatingActionButton actionC = new FloatingActionButton(getBaseContext());
actionC.setIcon(R.drawable.notes);
actionC.setTitle("Add note");
actionC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//
}
});
FloatingActionsMenu floatingActionsMenu = (FloatingActionsMenu)findViewById(R.id.multiple_actions);
floatingActionsMenu.addButton(actionC);
// ((FloatingActionsMenu) findViewById(R.id.multiple_actions)).addButton(actionC);
final FloatingActionButton actionA = (FloatingActionButton) findViewById(R.id.action_a);
actionA.setIcon(R.drawable.event);
actionA.setTitle("Make life Event");
actionA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//
}
});
and the answer is .
floatingActionsMenu.setOnFloatingActionsMenuUpdateListener(new FloatingActionsMenu.OnFloatingActionsMenuUpdateListener() {
#Override
public void onMenuExpanded() {
Toast.makeText(Launcher.this, "Fdf", Toast.LENGTH_LONG).show();
}
#Override
public void onMenuCollapsed() {
}
});
I have started with android programming and I am trying to do:
Let the user enter their login credential.
If the user login credential is valid let them login.
If login credential is invalid then give them an error message saying that the user name or password entered is invalid.
For the last part, the problem is that I have a TextView with default hidden visibility. I want to make it visible for a few seconds and make it disappear.
This post and this post have helped but they are essentially trying to do the opposite.
My code is given below:
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Check the validity of the user name.
if (valid) {
// Let the user log in.
} else {
// Make TextView visible for a few seconds.
}
}
});
How do I go about this?
In your case first you need to show TextView then make it INVISIBLE .
Use textView.postDelayed method as:
if(valid){ //let the user login }
else
{
// make TextView visible here
textView.setVisibility(View.VISIBLE);
//use postDelayed to hide TextView
textView.postDelayed(new Runnable() {
public void run() {
textView.setVisibility(View.INVISIBLE);
}
}, 3000);
//how to make the textview visible for a few seconds
}
To show any information or error message you can use small pop-up message for some time. Following the code and image for toast.
//display in short period of time
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
You can use toast.
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Toasts in android
try this
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mTextView.setVisibility(View.VISIBLE);
v.postDelayed(new Runnable() {
#Override
public void run() {
mTextView.setVisibility(View.GONE);
}
}, 2000);
}
});
you can achieve this with the help of timertask,as your textview is invisible first make it visible and then with the help of timertask make it invisible again
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//check the validity of the user name
if(valid)
{
//let the user login
}
else
{
txt.setVisibility(View.VISIBLE);
Timer t = new Timer(false);
t.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
txt.setVisibility(View.INVISIBLE);
}
});
}
}, 5000);
}
});
but i would suggest if you want user confirmation show error in alertdialog or a toast to show error would be fine
You can do this by using handler.
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//check the validity of the user name
if(valid)
{
//let the user login
}
else
{
//how to make the textview visible for a few seconds
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
mTextView.setVisibility(View.VISIBLE);
}
}, 1000/* 1sec delay */);
}
});