Enable/Disable Android ActionBar.Tab - android

I'm developing an application that uses the new Tabs system (ActionBar.addTab()), I will have 3 tabs.
During a determined process I would like to lock (disable) two of them (not remove, only do not accept clicks and not change the 'selector'.
I was able to do not change the content (by implementing the TabListener and not changing the fragment, etc) but the selector changes.
Is there anyway to only disable/enable the tabs? without have to remove and add again?
Thanks in advance!
BR,
Danilo

Sorry, there is no "enable" or "disable" with tabs in the action bar. As you note, you can remove and re-add them -- that is the closest you can get AFAIK.

I got into the exact same issue. Finally was able to solve it. As said already, there is no way to disable/enable a tab. There is only one way out - and that is to go back to the previous tab using
getActionBar().setSelectedNavigationItem(tab);
Note however, that this should NOT be done from within the
onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction)
method, because then it gets stuck in recursive loop. So within the onTabSelected() - send a messsage to the Activity Handler to do the setSelectedNavigation().
One catch here is (you will know when you implement): what "tab" value to pass in the setSelectedNavigation. For this, we have to keep track of whether it is a user pressed tab switch or forced switch to the last tab via the code.
For this:
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
if (forcedTabSwitch)
forcedTabSwitch = false;
else
mTabToBeSwitchedTo = tab.getPosition();
}
and in the Handler,
switch (msg.what) {
case SWITCH_TO_TAB :
forcedTabSwitch = true;
break;
}

I also faced same problem.My application has three tabs and each tab has buttons to move onto next and previous tab.
I don't find any method for enabling or disabling tab.
I used DataSetter class to hold previously selected tab position and boolean value to determine tab selection.
If boolean flag is true then user has pressed next/previous button. Otherwise user is trying to select tab by pressing
them which we don't allow.
I used handler for tab selection.Inside fragment when user clicks next button I broadcast that request.
Broadcast request has integer parameter which is position of next or previous tab that we want to set.
Code inside fragment is:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.first_tab_fragment, container, false);
next = (Button)rootView.findViewById(R.id.nextbutton);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("TAB_CLICKED");
Bundle bundle = new Bundle();
bundle.putInt(MainActivity.POSITION,1);
intent.putExtras(bundle);
getActivity().sendBroadcast(intent);
}
});
return rootView;
}
I have used CustomViewPager instead of ViewPager to prevent tab swiping.
CustomViewPager class is :
public class CustomViewPager extends ViewPager {
private boolean enabled;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
}
}
Inside oncreate method of MainActivity.java add this two lines:
viewPager = (CustomViewPager) findViewById(R.id.pager);
viewPager.setPagingEnabled(false);
Below is remaining code from MainActivity.java:
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Log.d("MainActivity","onTabSelected");
if(setter.getFlag() == true)
{
viewPager.setCurrentItem(setter.getposition());
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
Log.d("MainActivity","onTabUnselected");
//handler.sendEmptyMessage(0);
Message message = new Message();
message.arg1 = setter.getposition();
handler.sendMessage(message);
}
Handler handler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
int arg1 = msg.arg1;
Log.d("arg1",arg1+"");
//viewPager.setCurrentItem(arg1);
mActionBar.setSelectedNavigationItem(arg1);
Log.d("handler","Inside handler");
}
};
BroadcastReceiver receiver = new BroadcastReceiver(){
public void onReceive(Context context, android.content.Intent intent) {
String action = intent.getAction();
Bundle bundle = intent.getExtras();
int position = bundle.getInt(MainActivity.POSITION);
Log.d("Action",action);
Log.d("position",position+"");
setter.setposition(position);
setter.setflag(true);
Message message = new Message();
message.arg1 = position;
handler.sendMessage(message);
};
};
}
Finally DataSetter.java :
public class DataSetter
{
int position;
boolean flag;
void setposition(int value)
{
position = value;
}
void setflag(boolean value)
{
flag = value;
}
int getposition()
{
return position;
}
boolean getFlag()
{
return flag;
}
}
More you can find on: http://suhas1989.wordpress.com/2014/10/13/enable-or-disable-actionbar-tab-in-android/

Related

Android: Replacing fragments inside tabs

