I learnded to use Android NavigationView,it was shown well but could't response to click event.
Here are my codes. I want to show a Toast when i click any item.but it doesn't work...
nav = (NavigationView) findViewById(R.id.navigation);
//nav.setClickable(true);
nav.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
item.setChecked(true);
Toast.makeText(getApplicationContext(), "caonima", Toast.LENGTH_SHORT).show();
return false;
}
});
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:icon="#drawable/homepage"
android:title="首页"
/>
<item android:id="#+id/psychology_item" android:title="日常心理学" android:icon="#drawable/right"/>
<item android:id="#+id/recommand" android:title="用户推荐日报" android:icon="#drawable/right"/>
<item android:id="#+id/movies" android:title="电影日报" android:icon="#drawable/right"/>
<item android:id="#+id/internet" android:title="互联网安全" android:icon="#drawable/right"/>
<item android:id="#+id/noboring" android:title="不许无聊" android:icon="#drawable/right"/>
<item android:id="#+id/desingn" android:title="设计日报" android:icon="#drawable/right"/>
</group>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/tool_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<android.support.design.widget.NavigationView
android:fitsSystemWindows="true"
android:id="#+id/navigation"
android:layout_gravity="start"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/nav_menu"
/>
</android.support.v4.widget.DrawerLayout>
it has disturbed me for two days, i find some question use listView ,
or add drawerView.bringToFront();drawerView.requestLayout();
But nothing changed.
by the way , i used toolbar in a fragment like this
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main_title,container,false);
mToolbar = (Toolbar)v.findViewById(R.id.main_title_toolbar);
mDrawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
mToolbar.setTitle("首页");
mToolbar.setTitleTextColor(getResources().getColor(R.color.text));
((AppCompatActivity)getActivity()).setSupportActionBar(mToolbar);
mToolbar.setNavigationIcon(R.drawable.navigation);
setHasOptionsMenu(true);
mToggle = new ActionBarDrawerToggle(getActivity(),mDrawerLayout,mToolbar,
R.string.drawer_open,R.string.drawer_shut){
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
drawerView.bringToFront();
drawerView.requestLayout();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
//mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
return v;
}
Oh, i just so a article.it has solved my problem.
here is the link : enter link description here
You just have to bring your drawerLayout to the front,
so try to use these two method
mDrawerLayout.bringToFront();
mDrawerLayout.requestLayout();
then you can find it solved,if you want know more about this,search"Z order".
Related
I have created the default template with menu from android studio. This project is working now but sometimes a user can click on meny only on small area:
But sometimes all items in menu work fine(each of item has normal area for click listener), It happens randomly.
Maybu someone has this problem, i cant understand why it happens.
My code:
xml
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/view_nav_header_main"
app:menu="#menu/activity_main_drawer" />
activity_main_drawer
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_overview"
android:icon="#drawable/icon_overview"
android:title="#string/overview" />
<item
android:id="#+id/nav_library"
android:icon="#drawable/icon_library"
android:title="#string/library_of_events" />
<item
android:id="#+id/nav_statistic"
android:icon="#drawable/icon_chart"
android:title="#string/view_your_stats" />
<item
android:id="#+id/nav_add_event"
android:icon="#drawable/icon_add_event"
android:title="#string/create_a_new_event" />
<item
android:id="#+id/nav_settings"
android:icon="#drawable/icon_settings"
android:title="#string/settings" />
<item
android:id="#+id/nav_export"
android:icon="#drawable/icon_table"
android:title="#string/data_export" />
<item
android:id="#+id/nav_help"
android:icon="#drawable/icon_help"
android:title="#string/help" />
</group>
</menu>
activity has default template code
public class MainActivity extends TimeActivity implements NavigationView.OnNavigationItemSelectedListener, DrawerLayout.DrawerListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.open, R.string.close);
drawer.addDrawerListener(toggle);
navigationView.setNavigationItemSelectedListener(this);
drawer.addDrawerListener(this);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Log.e(TAG, "onNavigationItemSelected " + String.valueOf(item.getItemId()));
switch (item.getItemId()) {
case R.id.nav_overview:
openOverview();
break;
case R.id.nav_library:
openEventsLibrary();
break;
case R.id.nav_statistic:
openStatistic();
break;
case R.id.nav_add_event:
openAddEvent();
break;
case R.id.nav_settings:
openSettings();
break;
case R.id.nav_export:
openDataExport();
break;
case R.id.nav_help:
VideoActivity.start(this, false);
//youTubePopUp.show(view);
break;
default:
openWelcome(0);
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onDrawerSlide(#NonNull View view, float v) {
}
#Override
public void onDrawerOpened(#NonNull View view) {
Utils.hideKeyboard(this);
}
#Override
public void onDrawerClosed(#NonNull View view) {
}
}
I am trying to add DrawerLayout on my app but the items are not responding to click. I have tried logging the output on the console but nothing show up when the items are clicked. Here is my code
MainActivity
public class DashboardActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar;
DrawerLayout drawerLayout;
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawal);
drawerLayout = (DrawerLayout) findViewById(R.id.drawal_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer,R.string.close_drawer);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
Log.i("info", String.valueOf(id));
switch (id){
case R.id.dashoard:
Toast.makeText(DashboardActivity.this, "This is dasbhard", Toast.LENGTH_SHORT).show();
case R.id.hire_mechanic_id:
Intent intents = new Intent(DashboardActivity.this, LIkelyProblemsActivity.class);
startActivity(intents);
break;
case R.id.tow_van_id:
Toast.makeText(getApplicationContext(),"Hire a Toyin Van", Toast.LENGTH_LONG).show();
break;
}
drawerLayout.closeDrawer(Gravity.START);
return true;
}
}
DrawerLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawal_layout"
android:fitsSystemWindows="false"
tools:openDrawer="start">
<include layout="#layout/activity_dashboard" />
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:id="#+id/navigation_view"
android:layout_gravity="start"
android:fitsSystemWindows="false"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:headerLayout="#layout/activity_navigation_header"
app:menu="#menu/navigation_menu"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
item.xml
<item android:title="Dashboard">
<menu>
<item
android:title="item 1"
android:icon="#drawable/mechanic"
android:id="#+id/dashoard"/>
<item
android:title="item 2"
android:icon="#drawable/mechanic"
android:id="#+id/hire_mechanic_id"/>
<item
android:title="item 3"
android:icon="#drawable/spareparts"
android:id="#+id/spare_parts_id"/>
<item
android:title="item 4"
android:id="#+id/tow_van_id"
android:icon="#drawable/toyin_van" />
</menu>
</item>
</menu>
I have checked the drawerlayout, menu and the main activity but I can't really figure out what the problem is. Anyone one who can help me.
You forget to call navigationView.setNavigationItemSelectedListener listener that's why navigation item not being called, just add this line
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this)
Hope it will help you!!
I'm building Xamarin Android application and I want to implement right navigation drawer with drawer toogle in a custom action bar. Everything is working (drawer slides from right side,...) except one thing: the navigation drawer icon is not showing.
So, this is my Activity.axml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:id="#+id/drawerLayout">
<!-- Main Content -->
<FrameLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:id="#+id/map" />
...
</FrameLayout>
<!-- Right Navigation Drawer -->
<LinearLayout
android:orientation="vertical"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#f2f2f2">
...
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
And this is my Activity.cs:
using Android.Support.V4.App;
using Android.Support.V4.Widget;
...
public class Activity : Activity
{
...
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToogle;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
ActionBar.SetDisplayShowHomeEnabled(false);
ActionBar.SetDisplayShowTitleEnabled(false);
ActionBar.SetCustomView(Resource.Layout.CustomActionBar);
ActionBar.SetDisplayShowCustomEnabled(true);
SetContentView(Resource.Layout.Activity2);
mDrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawerLayout);
mDrawerToogle = new ActionBarDrawerToggle(this, mDrawerLayout, Resource.Drawable.ic_drawer,Resource.String.open_drawer_layout, Resource.String.close_drawer_layout);
mDrawerLayout.SetDrawerListener(mDrawerToogle);
ActionBar.SetDisplayHomeAsUpEnabled(true);
ActionBar.SetHomeButtonEnabled(true);
...
}
protected override void OnPostCreate(Bundle savedInstanceState)
{
base.OnPostCreate(savedInstanceState);
mDrawerToogle.SyncState();
}
public override void OnConfigurationChanged(Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);
mDrawerToogle.OnConfigurationChanged(newConfig);
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
if (mDrawerToogle.OnOptionsItemSelected(item))
{
return true;
}
return base.OnOptionsItemSelected(item);
}
...
}
This is my Themes.xml:
<resources>
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
<style name="ActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:height">75dp</item>
<item name="android:background">#00000000</item>
</style>
</resources>
Am I doing something wrong? Any help is appreciated.
UPDATE
My CustomActionBar has app icon and app title. Is this maybe interrupting navigation drawer icon?
This is the solution I have found.
First, you need to add action_bar.xml file with the following content to the menu folder:
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/personalInfoActionBar"
android:title="Informations"
android:icon="#drawable/infoIcon"
android:showAsAction="always"/>
</menu>
Then insert this code in your activity:
public override bool OnOptionsItemSelected(IMenuItem item)
{
if (item.ItemId == Resource.Id.personalInfoActionBar)
{
if (mDrawerLayout.IsDrawerOpen(mRightDrawer))
{
mDrawerLayout.CloseDrawer(mRightDrawer);
}
else
{
mDrawerLayout.OpenDrawer(mRightDrawer);
}
return true;
}
return base.OnOptionsItemSelected(item);
}
Don't forget call actionbarDrawerToggle.syncState()
I have a drawer layout that needs to have two navigation views in it, because there's a couple of options that need to be aligned on top and another on the bottom, with a divider separating them.
I've managed to make it work, but now I have two different problems with my setup.
1) The first one, I can't change the selected items text color (the background color works as intented via a drawer selector, but for some reason, the selected item's text color is always colorPrimary)
2) The second one is trickier, since there's two navigation views, it can happen that there's a selected row on the first and on the second one, even though they all work loading fragments in the same container. Is there a way to manage that, when an item for the first navigation view is selected, the second navigation view's selected item gets deselected?
This is the drawer's layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- 1º Layout de la activity -->
<include layout="#layout/activity_main"/>
<!-- 2º Layout del drawer -->
<android.support.design.widget.NavigationView
android:id="#+id/container_navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemBackground="#drawable/drawer_list_selector"
android:nestedScrollingEnabled="true"
android:scrollIndicators="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height"
android:background="#color/colorAccent"
android:layout_above="#+id/navigation_view"
android:id="#+id/top_separator"/>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_above="#+id/bottom_separator"
app:itemBackground="#drawable/drawer_list_selector"
app:menu="#menu/menu_navigation" />
<View
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height"
android:background="#color/colorAccent"
android:layout_above="#+id/navigation_bottom"
android:id="#+id/bottom_separator"/>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:menu="#menu/menu_navigation_bottom"
app:itemBackground="#drawable/drawer_list_selector"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
This is the top navigation view's layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_tus_ofertas"
android:title="#string/tus_ofertas"
android:icon="#drawable/ic_offer" />
<item
android:id="#+id/nav_movimiento_puntos"
android:title="#string/movimiento_puntos"
android:icon="#drawable/ic_points"/>
<item
android:id="#+id/nav_asociaciones"
android:title="#string/asociaciones"
android:icon="#drawable/ic_flag"/>
</group>
</menu>
Bottom navigation view's layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_terminos"
android:title="#string/terminos"
android:icon="#drawable/ic_document" />
<item
android:id="#+id/nav_contacto"
android:title="#string/contacto"
android:icon="#drawable/ic_email"/>
<item
android:id="#+id/nav_valorar"
android:title="#string/valorar"
android:icon="#drawable/ic_rate_review"/>
<item
android:id="#+id/nav_exit"
android:title="#string/exit"
android:icon="#drawable/ic_shutdown"/>
</group>
</menu>
Selected item's background selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/colorAccent" />
<item android:state_activated="true" android:drawable="#color/colorAccent" />
<item android:state_selected="true" android:drawable="#color/colorAccent" />
<item android:state_checked="true" android:drawable="#color/colorAccent" />
<item android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:state_activated="false" android:drawable="#android:color/transparent" />
<item android:state_selected="false" android:drawable="#android:color/transparent" />
<item android:state_checked="false" android:drawable="#android:color/transparent" />
</selector>
The activity that calls the drawer and handles the events:
public class MainActivity extends AppCompatActivity {
#Bind(R.id.toolbar) Toolbar toolbar;
#Bind(R.id.tabs) TabLayout tabs;
#Bind(R.id.drawer_layout) DrawerLayout drawerLayout;
#Bind(R.id.container_navigation) NavigationView containerNavigator;
#Bind(R.id.navigation_view) NavigationView navigationView;
#Bind(R.id.navigation_bottom) NavigationView navigationBottom;
#Bind(R.id.vsFooter) ViewStubCompat stub;
ImageView userPicture;
TextView userMail;
ViewPager viewPager;
Menu menu;
ActionBar actionBar;
ViewPagerFragment viewPagerFragment;
Usuario currentUser;
public Usuario getCurrentUser() {
return currentUser;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
ButterKnife.bind(this);
currentUser = UserApiManager.getInstance(getApplicationContext()).getCurrentUser();
viewPagerFragment = ViewPagerFragment.newInstance();
setupToolbar();
setupNavigationDrawer();
ApiManager.getInstance(getApplicationContext()).setupFooter(stub, currentUser.getIdLocalidad(), this);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,viewPagerFragment).commit();
}
public void setupViewPager(ViewPager viewPager) {
tabs.setupWithViewPager(viewPager);
}
private void setupToolbar() {
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
if (actionBar!=null){
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_black);
actionBar.setTitle(getString(R.string.app_title));
}
}
private void setupNavigationDrawer() {
View headerView = navigationView.inflateHeaderView(R.layout.navigation_header);
userPicture = (ImageView) headerView.findViewById(R.id.user_picture);
userMail = (TextView) headerView.findViewById(R.id.user_mail);
userMail.setText(currentUser.getEmail());
Picasso.with(this).load(currentUser.getImagenURL()).transform(new CircleTransformation()).into(userPicture);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_tus_ofertas:
showTabLayout();
actionBar.setTitle(getString(R.string.app_title));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,viewPagerFragment).commit();
break;
case R.id.nav_movimiento_puntos:
hideTabLayout();
Bundle bundle = new Bundle();
bundle.putString("idcliente", currentUser.getId());
MovimientoPuntosFragment movimientoPuntosFragment = MovimientoPuntosFragment.newInstance();
movimientoPuntosFragment.setArguments(bundle);
actionBar.setTitle(getString(R.string.movimiento_puntos));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, movimientoPuntosFragment).commit();
break;
case R.id.nav_asociaciones:
hideTabLayout();
actionBar.setTitle(getString(R.string.asociaciones));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, AsociacionFragment.newInstance()).commit();
break;
}
drawerLayout.closeDrawers();
return true;
}
});
navigationBottom.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_terminos:
hideTabLayout();
actionBar.setTitle(getString(R.string.terminos));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, LegalesFragment.newInstance()).commit();
break;
case R.id.nav_contacto:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + getString(R.string.contacto_email));
intent.setData(data);
startActivity(intent);
break;
case R.id.nav_valorar:
final String appPackageName = BuildConfig.APPLICATION_ID;
try {
Log.i("url", "market://details?id=" + appPackageName);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
break;
case R.id.nav_exit:
break;
}
drawerLayout.closeDrawers();
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_over, menu);
this.menu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
break;
case R.id.action_user:
Intent intent = new Intent(this, PerfilUsuarioActivity.class);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
public void hideTabLayout(){
tabs.setVisibility(View.GONE);
}
public void showTabLayout(){
tabs.setVisibility(View.VISIBLE);
}
}
Any help would be appreciated!
Thanks in advance!
Solved!
The problem with the color I solved it with a selector, it didn't work before because I had a syntax error (selector tag inside the main selector tag).
This is the color selector's code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FFFFFFFF" android:state_checked="true" />
<item android:color="#E6000000" android:state_checked="false"/>
</selector>
The second problem I solved it by programmatically deselecting the other navigationview's items onNavigationItemSelected.
This is the function I call:
public void deselectItems(NavigationView view){
int size = view.getMenu().size();
for (int i = 0; i < size; i++) {
view.getMenu().getItem(i).setChecked(false);
}
}
I know there must be a better way to solve the second problem, but this one worked.
See the screenshot attached. My "Previous" and "Next" icons, whose color code is #FFFFFF, are somwhat darker than the system icons for "Up" and "Overflow".
This is a problem because it leads some users to assume that the buttons are disabled.
My theme on the AppBarLayout is ThemeOverlay.AppCompat.Dark.ActionBar and on the Toolbar is ThemeOverlay.AppCompat.Light, in case that matters. I am using appcompat-v7:23.0.1.
Here's the layout XML ...
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
... and the relevant parts of my Activity code.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
adapter = new HomePagerAdapter(getFragmentManager(), this);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
}
#Override
protected void onResume() {
super.onResume();
if (forms == null) {
forms = getConfiguredForms();
}
}
#Override
protected void onStart() {
super.onStart();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.uploadButton:
intent = new Intent(HomeActivity.this, UploadActivity.class);
startActivity(intent);
return true;
case R.id.settingsButton:
intent = new Intent(HomeActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Here's the XML for the options menu. They are added by a fragment.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/firstButton"
android:icon="#drawable/first_white"
android:title="#string/first_action"
app:showAsAction="ifRoom" />
<item
android:id="#+id/previousButton"
android:icon="#drawable/previous_white"
app:showAsAction="always"
android:title="#string/previous_action" />
<item
android:id="#+id/nextButton"
android:icon="#drawable/next_white"
app:showAsAction="always"
android:title="#string/next_action" />
<item
android:id="#+id/lastButton"
android:icon="#drawable/last_white"
app:showAsAction="ifRoom"
android:title="#string/last_action" />
<item
android:id="#+id/resetButton"
android:icon="#drawable/reset_white"
app:showAsAction="never"
android:title="#string/reset_action" />
</menu>
In your styles.xml add the following attributes to the style to hide the default ActionBar:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>