I have two problems with the following code. This code is supposed to provide an overflow menu with three options, left, right and down. They must move the button accordingly with default animations. The first problem is that the TransitionManager doesn't work at all. The button moves with no animation whatsoever. And the second problem is, when the button moves to left and right, it gets lost behind action bar, what can I do about it?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#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_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Button button = (Button)findViewById(R.id.button);
RelativeLayout main_view = (RelativeLayout) findViewById(R.id.main_view);
switch(item.getItemId()){
case R.id.right:
if(item.isChecked()){
item.setChecked(false);
}
else
item.setChecked(true);
TransitionManager.beginDelayedTransition(main_view);
RelativeLayout.LayoutParams positionRulesRight = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
positionRulesRight.addRule(RelativeLayout.ALIGN_PARENT_TOP);
positionRulesRight.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(positionRulesRight);
return true;
case R.id.left:
if(item.isChecked()){
item.setChecked(false);
}
else
item.setChecked(true);
TransitionManager.beginDelayedTransition(main_view);
RelativeLayout.LayoutParams positionRulesLeft = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
positionRulesLeft.addRule(RelativeLayout.ALIGN_PARENT_TOP);
positionRulesLeft.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
button.setWidth(250);
button.setHeight(250);
button.setLayoutParams(positionRulesLeft);
return true;
case R.id.down:
if(item.isChecked()){
item.setChecked(false);
}
else
item.setChecked(true);
TransitionManager.beginDelayedTransition(main_view);
RelativeLayout.LayoutParams positionRulesDown = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
positionRulesDown.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
positionRulesDown.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(positionRulesDown);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.revolise.animations.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main"
android:id="#+id/include" />
<Button
android:id="#+id/button"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="70dp"
android:text="#string/button_text"/>
<RelativeLayout
android:id="#+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</RelativeLayout>
Related
I've constructed a simple app that consist of a FloatingActionButton and a Snackbar.
When the user clicks the FAB, the actual date should be displayed in main content of app.
Unfortunately, when I'm clicking on the FAB only the Snackbar's working. ListItems doesn't show anything.
What's wrong with my code?
MainActivity.java:
public class MainActivity extends AppCompatActivity {
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
private ListView myListView;
#Override
protected void onStart() {
super.onStart();
myListView = findViewById(R.id.listView);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listItems);
myListView.setAdapter(adapter);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addListItem();
Snackbar.make(view, "Item added to list", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#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_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 addListItem(){
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss MM/dd/yyyy", Locale.US);
listItems.add(dateFormat.format(new Date()));
adapter.notifyDataSetChanged();
}
}
XML of it:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.FabExample.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/Theme.FabExample.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/content_main" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#drawable/ic_add_entry" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Fragment's java:
public class FirstFragment extends Fragment {
#Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false);
}
public void onViewCreated(#NonNull View view, Bundle savedState) {
super.onViewCreated(view, savedState);
}
}
Fragment's XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstFragment">
<ListView
android:id="#+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
tools:layout_editor_absoluteX="117dp"
tools:layout_editor_absoluteY="332dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
I've not run your code, but it looks pretty good overall. Items are being added to the list and adapter.notifyDataSetChanged(); is being called afterwards. But one small item, you can call add directly on the adapter and it will notify regarding the change.
Platform source code for ArrayAdapter:
public void add(#Nullable T object) {
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.add(object);
} else {
mObjects.add(object);
}
mObjectsFromResources = false;
}
if (mNotifyOnChange) notifyDataSetChanged();
}
One thing that stands out, the ListView layout does not look right:
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstFragment">
<ListView
android:id="#+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
tools:layout_editor_absoluteX="117dp"
tools:layout_editor_absoluteY="332dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
It has no height or width
There are no constraints
I would suggest changing it to:
android:layout_width="match_parent"
android:layout_height="match_parent"
[Perhaps wrap_content depending on your requirements.]
Also be sure to add some constraints to your ConstraintLayout. If you're not sure, press "infer constraints" at the top of the design editor or use a simpler ViewGroup container. But the ones there now in the "tools:" namespace are only used by the Android Studio "design" tab. They are not meant for the actual device.
For example:
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
//Here is my java code
public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
BottomNavigationView bottomNavigationView;
NavigationView navigationView;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull final MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
HomeFragment homeFragment=new HomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout,homeFragment).commit();
return true;
case R.id.navigation_stylist:
StylistFragment stylistsFragment=new StylistFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction1=getSupportFragmentManager().beginTransaction();
fragmentTransaction1.replace(R.id.frameLayout,stylistsFragment).commit();
return true;
case R.id.navigation_apps:
MyapptsFragment myaaptsFragment=new MyapptsFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction2=getSupportFragmentManager().beginTransaction();
fragmentTransaction2.replace(R.id.frameLayout,myaaptsFragment).commit();
return true;
case R.id.navigation_tips:
HairtipsFragment hairtipsFragment=new HairtipsFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction3=getSupportFragmentManager().beginTransaction();
fragmentTransaction3.replace(R.id.frameLayout,hairtipsFragment).commit();
return true;
case R.id.navigation_account:
AccountFragment accountFragment=new AccountFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction4=getSupportFragmentManager().beginTransaction();
fragmentTransaction4.replace(R.id.frameLayout,accountFragment).commit();
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//start onboarding when app is opening first time
isUserFirstTime = Boolean.valueOf(Utils.readSharedSetting(HomeActivity.this, PREF_USER_FIRST_TIME, "true"));
Intent introIntent = new Intent(HomeActivity.this, OnboardingActivity.class);
introIntent.putExtra(PREF_USER_FIRST_TIME, isUserFirstTime);
if (isUserFirstTime)
startActivity(introIntent);
setContentView(R.layout.activity_home);
bottomNavigationView = findViewById(R.id.bottom_navigation);
navigationView=findViewById(R.id.nav_drawer);
//bottom navigationview listener
bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
//navigation drawer listener
navigationView.setNavigationItemSelectedListener(this);
//open home fragment on first launch
HomeFragment homeFragment=new HomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout,homeFragment).commit();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
HomeFragment homeFragment=new HomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout,homeFragment).commit();
return true;
case R.id.nav_products:
StylistFragment stylistsFragment=new StylistFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction1=getSupportFragmentManager().beginTransaction();
fragmentTransaction1.replace(R.id.frameLayout,stylistsFragment).commit();
return true;
case R.id.nav_promotions:
MyapptsFragment myaaptsFragment=new MyapptsFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction2=getSupportFragmentManager().beginTransaction();
fragmentTransaction2.replace(R.id.frameLayout,myaaptsFragment).commit();
return true;
case R.id.nav_purchases:
HairtipsFragment hairtipsFragment=new HairtipsFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction3=getSupportFragmentManager().beginTransaction();
fragmentTransaction3.replace(R.id.frameLayout,hairtipsFragment).commit();
return true;
case R.id.nav_settings:
AccountFragment accountFragment=new AccountFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction4=getSupportFragmentManager().beginTransaction();
fragmentTransaction4.replace(R.id.frameLayout,accountFragment).commit();
return true;
}
return false;
}
}
// Here is my 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"
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">
<android.support.design.widget.NavigationView
android:id="#+id/nav_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:theme="#style/menu_text_style"
app:menu="#menu/navigation_drawer" />
<!--app:headerLayout="#layout/nav_header_main"-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/shadow"
android:animateLayoutChanges="true">
</FrameLayout>
<View
android:id="#+id/shadow"
android:layout_width="match_parent"
android:layout_height="#dimen/_1sdp"
android:layout_above="#id/bottom_navigation"
android:background="#color/shadow"/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemIconTint="#color/navigationitem"
app:itemTextColor="#color/navigationitem"
app:menu="#menu/navigation_item"/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
I want to create both navigation drawer and bottom navigation in the same activity. I created XML design now I have to write java code for both. Bottom navigation is working perfectly but navigation item clicklistener is not working. I created fragments for navigation drawer and also for bottom navigation. When I click the items in navigation drawer it's not redirecting to respective fragment. The given clicklistener for navigation drawer is not at all working.
Solution:
Try the following steps to get it working:
Step1: Implement onNavigationItemSelected as shown below:
.... extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Step2: Use the method generated by the interface as:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.your_drawer_item_id:
....
....
case R.id.your_drawer_item_id:
....
....
}
}
Step3: In your class declare a Global variable:
public NavigationView navigationView;
Step4: In your onCreate() initialize the variable as:
navigationView = (NavigationView) findViewById(R.id.your_nav_view);
Finally: Setup the navigationView:
navigationView.setNavigationItemSelectedListener(this);
That's it.
Hope it works.
DrawerLayout 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"
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">
<include
layout="#layout/app_bar_vendor_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_drawer_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/White"
android:fitsSystemWindows="true"
app:itemBackground="#android:color/white"
app:itemIconTint="#color/bottom_color"
app:itemTextColor="#color/bottom_color"
app:headerLayout="#layout/nav_header_vendor_drawer_layout"
app:menu="#menu/vendor_drawer_menu" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
app_bar_vendor_drawer_layout xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="logixtic.android.web.vd.driver.activities.DriverDrawerActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/MyMaterialTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/Project_Standard"
app:popupTheme="#style/MyMaterialTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:layout_above="#+id/bottom_navigation">
</FrameLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#drawable/toolbar_dropshadow_above"
android:layout_above="#id/bottom_navigation"/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/White"
app:itemIconTint="#color/bottom_color"
app:itemTextColor="#color/bottom_color"
app:menu="#menu/vendor_bottom_navigation"/>
</RelativeLayout>
Try it.
You can use on the same activity using a method that recive a MenuItem parameter like this:
private void myClickItem(MenuItem item){
switch (item.getItemId()) {
case R.id.bottom_home:
// DO SOMETHING
break;
case R.id.drawer_main:
// DO SOMETHING
break;
}
}
Then, call like this:
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(item -> { // using lamda
myClickItem(item); //call here
return true;
});
NavigationView navigationView = findViewById(R.id.nv);
navigationView.setNavigationItemSelectedListener(item -> {
myClickItem(item); // call the same method here
drawerLayout.closeDrawers(); // bonus: hide navigation after click
return false;
});
You don't need to modify your xml res, it'll work fine.
before I only used activity's and that worked but when I added the fragments to the activity's the menu icon disappeared and I could find a fix for it
my custom toolbar
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 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="wrap_content"
android:background="#color/apptheme"
app:theme="#style/AlertDialog.AppCompat.Light">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="100">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/actionbar_logo_click"
android:paddingLeft="3dp"
android:layout_weight="5">
<ImageView
android:id="#+id/actionbar_logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingRight="10dp" />
</FrameLayout>
<TextView
android:id="#+id/actionbar_titel"
style="#style/AudioFileInfoOverlayText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingTop="7dp"
android:textColor="#color/white"
android:textSize="27dp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
my menu items
<?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"
android:icon="#drawable/ic_more_vert_white_24dp"
app:showAsAction="always">
<item
android:id="#+id/menu_rate"
android:title="#string/menu_rate"
/>
<item
android:id="#+id/menu_about"
android:title="#string/menu_about" />
</menu>
my activity xml layout where I add the custom toolbar with the id
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/app_bar"></include>
<!-- Container for loading and the actual fragment-->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="#+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
android:indeterminate="true" />
</LinearLayout>
</FrameLayout>
my java activity where I call the toolbar and also inflate the menu items
#EActivity(R.layout.activity_main_layout)
public class MainActivity extends BaseActivity {
#ViewById(R.id.adview)
MoPubView mMoPubView;
#ViewById(R.id.app_bar)
Toolbar toolbar;
#ViewById(R.id.actionbar_logo)
ImageView mAactionbarImage;
#ViewById(R.id.actionbar_titel)
TextView mActionbarTitel;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setSupportActionBar(toolbar);
}
#AfterViews
protected void onViewsInitialized() {
gotoFragment(MainFragment_.builder().build(), false);
mAactionbarImage.setImageResource(R.mipmap.ic_launcher);
//Set the titel of the actionbar
mActionbarTitel.setText("How To Twitch");
}
#Override
public void onBackPressed() {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
menu.getItem(0).setIcon(R.drawable.ic_more_vert_white_24dp);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_rate:
final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
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("http://play.google.com/store/apps/details?id=" + appPackageName)));
}
break;
case R.id.menu_about:
Intent aboutIntent = new Intent(MainActivity.this, AboutActivity_.class);
startActivity(aboutIntent);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
for some reason my menu Icon is not showing up in the toolbar so if someone knows what I am doing wrong that would help me alot
I think you are missing an overridden method onPrepareOptionsMenu
Try to add this overridden method and check:
#Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id. menu_rate).setVisible(true);
menu.findItem(R.id. menu_about).setVisible(true);
super.onPrepareOptionsMenu(menu);
}
And add setHasOptionsMenu(true); within onCreateView() of your fragment.
here is the image
i am using BottomNavigationView to create this(click here is the image). i want center
image(pluse sign) big in height half on navigation bottom and half on
activity. i used ready activity while creating new project navigation, scrollingActivity, bottomNavigationActivity
this is my code
activity_main.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"
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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/app_bar_main"
android:layout_above="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="#menu/navigation" />
</RelativeLayout>
<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/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
// private TextView mTextMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);
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);
}
#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);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
// mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_explore:
// mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_more:
// mTextMessage.setText(R.string.title_notifications);
return true;
case R.id.navigation_request:
// mTextMessage.setText(R.string.title_notifications);
return true;
case R.id.navigation_my_diary:
// mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
}
app_bar_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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:contentScrim="#color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="#+id/toolbar">
<ImageView
android:background="#drawable/launch_banner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<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>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.NestedScrollView
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.MainActivity"
tools:showIn="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/large_text"
/>
</android.support.v4.widget.NestedScrollView>
please need suggestion
I am trying to implement Collapsabletoolbar, here is my code.
Main.xml
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">
<ImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/hgj_nav"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
<android.support.design.widget.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_done" />
</android.support.design.widget.CoordinatorLayout>
<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:background="#009688"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer"/>
</android.support.v4.widget.DrawerLayout>
In java:
public class MainActivityTab extends AppCompatActivity {
// Declaring Your View and Variables
Toolbar toolbar;
CharSequence HGJTitles[]={"Recent News","Category"};
int HGJNumboftabs =2;
private DrawerLayout mDrawerLayout;
private AdView mAdView;
private StartAppAd startAppAd = new StartAppAd(this);
private InterstitialAd interstitial;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StartAppAd.init(this, getString(R.string.startapp_dev_id), getString(R.string.startapp_app_id));
setContentView(R.layout.activity_main_tab);
// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
actionBar.setDisplayHomeAsUpEnabled(true);
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle("");
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
Toast.makeText(MainActivityTab.this, menuItem.getTitle(), Toast.LENGTH_LONG).show();
return true;
}
});
StartAppAd.showSlider(this);
mAdView = (AdView) findViewById(R.id.adView);
mAdView.loadAd(new AdRequest.Builder().build());
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(MainActivityTab.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
AdRequest adRequest = new AdRequest.Builder().build();
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
HitGovtJobAdapter adapter = new HitGovtJobAdapter(getSupportFragmentManager(),HGJTitles,HGJNumboftabs);
ViewPager viewPager = (ViewPager)findViewById(R.id.pager);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout)findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
MainActivityTab.this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
static class HitGovtJobAdapter extends FragmentStatePagerAdapter {
CharSequence HGJTitles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int HGJNumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
// Build a Constructor and assign the passed Values to appropriate values in the class
public HitGovtJobAdapter(FragmentManager fm,CharSequence hgjTitles[], int hgjTabNum) {
super(fm);
this.HGJTitles = hgjTitles;
this.HGJNumbOfTabs = hgjTabNum;
}
//This method return the fragment for the every position in the View Pager
#Override
public Fragment getItem(int position) {
if(position == 0) // if the position is 0 we are returning the First tab
{
News_Recent newsRecent = new News_Recent();
return newsRecent;
}
else // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
{
News_Category newsCategory = new News_Category();
return newsCategory;
}
}
// This method return the titles for the Tabs in the Tab Strip
#Override
public CharSequence getPageTitle(int position) {
return HGJTitles[position];
}
// This method return the Number of tabs for the tabs Strip
#Override
public int getCount() {
return HGJNumbOfTabs;
}
}
public static class HitGovtJobFragment extends Fragment {
private static final String TAB_POSITION = "tab_position";
public HitGovtJobFragment() {
}
public static HitGovtJobFragment newInstance(int tabPosition) {
HitGovtJobFragment fragment = new HitGovtJobFragment();
Bundle args = new Bundle();
args.putInt(TAB_POSITION, tabPosition);
fragment.setArguments(args);
return fragment;
}
}
#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 menuItem) {
switch (menuItem.getItemId()) {
case R.id.refresh:
finish();
startActivity(getIntent());
overridePendingTransition(R.anim.open_next, R.anim.close_next);
return true;
case R.id.menu_favorite:
startActivity(new Intent(getApplicationContext(), News_Favorite.class));
return true;
case R.id.menu_about:
Intent about = new Intent(getApplicationContext(), About_Us.class);
startActivity(about);
return true;
case R.id.menu_moreapp:
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(getString(R.string.play_more_apps))));
return true;
case R.id.menu_rateapp:
final String appName = getApplicationContext().getPackageName();
try {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + appName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id="
+ appName)));
}
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
#Override
protected void onPause() {
mAdView.pause();
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
mAdView.resume();
startAppAd.onResume();
}
#Override
protected void onDestroy() {
mAdView.destroy();
super.onDestroy();
}
public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}
But with above code toolbar not collapsed and viewpager scrolls freely. See this image.
And tablayout also not properly placed.
Any idea how can I fix this problem?
Thank you very much in advance.
Use this layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/r`enter code here`es/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"`enter code here`
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
android:background="#color/colorAccent"
app:tabMode="scrollable"
app:tabContentStart="72dp" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="parallax" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="8dp"
android:src="#drawable/ic_menu_camera"
app:layout_anchor="#id/tabLayout"
app:layout_anchorGravity="center|left|start"
app:fabSize="mini"
app:borderWidth="0dp" />
</android.support.design.widget.CoordinatorLayout>
<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/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
and than your activity
Note ItemFragment can be your fragment having list ItemFragment i am not enclosing this fragment:
package com.example.a61378.navigation;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
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 com.example.a61378.navigation.dummy.DummyContent;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,ItemFragment.OnListFragmentInteractionListener {
ViewPager mViewPager;
TabLayout mTabLayout;
String tabPageTitles[] = {"Tab1", "Tab2"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
FragmentManager fragment =getSupportFragmentManager();
FragmentHomePagerAdapter fragmentHomePagerAdapter = new FragmentHomePagerAdapter(fragment, tabPageTitles.length);
mViewPager.setAdapter(fragmentHomePagerAdapter);
mTabLayout.setupWithViewPager(mViewPager);
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();
}
});
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onListFragmentInteraction(DummyContent.DummyItem item) {
}
class FragmentHomePagerAdapter extends android.support.v4.app.FragmentPagerAdapter {
private int mTabCount;
public FragmentHomePagerAdapter(FragmentManager fm, int tabCount) {
super(fm);
mTabCount = tabCount;
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
ItemFragment ViewGroceryfragment = ItemFragment.newInstance(10);
return ViewGroceryfragment;// Fragment # 0 - This will show FirstFragment
}
#Override
public CharSequence getPageTitle(int position) {
return tabPageTitles[position] ;
}
#Override
public int getCount() {
return mTabCount;
}
}
#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);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
You can use Nested scrollview to achieve this
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabGravity="fill"
app:tabIndicatorColor="#color/dark_orange"
app:tabIndicatorHeight="4dp"
app:tabMode="scrollable"
/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
I have just did it in my project and its working like a boss :P
Edit 1
<?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_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">
<ImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/hgj_nav"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/tool_bar"
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>
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="4dp"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
>
<android.support.design.widget.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_done" />
</android.support.design.widget.CoordinatorLayout>
<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:background="#009688"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer"/>
</android.support.v4.widget.DrawerLayout>
I made a sample project which might help you in what you are looking for it has navigation view collapsing toolbar with viewpager and tabs over viewpager.