So I am trying to have a screen with three fragments in different tabs, and on one of them have them switch to be another fragment, but I am unable to figure out how I could do this. I have looked at other similar questions, however, I cannot seem to get them working. So if someone could help me exactly figure it out that would be great.
My tabsadapter is this
public class Tabsadapter extends FragmentStatePagerAdapter {
private int TOTAL_TABS = 3;
public Tabsadapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new fragment1();
case 1:
return new fragment2();
case 2:
return new fragment3();
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return TOTAL_TABS;
}
}
and my main is this:
public class main extends ActionBarActivity implements android.support.v7.app.ActionBar.TabListener {
private ViewPager tabsviewPager;
private ActionBar mActionBar;
private Tabsadapter mTabsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabsviewPager = (ViewPager) findViewById(R.id.tabspager);
mTabsAdapter = new Tabsadapter(getSupportFragmentManager());
tabsviewPager.setAdapter(mTabsAdapter);
getSupportActionBar().setHomeButtonEnabled(false);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab1 = getSupportActionBar().newTab().setText("Month").setTabListener(this);
Tab tab2 = getSupportActionBar().newTab().setText("Week").setTabListener(this);
Tab tab3 = getSupportActionBar().newTab().setText("Day").setTabListener(this);
getSupportActionBar().addTab(tab2);
getSupportActionBar().addTab(tab2);
getSupportActionBar().addTab(tab3);
tabsviewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
getSupportActionBar().setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
}
#Override
public void onTabSelected(Tab selectedtab, FragmentTransaction arg1) {
tabsviewPager.setCurrentItem(selectedtab.getPosition()); //update tab position on tap
}
#Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
}
}
Any help anyone can provide would be greatly appreciated. Thanks!
On Android Studio, you can create automatically the code for this :
Right click on a folder of your project, choose New -> Tabbed Activity.
Choose the navigation style. (ActionBarTabs if I understood you well)
Change other fields if needed
Click finish
You now have a brand new Tabbed Activity to use :)
You can read the generated code if you want to understand how it works.
Please ask me if you need any help.

Tabhost and fragment Activity

I am unable to understand properly what these functions are used for in my FragmentActivity class.
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int pos) {
// on changing the page
// make respected tab selected
position = pos;
actionBar.setSelectedNavigationItem(pos);
new Getquestions().execute();
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
I am using a view pager for page sliding, and what I want to do is, if a condition is false, for example I have three edit texts in fragment one, and if a user wants to slide to fragment two without filling up the texts then the must not slide.
I tried to find any such example but I am unable to find it. Can anyone help? Understanding these functions will help me implement my thing I guess
I have got an answer from another forum
public class CustomViewPager extends ViewPager {
private boolean isPagingEnabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.isPagingEnabled = true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (this.isPagingEnabled) {
return super.onTouchEvent(event);
}
return false;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.isPagingEnabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean b) {
this.isPagingEnabled = b;
}
}
Now when you want to disable swiping just setPagingEnable = false;
and in the layout file replace any <com.android.support.V4.ViewPager> tags with <com.yourpackage.CustomViewPager> tags.
Documented advantage of fragments, is that they can be added or removed to the activity on a run-time.
What you want is have a single fragment initially, and once the requirements are fulfilled - add a next fragment to your activity.

Using backstack and back button in viewpager

