I've generated a navigation drawer using Android Studio by New -> Activity -> Navigation Drawer Activity. As a recent feature of Android Studio I didn't find enough examples through Google to access TextView in the header. I want to dynamically change the header title.
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
TextView tv = (TextView) drawer.findViewById(R.id.tvNavHeader);
In onCreate does not help me.
The NavigationView has a bug in v23.1.0. You can't access to the views contained in the HeaderView in onCreate method.
A workaround is to inflate the HeaderView and add it programatically in the onCreate method like this:
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
View header = LayoutInflater.from(this).inflate(R.layout.nav_header_view, null);
navigationView.addHeaderView(header);
TextView tv = (TextView) header.findViewById(R.id.tvNavHeader);
}
}
There is no longer need to inflate the HeaderView anymore.
View header = navigationView.getHeaderView(0);
TextView text = (TextView) header.findViewById(R.id.textView);
This works for one header. If you have several, just browse the google issue provided by Mattia.
Related
What I am trying to achieve is to add a Toolbar to my Fragment UI when my Activity also has a bar that is NOT shown inside the fragment. Basically, how can my Activity and Fragment(s) have their own toolbar?
This is my code in my activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(mToolbar);
}
And here is my code in my fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_layout, container, false);
mToolbar = (Toolbar) mView.findViewById(R.id.fragment_toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
return mView;
}
What I get is that when I create the toolbar in my Fragment, the toolbar is the toolbar from the activity. What I want to do is I want the fragment to have it's separate toolbar from it's parent Activity.
Here are a few things that I can think of which are definitely not ideal:
Make the fragment an activity.
Simulate as if I have created a toolbar but simply have the Fragment toolbar defined as a UI element that I have pieced together myself.
Somehow edit the parent Activity's toolbar to make it specific to the fragment, and then somehow redraw the Activity toolbar? This seems weird and I am not even sure if it's possible.
Having 2 toolbars in one activity is not advisable since it'll look really bad.
with ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
you're replacing the activities toolbar with the one that you're defined in the fragment's layout file.
I would suggest you to use fragment specific actions for the toolbar when the fragment is visible on the screen and change it's behavior by accessing the content of the toolbar by
Toolbar mToolbar = ((AppCompatActivity) getActivity()).getSupportActionBar();
now use the mToolbar to change the actions
I am doing an application where I want to put the user name in nav_header_home.xml (top) but I am not able to send the name parameter obtained at the moment the user logs in to the activity home. I am using to send:
abreInicio.putExtra("nome_usuario", dados[3]);
and to receive, in the java da home I have:
public class home extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
String nomeusuario, email;
TextView txtpontosr, txtnome;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
txtnome = (TextView) findViewById(R.id.txtnome);
txtpontosr = (TextView) findViewById(R.id.txtpontosr);
nomeusuario = getIntent().getExtras().getString("nome_usuario");
email = getIntent().getExtras().getString("email_usuario");
Also I would like to put the user's points in the same place, this is not fixed, so how can I implement a swipe refresh in activity home to update this data?
Finally I would like advice, for a better UI would be better a navigation view with activities and in those activities in the tolbar a back or a navigation view with activities that have navigation view or fragments?
In your intent, your are passing an array of String. Instead, make your String an ArrayList and accept it in your activity
I have to reduce drawer menu item in drawable menu in android programmatically .Kindly suggest methods.I am attaching image also.
I have one app to debug, there are 7 menus in navigation-drawer menu .Out of these menus I have to keep 2 menus only by writing java code .Can anybody suggest.
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
setSupportActionBar((Toolbar) activityMainBinding.toolbarActionbar);
hideItem();/*Function declaration defined in outside of onCreate*/
}
private void hideItem()
{
navigationView = (NavigationView) findViewById(R.id.navigation_view);//navigation_view is id declare in xml
Menu nav_Menu = navigationView.getMenu();
nav_Menu.findItem(R.id.maps).setVisible(false);/* id of (maps,documents,reports,settings,logout) declare in drawer_menu.xml file*/
nav_Menu.findItem(R.id.documents).setVisible(false);
nav_Menu.findItem(R.id.reports).setVisible(false);
nav_Menu.findItem(R.id.settings).setVisible(false);
nav_Menu.findItem(R.id.logout).setVisible(false);
}
I have an application, that at the moment only has 2 fragments. Fragment 1, this has the nav drawer and the title.
Fragment 2 requires a custom view as adding menu items won't work as I need alignment. So I add the view as follows:
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.LEFT);
//Remove nav drawer "hamburger"
mMainActivity.mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
//Remove title from Toolbar
bar.setDisplayShowTitleEnabled(false);
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View postToolBar = layoutInflater.inflate(R.layout.upload_content_toolbar, null);
mMainActivity.mToolBar.addView(postToolBar, params);
That is fine and it displays correctly. However, when I want to return to the previous fragment Fragment 1, I then call mMainActivity.mToolBar.removeView(postToolBar); I call this on return to Fragment 1 as the user can navigate either by the back button or by button in the postToolBar. However, the view is still in place. I can't get rid of it. I have now tried setting the visibility to GONE but that won't work either.
This was pretty simple with the Action Bar, however things seem to have gotten a bit complicated with the Tool bar.
I must add that in each of my two fragments I extend a BaseFragment in which I declare the toolbar view.
Can any one help or send me in the direction of a tutorial?
This is how i achieved it at this time:
in activity onCreateView();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowCustomEnabled(true); // enable overriding the default toolbar_home layout
getSupportActionBar().setDisplayShowTitleEnabled(false); // disable the default title element here (for centered title)
getSupportActionBar().setDisplayShowHomeEnabled(false);
cutomToolbarView=getLayoutInflater().inflate(R.layout.custom_toolbar_home, null);
getSupportActionBar().setCustomView(cutomToolbarView);
}
and here is two simple method to do the magic
public void setToolbarTitleEnabled(String title) {
getSupportActionBar().setDisplayShowCustomEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(title);
}
public void setCustomToolbarEnabled() {
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
then simply when you can change actionbar like these:
when to select HomeFragment.java or whatever in your case
setToolbarTitleEnabled(CURRENT_TAG);
and when to select other fragment:
setCustomToolbarEnabled();
Why navigation drawer icon gets disappeared on setting customView by
actionbar.setCustomView(R.layout.blah);
How can this be resolved?
OK, you don't post any code, so I have to assume stuff here. So if you can update your question and show actual code!!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// Depending on what your custom Action Bar will do, you might want to disable these!
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.custom_actionbar, null);
// Here is how you can get specific items from your custom view!
TextView titleTextView = (TextView) customView.findViewById(R.id.title_text);
titleTextView.setText("My Own Title");
...
// MAKE SURE THESE ARE SET!! BOTH OF THEM!!
actionBar.setCustomView(mCustomView);
actionBar.setDisplayShowCustomEnabled(true);
}
The biggest issue is probably the code at the very end. Please make sure you have that code!