Hide support ActionBar while scrolling - android

I have an activity that has a support action bar, below that a sliding tab layout, below that a listview of images for the corresponding tab. I just want to hide the support action bar not the sliding tab strip and I am not able to hide it.
Here is my activity:
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
public class NewGalleriActivity extends AppCompatActivity implements ViewTreeObserver.OnScrollChangedListener {
Toolbar toolbar;
ViewPager viewPager;
NewGalleriPagerAdapter viewPagerAdapter;
SlidingTabLayout slidingTabLayout;
CharSequence Titles[] = {"Wildlife", "Architecture", "Black & White", "Close Up", "Night"};
int numOfTabs = 5;
private float mActionBarHeight;
private ActionBar mActionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_galleri);
final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
new int[]{android.R.attr.actionBarSize});
mActionBarHeight = styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
//Initialise views
viewPager = (ViewPager) findViewById(R.id.pager);
slidingTabLayout = (SlidingTabLayout) findViewById(R.id.tabs);
toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
mActionBar = getSupportActionBar();
int defaultValue = 0;
int page = getIntent().getIntExtra("ARG_PAGE", defaultValue);
viewPagerAdapter = new NewGalleriPagerAdapter(getSupportFragmentManager(), Titles, numOfTabs);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(page);
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return Color.parseColor("#FAC80A");
}
});
slidingTabLayout.setViewPager(viewPager);
if (android.os.Build.VERSION.SDK_INT >= 21)
{
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(Color.parseColor("#263238"));
}
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.ddd);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(NewGalleriActivity.this, ImageGridActivity.class);
startActivity(intent);
}
});
findViewById(R.id.parent).getViewTreeObserver().addOnScrollChangedListener(this);
}
#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_home, 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);
}
#Override
public void onScrollChanged() {
float y = findViewById(R.id.parent).getScrollY();
if(y >= mActionBarHeight && mActionBar.isShowing()) {
mActionBar.hide();
} else if(y==0 && !mActionBar.isShowing()) {
mActionBar.show();
}
}
}
Here is my layout for the activity:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/black"
android:orientation="vertical">
<include layout="#layout/toolbar2" />
<com.pipipzz.simpleapp.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#231F20" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#drawable/drop_shadows" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/ddd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="24dp"
android:layout_marginBottom="10dp"
android:src="#drawable/fab_icons"
app:borderWidth="#null"
app:elevation="4dp"
app:fabSize="normal" />
</FrameLayout>
</ScrollView>
</LinearLayout>
Here is my layout for the toolbar:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFC80A"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<ImageView
android:id="#+id/tool_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/tool_logo"
android:gravity="center_horizontal"
android:src="#drawable/logo_tool" />
</RelativeLayout>
<ImageView
android:id="#+id/tool_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="8dp"
android:contentDescription="#string/tool_search"
android:src="#drawable/search" />
</android.support.v7.widget.Toolbar>
Edit: Here is my layout for fragment that is shown in the tabs.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#231F20"
tools:context="com.pipipzz.simpleapp.ArchitectureFragment">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:dividerHeight="0dp"
android:scrollingCache="true"
android:smoothScrollbar="true" />
</FrameLayout>
Any help would be highly appreciated.

Please have a look at this library Android-ObservableScrollView
It has a lot of examples and it will be much easier to use it in your case.
There are several examples of using it with Toolbar, that you are currently using in your code.
It would be better if you prodived all source code to run it, maybe you have issue somewhere else.
First about your code. Don't use findViewById whenever you want, it is really heavy method. So when you are doing this each time onScrollViewChanged it makes your app laggy.
Find your scroll view only once in onCreate method
mMainScrollView = findViewById(R.id.parent);
mMainScrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {#Override
public void onScrollChanged() {
int scrollY = rootScrollView.getScrollY(); //for verticalScrollView
// Hide or show toolbar here
}
});
Why not directly to call hide on toolbar without getting support
action bar ? Try do to it directly on toolbar.
If you call hide
method directly on ActionBar/Toolbar does it work ?
Try to do something like this
To hide toolbar
toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
And show it again
toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();

Related

Adding Tablayout and Toolbar to the Top of the screen

