android.R.id.home can not find symbol - android

I used Toolbar component in AppCompat library instead of Default Actionbar.
Compile time: I get compile-error that cannot find symbol android.R.id.home
public void setupActionBar() {
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.action_help) {
showHelp();
return true;
}else if(id == android.R.id.home){
Log.d(TAG, "Back Button clicked!");
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}

android.R.id.home was introduced in API level 11.
here is more detail:
https://stackoverflow.com/a/18719090/2178694

add this to your activity.
#Override
public void onBackPressed() {
// your code.
this.finish();
}

Related

How to add click on the action bar back button

I am trying to add click functionality on the Actionbar back button. I have tried the below things till now, but unable to get the desired result.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Observed that the onClick method is not being called when i click on the back button in the action bar
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
};
drawer.setDrawerListener(toggle);
drawer.post(new Runnable() {
#Override
public void run() {
toggle.syncState();
}
});
toggle.setDrawerIndicatorEnabled(true);
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onKeyDown(4, null);
}
});
Use the updated code
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
switch (item.getItemId()) {
case android.R.id.home:
showToast("Test");
return true;
}
you can try like this
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
use following code inside your Activity :
//for going back or finish current activity.
#Override
public boolean onSupportNavigateUp() {
finish();
startActivity(new Intent(ResultActivity.this, MainActivity.class));
return true;
}
Below code are for actionBar menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.logout) {
// toast Here
return true;
}
if (id == R.id.home) {
// toast Here
return true;
}
return super.onOptionsItemSelected(item);
}
onCreateOptionsMenu() is used for creating your menu & setting your menu.xml
onOptionsItemSelected() is used for selection of menu items & actions.

Method invocation 'setDisplayHomeAsUpEnabled' may produce 'java.lang.NullPointerException

Why this line produce NullPointerException?
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I have imported android.support.v7.app.AppCompatActivity
in build.grade I am using com.android.support.design:25.0.1
public class PhotosActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photos_activity);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
// finish the activity
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Errors: http://pastebin.com/aCQ4Hvpi
This conditional will fix that warning:
if (getSupportActionBar() != null) {
This is how I have my SupportActionBar set up, maybe this will help!
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_top);
setSupportActionBar(myToolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
}

pressed the back button on the toolbar [duplicate]

This question already has answers here:
When toolbar back button is pressed
(4 answers)
Closed 6 years ago.
how can I create a code for my android app where When I pressed the back button on the toolbar (Action Bar) some code will happening.
I tried but it does not work.
Added in main activity. onbackpress method
#Override
public void onBackPressed() {
super.onBackPressed();
stopActivityTask();
}
You can Try this way..
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// this takes the user 'back', as if they pressed the left-facing
triangle icon on the main android toolbar.
// if this doesn't work as desired, another possibility is to call
stopActivityTask(); // finish() here.
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If Not Work
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Title and subtitle
toolbar.setTitle(R.string.about_toolbar_title);
toolbar.setNavigationIcon(R.drawable.ic_action_back);
toolbar.setNavigationOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
stopActivityTask();
finish();
}
});
private Toolbar toolbar;
Then add this in your onCreate method
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Then you add these lines :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
/*
ADD WHAT YOU WANT TO DO WHEN ARROW IS PRESSED
*/
return super.onOptionsItemSelected(item);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
// click on icon to go back
//triangle icon on the main android toolbar.
if (id == android.R.id.home) {
//your code
return true;
}
return super.onOptionsItemSelected(item);
}

How to use onBackPressed in Android of navigation drawer when click on the arrow icon?

I'm using the navigation drawer and the method of onBackPressed don't run when I click on the arrow of toolbar, but when I click on back of the bottom panel of the movil is running.
I don't understand how to do the same but using the arrow button.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,
BlankFragment.OnFragmentInteractionListener {
ActionBarDrawerToggle toggle;
DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.setFocusableInTouchMode(true);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
getSupportFragmentManager().beginTransaction()
.replace(R.id.vista, new BlankFragment())
.addToBackStack(null)
.commit();
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onFragmentInteraction() {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportFragmentManager().beginTransaction()
.replace(R.id.vista, new BlankFragment2())
.addToBackStack(null)
.commit();
}
}
First enable the back arrow of action bar..
ActionBar actionBar = getSupportActionBar();
if(actionBar!=null){
actionBar.setDisplayHomeAsUpEnabled(true);
}
Second use android.R.id.home to access to button on onOptionsItemSelected()
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
You can change theme
<style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
<item name="android:homeAsUpIndicator">#drawable/ic_your_icon</item>
</style>
Or in onCreate()
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_your_icon);
You can try this:
toolbar.setNavigationOnClickListener(yourOnClickListener);

Back Button isn't showing

I've just followed answers from this site and but they doesn't work for me. I am gonna have few activities and I want to have a back button on the action bar but it doesn't seems to appear. I am using API 16 for minimum sdk and I've set the parent Activity to the secondary activity.
Here's my code.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
if (id == R.id.action_options) {
Intent i = new Intent(this, Settings.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
}
You probably meant setDisplayHomeAsUpEnabled, not setDefaultDisplayHomeAsUpEnabled. Not sure what the latter is anyway.
You missed setting setDisplayShowHomeEnabled(true). setDisplayHomeAsUpEnabled(true) is not enough.
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}

Categories

Resources