I have implemented my Toolbar. I am attaching the screenshot how it looks like
As you can see there are two problems.
There is an arrow intead of hamburger, whats more this arrow does not turn at all when I swipe drawer from the edge.
Those icons (array, plus and search) are not centered vertically as text (Abc).
Here is my code:
BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
public ActionBarDrawerToggle actionBarDrawerToggle;
private DrawerLayout drawerLayout;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initFields();
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_login) {
drawerLayout.closeDrawers();
startActivity(new Intent(this, ActivityCredentials.class));
}
return false;
}
public abstract View getView();
private void initFields() {
View view = getView();
NavigationView navigationView = view.findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this);
drawerLayout = view.findViewById(R.id.my_drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.find_word, R.string.add);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
setSupportActionBar(view.findViewById(R.id.toolbar));
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
}
}
ActivityMain.java
public class ActivityMain extends BaseActivity {
private final FragmentMainHome fragmentMainHome = new FragmentMainHome();
ActivityMainBinding binding;
private BottomNavigationView bottomNavView;
private int startingPosition, newPosition;
#Override
public View getView() {
binding = ActivityMainBinding.inflate(getLayoutInflater());
return binding.getRoot();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(binding.getRoot());
initBottomNavView();
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragmentMainHome).commit();
}
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_action_bar, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint(getString(R.string.find_word));
searchView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
#Override
public void onViewAttachedToWindow(View arg0) {
setItemsVisibility(menu, searchItem, false);
}
#Override
public void onViewDetachedFromWindow(View arg0) {
setItemsVisibility(menu, searchItem, true);
}
});
return super.onCreateOptionsMenu(menu);
}
//////////////////////////////////////////////// ORDINARY METHODS ////////////////////////////////////////////////////////////
private void initBottomNavView() {
bottomNavView = binding.bottomNavigation;
bottomNavView.setSelectedItemId(R.id.action_home);
bottomNavView.setItemIconTintList(null);
bottomNavView.setOnItemSelectedListener(item -> {
Toast.makeText(getApplicationContext(), "abc", Toast.LENGTH_SHORT).show();
changeFragmentInActivityMain(item.getItemId());
return true;
});
}
public void changeFragmentInActivityMain(int viewId) {
Fragment fragment = null;
switch (viewId) {
case R.id.action_home:
if (bottomNavView.getSelectedItemId() != R.id.action_home) {
fragment = fragmentMainHome;
newPosition = 0;
}
break;
}
if (fragment != null) {
if (startingPosition > newPosition) {
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right)
.replace(R.id.content_frame, fragment).commit();
}
if (startingPosition < newPosition) {
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left)
.replace(R.id.content_frame, fragment).commit();
}
startingPosition = newPosition;
}
}
private void setItemsVisibility(Menu menu, MenuItem exception, boolean visible) {
for (int i = 0; i < menu.size(); ++i) {
MenuItem item = menu.getItem(i);
if (item != exception) item.setVisible(visible);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:minHeight="?actionBarSize"
android:background="?colorToolbarBg"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="8dp" />
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="#+id/bottom_navigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?attr/colorBottomNavigationViewBg"
app:itemHorizontalTranslationEnabled="true"
app:itemTextColor="?attr/colorBottomNavigationViewItem"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="#menu/menu_bottom_navigation" />
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemIconTint="?colorDayNight"
app:menu="#menu/menu_navigation_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
</LinearLayout>
EDIT First problem solved, now the second remaining...
EDIT 2 Fully solved :)
Related
Here is a piece of Java code.
The Android app stops when I tap on an item in the BottomNavigationView, it should open a fragment.
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
private BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths:
switch (item.getItemId()) {
case R.id.item0:
transaction.replace(R.id.content, new Item0_Fragment()).commit();
return true;
case R.id.item1:
transaction.replace(R.id.content, new Item1_Fragment()).commit();
return true;
case R.id.item2:
transaction.replace(R.id.content, new Item2_Fragment()).commit();
return true;
case R.id.item3:
transaction.replace(R.id.content, new Item3_Fragment()).commit();
return true;
}
return false;
}
});
}
First, you did declares two times the BottomNavigationView. In second, you not writed the complete code, leaving aside parts that may be important. However I did write a code for this. Try read my code or just copy and paste. You just need create your fragments, if needs help to do this let me know.
MainActivity.java
public class MainActivity extends AppCompatActivity {
List<Fragment> fragmentList;
MyAdapter myAdapter;
ViewPager viewPager;
BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG,"onCreate()");
fragmentList = new ArrayList<>();
fragmentList.add(new Fragment1());
fragmentList.add(new Fragment2());
bottomNavigationView = findViewById(R.id.bottomNavigationView);
viewPager = findViewById(R.id.viewPagerAppActivity);
myAdapter = new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(myAdapter);
viewPager.setCurrentItem(0);
bottomNavigationView.setOnNavigationItemSelectedListener(botNavViewItemSelectedListener());
}
public BottomNavigationView.OnNavigationItemSelectedListener botNavViewItemSelectedListener() {
return new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.itemFragment1:
viewPager.setCurrentItem(0);
break;
case R.id.itemFragment2:
viewPager.setCurrentItem(1);
break;
}
return true;
}
};
}
public class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
}
}
activity_main.xml
<android.support.constraint.ConstraintLayout android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v4.view.ViewPager
android:id="#+id/viewPagerAppActivity"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#id/bottomNavigationView"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="0dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="0dp"
app:layout_constraintVertical_bias="1.0" />
<android.support.design.widget.BottomNavigationView
app:menu="#menu/menu_bottom"
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="#color/colorPrimary"
app:itemIconTint="#drawable/bottom_bar_item_selector"
app:itemTextColor="#drawable/bottom_bar_item_selector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
menu_bottom.xml to populate my menu of navigation
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/itemFragment1" android:title="Frag1" android:enabled="true" app:showAsAction="always|withText" android:icon="#mipmap/ic_launcher"/>
<item android:id="#+id/itemFragment2" android:title="Frag2" android:enabled="true" app:showAsAction="always|withText" android:icon="#mipmap/ic_launcher"/>
</menu>
*For fragment click this http://www.truiton.com/2017/01/android-bottom-navigation-bar-example/
*For activities
Following code makes the bottom navigation for activites(same feel as to fragment)
STEP 1: Create a BaseActivity and name it as BaseActivity
BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity implements
BottomNavigationView.OnNavigationItemSelectedListener{
protected BottomNavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewId());
navigationView = findViewById(R.id.navigation);
navigationView.setOnNavigationItemSelectedListener(this);
BottomNavigationViewHelper.disableShiftMode(navigationView);
}
#Override
protected void onStart() {
super.onStart();
updateNavigationBarState();
}
#Override
public void onPause() {
super.onPause();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.menuBuilding) {
startActivity(new Intent(this, ActivityA.class));
} else if (itemId == R.id.menuChat) {
startActivity(new Intent(this, ActivityB.class));
} else if (itemId == R.id.menuProfile) {
startActivity(new Intent(this, ActivityC.class));
}
overridePendingTransition(0, 0);
finish();
return true;
}
private void updateNavigationBarState() {
int actionId = getNavigationMenuItemId();
selectBottomNavigationBarItem(actionId);
}
void selectBottomNavigationBarItem(int itemId) {
Menu menu = navigationView.getMenu();
for (int i = 0, size = menu.size(); i < size; i++) {
MenuItem item = menu.getItem(i);
boolean shouldBeChecked = item.getItemId() == itemId;
if (shouldBeChecked) {
item.setChecked(true);
break;
}
}
}
abstract int getContentViewId();
abstract int getNavigationMenuItemId();
}
STEP 2:
Create a child activity and name it as ActivityA and copy paste the following code
ActivityA.java
public class ActivityA extends BaseActivity implements {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
int getContentViewId() {
return R.layout.activity_a_layout;
}
#Override
int getNavigationMenuItemId() {
return R.id.menuBuilding;
}
}
Similary create ActivityB.java and ActivityC.java .Make both classes similar to ActivityA.java.But the different is ActivityA.java returns
#Override
int getNavigationMenuItemId() {
return R.id.menuBuilding;
}
and
ActivityB.java returns
#Override
int getNavigationMenuItemId() {
return R.id.menuChat;
}
and
ActivityC.java returns`
#Override
int getNavigationMenuItemId() {
return R.id.menuProfile;
}
Also create layout for two java class and copy paste the activity_a_layout.xml code to both layouts of ActivityB.java and ActivityC.java
STEP 3 : Create a layout named activity_a_layout and copy paste the following code
'activity_a_layout.xml'
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:background="#android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coming soon..."
android:textSize="19sp"
android:textStyle="bold"
android:layout_centerInParent="true"/>
</RelativeLayout>
<include
android:id="#+id/navigation"
layout="#layout/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
STEP 4:
Create a layout named bottom_navigation and copy pase the following code
bottom_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.BottomNavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/dark_grey"
app:itemIconTint="#drawable/nav_item_menu_selector"
app:itemTextColor="#drawable/nav_item_menu_selector"
app:menu="#menu/bottom_nav_items" />
STEP 5: Create a menu folder inside res and create a file named bottom_nav_items inside menu and copy paste the below code
bottom_nav_items.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menuChat"
android:icon="#drawable/ic_chat"
android:title="Chat" />
<item
android:id="#+id/menuBuilding"
android:icon="#drawable/ic_building_white"
android:title="Company" />
<item
android:id="#+id/menuProfile"
android:icon="#drawable/menu_item_profile"
android:title="Profile"
app:itemTextColor="#android:color/transparent" />
</menu>
**Dont forgot to declare ActivityA.java,ActivityB.java and ActivityC.java in manifest
HAPPY CODING
I have created A layout and I dynamically add tabs according to data I receive. I do the same with fragments updating the Text View dynamically. Although when the layout loads and if I swipe through the fragments or view pager (sorry I am new to all these hope my terminology is right) they update just okay (meaning there is sometimes lag in updating not much of an issue) and with right data. although, if in fresh I open the layout and click on tab to change my fragments I get no data or wrong data. Example:- when Layout loads for first time my first & third tab load up fine. If I click on a second Tab (not swiping but touching the tabs on top for the whole time), my second tab doesn't have any data in its fragment. On moving round here and there selecting tabs randomly first tab loads second tabs data, but second tab never loads anything. Its not the same when I swipe if I load page new or for first time.
Let me know where I am doing wrong. Thank you.
Here is my code:-
Layout file - show_score.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.lp.activity.ShowScorePassagesActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/children_school"
android:scaleType="centerCrop"
android:alpha="0.5"
android:layout_below="#+id/appBackBar"/>
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<include android:id="#+id/appBackBar"
layout="#layout/detail_appbar"/>
<android.support.design.widget.TabLayout
android:id="#+id/passagetabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Fragment File - fragment_show_score_passage.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lp.activity.ShowScorePassagesActivity$PlaceholderFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.CardView
android:id="#+id/cardView4"
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardElevation="5dp"
android:scaleType="fitXY"
android:layout_margin="5dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/scrollbar_shape_style">
<TextView
android:id="#+id/showScore"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="18dp"
android:scaleType="fitXY"
android:text="TextView"
android:autoLink="web"
android:linksClickable="true"
android:padding="20dp"
android:clipToPadding="false"/>
</ScrollView>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/cardView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
android:layout_below="#+id/cardView4"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/scrollbar_shape_style">
<GridView
android:id="#+id/fluencyAudioGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:horizontalSpacing="2dp"
android:verticalSpacing="5dp"
android:columnWidth="120dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clipToPadding="false"
android:stretchMode="spacingWidthUniform"
layout="#layout/icons_for_dashboard"
android:scrollbarThumbVertical="#drawable/scroolbar_style"
android:scrollbarTrackVertical="#drawable/scroolbar_style_background">
</GridView>
</ScrollView>
</android.support.v7.widget.CardView>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
And the main Java File where the magic happens - ShowScorePassageActivity.java
public class ShowScorePassagesActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
public GridView gridView;
public ArrayList<String> gridItems = new ArrayList<String>();
public static TextView fragTextView;
static String fragNewTextView = "";
public static ArrayList<String> fluencyMarksList = new ArrayList<>();
public TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_score_passages);
Intent in = getIntent();
String intentData = in.getStringExtra("intentData");
String fluencyMarks = in.getStringExtra("fluencyMarks");
tabLayout = (TabLayout) findViewById(R.id.passagetabs);
String mFluencyMarkList []= fluencyMarks.split("#");
fluencyMarksList = new ArrayList<String>(Arrays.asList(mFluencyMarkList));
int count = 1;
int counts=fluencyMarksList.size();
for(int i = 0; i < counts; i++){
tabLayout.addTab(tabLayout.newTab().setText("Passage "+count));
count=count+1;
}
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();
}
});
mViewPager = (ViewPager) findViewById(R.id.container);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
int position = tab.getPosition();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
if (position ==0){
fragTextView.setText(Html.fromHtml(fluencyMarksList.get(0), Html.FROM_HTML_MODE_COMPACT));
}
else if (position ==1){
fragTextView.setText(Html.fromHtml(fluencyMarksList.get(1) , Html.FROM_HTML_MODE_COMPACT));
}
else if (position ==2){
fragTextView.setText(Html.fromHtml(fluencyMarksList.get(2) , Html.FROM_HTML_MODE_COMPACT));
}
} else {
if (position ==0){
fragTextView.setText(Html.fromHtml(fluencyMarksList.get(0)));
}
else if (position ==1){
fragTextView.setText(Html.fromHtml(fluencyMarksList.get(1)));
}
else if (position ==2){
fragTextView.setText(Html.fromHtml(fluencyMarksList.get(2)));
}
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
fragTextView.setText("");
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_show_score_passages, 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;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_show_score_passages, container, false);
fragTextView = (TextView) rootView.findViewById(R.id.showScore);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
fragTextView.setText(Html.fromHtml(fluencyMarksList.get(0) , Html.FROM_HTML_MODE_COMPACT));
} else {
fragTextView.setText(Html.fromHtml(fluencyMarksList.get(0)));
}
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
int mNumOfTabs;
Fragment fragment = null;
public SectionsPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs=NumOfTabs;
}
#Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
}
This is what I did:
It is only a example of what worked for me in the hopes of helping you
In OnCreate:
ViewPager mViewPager = (ViewPager) findViewById(R.id.container);
this.addPages(mViewPager);
mViewPager.setOffscreenPageLimit(3);
TabLayout tabLayout = (TabLayout) findViewById(R.id.mTab_ID);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.addOnTabSelectedListener(listener(mViewPager));
Then add the fragments to the TabLayout by doing:
//ADD ALL PAGES TO TABLAYOUT
private void addPages(ViewPager pager) {
adapter = new MyFragPagerAdapter(getSupportFragmentManager());
adapter.addPage(new FragmentAdapter1());
adapter.addPage(new FragmentAdapter2());
adapter.addPage(new FragmentAdapter3());
//SET ADAPTER TO PAGER
pager.setAdapter(adapter);
}
Then implement TabLayout click events OnTabSelectedListener by doing:
//TabLayout Click Events
private TabLayout.OnTabSelectedListener listener(final ViewPager pager) {
return new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
}
I want to admob advertisement but i am getting java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yavuzoktay.deprem/com.yavuzoktay.deprem.MainActivity}: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
MainActivity.java
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
private BottomNavigationView navigation;
private ViewPager viewPager;
private FragmentA fragmentA = new FragmentA();
private FragmentB fragmentB = new FragmentB();
private FragmentC fragmentC = new FragmentC();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
setTitle("Depremler");
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.addOnPageChangeListener(this);
navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return fragmentA;
case 1:
return fragmentB;
case 2:
return fragmentC;
}
return null;
}
#Override
public int getCount() {
return 3;
}
});
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
viewPager.setCurrentItem(item.getOrder());
return true;
}
};
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
navigation.getMenu().getItem(position).setChecked(true);
//setTitle(String.valueOf(position));
switch (position) {
case 0:
setTitle("Depremler");
break;
case 1:
setTitle("Büyük Depremler");
break;
case 2:
setTitle("Önemli Depremler");
break;
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3040537334820006/3047566211">
</com.google.android.gms.ads.AdView>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation"/>
Your advice important for me
Thank you very much !
I need help to fix a problem with PreferenceFragmentCompat, which simply doesn't shows, no error in console, no message, nothing, just a blank page.
The fragment is shown as a result of the selection of an element in a navigation drawer. Here's the Activity XML:
<?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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/list_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="24dp"
android:layout_marginEnd="24dp"
app:elevation="12dp"
app:fabSize="normal"
app:rippleColor="#color/nowControlsNormal"
app:srcCompat="#android:drawable/ic_popup_sync" />
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_top"
app:itemIconTint="#color/nowBlack"
app:itemTextColor="#color/nowBlack"
app:menu="#menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>
Here's the activity Java:
public class HomeActivity extends AppCompatActivity {
SharedPreferences settings;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
NavigationView nav;
TextView drawerUsername;
FloatingActionButton fab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = getSharedPreferences("NowCLOUD", 0);
setContentView(R.layout.activity_home);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
nav = (NavigationView) findViewById(R.id.drawer);
fab = (FloatingActionButton) findViewById(R.id.fab);
View headerView = nav.getHeaderView(0);
drawerUsername = (TextView) headerView.findViewById(R.id.drawer_username);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO: Gestire aggiornamento al click della FAB
}
});
getSupportActionBar().setTitle("Home");
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawerLayout.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportFragmentManager().beginTransaction()
.add(R.id.list_container, HomeFragment.newInstance())
.commit();
drawerUsername.setText(settings.getString("username", "Error"));
nav.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
drawerLayout.closeDrawers();
displayView(menuItem.getItemId());
return true;
}
});
}
public void displayView(int viewId) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (viewId) {
case R.id.drawer_home:
fragment = HomeFragment.newInstance();
title = "Home";
break;
case R.id.drawer_settings:
fragment = SettingsFragment.newInstance();
title = "Settings";
break;
}
if (fragment != null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.list_container, fragment)
.commit();
}
// set the toolbar title
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(title);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
}
Here's the Fragment's XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v7.preference.PreferenceCategory
android:title="#string/settings_synchronization">
<android.support.v7.preference.SwitchPreferenceCompat
android:key="auto_updates"
android:title="#string/settings_auto_sync" />
</android.support.v7.preference.PreferenceCategory>
</android.support.v7.preference.PreferenceScreen>
Here's the Fragment's Java:
public class SettingsFragment extends PreferenceFragmentCompat {
View rootView;
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.fragment_settings);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
//rootView = inflater.inflate(R.xml.fragment_settings,container,false);
return rootView;
}
#Override
public PreferenceFragmentCompat getCallbackFragment() {
return this;
}
public static SettingsFragment newInstance() {
Bundle args = new Bundle();
SettingsFragment fragment = new SettingsFragment();
fragment.setArguments(args);
return fragment;
}
}
I've removed all of the code which was useless for this purpose. Sorry for the huge amount of code.
I've figured it out by myself:
when you are using preferencefragment you cannot override the onCreateView method, or you'll get a blank screen.
I'm trying to develop an app for bikes with multiple activities.
I developed a navigation drawer with multiple activities and toolbar instead of actionbar. In the main activity(TimerActivity), the toolbar works, but it doesn't works in the other activities when I click on the icon_drawer (it works only with the swipe). How can I fix this problem?
It seems that the toolbar is covered by another layout located above.
Sorry for bad english.
TimerActivity.java
public class TimerActivity extends ActionBarActivity {
private static String CLASS_NAME;
private Vibrator vibrate;
private Notify notify;
protected Toolbar toolbar;
protected DrawerLayout drawerLayout;
protected ActionBarDrawerToggle drawerToggle;
public ListView leftDrawerList;
protected ArrayList<navDrawerItems> NavDrawerItems;
protected NavDrawerListAdapter adapter;
private TypedArray navMenuIcons;
protected String[] navMenuTitles;
private CharSequence mTitle;
private LinearLayout linearLayout;
protected FrameLayout frameLayout;
public TimerActivity() {
CLASS_NAME = getClass().getName();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(CLASS_NAME, "onCreate");
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout) findViewById(R.id.frame_container);
initView();
toolbar.setTitle("Navigation Drawer");
setSupportActionBar(toolbar);
initDrawer();
leftDrawerList.setOnItemClickListener(new SlideMenuClickListener());
}
protected void initView() {
//load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
//nav drawer icons from resources
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
leftDrawerList = (ListView) findViewById(R.id.left_drawer);
toolbar = (Toolbar) findViewById(R.id.toolbar);
NavDrawerItems = new ArrayList<navDrawerItems>();
//adding nav drawer items to array
//Home
NavDrawerItems.add(new navDrawerItems(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
//User
NavDrawerItems.add(new navDrawerItems(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
//Map
NavDrawerItems.add(new navDrawerItems(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
//Routes
NavDrawerItems.add(new navDrawerItems(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
//Camera
NavDrawerItems.add(new navDrawerItems(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
//Weather
NavDrawerItems.add(new navDrawerItems(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
//Recycle the typed array
navMenuIcons.recycle();
//setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),NavDrawerItems);
leftDrawerList.setAdapter(adapter);
}
protected void initDrawer() {
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
private void gotoRoutes() {
Log.d(CLASS_NAME, "gotoRoutes");
Intent routes = new Intent(this, RoutesActivity.class);
startActivity(routes);
}
/**
* Called when the settings button is clicked on.
*
* #param view
* the button clicked on
*/
public void gotoSettings(View view) {
Log.d(CLASS_NAME, "gotoSettings");
Intent settings = new Intent(this, SettingsActivity.class);
startActivity(settings);
}
private void gotoHome() {
Log.d(CLASS_NAME, "gotoHome");
Intent timer = new Intent(this, TimerActivity.class);
timer.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(timer);
}
public void gotoMap() {
Log.d(CLASS_NAME, "gotoMap");
Intent map = new Intent(this, MapActivity.class);
startActivity(map);
}
public void gotoPhoto() {
Log.d(CLASS_NAME, "gotoPhoto");
Intent photo = new Intent(this, PhotoActivity.class);
startActivity(photo);
}
public void shareActivity() {
Log.d(CLASS_NAME, "shareActivity");
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case android.R.id.home:
gotoHome();
return true;
case R.id.menu_settings:
gotoSettings(null);
return true;
case R.id.menu_routes:
gotoRoutes();
return true;
case R.id.menu_map:
gotoMap();
return true;
case R.id.menu_photo:
gotoPhoto();
return true;
case R.id.menu_share:
shareActivity();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void notification(String title, String message) {
notify.notify(title, message);
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d(CLASS_NAME, "onDestroy");
}
#Override
public void onRestart() {
super.onRestart();
Log.d(CLASS_NAME, "onRestart");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(CLASS_NAME, "onCreateOptionsMenu");
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
/**
* Keep the screen on depending on the stay awake setting
*/
protected void stayAwakeOrNot() {
Log.d(CLASS_NAME, "stayAwakeOrNot");
Settings settings = ((OnYourBike) getApplication()).getSettings();
if (settings.isCaffeinated(this)) {
// Log.i(CLASS_NAME, "Staying awake");
getWindow()
.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
// Log.i(CLASS_NAME, "Not staying awake");
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
public void takePhoto(View view){
Log.d(CLASS_NAME, "takePhoto");
//if(camera.hasCamera() && camera.hasCameraApplication()){
// camera.takePhoto();
Intent photo = new Intent(this, PhotoActivity.class);
startActivity(photo);
}
public void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch(position){
case 0:
Intent intent1 = new Intent(this, CounterActivity.class);
startActivity(intent1);
finish();
break;
case 1:
Intent intent2 = new Intent(this, UserActivity.class);
startActivity(intent2);
finish();
break;
case 2:
Intent intent3 = new Intent(this, MapActivity.class);
startActivity(intent3);
finish();
break;
case 3:
Intent intent4 = new Intent(this, RoutesActivity.class);
startActivity(intent4);
finish();
break;
case 4:
Intent intent5 = new Intent(this, PhotoActivity.class);
startActivity(intent5);
finish();
break;
case 5:
Intent intent6 = new Intent(this, FragmentWeather.class);
startActivity(intent6);
finish();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
leftDrawerList.setItemChecked(position,true);
leftDrawerList.setSelection(position);
//setTitle(navMenuTitles[position]);
//drawerLayout.closeDrawer(leftDrawerList);
drawerLayout.closeDrawer(leftDrawerList);
toolbar.setTitle(navMenuTitles[position]);
}
else{
// error in creating fragment
Log.e("TimerActivity", "Error in creating fragment");
}
}
class SlideMenuClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
displayView(position);
}
}
}
activity_main.xml (layout for TimerActivity)
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar" />
</LinearLayout>
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#A0E843"
android:divider="#eee"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
UserActivity.java
public class UserActivity extends TimerActivity {
private int position;
private DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_user);
getLayoutInflater().inflate(R.layout.activity_user, frameLayout );
leftDrawerList.setItemChecked(position,true);
toolbar = (Toolbar) findViewById(R.id.toolbar);
//frameLayout = (FrameLayout) findViewById(R.id.frame_container);
toolbar.setTitle("Useeeer activity");
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
setSupportActionBar(toolbar);
super.initView();
super.initDrawer();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.user, menu);
return true;
}
}
user_activity.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="#layout/toolbar" />
</FrameLayout>
<!-- Framelayout to display Fragments -->
<!-- navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#A0E843"
android:divider="#eee"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
what i see from your codes is in the working activity you use toolbar in this way :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar" />
</LinearLayout>
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
which is true!
but the other layout does not follow this structure , its like this :
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="#layout/toolbar" />
</FrameLayout>
try to fix this and tell me if it works!
I have had problems like this before. Your activities could be taking the focus away from your toolbar.
In your Activity XML which contains your toolbar, try adding these to your <Toolbar> item in the .xml file:
android:descendantFocusability="false
android:focusable="false"
Try to change activity_main.xml - move the toolbar outside to DrawerLayout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar" />
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp" >
</LinearLayout>
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#A0E843"
android:divider="#eee"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>