public class TaskDetailTabHome extends Activity implements ActionBar.TabListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablayout);
ActionBar bar = getActionBar();
bar.addTab(bar.newTab().setText("TASK").setTabListener(this));
bar.addTab(bar.newTab().setText("COMMENT").setTabListener(this));
bar.addTab(bar.newTab().setText("FLIGHT").setTabListener(this));
bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowHomeEnabled(true);
bar.setDisplayShowTitleEnabled(false);
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Here what I would like to do is ...
// if (tabselect is TASK)
// Go to Task.class
// if (tabselected is COMMENT)
// Go to Comment.class
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
What do I do in onTabSelected method ? Do I need that Fragment ?
What do I do in onTabSelected method ?
Update your UI to reflect the selected tab. This could involve:
Using the supplied FragmentTransaction to replace a fragment
Replacing the child View of a FrameLayout
Setting the active child of a ViewFlipper
Etc.
Do I need that Fragment ?
You do not appear to have a fragment.
// Here what I would like to do is ...
// if (tabselect is TASK)
// Go to Task.class
// if (tabselected is COMMENT)
// Go to Comment.class
You do not use tabs to "go to" something. You use buttons, menus, list item clicks, etc. to "go to" another activity.
You use tabs to show something. That "something" could be implemented by other classes, if they are Fragments or are ViewGroups.
Related
I have Activity for tabLayout using ViewPager.
Code :
public class Home extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "Instant Opportunity", "Events", "Experts" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#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) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
And another activity for Slider, code is as below :
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new Home();
break;
case 1:
fragment = new Gallery();
break;
default:
break;
}
Now, here at case 0: I want to call that Home activity. But It is showing error. How to call this ?
When I take cursor on new Home(), It says can't convert from Home to Fragment.
You can use a ViewPager inside a Fragment. You have to use inner fragments with the nested fragments method, as you can read on the Documentation:
You can now embed fragments inside fragments. This is useful for a variety of situations in which you want to place dynamic and re-usable UI components into a UI component that is itself dynamic and re-usable. For example, if you use ViewPager to create fragments that swipe left and right and consume a majority of the screen space, you can now insert fragments into each fragment page.
You need to change Home as extends Fragment and use getChildFragmentManager() method for your adapter. There are some revelant posts on this kind of behaviour:
ViewPager inside ViewPager
How set ViewPager inside a Fragment
Display fragment viewpager within a fragment
How to add a Fragment inside a ViewPager using Nested Fragment (Android 4.2)
Hope this helps.
hi I had created an android app which contains actionbar with 3 tabs.I had created it using a view pager and adapter.It works successfully . But Now I had created a new activity with popup window.I added my activity_main as my contentview. But it doesn't shows action bar tabs.I don't know why.I am new to android . So please help me, thanks in advance .
here is my code
MainActivity.java
public class MainActivity1 extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Featured", "Games", "Entertainment" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#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) {
}
}
Here is Code in which I want to popup
public class Main extends FragmentActivity implements
ActionBar.TabListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hii);
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(Main.this);
// Include dialog.xml file
dialog.setContentView(R.layout.activity_main);
// Set dialog title
dialog.setTitle("Custom Dialog");
// set values for custom dialog components - text, image and button
dialog.show();
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
The main problem is in your Dialog view. You have to create a dialog which is custom and have to add style with actionbar tab. If you use that style in your dialog box then all those actionbar tabs will come.
I would reccommend instead of using dialog in your b1.setOnclick method create a separate Dialog class which will extend the Dialog class.There you set the content view and make a proper layout with actionbar tabs same as what you have done for your activity.
and inflate the layout from that dialog class.Something like this:--
b1.setOnClickListener(new View.setOnClickListener){
new actionbarDialog(MainActivity.this).show();
}
and create a class of actionbarDialog which will extend Dialog class.
and in onCreate() method of that class write the logic of actionbar that you have written in MainActivity.You will get actionbar tab in Dialog.
Hi in my project i am using a actionbar with three tabs which are fragments. now i have a button in fragment C, when i click the button i need to swipe back to fragment B, also i want to carry my data from fragment C to B this is secondary, can some one point me in right direction how can i achieve this .
Below is my MainActivty which has all the fragments
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private String[] tabs = { "fragA", "fragB", ""fragC", " };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#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 think this question is a duplicate, take a look here Android ActionBar Tabs - Swipe code is almost the same: a viewpager, an actionbar and you need transiction between fragments to be done with a swipe gesture.
Hope it helped you out ;)
Is my following Action bar Tab implementation method is efficient or not? Because whenever i switch between tabs the Tab content fragments are replaced and load again in frame layout.
I want to add fragments at first time itself and show the fragments smoothly(without reloading) when switch between tabs.
Note: i tried to add all fragments on onCreate method.But the fragments are overlapped with each other and display all fragments in frame layout at same time.
My Code:
public class ManageActivity extends SherlockFragmentActivity implements ActionBar.TabListener {
private Activity mActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = getSupportActionBar().newTab();
tab1.setText("Tab1");
tab1.setTabListener(this);
getSupportActionBar().addTab(tab1);
ActionBar.Tab tab2 = getSupportActionBar().newTab();
tab2.setText("Tab2");
tab2.setTabListener(this);
getSupportActionBar().addTab(tab2);
ActionBar.Tab tab3 = getSupportActionBar().newTab();
tab3.setText("Tab3");
tab3.setTabListener(this);
getSupportActionBar().addTab(tab3);
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
String selectedtab = tab.getText().toString();
if(selectedtab.equalsIgnoreCase("Tab1"))
{
getSupportFragmentManager().beginTransaction().replace(R.id.tabfragment_container, new Tab1Fragment()).commit();
}
else if (selectedtab.equalsIgnoreCase("Tab2")) {
getSupportFragmentManager().beginTransaction().replace(R.id.tabfragment_container, new Tab2Fragment()).commit();
}
else if (selectedtab.equalsIgnoreCase("Tab3")){
getSupportFragmentManager().beginTransaction().replace(R.id.tabfragment_container, new Tab3Fragment()).commit();
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
activity_manage.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/tabfragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ManageActivity" >
</FrameLayout>
Note: My Tab contents are Listfragments which query data from Sqlite using loader manager.
I use a ViewPager that contains all my fragments. Every time a tab is selected I only switch the position in the Viewpager.
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
int position = adapter.findItemPosition(this.id);
ViewPager viewPager = (YourActivity)getActivity().getViewPager(); // Convenience method
if (viewPager != null) {
viewPager.setCurrentItem(position, this.shouldScroll);
} else {
Log.d(getClass().getSimpleName(), "No pager available");
}
}
To make this code working you need a Viewpager and have to add all tabs into the ViewPager through an instance of PagerAdapter. I use a FragmentPagerAdapter as there are not that many tabs most of the time.
This also enables your user to swipe through the tabs. If the user changes the selected tab through swiping you need to update the selected tab. I use ActionBarSherlock in all of my Apps. Therefore the next Snippet uses a supportActionBar. This code example is a simple listener that can be set on the ViewPager to update tab changes through swiping.
private final class TabPageChangedListener extends ViewPager.SimpleOnPageChangeListener {
#Override
public void onPageSelected(int position) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setSelectedNavigationItem(position);
} else {
Log.e(getClass().getSimpleName(),
"No actionbar available to change selected tab.");
}
}
}
There is also the possibility to disable swiping by overwriting the ViewPager and intercepting the swiping touch motions if you do not want the tabs to be swipeable.
I have an example with 3 tabs and one button.
public class MainActivity extends SherlockActivity implements TabListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
setContentView(R.layout.activity_main);
addTab("1", 0, false);
addTab("2", 1, false);
addTab("3", 2, false);
Button cmdClick = (Button) findViewById(R.id.cmdClick);
cmdClick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
getSupportActionBar().setSelectedNavigationItem(0);
}
});
}
private void addTab(String tabTitle, int position, boolean setSelected) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText(tabTitle);
tab.setTabListener(this);
getSupportActionBar().addTab(tab, position, setSelected);
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Log.d("example", "Selected " + tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Log.d("example", "Unselected " + tab.getPosition());
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Log.d("example", "Reselected " + tab.getPosition());
}
}
When I click on the button it automatically selects first tab. I would like to automatically select first tab whenever I click on the second or on the third tab. I tried like this
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Log.d("example", "Selected " + tab.getPosition());
getSupportActionBar().setSelectedNavigationItem(0);
}
but it doesn't work. Any ideas?
Thanks.
Edit:
Maybe this example doesn't have any sense but this is just a simplified example of what I'm trying to do. I would like to have 2 tabs by default, one with title "1" and second one with title "+". When user selects "+" tab I would like to create new tab with title "2" (between tabs "1" and "+") and to automatically select tab "2".
First of all I'd like to discourage this kind of behavior. This will be VERY confusing for your users and frankly doesn't make any sense.
That said I tried hacking it but it doesn't seem like it's that easy to do. I did following:
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(tab.getPosition() == 0) {
int tabs = mActivity.getSupportActionBar().getTabCount();
if(tabs > 3) {
ActionBar.Tab test = mActivity.getSupportActionBar().getTabAt(2);
test.select();
return;
}
}
Edit:
For the behavior your describing I think it would be much easier to implement it by creating your own tabs. You could use radio buttons and/or buttons to implement it easily.
Now this causes the tab indicator to still point at the old tab but the fragment inside is updated to the selected one =/ so not quite there...