setChecked and setSelected in CheckBox not working - java

I have an Activity which has 5 fragments in pager. I used
viewPager.setOffscreenPageLimit(fragmentList.size());
to be able to create all fragment in one time
I also have a listener which will pass parameter(object) to all fragment once I have a newIntent.
in one of my fragment I have CheckBox which should be selected according to the parameter from activity.
and other views which I were able to change the value of their texts and background.
just this check box I have set it on But when I see it, I have see it is off, all other events works well.
here is the activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.AppTheme_PopupOverlay);
setContentView(R.layout.activity_configurations);
ViewPager viewPager = findViewById(R.id.container);
TabLayout tabLayout = findViewById(R.id.tabs);
setNdef(getIntent().getExtras());
String type = getNdef().getString("id");
List<Fragment> fragmentList = createFragments(type);
SectionsPagerAdapter pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), fragmentList);
viewPager.setAdapter(pagerAdapter);
fillTableLayout(tabLayout, type);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
viewPager.setOffscreenPageLimit(fragmentList.size());
readNfcDialog = createReadDialog();
writeNfcDialog = createWriteDialog();
readNfcDialog.show();
setForeground();
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
ClassNfcTag wepTag = new ClassNfcTag(intent);
String type = wepTag.defineClassTag();
OmsRepeaterTag omsRepeaterTag = new OmsRepeaterTag(intent);
byteMap = omsRepeaterTag.readTag();
if (byteMap != null) {
setTagOmsConfiguration(OmsConfiguration.fromByteArray(byteMap));
for (OmsListener listener : onReceiveOmsList) {
listener.gotOms(getInTagOmsConfiguration());
}
}
}
and here is one of the fragments
#Override
public void gotOms(OmsConfiguration configuration) {
setConfiguration(configuration);
if (isAdded()) {
boolean status = getConfiguration().getDeleteRsl();
delete.setChecked(status);
delete.setSelected(status);
}
}
as a solution I have tried to use Switch instead of CheckBox and it worked well.
and I also tried to set the pager to start from exactly the fragment which has the CheckBox and it also work.
viewPager.setCurrentItem(checkBoxFragmentPosition);
I also tried to debug the code and It shows me that the checkBox is checked and when I tried to change it to checked by touching it (programmatically it is checked, in UI I see it unchecked) it change itself from unchecked to unchecked .and then with the next touch changed to checked.

just for the people who will read this question, simply this was an unresolved bug in android.
For my case I used an toggleButton instead checkBox.

Related

Actionbar.removealltabs() leaving behind the tabs space

Actionbar.removealltabs removes all my tabs. However, the remaining space used to hold the tabs are still there. As a result my actionbar looks like a very tall actionbar with a empty bottom half.
Using support actionbar with appcompatactivity.
I've tried calling Actionbar.removealltabs in both my fragment and Mainactivity... same result.
Tabs pre-destruction
Tabs Destroyed, leaving behind it's empty space
How it originally looked before adding tabs. This is my desired result
Here's my Fragment that created the tabs.
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mViewPager = view.findViewById(R.id.reddit_search_pager);
mActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
setViewPager();
setTabs();
}
public void setTabs() {
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
};
for (int i = 0; i < 3; i++) {
String tabTitle = "";
switch (i) {
case 0:
tabTitle = TAB_TITLE_SUBREDDITS;
break;
case 1:
tabTitle = TAB_TITLE_POSTS;
break;
case 2:
tabTitle = TAB_TITLE_USERS;
break;
}
mActionBar.addTab(
mActionBar.newTab()
.setText(tabTitle)
.setTabListener(tabListener));
}
}
Here's my primary MainActivity pre-existing modification's to the toolbar.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NetworkingUtils myClass = new NetworkingUtils(this);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_action_menu);
When I am working with ListViews I usually use adapters. With adapters you have to call yourAdapter.notifyDataSetChaged() every time you make a new change on your list so the list can update normally. Maybe that is what is happening to you so try to call some method like the one I showed above and it will maybe update and clear the empty space.
I fixed it by using another way to use tabs, the Tablayout as described in this video. https://www.youtube.com/watch?v=zQekzaAgIlQ. I had to make my own Toolbar instead of the default theme one.
I have no idea why Android is suggesting a deprecated and buggy way to implement tabs on their official website instead of this. In fact, the TabLayout examples provided in the Youtube video are all 404 not found.