I'm using a viewpager to swipe between fragments and would like the back button to navigate to the previously viewed fragment rather than ending the activity. Sorry if this is a duplicate of this question however I didn't find the answer very helpful. Obviously onBackPressed needs to be overridden, but I don't know how to get and display the correct fragment. I assume that I should use the fragmentmanager's backstack, but getSupportFragmentManager().getBackStackEntryCount() always returns 0. Do I need to manually add fragments to the backstack using FragmentTransaction.addToBackStack()? If so, where would I add this in my adapter?
Here is the code for my activity,
public class PagerActivity extends FragmentActivity {
ArrayList<Sale> sales;
MyAdapter mAdapter;
ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent it = getIntent();
this.sales = (ArrayList<Sale>) it.getExtras().get("sales");
int position = it.getExtras().getInt("position");
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.fragment_pager);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(position);
}
public class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public int getCount() {
return sales.size();
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
#Override
public Fragment getItem(int position) {
SalesThumbFragment frag = new SalesThumbFragment();
return frag.newInstance(sales.get(position));
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sales_controller, menu);
return true;
}
#Override
public void onBackPressed() {
if(getSupportFragmentManager().getBackStackEntryCount() != 0) {
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
}
In new design support library, i use this
I have same issue and i follow this step
In the main activity where there are 3 fragment in viewpager i create stack
and push and pop data.
//private Stack<Integer> stackkk; ==> As i get edit suggestion
private Stack<Integer> stackkk = new Stack<>(); // Edited
private ViewPager mPager;
private int tabPosition = 0;
mTabLayout.setupWithViewPager(mPager);
mPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
tabPosition = tab.getPosition();
mPager.setCurrentItem(tab.getPosition());
if (stackkk.empty())
stackkk.push(0);
if (stackkk.contains(tabPosition)) {
stackkk.remove(stackkk.indexOf(tabPosition));
stackkk.push(tabPosition);
} else {
stackkk.push(tabPosition);
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
tabPositionUnselected = tab.getPosition();
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
and in the onBackPressed in activity,
#Override
public void onBackPressed() {
if (stackkk.size() > 1) {
stackkk.pop();
mPager.setCurrentItem(stackkk.lastElement());
} else {
}
}
Use this code in Fragment Activity class. Don't forget to add return true;
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
mViewPager.setCurrentItem(viewPageSelected - 1);
return true;
}
return super.onKeyDown(keyCode, event);
}
I had a similar problem, this is how I solved it. I think you can adapt the code to your problem, if I understood what you problem is. I had a ViewPager with 6 fragments and wanted to keep track of the page history and to be able to use the back button to navigate backwards in the history. I create a java.util.Stack<Integer> object, add fragment numbers to it (except when you use the back button, see below), and override onBackPressed() to make it pop the last viewed fragment instead of using the back stack, when my history stack is not empty.
You want to avoid pushing elements on the Stack when you press the back button, otherwise you will get stuck between two fragments if you keep using the back button, instead of eventually exiting.
My code:
MyAdapter mAdapter;
ViewPager mPager;
Stack<Integer> pageHistory;
int currentPage;
boolean saveToHistory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.container);
mPager.setAdapter(mAdapter);
mPager.setOffscreenPageLimit(5);
pageHistory = new Stack<Integer>();
mPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
if(saveToHistory)
pageHistory.push(Integer.valueOf(currentPage));
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
saveToHistory = true;
}
#Override
public void onBackPressed() {
if(pageHistory.empty())
super.onBackPressed();
else {
saveToHistory = false;
mPager.setCurrentItem(pageHistory.pop().intValue());
saveToHistory = true;
}
};
If you use a field to keep track of the index of the previous page using mPager.getCurrentItem() after each time the user navigates to a new fragment, then in the onBackPressed() method, you should be able to call mPager.setCurrentItem(previousPage)
Or, if the user can only page in order, then you don't need a field at all, and you could just do mPager.setCurrentItem(mPager.getCurrentItem()-1)
I've made custom ViewPager and implement stack functionality in it.
public class CustomViewPager extends ViewPager {
private Stack<Integer> stack = new Stack<>();
#Override
public void setCurrentItem(int item, boolean smoothScroll) {
stack.push(getCurrentItem());
super.setCurrentItem(item, smoothScroll);
}
public int popFromBackStack(boolean smoothScroll) {
if (stack.size()>0) {
super.setCurrentItem(stack.pop(), smoothScroll);
return getCurrentItem();
} else return -1;
}

How to toggle between ascending/descending sort in actionBar tab using ActionBarSherlock

I'm using ActionBarSherlock for my tabs in my application. I've got some items to display in the form of a list (using ArrayList) in the tabs.
Currently, I'm trying to emulate a scenario wherein I should be able to toggle between ascending and descending sort by the click on the actionBar. Has anyone tried this scenario?
If yes, can anyone help me please on this?
After a lot of thinking and analyzing, I could achieve this in the following way.
In the main activity file you can add the below code
boolean flag = false;
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Fragment f = mFragmentList.get(tab.getPosition());
if(f instanceof Frag_One){
((Frag_All) f).toggleSortOrder(flag);
}else if(f instanceof Frag_Due){
((Frag_Two) f).toggleSortOrder(flag);
}
flag = !flag;
}
On doing that you need to add the below code in Frag_One and/or Frag_Two
#Override
public void toggleSortOrder(boolean flag) {
// TODO Auto-generated method stub
super.toggleSortOrder(flag);
if(flag) {
Collections.sort(list, new Comparator<CompanyInfo>() {
#Override
public int compare(CompanyInfolhs, CompanyInforhs) {
// TODO Auto-generated method stub
return lhs.getName().compareToIgnoreCase(rhs.getName());
}
});
mListAdapter.notifyDataSetChanged();
}else {
Collections.sort(list, new Comparator<CompanyInfo>() {
#Override
public int compare(CompanyInfolhs, CompanyInforhs) {
// TODO Auto-generated method stub
return rhs.getName().compareToIgnoreCase(lhs.getName());
}
});
mListAdapter.notifyDataSetChanged();
}
}

