Just want to perform slide animation on back press..Here is my code:
txtBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
overridePendingTransition(R.anim.left_out, R.anim.right_in);
onBackPressed();
}
});
I don't want to use intent for a reason.
Try:
finish();
overridePendingTransition(R.anim.left_out, R.anim.right_in);
overridePendingTransition() must be called after finish().
Related
i have a little question. How can i implement click, doble click and hold in a Button. I really need add this functions to my button. I have AndroidStudio 2,3,3. Thanks you!!
button.setOnLongClickListener and button.setOnClickListener should do the trick for long and single clicks respectively.
For double tap here's what I do in the setOnClickListener.
boolean click=false;
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(click==true)
//DO SOMETHING
new Handler().postDelayed(new Runnable(){
public void run(){
click=true;
}, 1000};
});
Your activity has to implement the following interfaces: View.OnClickListener, View.OnLongClickListener.
When you have your ButtonId defined like this: android:id="#+id/button":
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button: //what should happen when the button is pressed
break;
}
}
You also have to set the Listeners in onCreate:
setOnClickListener(this);
setOnLongClickListener(this);
The code for onLongClick looks exactly the same.
Double Tap is a bit more complicated, here you can find how to implement double tap.
There are two buttons, button A starts another activity inside its onClickListener using simple Activity.startActivity() method, button B does some other work inside its onClickListener.
When I click button B and immediately after button A, then new activity is started, onPause() lifecycle event for old activity is fired, but also onClick event for second button is fired, but after Activity.onPause() which leads to some state inconsistencies in my app.
Is there any way to prevent touch/click events from being delivered after onPause() without using isPaused flag?
**Edit:**My code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonA = (Button) findViewById(R.id.activity_button);
buttonA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, TestActivity.class));
}
});
Button buttonB = (Button) findViewById(R.id.log_button);
buttonB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("TEST", "onClick");
}
});
}
#Override
protected void onPause() {
super.onPause();
Log.e("TEST", "onPause");
}
}
Basically if you are fast enough and click button B right after button A then B's onClick is fired after onPause.
In OnClickListener of button A, disable the button b.
Button.setEnabled(false);
Just enable the button at the of A's onClickListener or at onResume depending on your requirements.
I know this question is very old and far far away from being active. I came across this question via a blog.
A simple solution which I can think off is maintaining a flag globally in Activity A and setting it immediately inside onClick of Button A. This flag can be reset in onResume of Activity A.
This flag should be used at the top of onClick handler and all the clicks of all the views must be ignored. Of course, this requires that there is a single onClick method.
private boolean isOnClickIgnored= false;
#Override
public void onResume() {
super.onResume();
isOnClickIgnored = false;
}
#Override
public void onClick(View v) {
super.onClick(v);
if(isOnClickIgnored){
return;
}
switch (v.getId()){
case R.id.btnA:
isOnClickIgnored = true;
// Do button a things like opening Activity B
break;
case R.id.btnB:
// Do button b things
break;
case R.id.btnZ:
// Do button z things
break;
}
}
This whole concept can be easily solved using ViewModels + lifecycle-aware LiveDatas where LiveData expose events to UI-layer only when it's allowed.
I want my btnAppShare button to be clicked even if the button is invisible, I have written this code after button 1 click listener which is visible. Basically, I want that after completely executing setOnClickListener event of button1 my button2 gets automatically clicked and perform its `setOnClickListener.
btnAppShare = (Button) findViewById(R.id.btnAppShare);
btnAppShare.setVisibility(View.INVISIBLE);
btnAppShare.performClick();
btnAppShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (TextUtils.isEmpty(regId)) {
Toast.makeText(getApplicationContext(), "RegId is empty!",
Toast.LENGTH_LONG).show();
} else {
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
i.putExtra("regId", regId);
startActivity(i);
finish();
}
}
});
Use View#callOnClick():
Button b = (Button)findViewById(R.id.button);
//set here listener
b.callOnClick();
Unlike performClick() it calls onClickListener method directly, without view related stuff. Button has to have listener BEFORE you call click action!
Make a method and call that after setting the button invisble, then inside the onClick method of the listener, call that same method.
btnAppShare = (Button) findViewById(R.id.btnAppShare);
btnAppShare.setVisibility(View.INVISIBLE);
myMethod();
btnAppShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0)
myMethod();
}
});
public void myMethod()
{
if (TextUtils.isEmpty(regId)) {
Toast.makeText(getApplicationContext(), "RegId is empty!",
Toast.LENGTH_LONG).show();
} else {
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
i.putExtra("regId", regId);
startActivity(i);
finish();
}
}
You cannot click something that is not visible but what you can do is make that button background transparent with this attribute in its xml
android:background="#android:color/transparent"
this way button will not be visible but clickable.
Hope it helps
if you want to
view.performClick()
something like that, you should write your setOnclick method upper the performclick method. Because codes flow up to down and button should generate setOnclick method before calling.
I have a menu button on the bottom and I have display menu icons from another activity like MenuTask.
But how to close that MenuTask activity if the user clicks again in the menu?
imgMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (key == 0) {
// I want to show menu here
key = 1;
Intent intent = new Intent(MainActivity.this, MenuAction.class);
startActivityForResult(intent, 2);
setResult(0);
} else if (key == 1) {
// I want to Delete menu here
key = 0;
//onBackPressed();
finish();
}
}
});
Note: I think if i click second time , button could not fire. it means else part not execute.
Problem:
I have Clicked, right top menu button and open new activity menu action. but if i want to click again same menu button, i want to disappers that menu action. but menu button could not clicked.
How to make MenuButton Clickable? any idea? any other menthod? But when i click mobile backbutton menuaction layout disappeared.
Thanks in advance.
I thing this is not best way. You must use Fragment
If you want only kill activity MenuAction.this.finish();
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
// handler to delay
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
you can go though this link Clicking the back button twice to exit an activity
same question
I am having an issue with the toolbar and the back button. Here is the setup I have:
When I add a detail fragment, I animate the toolbar hamburger as outlined here. and this causes the hamburger to animate to an arrow.
Even in the comments section, a user mentions:
This works perfectly. Just set start=0 and end=1 to go from hamburger
to arrow, and start=1 and end=0 for arrow to hamburger. One thing
you'll have to keep track of is when the drawer is closed when the
arrow is shown. At this point, the hamburger ends up being shown
(because of the drawer's slide), which you'll have to correct.
But I cannot figure out how to get the back arrow to function properly. When I press the back arrow, the drawer opens and the detail fragment does not pop. How should I go about implementing this?
Questions
How should I animate hamburger to back arrow when adding a detail fragment? assuming the linked solution is not good enough.
How do I override the back arrow to perform only specific functions I wish? like animate to hamburger, pop back stack and NOT open the drawer.
After several hours of searching and playing around, I was able to build a solution that delivered on each requirement. Sources: 1,2
detailFragmentActive = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
setSupportActionBar(mToolbar);
...
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(detailFragmentActive) {
onBackPressed();
//if(displayBackAgain)
//return; //return after so you don't call syncState();
}else if (mDrawerLayou.isDrawerOpen(GravityCompat.START))
mDrawerLayout.closeDrawer(GravityCompat.START);
else
mDrawerLayout.openDrawer(GravityCompat.START);
mDrawerToggle.syncState();
}
});
}
private void animateHamburger(boolean isArrow){
int start = 0, end = 1;
if(isArrow){
detailFragmentActive = false;
start = 1; end = 0;
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}else{
detailFragmentActive = true;
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
ValueAnimator anim = ValueAnimator.ofFloat(start, end);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
mDrawerToggle.onDrawerSlide(mDrawerLayout, slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(500);
anim.start();
}
#Override
public void onBackPressed() {
super.onBackPressed();
animateHamburger(true);
}
public void onFragmentChange(){
...
animateHamburger(false);
}
You can set listener for this button:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (flagDeterminingAction) {
drawerLayout.openDrawer(drawerListView);
} else {
onBackPressed();
//or popbackstack or whatever you are using to going back in navigation
}
}