How to acess a external view from the MainActivity

This problem let my brain burst. I got two Fragments, which contain a WebView each. These two Fragments are then placed in a ViewPagerAdapter, an adapter for my ViewPager. This ViewPager is then setup with a TabLayout. In code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebViewFragment webViewFragment1 = WebViewFragment.newInstance(1);
WebViewFragment webViewFragment2 = WebViewFragment.newInstance(2);
WebViewFragment[] fragments = new WebViewFragment[]
{
webViewFragment1,
webViewFragment2
};
String[] fragmentTitles = new String[]
{
"WebView1",
"WebView2"
};
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments, fragmentTitles);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
}
This construction let me view the fragments like in e.g WhatsApp(gesture-controlled).
Nonetheless, I want to control the contents of the WebViews in the WebViewFragments in the MainActivty. But I got no idea, how to access the WebViews externally. The two webviews have IDs:
webView1 //and
webView2
When I try them to access with help of the
findViewById(R.id.webView1)
method, I get null as response.
Now, how I can access these views from the MainActivity?
WebViewFragment
private WebView webView;
public onViewCreated(....) {
webView = view.findViewBy(R.id.webview);
....
}
public WebView getWebView() {
return webView;
}
Activity
WebViewFragment webViewFragment1 = WebViewFragment.newInstance(1);
WebView webView = webViewFragment1.getWebView();
webView.... //WebView Stuff
Please make sure that the 2 Fragments are inflated and not Null.

onCreateView for tab being called whenever selecting adjacent tab?

