I'm trying to create a profile activity like WhatsApp has, having 3 textviews not 2 like WhatsApp, the first two should stay in the toolbar when its collapsed and the last should fade out. but I can't figure out how, I've tried so much and here's my code
XML file:
<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/rootLayout"
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="250dp"
android:background="#drawable/img"
android:theme="#style/ThemeOverlay.AppCompat">
<com.ahmedroid.bayttask.CustomToolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="50dp"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="120dp"
app:layout_collapseMode="parallax"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_scrollFlags="scroll|enterAlways"
app:tabIndicatorColor="#color/error_color"
app:tabSelectedTextColor="#color/error_color" />
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
As you can see, I've tried to even create my custom view based on top of it but with no luck.
Java file:
public class ProfileActivity extends AppCompatActivity {
private ViewPager mViewPager;
private TabLayout mTabLayout;
private PagerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
CustomToolbar collapsingToolbarLayout = new CustomToolbar(this);
collapsingToolbarLayout.setTitle("Ahmed Awad\n helloo");
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
mTabLayout.addTab(mTabLayout.newTab().setText("Profile"));
mTabLayout.addTab(mTabLayout.newTab().setText("Activity"));
adapter = new PagerAdapter
(getSupportFragmentManager(), mTabLayout.getTabCount());
mViewPager.setAdapter(adapter);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_profile, 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);
}
}
I've added the break line escape character but that didn't work because in the API they set it to single line.
enter code here
Related
I'm trying to add clickable items from menu.xml. It just not working, even Toast effect.
Layout is copy/paste below so you can check it.
There is my MainActivity.java when Im trying using id's.
MainActivity:
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
private SectionsPagerAdapter mSectionsPagerAdapter;
private static final String TAG = "MainActivity";
private ViewPager mViewPager;
ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.hamburgermenu);
DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
tabLayout.getTabAt(0).setText("Home").setIcon(R.drawable.homeicon);
tabLayout.getTabAt(1).setText("Section 1").setIcon(R.drawable.explorer);
tabLayout.getTabAt(2).setText("Section 2").setIcon(R.drawable.innovator);
tabLayout.getTabAt(3).setText("Section 3").setIcon(R.drawable.collar);
tabLayout.setTabTextColors(getResources().getColorStateList(R.color.white));
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.dashboard:
Intent intentExplorer = new Intent(this, Tab1Explorer.class);
startActivity(intentExplorer);
break;
case R.id.action_search:
//Display Toast for test
Log.d(TAG,"test");
Toast.makeText(this,"Search!", Toast.LENGTH_SHORT).show();
break;
case R.id.action_help:
Intent intentContact = new Intent(this,Tab3Collar.class);
startActivity(intentContact);
break;
case R.id.action_about:
Toast.makeText(this,"This is simple About Demo", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(this,"XYZ", Toast.LENGTH_LONG).show();
return super.onOptionsItemSelected(item);
}
Toast.makeText(this,"XYZ", Toast.LENGTH_LONG).show();
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0:
Tab0Home tab0 = new Tab0Home();
return tab0;
case 1:
Tab1Section tab1 = new Tab1Section();
return tab1;
case 2:
Tab2Section tab2 = new Tab2Section();
return tab2;
case 3:
Tab3Section tab3 = new Tab3Section();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
// Show 4 total pages.
return 4 ;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return "Home";
case 1:
return "Section11";
case 2:
return "Section22";
case 3:
return "Section33";
}
return null;
}
}
}
menu_main.xml file looks fine. I have android:id's, icons, titles...
Activity_main.xml file looks:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:title="#string/app_name"/>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="#style/CustomTextStyle">
<android.support.design.widget.TabItem
android:id="#+id/tabItem0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_0" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_1" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_2" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_3" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start">
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/white"
app:menu="#menu/menu_main">
</android.support.design.widget.NavigationView>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
I just want to display for example Toast when I click "Search" from Menu, but it not working. It just colapse hamburger menu without effect.
What should I change?
The navigation view does not use the onOptionsItemSelected method, instead you need to call navigationView.setOnNavigationItemSelectedListener
I have toolbar and framelayout(for fragments) in my main activity layout. I have to show the toolbar in every fragments. But the issue is each fragment has different background image that is dynamically changing. Toolbar menu icon is used for navigation drawer open and close operation. How I will handle these issue?
Before posting the code block let's first understand what are we are going to do.
1.Create a HomeActivity that holds all the fragments.
Here is the activity_home.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_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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_home"
app:menu="#menu/activity_home_drawer" />
</android.support.v4.widget.DrawerLayout>
Here is the app_bar_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
the other xmls of navigationview -- activity_home_drawer.xml contains navigation drawer contents and nav_header_home.xml is the navigation drawer header layout.
2.Let's come to java part of the HomeActivity.java
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
private HomeBean bean;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
initListener();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//this menu contains all the common menu item actions
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
//common options bar item click here i have notifications icon common for all the fragments
#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.
switch (item.getItemId()){
case R.id.action_notifications:
AppUtils.launchActivity(HomeActivity.this, NotificationActivity.class,intentData);
return false;
default:
return super.onOptionsItemSelected(item);
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// Handle navigation view item clicks here.
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.nav_dashboard:
//you can cache this fragment in case of repeated creations
selectedFragment = new DashBoardFragment();
}
break;
}
if(selectedFragment != null){
displayFragment(selectedFragment);
DrawerLayout drawer = ButterKnife.findById(this,R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
return true;
}
private void displayFragment(Fragment fragment) {
if(fragment !=null){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
}
#Override
public void initToolBar() {}
public void setToolbar(Toolbar toolbar){
setSupportActionBar(toolbar);
setActionBarToggle(toolbar);
}
private void setActionBarToggle(Toolbar toolbar) {
DrawerLayout drawer = ButterKnife.findById(this, 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();
}
#Override
public void initListener() {
NavigationView navigationView = ButterKnife.findById(this,R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//as the first item is show by default
navigationView.getMenu().getItem(0).setChecked(true);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
Now as an example let see how the fragments are written.
public class SettingsFragment extends Fragment {
#BindView(R.id.toolbar)
Toolbar toolbar;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings,container,false);
ButterKnife.bind(this,view);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((HomeActivity)getActivity()).setToolbar(toolbar);
}
}
Corresponding fragment xml fragment_settings.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"
android:fitsSystemWindows="true"
tools:context="Fragment Name">
<android.support.design.widget.AppBarLayout
android:id="#+id/notification_appbar"
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"
app:title="#string/settings"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_settings" />
</android.support.design.widget.CoordinatorLayout>
the corresponding xml content_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/min_widget_padding"
android:id="#+id/nsv_settings_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
// your content goes here
</<android.support.v4.widget.NestedScrollView>
I hope this gives clear picture on how to do in case if you have any other doubts please comment.
Use this code. I helped
public class MyFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);// To enable actionbar in fragments
super.onCreate(savedInstanceState);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//Here it is necessary to connect the layout menu
inflater.inflate(R.menu.fragment2, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
I have a collapsing toolbar in MainActivity with a SearchView in toolbar. When the searchview icon is clicked the searchview gets collapsed in the toolbar. It works fine when the toolbar is collapsed. But if i click on the search icon and scroll up, the collapsed searchview will hide. I want to the collapsed searchview to be visible when scrolled up. I am following
this link MaterialSearchView
This is the screen when the collapsing toolbar is collapsed
Now if i click on the search icon the screen looks like this
Till now the searchview is working fine. Now if i scroll up the screen looks like this
Now if i click the search icon, the searchview gets collapsed but it gets hided. So i want the searchview to be collapsed in the toolbar even when the collapsing toolbar is not collapsed.
This is the screen shot which i want when the toolbar is not collapsed and the search icon is clicked
This is my 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.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"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="#android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="150dp"
/>
</RelativeLayout>
<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" />
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
app:tabIndicatorColor="#color/pink"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
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_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_margin="16dp"
android:src="#drawable/ic_launcher_white"
android:layout_gravity="end|bottom"
app:rippleColor="#android:color/white"
app:backgroundTint="#color/primary"
app:elevation="10dp"
/>
and this is my MainActivity.java
public class MainActivity extends AppCompatActivity {
private MaterialSearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout)
findViewById(R.id.collapsing_toolbar);
AppBarLayout appBarLayout = (AppBarLayout)
findViewById(R.id.appbar);
collapsingToolbar.setTitle(" ");
appBarLayout.setExpanded(true);
// hiding & showing the title when toolbar expanded & collapsed
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;
#Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
toolbar.setBackgroundColor(getResources().getColor(R.color.black_tint));
}
if (scrollRange + verticalOffset == 0) {
collapsingToolbar.setTitle(getString(R.string.app_name));
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
isShow = true;
toolbar.setBackgroundColor(getResources().getColor(R.color.primary));
} else if (isShow) {
collapsingToolbar.setTitle(" ");
isShow = false;
toolbar.setBackgroundColor(getResources().getColor(R.color.black_tint));
}
}
});
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
searchView = (MaterialSearchView) findViewById(R.id.search_view);
searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
//Do some magic
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
//Do some magic
return false;
}
});
searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
#Override
public void onSearchViewShown() {
//Do some magic
}
#Override
public void onSearchViewClosed() {
//Do some magic
}
});
}
}
I solve this with edittext and textchangedlistener
in xml
<RelativeLayout
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">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorTransparent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:background="#color/colorBlack_50"
android:theme="#style/AppTheme.AppBarOverlay">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="250dp"
android:fitsSystemWindows="true"
android:gravity="bottom"
app:contentScrim="#color/colorBlack_50"
app:expandedTitleMargin="10dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
//some views
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
in activity
editTextSearch.setVisibility(View.VISIBLE);
editTextSearch.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
searchAudio(editTextSearch.getText().toString());
}
#Override
public void beforeTextChanged(CharSequence cs, int s, int b, int c) {}
#Override
public void onTextChanged(CharSequence query, int s, int b, int c) {}
});
I am trying to implement MiniDrawer using mikepenz/MaterialDrawer github library. I could get some result as below picture. but the top part of MiniDrawer is hidden by Toolbar. How can i solve this problem.
This is MainActivity.Java
public class MainActivity extends AppCompatActivity {
private Drawer result = null;
private MiniDrawer miniResult = null;
private Crossfader crossFader;
#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();
}
});
result = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.withTranslucentNavigationBar(false)
.addDrawerItems(
new PrimaryDrawerItem().withName(R.string.drawer_item_compact_header).withIcon(GoogleMaterial.Icon.gmd_wb_sunny).withIdentifier(1),
new PrimaryDrawerItem().withName(R.string.drawer_item_action_bar_drawer).withIcon(FontAwesome.Icon.faw_home).withBadge("22").withBadgeStyle(new BadgeStyle(Color.RED, Color.RED)).withIdentifier(2).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_multi_drawer).withIcon(FontAwesome.Icon.faw_gamepad).withIdentifier(3),
new PrimaryDrawerItem().withName(R.string.drawer_item_non_translucent_status_drawer).withIcon(FontAwesome.Icon.faw_eye).withIdentifier(4),
new PrimaryDrawerItem().withDescription("A more complex sample").withName(R.string.drawer_item_advanced_drawer).withIcon(GoogleMaterial.Icon.gmd_adb).withIdentifier(5),
new SectionDrawerItem().withName(R.string.drawer_item_section_header),
new SecondaryDrawerItem().withName(R.string.drawer_item_open_source).withIcon(FontAwesome.Icon.faw_github),
new SecondaryDrawerItem().withName(R.string.drawer_item_contact).withIcon(GoogleMaterial.Icon.gmd_format_color_fill).withTag("Bullhorn"),
new DividerDrawerItem(),
new SwitchDrawerItem().withName("Switch").withIcon(Octicons.Icon.oct_tools).withChecked(true).withOnCheckedChangeListener(onCheckedChangeListener),
new ToggleDrawerItem().withName("Toggle").withIcon(Octicons.Icon.oct_tools).withChecked(true).withOnCheckedChangeListener(onCheckedChangeListener)
) // add the items we want to use with our Drawer
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
if (drawerItem instanceof Nameable) {
Toast.makeText(MainActivity.this, ((Nameable) drawerItem).getName().getText(MainActivity.this), Toast.LENGTH_SHORT).show();
}
return false;
}
})
.withGenerateMiniDrawer(true)
.withSavedInstance(savedInstanceState)
.buildView();
miniResult = result.getMiniDrawer();
int firstWidth = (int) UIUtils.convertDpToPixel(300, this);
int secondWidth = (int) UIUtils.convertDpToPixel(72, this);
crossFader = new Crossfader()
.withContent(findViewById(R.id.main_content))
.withFirst(result.getSlider(), firstWidth)
.withSecond(miniResult.build(this), secondWidth)
.withSavedInstance(savedInstanceState)
.build();
miniResult.withCrossFader(new CrossfadeWrapper(crossFader));
//define a shadow (this is only for normal LTR layouts if you have a RTL app you need to define the other one
crossFader.getCrossFadeSlidingPaneLayout().setShadowResourceLeft(R.drawable.material_drawer_shadow_left);
}
private OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(IDrawerItem drawerItem, CompoundButton buttonView, boolean isChecked) {
if (drawerItem instanceof Nameable) {
Log.i("material-drawer", "DrawerItem: " + ((Nameable) drawerItem).getName() + " - toggleChecked: " + isChecked);
} else {
Log.i("material-drawer", "toggleChecked: " + isChecked);
}
}
};
#Override
protected void onSaveInstanceState(Bundle outState) {
//add the values which need to be saved from the drawer to the bundle
outState = result.saveInstanceState(outState);
//add the values which need to be saved from the crossFader to the bundle
outState = crossFader.saveInstanceState(outState);
super.onSaveInstanceState(outState);
}
#Override
public void onBackPressed() {
//handle the back press :D close the drawer first and if the drawer is closed close the activity
if (crossFader != null && crossFader.isCrossFaded()) {
crossFader.crossFade();
} 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.menu_main, menu);
menu.findItem(R.id.menu_1)
.setIcon(new IconicsDrawable(this, FontAwesome.Icon.faw_repeat)
.color(Color.WHITE).actionBar());
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_1:
crossFader.crossFade();
return true;
case R.id.act_settings:
return true;
default:
}
return super.onOptionsItemSelected(item);
}
}
This is activity_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"
android:fitsSystemWindows="true"
tools:context="com.sldroids.minidrawer_v3.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" />
</android.support.design.widget.CoordinatorLayout>
#sendtodilanka the problem is that you add the Crosssfader as the parent of the main_content view, which is a direct child of your CoordinatorLayout.
As the CoordinatorLayout is similar to the FrameLayout and does overlap views your Crossfader will simply be displayed below the AppBarLayout which you have defined to be over the main_content.
To get the Crossfader below the Toolbar you should add a new View around your main_content, let's choose a FrameLayout which has the attribute (don't forget to define the appBarLayout id to your AppBarLayout)
xml
app:layout_behavior="#string/appbar_scrolling_view_behavior"
After this the Crossfader will get inflated as a child of the FrameLayout we added above, which is displayed below the AppBarLayout.
A more detailed information regarding this can be found here.
So your layout will look something like this:
<?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.sldroids.minidrawer_v3.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>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<include layout="#layout/content_main" />
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
I have an activity which has a coordinator layout with viewpager and CircularPager Indicator. I am trying to use MaterialRefreshLayout Library to do pull to refresh
The library can be found at Pull to refresh library
However, after I use pull to refresh my data, the viewpager just doesn't update the rest of the fragment and hence it dosn't scroll. Once I do background foreground it scrolls alright. Below is the Layout and activity
<?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:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="myPackage.HomeActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<com.cjj.MaterialRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:overlay="true"
app:progress_show_arrow="true"
app:wave_show="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginTop="65dp"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="#dimen/viewpager_height_none"
android:layout_weight="1"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<mypackage.ui.CirclePageIndicator
android:id="#+id/indicator"
style="#style/PageIndicator"
android:clickable="false"
app:fillColor="#color/default_circle_indicator_stroke_color"
app:pageColor="#color/circlepageindicator_fill_color"
app:radius="#dimen/circlepageindicator_radius" />
</LinearLayout>
</com.cjj.MaterialRefreshLayout>
<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="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
And Below is the Activity
public class HomeActivity extends AppCompatActivity {
private SectionPagerAdapter mSectionPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
private CirclePageIndicator mPagerCountIndicator;
private MaterialRefreshLayout materialRefreshLayout;
private Integer mTaskCount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Bundle extras = getIntent().getExtras();
if(extras != null) {
mTaskCount= extras.getInt("task_Count");
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
updateandShowViewPager();
materialRefreshLayout = (MaterialRefreshLayout) findViewById(R.id.refresh);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
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
protected void onResume() {
super.onResume();
materialRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
#Override
public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
refresh();
}
});
materialRefreshLayout.finishRefresh();
}
#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);
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB) // API 11
private void startMyAsyncTask(AsyncTask asyncTask){
//execute my AsyncTask and call Update and Show ViewPager
}
private void updateandShowViewPager(){
AsyncTaskCallBack asyncTaskCallBack = new AsyncTaskCallBack() {
#Override
public void onSuccess() {
displayViewPager();
}
}
};
for(int taskCount = 1; taskCount <= mTaskCount; taskCount ++){
myAsyncTask = new MyAsyncTask();
startMyAsyncTask(myAsyncTask );
}
}
private void displayViewPager(){
mSectionPagerAdapter = new SectionPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionPagerAdapter);
mTaskCountIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
mTaskCountIndicator .setViewPager(mViewPager);
}
private void refresh() {
MyAsyncTask myAsyncTask = null;
MyAsyncTaskCallBack myAsyncTaskCallBack = new MyAsyncTaskCallBack () {
#Override
public void onSuccess() {
updateViewPager();
}
}
};
for(int taskCount = 1; taskCount <= mTaskCount; taskCount ++){
myAsyncTask = new MyAsyncTask();
startMyAsyncTask(myAsyncTask );
}
}
private void updateViewPager(){
mSectionPagerAdapter.setPagerItem(myMap);
mSectionPagerAdapter.notifyDataSetChanged();
mCartCountIndicator.notifyDataSetChanged();
mViewPager.invalidate();
}
}
Not Sure what am I doing wrong here. Appreciate any suggestion.
use
mViewPager.setOffscreenPageLimit(numberOfPages);
Okay, figured since it is the activity that is not being refreshed itself I tried this.onResume() in the refresh method once I do notifyDataSetChanged(). It works.
Not sure it is the cleanest solution though