I want to create DetailActivity for My app . I want To achieve layout some thing like This .
Look at to the green rectangle .it is contain Toolbar and Tablayout .
When we scroll down and when toolbar Touch the Tablayout . They pinned to the top of the screen.
I Think this Layout use the Scroll and ... and use The Toolbar and Tablayout in The Activity .
This is my code where I try to achieve That.but nothing ...
DeatailActivity.java
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import ir.dink.sevom.R;
import ir.dink.sevom.adapters.*;
public class DetailActivity extends AppCompatActivity {
private ViewPager detailActivityViewPager;
private TabLayout detailActivityTabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
initToolbar();
initTablayout();
}
//============================ Init Toolbar ===================================//
private void initToolbar() {
Toolbar mainToolbar = (Toolbar)findViewById(R.id.toolbar_detail_activity);
setSupportActionBar(mainToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
//========================== OnCreate Option Menu =====================================//
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//=============================== onOption Item Selected ================================//
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.search_action_toolbar:
// startActivity(new Intent(MainActivity.this ,DetailActivity.class ));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void initTablayout() {
detailActivityViewPager = (ViewPager) findViewById(R.id.view_pager_detail_activity);
detailActivityTabLayout = (TabLayout) findViewById(R.id.tab_layout_detail_activity);
FragmentManager manager = getSupportFragmentManager();
DetailActivityPagerAdapter detailadapter = new DetailActivityPagerAdapter(manager);
detailActivityViewPager.setAdapter(detailadapter);
detailActivityViewPager.setCurrentItem(detailActivityViewPager.getAdapter().getCount() - 1);
detailActivityTabLayout.setupWithViewPager(detailActivityViewPager);
detailActivityViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(detailActivityTabLayout));
}
}
And This is The Xml of That :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ir.dink.sevom.activities.DetailActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_detail_activity"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/transparent"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<ImageView
android:id="#+id/img_detail"
android:layout_width="96dp"
android:layout_height="124dp"
android:layout_below="#id/toolbar_detail_activity"
android:src="#drawable/ic_hamburger"
android:layout_alignParentRight="true"
android:layout_margin="8dp"/>
<TextView
android:id="#+id/txt_name_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="نام :"
android:layout_toLeftOf="#+id/img_detail"
android:layout_alignTop="#+id/img_detail"/>
<TextView
android:id="#+id/txt_count_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="تعداد"
android:layout_toLeftOf="#+id/img_detail"
android:layout_below="#+id/txt_name_detail"
android:layout_marginTop="8dp"/>
<TextView
android:id="#+id/txt_last_update_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="آخرین آپدیت"
android:layout_toLeftOf="#+id/img_detail"
android:layout_below="#+id/txt_count_detail"
android:layout_marginTop="8dp"/>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout_detail_activity"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#4d4d4d"
android:layout_below="#id/img_detail"
app:tabMode="scrollable"
/>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager_detail_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/tab_layout_detail_activity"/>
</RelativeLayout>

Android BottomSheet Initial Behaviour

So I want to implement a Bottom Sheet behavior and everything works fine except at first (when activity is created) the bottom sheet is not positioned how I configured it.
I can't find any information on why this is happening or if it is some configuration I am missing.
I prepared a sample code and some images to show the problem:
The activity (I haven't done anything, this is the initial template)
package com.example.testbottomsheet;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
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);
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();
}
});
}
#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);
}
}
The Layout
<?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"
android:fitsSystemWindows="true"
tools:context="com.example.flavio.testbottomsheet.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.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_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
<LinearLayout
android:id="#+id/transaction_history"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#android:color/holo_orange_dark"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="40dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<ImageView
android:id="#+id/bottomSheetHandler"
android:layout_width="match_parent"
android:layout_height="40dp"
android:contentDescription="Handles for bottom sheet"
android:src="#drawable/ic_expand_less_24dp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#156de8"></FrameLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
As one can see I have a peekHeight of 40dp, and it works fine after I expand and close it )or just a small flick on top of the bottom sheet), however it is not initially set like this. I colored everything to be easier to spot:
Open Pane
Should-be initial state
IS initial state
From what I could see the bottom sheet is being placed 20dp bellow of the desired number configured in the XML.
Its a bug in design support lib.. take a look at this link:
https://code.google.com/p/android/issues/detail?id=203057
Try adding android:fitsSystemWindows="true" to the LinearLayout that you have added the BottomSheetBehaviour to.

