This question already has answers here:
getActionBar() returns Null (AppCompat-v7 21)
(4 answers)
Closed 7 years ago.
I have a activity
and I have added
setSupportActionBar(toolbar);
And I want to display home button on action bar.
Home button is from android predefined.
And I am calling this method
getActionBar().setDisplayHomeAsUpEnabled(true);
But when I run the app, it gives that getActionBar retured null
Full code of activity is following
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
. setAction("Action", null).show();
}
});
getActionBar().setDisplayHomeAsUpEnabled(true);
Why getActionBar returning null? thats my question.
You should try getSupportActionBar()
Use in Activity
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Use in fragment
((ActionBarActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
instead of
getActionBar().setDisplayHomeAsUpEnabled(true);
Related
I am calling fragments from Main_activity.java and also calling fragments from other fragments but i want to show backspace(<-) arrow button when any fragment is loaded(either from Main_activity or any other fragments) instead of ActionBarDrawerToggle. how can I achieve this?
Main Activity code
public class MainActivity extends AppCompatActivity {
NavigationView nav;
ActionBarDrawerToggle toggle;
DrawerLayout drawerLayout;
Toolbar toolbar;
ImageView homeInToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
nav=findViewById(R.id.navigationview);
drawerLayout=findViewById(R.id.drawer);
// homeInToolbar=findViewById(R.id.homeInToolbar);
toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
FragmentManager manager=getSupportFragmentManager();
nav.setCheckedItem(R.id.menu_home);
FragmentTransaction transaction=manager.beginTransaction();
manager.beginTransaction().replace(R.id.framelayout,new HomeFragment()).commit();}
Main_Activity where the toogle is shown and after click on students, the image should look like
Add these 2 lines after setSupportActionBar(toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
This works for me.
In main_activity from where I calling the fragment, I have disabled toggle as shown in below code.
public void onClick(View v) {
toggle.setHomeAsUpIndicator(R.drawable.ic_back);
toggle.setDrawerIndicatorEnabled(false);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.framelayout,new FeeFragment(toggle)).addToBackStack(null).commit();
}
});
and where ever necessary, I can enable toggle by below code.
toggle.setDrawerIndicatorEnabled(true);
In the default layout provided in android studio with a collapsing toolbar, I've added a floating action button. Within the Scrolling Activity's onCreate() method, I wrote this -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
final Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ScrollingActivity.this.getSupportActionBar().setTitle("New Title");
}
});
}
Please tell me why the title won't change and how to change the title whenever fab is pressed.
To change collapsing toolbar title use :
collapsingToolbarLayout.setTitle("yourTitle");
How can I remove the elevation (drop shadow) from a DrawerLayout in Android Studio? I've looked everywhere, and tried adding app:elevation="0dp" and android:elevation="0dp" to my DrawerLayout xml, but it doesn't work.
I even tried adding setElevation(0f) to my MainActivity, but that didn't work either.
What am I doing wrong? Thanks.
(P.S. The project's base API is 16... I tried changing it to 21, but that didn't make any difference.)
Here's my onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "MUHAHA BURP BARF BARF WHAHAHA", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(drawer_layout);
ActionBarDrawerToggle 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);
populateListView();
}
Solved it. Turns out I was trying to remove the elevation from drawer_layout when I should've been removing it from drawer. Putting drawer.setDrawerElevation(0f); in my MainActivity fixed the problem.
I am using mikepenz drawer library but I want to change default humburger icon and back arrow icon with my own drawable icon.
I have tried many times but I am unable to change the icon with my own icon .
Can anyone help me ?
new DrawerBuilder()
.withActivity(this)
.withTranslucentStatusBar(false)
.withActionBarDrawerToggle(false)
.withToolbar(toolbar)
.addDrawerItems(
//pass your items here
)
.build();
CODE TO SHOW THE HUMBURGER ICON:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
following is the code I found many times but i tried this also but it did not work
Drawable upArrow = getResources().getDrawable(R.drawable.my_drawable);
actionBar.setHomeAsUpIndicator(upArrow);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
And when I am searching I also come to know that you can not change the icon if you passing the toolbar in drawer builder so can anyone tell me what can I do?
I haven't tried it with that library but, try the following:
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
final Drawable upArrow = getResources().getDrawable(R.drawable.my_drawable);
actionBar.setHomeAsUpIndicator(upArrow);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
As per this link, you need to remove the withToolbar() from the DrawerBuilder and then you will have to handle open/close completely on your own.
For that you can do some thing like that
protected void onCreate(Bundle savedInstanceState) {
...
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(false);
toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon);
...
}
Also you had to add a toolbar navigation click listener to listen for click events on the custom drawer icon.
protected void onCreate(Bundle savedInstanceState) {
...
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
});
...
}
You can update the icon dynamically whenever required as
toggle.setHomeAsUpIndicator(R.drawable.ic_new_icon);
Hope this will help you.
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
String Drawer_Open,Drawer_Close;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//set it button icon
getSuppotActionBar().setDisplayHomeAsUpEnabled(true);
//set it makes button Clickble
getSuppotActionBar().setHomeButtonEnabled(true);
//set your own icon by using this code
getSuppotActionBar().setHomeAsUpIndicator(R.drawable.my_icon);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,Drawer_Open,Drawer_Close);
drawerLayout.serDrawerListener(actionBarDrawerToggle);
}
}
Again Do you have any quires get Consult me here.....,hope you got solution to your problem...
Try this by modify following:
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
to
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(false);
this disable library default icon then change the icon...
getSupportActionBar().setHomeAsUpIndicator(R.drawable.my_drawable);
I am the android developer (freshers).
Here is my java code. where I have tried TabSpec.addTab(.......) but it gives me error of nullpointerexception. plz let me know what should I do? please provide me the code for tabhost with xml and java...thank you.
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
Is this the error where I use supportactionbar methods?
please help me I want to use these tabs with the listview and searchview..
thank you.