how to disable viewpager adapter on touching specific views?

I have a viewpager which switches between tabs when swiping left/right.In my second tab, i have some custom views which have listeners for pinching and dragging but when i try to pinch or drag, the viewpager starts to swipe the page.
A solution comes to my mind is to disable swiping when touching those specific views and only swipe when touching outside those views.Is this possible?
Updated:
#Asok kindly provided the solution. But then updated the code which wouldnt work in my case so i post the previous piece of code which worked for me:
public class CustomViewPager extends ViewPager {
private boolean swipeable = true;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
// Call this method in your motion events when you want to disable or enable
// It should work as desired.
public void setSwipeable(boolean swipeable) {
this.swipeable = swipeable;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
return (this.swipeable) ? super.onInterceptTouchEvent(arg0) : false;
}
Lets suppose i have a draggable view and i need to disable swipping when dragging start and re enable when dragging finished so in TouchEvent of my so called view:
#Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
//disable swiping when the button is touched
((ActivityOriginal) getActivity()).setSwipeable(false);
//the rest of the code...
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
//re enable swipping when the touch is stopped
//the rest of the code...
((ActivityOriginal) getActivity()).setSwipeable(true);
break;
}
return true;
}
This first thing that comes to mind for me is to have a custom ViewPager in which, when your touch listeners get notified of a specific event you could set the swipeable boolean in ViewPager to false and set it back to true whichever way best fits your application.
public class CustomViewPager extends ViewPager {
private boolean swipeable = true;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
// Call this method in your motion events when you want to disable or enable
// It should work as desired.
public void setSwipeable(boolean swipeable) {
this.swipeable = swipeable;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
return (this.swipeable) ? super.onInterceptTouchEvent(arg0) : false;
}
}
Make sure to change your layout file to show:
<com.your.package.CustomViewPager .. />
Instead of:
<android.support.v4.view.ViewPager .. />
Edit 2
Here is my setup (Working with the above CustomViewPager):
CustomViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the CustomViewPager with the sections adapter.
mViewPager = (CustomViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
public void swipeOn(View v) {
mViewPager.setSwipeable(true);
}
public void swipeOff(View v) {
mViewPager.setSwipeable(false);
}
The above shown onCreate is in my MainActivity class which extends FragmentActivity and implements ActionBar.TabListener
I am using requestDisallowInterceptTouchEvent(true) int the onTouchEvent listener of the view that also has drag events.
#Override
public boolean onTouchEvent(MotionEvent event) {
ViewParent parent = getParent();
// or get a reference to the ViewPager and cast it to ViewParent
parent.requestDisallowInterceptTouchEvent(true);
// let this view deal with the event or
return super.onTouchEvent(event);
}
I am using like this
public void setSwipeable(boolean swipeable)
{
this.swipeable = swipeable;
}
#Override
public void scrollTo(int x, int y){
if (swipeable){
super.scrollTo(x, y);
}
}
If you know the positions on which you don't want to intercept touch events in the view pager you can do something like I did.
#Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if(arg0.getY()>getHeight()/2)return false;
return super.onInterceptTouchEvent(arg0);
}

Categories

Resources