Created
July 7, 2017 19:49
-
-
Save groverankush/9d09fbba07879790d5395434fda1e2d4 to your computer and use it in GitHub Desktop.
Collapsing toolbar in one fragment, Normal toolbar in others
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/drawer_layout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:fitsSystemWindows="true" | |
tools:openDrawer="start"> | |
<FrameLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<FrameLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:id="@+id/container"/> | |
<android.support.v7.widget.Toolbar | |
android:id="@+id/toolbar" | |
android:layout_width="match_parent" | |
android:layout_height="?attr/actionBarSize" | |
android:background="?attr/colorPrimary" | |
app:elevation="4dp" | |
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" | |
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> | |
</FrameLayout> | |
<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:menu="@menu/activity_main_drawer" /> | |
</android.support.v4.widget.DrawerLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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:background="@android:color/background_light" | |
android:fitsSystemWindows="true" | |
> | |
<android.support.design.widget.AppBarLayout | |
android:id="@+id/main.appbar" | |
android:layout_width="match_parent" | |
android:layout_height="300dp" | |
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" | |
android:fitsSystemWindows="true" | |
> | |
<android.support.design.widget.CollapsingToolbarLayout | |
android:id="@+id/main.collapsing" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
app:layout_scrollFlags="scroll|exitUntilCollapsed" | |
android:fitsSystemWindows="true" | |
app:contentScrim="?attr/colorPrimary" | |
app:expandedTitleMarginStart="48dp" | |
app:expandedTitleMarginEnd="64dp" | |
> | |
<ImageView | |
android:id="@+id/main.backdrop" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:scaleType="centerCrop" | |
android:src="@drawable/bg" | |
android:fitsSystemWindows="true" | |
app:layout_collapseMode="parallax" | |
/> | |
<android.support.v7.widget.Toolbar | |
android:id="@+id/main.toolbar" | |
android:layout_width="match_parent" | |
android:layout_height="?attr/actionBarSize" | |
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" | |
app:layout_collapseMode="pin" | |
/> | |
</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" | |
> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:textSize="20sp" | |
android:lineSpacingExtra="8dp" | |
android:text="@string/lorel" | |
android:padding="@dimen/activity_horizontal_margin" | |
/> | |
</android.support.v4.widget.NestedScrollView> | |
</android.support.design.widget.CoordinatorLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle; | |
import android.support.design.widget.FloatingActionButton; | |
import android.support.design.widget.Snackbar; | |
import android.support.v4.app.FragmentTransaction; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v4.view.MarginLayoutParamsCompat; | |
import android.view.View; | |
import android.support.design.widget.NavigationView; | |
import android.support.v4.view.GravityCompat; | |
import android.support.v4.widget.DrawerLayout; | |
import android.support.v7.app.ActionBarDrawerToggle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.ViewGroup; | |
import android.widget.FrameLayout; | |
import com.example.ankush.bottomnavigation.R; | |
import com.example.ankush.bottomnavigation.fragments.OtherFragment; | |
import com.example.ankush.bottomnavigation.fragments.SpecialFragment; | |
public class MainActivity extends AppCompatActivity | |
implements NavigationView.OnNavigationItemSelectedListener { | |
private DrawerLayout drawer; | |
private Toolbar toolbar; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
init(); | |
} | |
private void init() { | |
toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
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); | |
} | |
@Override | |
public void onBackPressed() { | |
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); | |
if (drawer.isDrawerOpen(GravityCompat.START)) { | |
drawer.closeDrawer(GravityCompat.START); | |
} else { | |
super.onBackPressed(); | |
} | |
} | |
@SuppressWarnings("StatementWithEmptyBody") | |
@Override | |
public boolean onNavigationItemSelected(MenuItem item) { | |
// Handle navigation view item clicks here. | |
int id = item.getItemId(); | |
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); | |
if (id == R.id.special) { | |
ft.replace(R.id.container, new SpecialFragment()); | |
toolbar.setBackgroundColor(ContextCompat.getColor(this, android.R.color.transparent)); | |
} else if (id == R.id.fragment1) { | |
ft.replace(R.id.container, new OtherFragment()); | |
toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary)); | |
} else if (id == R.id.fragment2) { | |
ft.replace(R.id.container, new OtherFragment()); | |
toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary)); | |
} | |
ft.commit(); | |
drawer.closeDrawer(GravityCompat.START); | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import com.example.ankush.bottomnavigation.R; | |
/** | |
* A simple {@link Fragment} subclass. | |
*/ | |
public class SpecialFragment extends Fragment { | |
public SpecialFragment() { | |
// Required empty public constructor | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
// Inflate the layout for this fragment | |
return inflater.inflate(R.layout.fragment_special, container, false); | |
} | |
} |
@devmichaelgirgis It will work. Two toolbars will be there but they should overlap, so you won't see them. Refer https://stackoverflow.com/a/44978781/2577750
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is that work for you? two toolbars appear