I am having a weird issue where onCreateView is being called every time I navigate to an adjacent tab in my TabLayout. Here is my code:
news_feed.java:
private static TabLayout tabLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Public"));
tabLayout.addTab(tabLayout.newTab().setText("Friends"));
tabLayout.addTab(tabLayout.newTab().setText("My Tabs"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
PublicTab.java:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("Creating a new view");
fragmentView = inflater.inflate(R.layout.public_tab, container, false);
progressOverlay = fragmentView.findViewById(R.id.progress_overlay);
AndroidUtils.animateView(progressOverlay, View.VISIBLE, 0.9f, 200);
getPublicPosts(progressOverlay, fragmentView);
return fragmentView;
}
I have 3 other tabs that look exactly like PublicTab.java but with different layouts. I am not sure why onCreateView is being called so frequently. I thought in the lifecycle, onCreateView is only invoked for 2 reasons:
1. when we first initialize the tabs
2. if we come out from onStop or onPause method.
However, when switching between the tabs that are next to the actual tab, the println message is called like above and this is not what I want because I don't want to update the View of that fragment so frequently: only during the first 2 reasons above. Anyone know why this is happening? Any help would be appreciated! Thanks!
Use setoffScreenPageLimit
Android documentation:
public void setOffscreenPageLimit (int limit)
Set the number of pages that should be retained to either side of the
current page in the view hierarchy in an idle state. Pages beyond this
limit will be recreated from the adapter when needed
The default value is set to 1.
First and foremost of all, Viewpager default working property is this.
if you have around ten tabs and you are choice second tab.
viewpager will create tab2, tab1 and tab 3. This to provide smooth navigation between viewpagers. So whatever you do, your oncreate view will get called, because it will recreate the fragment everytime.
One thing you can do, is if you don't wan't to make a network call/or fetch data from online everytime a tab has been revisited after it's first creation.
you can do this.
save the data in TabLayout activity, use an interface create a setdata and getData method.
so whenever it reaches onCreateview. check if there is any data left in your get method, passing position to the method.
if exist display that data, else make a network call.

Is it possible to hide the Toolbar and disable the scroll when swiping to specific page?

I would like to be able to hide the Toolbar on swipe to a specific tab and then lock it. Expanding it and then locking would also work for me although i prefer the first.
I have tried doing stuff like in the code below, but it gives rise to some snappy behavior because the page is scrolled of the screen originally. As soon as i set the scroll flags to 0, the whole page snaps back up and then locks the screen with the toolbar expanded which makes sense because with scroll flags set to zero, the page should not be able to scroll off the screen at all so it just snaps back up.
The page I am tending to is a chat page and in order to have a static text input bar at the bottom I really need to disable the scrolling for that page while making it possible for the others.
Can you guys think of some way to accomplish this?
Activity:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group);
appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// add all the needed fragments for the tabs inside a Vector
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(pagerAdapter);
viewPager.setOffscreenPageLimit(4);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.clearOnPageChangeListeners();
viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListenerGroups(tabLayout, this, fab, appBarLayout));
viewPager.setCurrentItem(CURRENT_TAB);
}
public void disableToolbarScrolling()
{
logger.d("disabling scrolling toolbar");
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(0); // clear all scroll flags
toolbar.setLayoutParams(params);
}
public void enableToolbarScrolling()
{
logger.d("enabling scrolling toolbar");
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); // clear all scroll flags
toolbar.setLayoutParams(params);
}
And then using a PageChangeListener to handle the event:
public TabLayoutOnPageChangeListenerGroups(TabLayout tabLayout, Activity activity, FloatingActionsMenu fab, AppBarLayout appBarLayout)
{
...
this.appBarLayout = appBarLayout;
...
}
private void enableScrolling()
{
((GroupActivity) mActivity).enableToolbarScrolling();
toolbarState = toolbarState_enabled;
}
private void disableScrolling()
{
((GroupActivity) mActivity).disableToolbarScrolling();
toolbarState = toolbarState_disabled;
}
#Override
public void onPageSelected(int position)
{
if(position == 0)
{
logger.d("pos 0");
disableScrolling();
appBarLayout.setExpanded(false);
}
else
{
logger.d("pos != 0");
enableScrolling();
}
}
EDIT 1:
I have tried setting toolbar visibility to both GONE or INVISIBLE. But they only make the toolbar white, giving a white bar at the top of the screen. Still allowing for the scrolling behaviour.
add this line to your code in onCreate
setSupportActionBar(toolbar); and then add
toolbar.setVisibility(View.GONE);
For hiding the toolbar you can just try :
getSupportActionBar().hide();

Android ViewPager Bug on Swipe

I have this weird issue with the viewPager. When I swipe from one fragment to the next and then swipe back, the layout is correct, but when pressing an element (Listview item for example), the previous java code still did not update with the view. I don't know if I can explain this any better, as it works when I play with the swipe until the fragment works like its supposed to. here is my code:
Main.Java:
public class Main extends FragmentActivity {
SectionsPagerAdapter mAdapter;
ViewPager mViewPager;
// for alert transitions
AlertDialog dialog;
// share button
private ShareActionProvider mShareActionProvider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// set up button
getActionBar().setDisplayHomeAsUpEnabled(true);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAdapter);
mViewPager.setPageTransformer(true, new DepthPageTransformer());
}
#Override
public void onBackPressed() {
if (mViewPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
}
}
}
SectionsPagerAdapter.java:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new Important();
case 1:
return new Propositions();
case 2:
return new Information();
}
return null;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return ("Important Dates").toUpperCase(l);
case 1:
return ("Props").toUpperCase(l);
case 2:
return ("Information").toUpperCase(l);
}
return null;
}
}
The Fragments all use the onCreateView() method.
Thanks in advance for your help!
Maybe a late response, but I've experienced the same issue.
The problem was the DepthPageTransformer you are using from the Android Developers example for PageTransformer.
There is a bug with stacking fragments and layer priority, I assume. By fixing the custom PageTransformer will fix your "swipe back" issue. Or if you don't mind the animation, just remove your custom PageTransformer and use the default animation (by adding nothing).

Categories