Android ListView doesn't display anything (Relative Layout)

I am having problems with ListView. It doesn't display. I know it's a common problem but I'm new to Android. In most examples, the problem is solved with layout_bellow property but I do not have any other controls.
Here is my XML code:
Here is my 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.gkmicro.trrrrrrrrrrrrrr.MainActivity"
tools:showIn="#layout/activity_main">
<ListView
android:id="#+id/myListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
And here is my MainActivity.java:
package com.gkmicro.trrrrrrrrrrrrrr;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
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);
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();
}
});
}
#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);
}
}
As your ListView is android:layout_alignParentBottom="true" it stick to bottom with android:layout_height="wrap_content"
So make ListView like this
<ListView
android:id="#+id/myListView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
Change your ListView to use all width and height of your parent RelativeLayout
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.gkmicro.trrrrrrrrrrrrrr.MainActivity"
tools:showIn="#layout/activity_main">
<ListView
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
By the way as recommended #Erik Jhordan Rey Caffrey use RecyclerView
EDIT: If you have any issues here is a good sample to implement a simple listView inside a RelativeLayout
Hope this helps!

Android drag margin for navigation drawer is off screen

I have recently created an app called Hoops and I have been developing it for quite some time now. However, the main problem that I seem to be getting, is that the navigation drawer's swipe margin is off screen and other users find it incredibly hard to use the navigation drawer's swipe feature. The menu button works just fine though. I have looked at other tutorials online and all of them seem to be showing the same answer as the one in the forum I have linked: Set drag margin for Android Navigation Drawer
However, I have tried this solution, and it does not seem to be working for me. If anyone has any ideas why it is not working please can you help me out. Here is the coding for the Main activity with the navigation drawer below:
package com.jehan.sportstutorials;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
Toolbar toolbar;
ActionBar actionBar;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
actionBar.setDisplayHomeAsUpEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
if (navigationView != null) {
setupNavigationDrawerContent(navigationView);
}
setupNavigationDrawerContent(navigationView);
}
#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) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupNavigationDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
textView = (TextView) findViewById(R.id.textView);
switch (menuItem.getItemId()) {
case R.id.item_navigation_drawer_basketball:
menuItem.setChecked(true);
textView.setText(menuItem.getTitle());
drawerLayout.closeDrawer(GravityCompat.START);
Intent i = new Intent(MainActivity.this, BasketballTutorial.class);
startActivity(i);
return true;
case R.id.item_navigation_drawer_help:
menuItem.setChecked(true);
textView.setText(menuItem.getTitle());
drawerLayout.closeDrawer(GravityCompat.START);
Intent intent2 = new Intent(MainActivity.this, HelpScreen.class);
startActivity(intent2);
return true;
}
return true;
}
});
}
}
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"
android:id="#+id/navigation_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="#bool/fitsSystemWindows">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="#dimen/status_bar_kitkat_height"
android:background="?colorPrimary"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="?colorPrimaryDark"/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/welcome"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#color/md_text"
android:singleLine="false"
android:padding="20dp"
android:gravity="center"
android:textStyle="bold"
android:textIsSelectable="false" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:text="#string/swipe"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#color/md_text"
android:singleLine="false"
android:padding="20dp"
android:gravity="center"
android:textSize="20sp"
android:textStyle="italic"
android:layout_marginBottom="30dp"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:background="?colorPrimaryDark"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="#drawable/action_bar_color"
android:elevation="4dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ToolbarTheme"
android:layout_marginTop="25dp"/>
</FrameLayout>
<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:fitsSystemWindows="#bool/fitsSystemWindows"
app:headerLayout="#layout/navigation_drawer_header"
app:menu="#menu/navigation_drawer_menu"
app:theme="#style/NavigationViewTheme" />
If anyone wants a live example of the navigation drawers swipe feature not working you can test out my app in the app store. Here is the link: https://play.google.com/store/apps/details?id=com.jehan.sportstutorials
Summary: Does anyone know how to increase the navigation drawers drag margin, however not following the conventional methods stated in the link in my first paragraph? Help would be appreciated.
Check your R.layout.activity_main layout. Android Studio IDE, for some reason, sets 15dp margin to the topmost container of automatically-created Activity layouts. This could interfere with the Drawer.
Just check if the topmost Relative/Frame/Whichever Layout has any android:layout_marginX attributes set and remove them.

