settitle() is not working in android on button clicklistner - android

I use the setActionBarTitle(); to change the title of the action bar it work fine in onCreate() method but it did not work when i want to implement it on button click listener
public class ScrollingActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
toolbar = (Toolbar) findViewById(R.id.toolbar);
CollapsingToolbarLayout mToolbarlayout= (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
setSupportActionBar(toolbar);
Button newbutton= (Button) findViewById(R.id.changetitle);
if (newbutton != null) {
newbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(ScrollingActivity.this, "Hi", Toast.LENGTH_SHORT).show();
setActionBarTitle("HI");
}
});
}
}
public void setActionBarTitle(String title) {
//noinspection ConstantConditions
getSupportActionBar().setTitle(title); //check not working
}
}
The xml which is used for the activity in which i want to change the title
<?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="appcom.taskremainder.calender.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<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_scrolling" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_menu_edit"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout
>

Yes you are right that in onCreate() the Title of ActionBar/ToolBar changes , and if we call the method to change the Title on Button click it doesn't.
The button is defined in the:
<include layout="#layout/content_scrolling"/>
I tried many different ways and stumbled upon this issue https://code.google.com/p/android/issues/detail?id=77763
I have a work around if you would like to try (to change title)
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
android.support.v7.app.ActionBar actionBar;
CollapsingToolbarLayout mToolbarlayout;
Button newButton;
// On every click I will change the value of i
static int i=1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbarlayout= (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
newButton= (Button) findViewById(R.id.changetitle);
setSupportActionBar(toolbar);
actionBar=getSupportActionBar();
newButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//oldWay();
newWay();
}
});
}
private void oldWay() {
Toast.makeText(MainActivity.this, ""+actionBar.getTitle(), Toast.LENGTH_SHORT).show();
actionBar.setTitle("Something Else");
Toast.makeText(MainActivity.this/, ""+actionBar.getTitle(), Toast.LENGTH_SHORT).show();
//Toast says that value has changed, but it doesn't on screen
}
private void newWay() {
mToolbarlayout.setTitle("" + i++);
}
}
Let me know if this helps or not.

Related

SearchView in Collapsing toolbar gets hided when scrolled up

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) {}
});

How to implement MiniDrawer using mikepenz/MaterialDrawer

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>

The ViewPager doesn't scroll at al after refreshing the data

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

Android - CollapsingToolbarLayout display menu items only on top

I try to hide menu items when the CollapsingToolbarLayout is open.
I've do this: XML
<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.compafone.app.ViewNews">
<android.support.design.widget.AppBarLayout android:id="#+id/app_bar"
android:fitsSystemWindows="true" android:layout_height="#dimen/app_bar_height"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout android:id="#+id/toolbar_layout"
android:fitsSystemWindows="true" android:layout_width="match_parent"
android:scaleType="centerCrop" android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize" android:layout_width="match_parent"
app:layout_collapseMode="pin" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_view_news" />
<android.support.design.widget.FloatingActionButton android:id="#+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin" app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end" android:src="#drawable/chatboxes" />
And this is the XML menu:
<menu 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" tools:context="com.compafone.app.ViewNews">
<item
android:id="#+id/action_comments"
android:title="Comments"
android:orderInCategory="200"
android:icon="#drawable/chatboxes"
app:showAsAction="ifRoom" />
<item
android:id="#+id/menu_item_share"
android:title="Partager"
android:icon="#android:drawable/ic_menu_share"
android:orderInCategory="100"
app:showAsAction="ifRoom"/>
Then, this is the Java activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_news);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.arrowleft);
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();
}
});
//On récupère l'intent
Intent i = getIntent();
id = i.getStringExtra("ID");
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(android.R.color.transparent));
collapsingToolbarLayout.setTitle("Compafone");
appbar = (AppBarLayout) findViewById(R.id.app_bar);
new NewsListActivityLoad().execute();
title = (TextView) findViewById(R.id.news_title);
content = (TextView) findViewById(R.id.news_content);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu resource
getMenuInflater().inflate(R.menu.menu_view_news, menu);
MenuItem menuItem = menu.findItem(R.id.menu_item_share);
//mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
this.overridePendingTransition(0, R.anim.abc_fade_out);
return true;
case R.id.action_comments:
Intent intent = getIntent();
//Intent intent3 = new Intent(this, comments.class);
try {
//intent3.putExtra(Intent_ID, intent.getStringExtra(EXTRA_PROJET));
//intent3.putExtra(Intent_SUCCES, "null");
//startActivity(intent3);
} catch (Exception e) {
Log.i("notFoundProject", "Le projet n'existe pas");
}
return true;
case R.id.menu_item_share:
//doShare(shareintent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
So, I want to hide items from menu and display items only when users scroll down (When toolbar is in normal size, so display items at the same time when toolbar title is show in the toolbar).
Sorry for bad english, thanks in advance.

Android findViewById returns null ...

i am try to find my drawer layout but findViewById returns null and i don't know why... here is my code.
// NavigationBaseActivity
#Override
public void setContentView(int layoutResID) {
LayoutInflater.from(this).inflate(layoutResID, (ViewGroup) findViewById(R.id.content), true);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawerLayout != null) {
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.navigation_open, R.string.navigation_close);
drawerLayout.setDrawerListener(drawerToggle);
}
setupToolbar();
}
public class OverviewActivity extends NavigationBaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
}
}
My drawerLayout is always null... this is my xml layout file
<?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_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar"/>
</LinearLayout>
<include layout="#layout/navigation"/>
</android.support.v4.widget.DrawerLayout>
Can anybody help me.. i can find the others views but the drawer layout is stubborn... thanks

Categories

Resources