-2

I have a xml file(fragment_about_sl) in which I added a coordinatorLayout.The code is given below.After adding that and running the app on device the specific layout shows two toolbars.I think one is the default toolbar created in the MainActivity.java and the other is the blank toolbar created inside the coordinatorLayout.I have used a class extended to fragment.java for the fragment_about_sl.xml and the reason for extending to that is becuase I used a navigationDrawer.

I want to know how to remove the default toolbar and replace it with the toolbar created in the coordinatorLayout.Also when I run the app the toolbar created in the coordinatorLayout is blank.I want that to have everything whats on the default toolbar which is a hamburger icon,layout name and 3 dots for settings in the new toolbar.

fragment_about_sl

<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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.birjuvachhani.myapplication.ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:titleEnabled="false"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_collapseMode="parallax">

                    <android.support.v4.view.ViewPager
                        android:id="@+id/viewPager"
                        android:layout_width="match_parent"
                        android:layout_height="250dp" />

                    <LinearLayout
                        android:id="@+id/SliderDots"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@id/viewPager"
                        android:layout_marginBottom="20dp"
                        android:gravity="center_horizontal"
                        android:orientation="horizontal" />

            </RelativeLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

    <!-- Main content goes here. -->

</android.support.v4.widget.NestedScrollView>

aboutSLFragment.java

public class aboutSLFragment extends Fragment{

ViewPager viewPager;
LinearLayout sliderDotspanel;
private int dotscount;
private ImageView[] dots;
TextView aboutslintro;
Button readmorebtn1;
TextView aboutslhistory;
Button readmorebtn2;
TextView aboutslquickfacts;
Button readmorebtn3;

public aboutSLFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_about_sl, container, false);
    viewPager = (ViewPager) rootView .findViewById(R.id.viewPager);

    sliderDotspanel = (LinearLayout)rootView.findViewById(R.id.SliderDots);

    ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getContext());

    viewPager.setAdapter(viewPagerAdapter);

    dotscount = viewPagerAdapter.getCount();
    dots = new ImageView[dotscount];

    for(int i = 0; i < dotscount; i++){

        dots[i] = new ImageView(getContext());
        dots[i].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.non_active_dot));

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        params.setMargins(8, 0, 8, 0);

        sliderDotspanel.addView(dots[i], params);

    }

    dots[0].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot));

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

            for(int i = 0; i< dotscount; i++){
                dots[i].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.non_active_dot));
            }

            dots[position].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot));

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    aboutslintro=(TextView)rootView.findViewById(R.id.aboutslintro);
    readmorebtn1=(Button)rootView.findViewById(R.id.readmorebtn1);
    readmorebtn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (readmorebtn1.getText().toString().equalsIgnoreCase("Read 
More"))
            {
                aboutslintro.setMaxLines(Integer.MAX_VALUE);//your TextView
                readmorebtn1.setText("Read Less");
            }
            else
            {
                aboutslintro.setMaxLines(5);//your TextView
                readmorebtn1.setText("Read More");
            }
        }
    });

    aboutslhistory=(TextView)rootView.findViewById(R.id.aboutslhistory);
    readmorebtn2=(Button)rootView.findViewById(R.id.readmorebtn2);
    readmorebtn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (readmorebtn2.getText().toString().equalsIgnoreCase("Read 
More"))
            {
                aboutslhistory.setMaxLines(Integer.MAX_VALUE);//your 
TextView
                readmorebtn2.setText("Read Less");
            }
            else
            {
                aboutslhistory.setMaxLines(5);//your TextView
                readmorebtn2.setText("Read More");
            }
        }
    });

    aboutslquickfacts=
(TextView)rootView.findViewById(R.id.aboutslquickfacts);
    readmorebtn3=(Button)rootView.findViewById(R.id.readmorebtn3);
    readmorebtn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (readmorebtn3.getText().toString().equalsIgnoreCase("Read 
More"))
            {
                aboutslquickfacts.setMaxLines(Integer.MAX_VALUE);//your 
TextView
                readmorebtn3.setText("Read Less");
            }
            else
            {
                aboutslquickfacts.setMaxLines(5);//your TextView
                readmorebtn3.setText("Read More");
            }
        }
    });
    // Inflate the layout for this fragment
    return rootView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    //you can set the title for your toolbar here for different fragments 
different titles
    getActivity().setTitle("About Sri Lanka");
}

}

MainActivity.java

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, 
R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) 
findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    displaySelectedScreen(R.id.nav_home);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


private void displaySelectedScreen(int itemId) {

    //creating fragment object
    Fragment fragment = null;

    //initializing the fragment object which is selected
    switch (itemId) {
        case R.id.nav_home:
            fragment = new homeFragment();
            break;
        case R.id.nav_aboutSL:
            fragment = new aboutSLFragment();
            break;
        case R.id.nav_topLandmarks:
            fragment = new topLandmarksFragment();
            break;
        case R.id.nav_searchArea:
            fragment = new searchAreaFragment();
            break;
        case R.id.nav_searchCat:
            fragment = new searchCategoryFragment();
            break;
        case R.id.nav_events:
            fragment = new eventsFragment();
            break;
        case R.id.nav_transport:
            fragment = new transportFragment();
            break;
        case R.id.nav_commonPhrases:
            fragment = new commonPhrasesFragment();
            break;
        case R.id.nav_utilities:
            fragment = new utilitiesFragment();
            break;
    }

    //replacing the fragment
    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.commit();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    displaySelectedScreen(item.getItemId());
    return true;
}

}
C. Chan
  • 57
  • 5

2 Answers2

1

Use Theme.AppCompat.Light.NoActionBar in style.xml

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Syed
  • 166
  • 3
  • 15
0

If you are including your toolbar in xml layout, then you have to set activity as no action bar activity. For that follow simple steps.

  • in manifest

    <activity                                                     
        android:name="yourActivity"
        android:theme="@style/ThemeNoActionBar" />
    
  • in res-values-style file paste this line

    <style name="ThemeNoActionBar" parent="yourAppTheme">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • cannot do that because the activity is extended to fragment.java and when adding the activity in manifest, error occurs saying activity not assignable to android.app.activity.What to do now? – C. Chan Mar 03 '18 at 17:15
  • Look brother, if class extends fragment then it is a fragment class, not activity class. you can do 2 things now, 1> you can remove toolbar from fragment xml and change title of parent activity class, 2> you can put answered style on fragment's activity from where you launch fragment. – Khemraj Sharma Mar 04 '18 at 07:04
  • Hello Khemraj can you tell me how to do the option 2. I am new to android – C. Chan Mar 05 '18 at 02:52
  • Put answered theme for MainActivity. But it will remove toolbar for your all fragments. – Khemraj Sharma Mar 05 '18 at 05:42