click on Back Arrow in Toolbar programmatically - java

hi i have an activity and I display 2 frigments I want to automatically close the toolbar when the back button is clicked
How can I do this? Should the code be written in an activity or in a fragment?

Do it in an activity:
boolean toolbarIsOpened = false;
#Override
public void onBackPressed() {
if (toolbarIsOpened){
//closeToolbar
toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
}else{
int count = getSupportFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
} else {
getSupportFragmentManager().popBackStack();
}
}
}
Another way to hide:
getSupportActionBar().hide();
Press "back arrow" programmatically:
onBackPressed();

You can use performclick method like below snippets
img_back.performClick();

Related

How to exit the app on double back press?

I am trying to write a code to exit the app when double pressing the back button. I don't want to call super.onBackPressed(); since it takes me back to the first activity which is splash screen, and I don't want that, I want to exit the app on double pressing. Thank you.
This is my onBackPressed method:
#Override
public void onBackPressed() {
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}
else{
if (doubleBackToExitPressedOnce) {
Intent a = new Intent(Intent.ACTION_MAIN);
a.addCategory(Intent.CATEGORY_HOME);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
}
If you didn't call finish() before you start your second Activity from the first one, it will remain, so when you close your second one,
the program takes you back to the first one, which is not finished yet.
So call finish() after startActivity() in your first Activity
Define two class level variables like
private long TIME_DELAY = 2000;
private long back_pressed;
on OnBackPressed() method, write below code.
if (back_pressed + TIME_DELAY > System.currentTimeMillis()) {
finish();
super.onBackPressed();
} else {
Toast.makeText(getBaseContext(), R.string.press_back_again_to_exit,
Toast.LENGTH_SHORT).show();
}
back_pressed = System.currentTimeMillis();
just use finish() after launching the intent to the next activity:
startActivity(new Intent(this, NextActivity.class));
//Below will enable the app to exit if back is pressed
finish();

Change button text before function call in Android Studio

I am designing a simple app in which myfunction() is called when the start button is pressed. myfunction() takes a few seconds to complete. Right now when I press the Start Button, myfunction() runs for a few seconds and after it is complete, the text of the button changes to "Stop". I want the text of the button to change to "Stop" as soon as the "Start" Button is pressed and before the function is called.
This is my first time working with Android Studio and Java. Any help is appreciated.
I've attached the function which is called when the button is pressed.
public void buttonClick(View v) {
Button tv = (Button)findViewById(R.id.button1);
if(tv.getText() == "Start") {
tv.setText("Stop");
myfunction();
}
else {
tv.setText("Start");
}
}
Don't use tv.getText()=="Start" use tv.getText().equals("Start") like this...
public void buttonClick(View v) {
Button tv = (Button)findViewById(R.id.button1);
if(tv.getText().equals("Start")) {
tv.setText("Stop");
myfunction();
}
else {
tv.setText("Start");
}
}
to use the equals, do something like :
public void buttonClick(View v) {
Button tv = (Button)findViewById(R.id.button1);
if(tv.getText().toString() == "Start") {
tv.setText("Stop");
myfunction();
}
else {
tv.setText("Start");
}
}

Long click listener on 2 buttons at the same time

I would like to do a long click listener for 2 buttons . I mean clicking on the 2 buttons(a and b) at the same time will close the app.
How can i manage this? thank you in advance .
I find this solution using isPressed method. It is working.
iconD.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
if (view.getId() == R.id.picture_d) {
if (iconR.isPressed() && iconD.isPressed()) {
askingForClosingTheApp();
}
}
return true;
}
});

How to hide default hamburger icon of Navigation view android

I need to hide the hamburger icon
This is my toolbar
I need to hide the default hamburger icon of navigation bar and load it from another button click.The navigation bar need to appear on the attachment icon click in my toobar and need to disappear when i click outside(anywhere).Can this be done ?
if you are using ActionBarDrawerToggle then you can add a line:
toggle.setDrawerIndicatorEnabled(false);
and opening and closing drawer you can write in your click event:
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
You can hide the hamburger icon by doing this:
toolbar.setNavigationIcon(null); // to hide Navigation icon
toolbar.setDisplayHomeAsUpEnabled(false); // to hide back button
If you have added the attachment icon manually (As an imageView inside a Toolbar) :
boolean isDrawerOpen = false;
imageViewAttachment..setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!isDrawerOpen) {
mDrawerLayout.openDrawer(Gravity.LEFT);
isDrawerOpen = true;
}
else {
drawerLayout.closeDrawer(Gravity.END);
isDrawerOpen = false;
}
}
});
Or, if you've added as a Menu item :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.attachment:
if(!isDrawerOpen) {
mDrawerLayout.openDrawer(Gravity.LEFT);
isDrawerOpen = true;
}
else {
drawerLayout.closeDrawer(Gravity.END);
isDrawerOpen = false;
}
return true;
}
return super.onOptionsItemSelected(item);
}

How to call new activity when click on Drop down navigation in actionbar in android

I am following this link to implement actionbar.
i have modified this code like this
/** Defining Navigation listener */
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Toast.makeText(getBaseContext(), "You selected : " + actions[itemPosition] , Toast.LENGTH_SHORT).show();
if(actions[itemPosition] == "Bookmark")
startActivity(new Intent(this,Bookmark.class))
else if(actions[itemPosition] == "Subscribe")
startActivity(new Intent(this,Subscribe.class))
else
startActivity(new Intent(this,Share.class))
return false;
}
};
When i run this code it opens Bookmark activity . but it call over main layout like when it opens bookmark and i click back then it opens main activity. i want when i click on list for e.g bookmark then only the activity should change not the title with list navigation. or is there any example that i should follow to call new activity when list navigation is called. i have also tried ActionBarSherlock it also change the textview on clicking list navigation not calling new activity
Try This instead of above
if(actions[itemPosition].compareTo("Bookmark")==0){
startActivity(new Intent(this,Bookmark.class));
}
else if(actions[itemPosition].compareTo("Subscribe")==0){
startActivity(new Intent(this,Subscribe.class));
}
else{
startActivity(new Intent(this,Share.class));
return false;
}
You cannot open new activity on current activity. That's not how it works. If you want to change the views within your activity when the item is clicked you can, and should look into fragments to do so.

Categories