How to put a complex Layout within CollapsingToolbarLayout

I am trying to put a complex layout within a CollapsingToolbarLayout. Attached image describes the details.
What I want - Add the described custom layout in my CollapsingToolbarLayout.
What are the problems
CirclePageIndicator is not visible (screenshot below)
ViewPager swiping through the ViewPager gives blank Screens. This is
depite PagerAdapter configured.
Any attempt to bring my custom layout outside CollapsingToolbarLayout
and make it a direct child of AppBarLayout brings a blank screen.
What I have done -
Tried creating a child LinearLayout of CollapsingToolbarLayout - this does not show the CirclePageIndicator and inconsistently shows the ViewPager
Tried creating a child LinearLayout of AppBarLayout - This shows a blank screen.
How do I go about resolving the issue ??
Any help is really appreciated !
activity_main.xml
<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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="250dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true" >
<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"
app:contentScrim="#android:color/darker_gray"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
android:fitsSystemWindows="true">
<!--<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/charts"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.1" />-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/charts" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/circlePageInd"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/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.design.widget.FloatingActionButton
android:id="#+id/fab"
app:layout_anchor="#id/appbar_layout"
app:layout_anchorGravity="bottom|right|end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation = "vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Info
Jarlsberg lancashire edam. Dolcelatte hard cheese brie st. agur blue
cheese caerphilly bavarian bergkase cheese and biscuits mascarpone. Cheeseburger swiss bavarian
bergkase cream cheese fromage frais cheesy feet port-salut airedale. St. agur blue cheese rubber
cheese caerphilly cheddar cheesecake cream cheese manchego lancashire. Roquefort squirty cheese
the big cheese."
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jarlsberg lancashire edam. Dolcelatte hard cheese brie st. agur blue
cheese caerphilly bavarian bergkase cheese and biscuits mascarpone. Cheeseburger swiss bavarian
bergkase cream cheese fromage frais cheesy feet port-salut airedale. St. agur blue cheese rubber
cheese caerphilly cheddar cheesecake cream cheese manchego lancashire. Roquefort squirty cheese
the big cheese." />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
package com.deep.;
import android.content.res.Configuration;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TableLayout;
import com.viewpagerindicator.CirclePageIndicator;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final int VIEW_PAGER_COUNT = 3;
/* private ListView listView;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// toolbar.setBackgroundColor(Color.BLUE);
// toolbar.setTitle("Wealth Tracker");
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
ab.setTitle("WealthTracker");
ab.setHomeAsUpIndicator(R.drawable.ic_drawer);
ab.setDisplayHomeAsUpEnabled(true);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
ScreenSlidePagerAdapter pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
CirclePageIndicator circlePageIndicator = (CirclePageIndicator) findViewById(R.id.circlePageInd);
circlePageIndicator.setViewPager(viewPager);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new ScreenSlidePagerFragment();
}
#Override
public int getCount() {
return VIEW_PAGER_COUNT;
}
}
public static class ScreenSlidePagerFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// return super.onCreateView(inflater, container, savedInstanceState);
// View view = inflater.inflate(R.layout.fragment_main, container, false);
// return view;
TabLayout tabLayout = new TabLayout(getActivity());
tabLayout.addTab(tabLayout.newTab().setText("Expense"));
tabLayout.addTab(tabLayout.newTab().setText("Inflow"));
tabLayout.addTab(tabLayout.newTab().setText("Investment"));
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabTextColors(Color.WHITE, Color.YELLOW);
Log.i("ScreenSlidePagerFrag", "Returning tab layout");
return tabLayout;
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
// boolean drawerOpen = drawerLayout.isDrawerOpen(listView);
// menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
#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.
// if(actionBarDrawerToggle.onOptionsItemSelected(item)){ return true; }
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Categories

Resources