Android Tab view - android

I have issue in the tab view. I have to show tab view many navigation. For example . In the first tab called "Sales" , It list all sales route.If the user click one route it need to go list of retailer like wise its go in the first tab. There are many pages(views) available.
From my it only show tab in the first view , that means when it load tab, it showed me list of sales routes with tab view. When I click sales route, it display retailer but not appear tab view.
This is my code : tabview.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost">
<LinearLayout android:id="#+id/LinearLayout01"
android:orientation="vertical" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_height="wrap_content" android:layout_width="fill_parent"></TabWidget>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_height="fill_parent" android:layout_width="fill_parent"></FrameLayout>
</LinearLayout>
This is my mainActivity :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabview);
TabHost t = getTabHost();
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");
/** TabSpec setIndicator() is used to set name for the tab. */
/** TabSpec setContent() is used to set content for a particular tab. */
firstTabSpec.setIndicator("Sales").setContent(new Intent(this,SalesRouteActivity.class));
secondTabSpec.setIndicator("Admin").setContent(new Intent(this,SalesRoutesTab.class));
thirdTabSpec.setIndicator("Setting").setContent(new Intent(this,SalesRoutesTab.class));
/** Add tabSpec to the TabHost to display. */
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
tabHost.addTab(thirdTabSpec);
}
This is my SalesRouteActivity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sales_routes);
ArrayList<Object> routeList = getWmRoute();
ArrayList<String> routhPath = new ArrayList<String>();
for(int i = 0; i<routeList.size();i++){
routhPath.add(((WMRoute) routeList.get(i)).getDescription());
}
ArrayAdapter ad = new ArrayAdapter(this,android.R.layout.simple_list_item_single_choice,routhPath);
setListAdapter(ad);
final ListView list=getListView();
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setItemsCanFocus(true);
list.setTextFilterEnabled(true);
list.setItemChecked(positions,true);
keyword = (String) list.getItemAtPosition(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("OK");
menu.add("Cancel");
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
Intent showContent = new Intent(getApplicationContext(),ListRetailerActivity.class);
Bundle bundle = new Bundle();
bundle.putString("RouteName", keyword);
showContent.putExtras(bundle);
startActivity(showContent);
return true;
case 1:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is retailer part ListRetailerActivity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.retailer_list);
Bundle bundle = this.getIntent().getExtras();
String routeName = bundle.getString("RouteName");
setTitle(routeName + " - List Retailer ");
ArrayList<Object> routeList = getWmRoute();
// ArrayList<String> routhPath = new ArrayList<String>();
ArrayList<HashMap<String,String>> alist=new ArrayList<HashMap<String,String>>();
for(int i = 0; i<routeList.size();i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("RetailerCode", ((WMRoute) routeList.get(i)).getDescription());
map.put("RetailerName", ((WMRoute) routeList.get(i)).getBusinessUnit());
alist.add(map);
}
ListView list=getListView();
sd = new SimpleAdapter(this,alist,R.layout.retalier_rows,new String[]{"RetailerCode","RetailerName"},new int[]{R.id.retailerCode,R.id.retailerName});
list.setAdapter(sd);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setSelected(true);
list.setSelection(0);
list.setTextFilterEnabled(true);
list.setItemsCanFocus(true);
list.setTextFilterEnabled(true);
list.setItemChecked(positions,true);
keyword = ((WMRoute) routeList.get(0)).getBusinessUnit();
//keyword = (String) list.getItemAtPosition(0);
}
In here i have to show listActivity & TabActivity.How we can implement this.
All the child Activity need to show tab view.
Please help me how to call other xml for navigation with tab view.
Thanks in advance.

Ok i am providing a demo i hope it will help you ....
Firs of all declare one ActivityGroup like this SalesActivityGroup.java
public class SalesActivityGroup extends ActivityGroup {
// Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view
public static SalesActivityGroup group;
// Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
private ArrayList<View> history;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList<View>();
group = this;
// Start the root activity withing the group and get its view
View view = getLocalActivityManager().startActivity("Home", new
Intent(this,SalesRouteActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Replace the view of this ActivityGroup
replaceView(view);
}
public void replaceView(View v) {
// Adds the old one to history
history.add(v);
// Changes this Groups View to the new View.
setContentView(v);
}
public void back() {
if(history.size() > 0) {
history.remove(history.size()-1);
if(history.size() > 0) {
setContentView(history.get(history.size()-1));
}
else {
finish();
}
}else {
finish();
}
}
#Override
public void onBackPressed() {
SalesActivityGroup.group.back();
return;
}
}
After this change your host(mainActivity)(Change only one TabSpec : firstTabSpec which is related to sales i guess) like this ...
public class Host extends TabActivity {
public static Button btnRed;
public static TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.host);
tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec salesTabSpec = tabHost.newTabSpec("tid1");
Intent intent1 = new Intent(this, SalesActivityGroup.class);//SalesActivityGroup instead of SalesRouteActivity
salesTabSpec.setContent(intent2);
/* Add tabSpec to the TabHost to display. */
tabHost.addTab(salesTabSpec);
}
}
Afterward when ever you want to start new Activity in firstTab(salesTab) you just need to change view of ActivityGroup related to that salesTab
like this (start your listRetailerActivity following way )...
Intent intent = new Intent(SalesRouteActivity.this, ListRetailerActivity.class);
// Create the view using FirstGroup's LocalActivityManager
View view = SalesActivitytGroup.group.getLocalActivityManager()
.startActivity("", intent
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Again, replace the view
SalesActivityGroup.group.replaceView(view);

Related

How to navigate from listView Activity to specific tabbed fragment activity

I'm new at Android programming and I need your help. Please. What I want to do.
I created listview, from listview I created OnItemClickListener to TabbedActivity.
Now I want for each listView item to show different fragments on TabbedActivity.
This is the listView activity
public class BookListActivity extends AppCompatActivity {
android.support.v7.widget.Toolbar toolbar;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song_list);
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Best Selling Books by Author");
listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(SongListActivity.this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.bookList));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(ListViewActivity.this, TabbedActivity.class);
intent.putExtra("BookList", listView.getItemAtPosition(i).toString());
startActivity(intent);
}
});
listView.setAdapter(mAdapter);
}
}
Here is my Tabbed Activity
public class TabbedActivity extends AppCompatActivity {
private static final String TAG = "HymnActivity";
private SectionsPageAdapter mSectionsPageAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hymn);
Log.d(TAG, "onCreate: Starting");
mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
// Bundle bundle = getIntent().getExtras();
// if(bundle != null) {
//// HOW SHOULD I IMPLEMENT THIS
// }
}
private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new Book1(), "Book1");
adapter.addFragment(new Book2(), "Book2");
adapter.addFragment(new Book3(), "Book3");
adapter.addFragment(new Book4(), "Book4");
adapter.addFragment(new Book5(), "Book5");
adapter.addFragment(new Book6(), "Book6");
adapter.addFragment(new Book7(), "Book7");
adapter.addFragment(new Book8(), "Book8");
adapter.addFragment(new Book9(), "Book9");
adapter.addFragment(new Book10(), "Book10");
viewPager.setAdapter(adapter);
}
}
Here is my SectionsPagerAdapter
public class SectionsPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public SectionsPageAdapter(FragmentManager fm) {
super(fm);
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
}
This is one of my fragments
public class Book274 extends Fragment {
private static final String TAG = "Book274";
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.book274, container, false);
return view;
}
}
So far, the tabbed fragment works very well when i slide through them. But I cant go to a specific slide. It would always start from the beginning and i have over 200 tabs
That is why i want to implement the listView tab. So that from the listView i can visit a specific tab/fragment of my choice and still slide from there forward or backwards.
Thanks Papi.
OK, first, I will attempt to answer the question you actually asked. If you want to open the "tabbed" activity to a specific tab, you need to pass the information about which tab you want to select, then tell the adapter to make that the current selection.
Your post does not clarify how you determine which tab you want selected, so I'm going to assume that you want the tab that matches the index of the list item you clicked on. So, if you click at the item at position 5 in the listview, you will open the tab at index 5 in the tabs list.
With that assumption...
First, pass the desired index to the tabs activity:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(ListViewActivity.this, TabbedActivity.class);
intent.putExtra("SelectedTabIndex", i); // <- Send tab index through the intent here
startActivity(intent);
}
});
Then, in the onCreate of the "tabs" activity, you pull that index and tell the adapter to go there:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hymn);
Log.d(TAG, "onCreate: Starting");
mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
int index = getIntent().getIntExtra("SelectedTabIndex", 0)
mViewPager.setCurrentItem(index); // <- BOOM
}
That should be enough to get you going. If your logic for which tab you want to go to is different than just the index that was clicked, you should still be able to use the same basic idea.
Now I've answered your question, allow me to give you some unsolicited advice: given your post, if I didn't know any better, I'd say that you currently have hundreds of layout files and hundreds of classes, one for each and every book. If that is the case - stop. Stop right now. This is completely unmaintainable. If each fragment just represents a different book, then all you need is one fragment whose data is updated based on the properties of the specific book instance it represents.
Also, if you have "over 200 tabs", you're probably using the wrong UI pattern. Tabs are usually intended for a small amount of different data sets. If you want to see the book details of a single book after clicking its summary in a list, you probably just want to show that book and only that book. Not 200+ other tabs. Do a web search for "android master detail" for more info as this is a very common pattern.
Hope that helps!

How to add Run Time Tabs into TabHost in Android?

Hello I am developing an application
Which requires to add Run Time tabs in the android And all the tabs should add dynamically.
My Requirement is:
I want to add Tabhost with 5 tabs Which Should display the Screen.
But some Time it require only 3 tabs in the screen then design should not change.
Same if I want to add more then 5 tabs then tab should scroll Run time The main Thing is Design should not change.
Can any one tell me How can i do that?
Currently I am using Following Code:
public class Story_List extends ActivityGroup
{
ListView list_stories;
TabHost tab_stories;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.story_list);
tab_stories=(TabHost)findViewById(R.id.tabhoststories);
tab_stories.setup(this.getLocalActivityManager());
setupTab1(new TextView(this), "Album 1");
setupTab2(new TextView(this), "Album 2");
setupTab3(new TextView(this), "Album 3");
}
private void setupTab1(final View view, final String tag)
{
View tabview = createTabView(tab_stories.getContext(), tag);
Intent intent = new Intent().setClass(this, StoryAlbum1.class);
TabSpec tab = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent);
tab_stories.addTab(tab);
}
private void setupTab2(final View view, final String tag)
{
View tabview = createTabView(tab_stories.getContext(), tag);
Intent intent = new Intent().setClass(this, StoryAlbum2.class);
TabSpec tab = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent);
tab_stories.addTab(tab);
}
private void setupTab3(final View view, final String tag)
{
View tabview = createTabView(tab_stories.getContext(), tag);
Intent intent = new Intent().setClass(this, StoryAlbum3.class);
TabSpec tab = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent);
tab_stories.addTab(tab);
}
private static View createTabView(final Context context, final String text)
{
View view = LayoutInflater.from(context).inflate(R.layout.tabs_text, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
try this :
TabHost.TabSpec tabSpec = tabHost.newTabSpec("Tab1");
tabSpec.setContent(R.id.btnTab);
tabSpec.setIndicator("My Tab 1");
tabHost.addTab(tabSpec);
btnAddTab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TabHost.TabSpec spec = tabHost.newTabSpec("Tab"+i);
spec.setContent(new TabHost.TabContentFactory() {
#Override
public View createTabContent(String tag) {
return new AnalogClock(MainActivity.this);
}
});
spec.setIndicator("Clock");
tabHost.addTab(spec);
}
});

Adding ViewPager into Existing TabHost

Hi I would like to add view pager into existing code. What is the best way to add. Here is my RssTabActivitty.java and RssChannelActivity.java.
RssTabActivity is my main activity to initialize the tabs.
RssChannelActivity is load the rss feed.
here is full source code
https://github.com/itcuties/Android-Multicategory-RSS-Reader
public class RssTabsActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// First, set the content view
setContentView(R.layout.activity_rss_tabs);
// Then get the TabHost
TabHost tabHost = getTabHost();
/* *****************
* Art tab
*/
Intent artIntent = new Intent().setClass(this, RssChannelActivity.class);
// Set Art category RSS URL
artIntent.putExtra("rss-url", "http://feeds.reuters.com/news/artsculture?format=xml");
// The name of the art tab taken from the String resources
String artTabName = getResources().getString(R.string.tab_art);
TabSpec artTabSpec = tabHost.newTabSpec(artTabName)
.setIndicator(artTabName, getResources().getDrawable(R.drawable.rss_tab_art))
.setContent(artIntent);
// Add art tab to the TabHost
tabHost.addTab(artTabSpec);
/* *****************
* Tech tab
*/
Intent techIntent = new Intent().setClass(this, RssChannelActivity.class);
// Set Tech category RSS URL
techIntent.putExtra("rss-url", "http://feeds.reuters.com/reuters/technologyNews?format=xml");
// Tech tab name taken from the string resources
String techTabName = getResources().getString(R.string.tab_tech);
TabSpec techTabSpec = tabHost.newTabSpec(techTabName)
.setIndicator(techTabName, getResources().getDrawable(R.drawable.rss_tab_tech))
.setContent(techIntent);
// Add tech tab to the TabHost
tabHost.addTab(techTabSpec);
/* *****************
* Sports tab
*/
Intent sportsIntent = new Intent().setClass(this, RssChannelActivity.class);
// Set Sports category RSS URL
sportsIntent.putExtra("rss-url", "http://feeds.reuters.com/reuters/sportsNews?format=xml");
// Sports tab name - string resources
String sportsTabName = getResources().getString(R.string.tab_sports);
TabSpec sportsTabSpec = tabHost.newTabSpec(sportsTabName)
.setIndicator(sportsTabName, getResources().getDrawable(R.drawable.rss_tab_sports))
.setContent(sportsIntent);
// Add sports tab to the TabHost
tabHost.addTab(sportsTabSpec);
// Set current tab to Technology
tabHost.setCurrentTab(1);
}
}
public class RssChannelActivity extends Activity {
// A reference to this activity
private RssChannelActivity local;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rss_channel);
// Get the RSS URL that was set in the RssTabActivity
String rssUrl = (String)getIntent().getExtras().get("rss-url");
// Set reference to this activity
local = this;
GetRSSDataTask task = new GetRSSDataTask();
// Start process RSS task
task.execute(rssUrl);
}
/**
* This class downloads and parses RSS Channel feed.
*
* #author itcuties
*
*/
private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > {
#Override
protected List<RssItem> doInBackground(String... urls) {
try {
// Create RSS reader
RssReader rssReader = new RssReader(urls[0]);
// Parse RSS, get items
return rssReader.getItems();
} catch (Exception e) {
Log.e("RssChannelActivity", e.getMessage());
}
return null;
}
#Override
protected void onPostExecute(List<RssItem> result) {
// Get a ListView from the RSS Channel view
ListView itcItems = (ListView) findViewById(R.id.rssChannelListView);
// Create a list adapter
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local,android.R.layout.simple_list_item_1, result);
// Set list adapter for the ListView
itcItems.setAdapter(adapter);
// Set list view item click listener
itcItems.setOnItemClickListener(new ListListener(result, local));
}
}
}
To add a view pager to your activity:
Add the ViewPager resource to your activity resource:
<android.support.v4.view.PagerTitleStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
Create the fragment resource of your pager:
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Create the fragment class of your fragment:
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
}
Create the adapter class of your pager:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#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 getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
Use the pager in your activity (Your activity must extend FragmentActivity):
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

how to start new activity from list activity within tabview

I am developing new application where I am using tab view as parent layout. I'm using TabHost to display 3 tabs within my application. Each of these tab has separate Activity containing a ListView. This is working fine. When you click on an item within the ListView it currently loads up a brand new Activity full screen leaving the TabHost. I'd like to load up these Activities within the TabHost. I want to retain the tabview after calling another activities from list view.
Thank you both for your response. Here is my code where I need your help.
################HelloTabWidget
//This class displays the tab view with 3 tab - Customer, Company and City.
public class HelloTabWidget extends TabActivity {
//public class HelloTabWidget extends ActivityGroup {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, CustomerTabView.class);
spec = tabHost
.newTabSpec("Customer")
.setIndicator("Customer",
res.getDrawable(R.drawable.ic_tab_Customer))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, CompanyTabView.class);
spec = tabHost
.newTabSpec("Company")
.setIndicator("Company",
res.getDrawable(R.drawable.ic_tab_Company))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, CityTabView.class);
spec = tabHost
.newTabSpec("City")
.setIndicator("City", res.getDrawable(R.drawable.ic_tab_City))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
################CustomerTabView
//This class displays list view of customers names. On click on any item in the list, it should open customer detail page keeping same tabs view.
public class CustomerTabView extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] category = getResources().getStringArray(
R.array.category_array);
setListAdapter(new ArrayAdapter<String>(this, R.drawable.list_items,
category));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Need this logic where I can retain the tab view and call new activity class for customerdetails view.
Intent intent;
intent = new Intent(CustomerTabView.this,
C_DetailActivity.class);
startActivity(intent);
finish();
}
});
}
}
################C_DetailActivity
On click of any item from customertabview, this activity class gets call which shows details of the customer.
public class C_DetailActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the Customer Details view");
setContentView(textview);
}
}
After calling C_DetailActivity class, tab view disappear. I want to retain the main tab view.
So need this logic where I can retain the tab view and call new activity class for customerdetails view
ListView lv = (ListView) findViewById(R.id.myListView);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
LocalActivityManager manager = getLocalActivityManager();
String currentTag = tabHost.getCurrentTabTag();
manager.destroyActivity(currentTag, true);
manager.startActivity(currentTag, new Intent(this, newClass.class));
}
});
Not tested, but something like this should work. If is doesn't I'll help you to fix it.

(ActionBar) Tabs + Pager + detail Fragments inside ViewPager container

For ActionBarSherlock I would like to have (Action Bar) Tabs + Pager. I use Fragments inside that pager container. I already got the examples of http://actionbarsherlock.com/ working, but I can't manage to get a details fragment inside that pager container when I would click on let's say a listitem in the first fragment.
Is it impossible to have something like this:
Activity with Tabs and pager container
Fragment A inside pager container under Tab1
Click on something in Fragment A and show Fragment B in same pager container under Tab1.
Fragment A is then not visible, only Fragment B is visible, but also all the Tabs.
At the moment I think only a new activity (which would hold Fragment B inside it) can be started after clicking something in Fragment A.
Here is my solution for the (Tabs + Fragment + ViewPager) it is works for me as i wanted,
hope that works for you as well
here is the xml file
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="5" />
<FrameLayout
android:id="#+id/fragment_details"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="4.3" />
</LinearLayout>
here is the code for MainActivity.java I'll post relevant code only so you'll have to manage it
public class MainActivity extends FragmentActivity implements
DialogInterface.OnDismissListener, TabDataResponder {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
artistTab = getSupportActionBar().newTab().setText(
R.string.tab_name_artist);
albumTab = getSupportActionBar().newTab().setText(
R.string.tab_name_album);
songTab = getSupportActionBar().newTab().setText(
R.string.tab_name_songs);
map = new HashMap<String, Integer>();
mViewPager = (ViewPager) findViewById(R.id.pager);
FrameLayout deatil = (FrameLayout) findViewById(R.id.fragment_details);
mDualPane = (deatil != null) && (deatil.getVisibility() == View.VISIBLE);
mTabsAdapter = new TabsAdapter(this, getSupportActionBar(), mViewPager);
if (savedInstanceState != null) {
flag = true;
index = savedInstanceState.getInt("index");
}
setUpTabView();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("index", getSupportActionBar()
.getSelectedNavigationIndex());
}
private void setUpTabView() {
mTabsAdapter.addTab(artistTab, ArtistFragment.class, null);
mTabsAdapter.addTab(albumTab, AlbumFragment.class, null);
mTabsAdapter.addTab(songTab, SongFragment.class, null);
getSupportActionBar().setSelectedNavigationItem(index);
}
public static class TabsAdapter extends FragmentPagerAdapter implements
ViewPager.OnPageChangeListener, ActionBar.TabListener {
private FragmentActivity mContext;
private ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<String> mTabs = new ArrayList<String>();
private TabDataResponder responder;
public TabsAdapter(FragmentActivity activity, ActionBar actionBar,
ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = actionBar;
mViewPager = pager;
// TabDataResponder is an interface which is implemented in MainActivity
// You can find implementation # the last
responder = (TabDataResponder) activity;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
//I have used map to save state of the fragment
map.put(SongFragment.TYPE_FRAGMENT.trim(), 0);
map.put(AlbumFragment.TYPE_FRAGMENT.trim(), 0);
map.put(ArtistFragment.TYPE_FRAGMENT.trim(), 0);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
mTabs.add(clss.getName());
// mArgs.add(args);
mActionBar.addTab(tab.setTabListener(this));
notifyDataSetChanged();
}
#Override
public int getCount() {
return mTabs.size();
}
#Override
public Fragment getItem(int position) {
return Fragment
.instantiate(mContext, mTabs.get(position), /*
* mArgs.get(
* position)
*/null);
}
#Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Log.i(TAG, "PageSelected....");
mActionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
Log.i(TAG, "ScrollSateChanged....");
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
String a = null;
if (mDualPane) {
a = mTabs.get(tab.getPosition());
responder.loadData(a, map.get(a));
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Log.i(TAG, "Tab is released now....");
}
}
#Override
public void onDismiss(DialogInterface dialog) {
setUpTabView();
}
//This interface must be call from fragment class
//# the time of event you want to show detail
// pass the class name in the type argument using class.getName() method
#Override
public void loadData(String type, int index) {
DetailFragment viewer = (DetailFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment_details);
if (mDualPane) {
if (viewer == null || viewer.getShownIndex() != index
|| viewer.getTypeFragment() != type) {
DetailFragment df = DetailFragment.newInstance(index, type);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_details, df)
.setTransition(
FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit();
map.put(type.trim(), index);
}
} else {
Intent intent = new Intent();
intent.setClass(MainActivity.this, DetailActivity.class);
intent.putExtra("index", index);
intent.putExtra("type", type);
startActivity(intent);
}
}
}
and here is how i deal with detail fragment not very efficient but kind of working
public class DetailFragment extends Fragment{
public static DetailFragment newInstance(int index, String TYPE_FRAGMENT) {
DetailFragment f = new DetailFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
args.putString("type", TYPE_FRAGMENT);
f.setArguments(args);
return f;
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
public String getTypeFragment(){
String a = getArguments().getString("type");
return a;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//template is blank layout
View view = inflater.inflate(R.layout.template, container, false);
if(getTypeFragment().equals(ArtistFragment.TYPE_FRAGMENT)){
view = null;
view = inflater.inflate(R.layout.artist_details, container, false);
//....
}
else if(getTypeFragment().equals(AlbumFragment.TYPE_FRAGMENT)){
//do's for album fragment
}
else if(getTypeFragment().equals(SongFragment.TYPE_FRAGMENT)){
//do's for song fragment
}
return view;
}
}
do not save the state of tab in their individual fragment it will conflict, we are already doing it here
EDIT:
Cheered too soon. Now the details_container is not a viewpager and I cannot use it to 'swipe tabs'.
Found it! Just had to define two FrameLayouts, with in the first one the ViewPager and in the second the details fragments can be 'loaded'. This is done by adding fragments dynamically and replace them.
First the two FrameLayouts:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdge="none" >
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
<FrameLayout
android:id="#+id/details_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Then replace a fragment dynamically:
// Create new fragment and transaction
Fragment detailsFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment container view with this fragment
// and add the transaction to the back stack
transaction.replace(R.id.details_container, detailsFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Very simple and I don't understand why it took me hours to figure this out..
I still did not find a possibility to have a Pager container where fragments should be loaded in and also keep the (ActionBar) Tabs. I have however found a really dirty solution to acomplish this, with starting intens (Main Activity with the Tabs) and finishing the previous ones when the backbutton doesn't need it anymore.
I adapted the code from ABS: Support Demos - Tabs and Pager. But again it's really dirty:
LoaderCursorSupport.CursorLoaderListFragment under Tab2
#Override public void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent();
intent.setClass(getActivity(), ActionBarTabsPager.class);
intent.putExtra("index", position);
intent.putExtra("fragment", "details");
intent.putExtra("tab", 1);
ActionBarTabsPager.mPreviousActivity = getActivity();
startActivity(intent);
ActionBarTabsPager (Main Activity with Tabs)
public class ActionBarTabsPager extends FragmentActivity {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
static Activity mPreviousActivity;
static Activity mActivity;
static int mTabPosition = -1;
static Boolean mTabRefreshed = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actionbar_tabs_pager);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = getSupportActionBar().newTab().setText("Tab 1");
ActionBar.Tab tab2 = getSupportActionBar().newTab().setText("Tab 2");
ActionBar.Tab tab3 = getSupportActionBar().newTab().setText("Tab 3");
ActionBar.Tab tab4 = getSupportActionBar().newTab().setText("Tab 4");
String fragment = "";
try {
Bundle bundle = this.getIntent().getExtras();
fragment = bundle.getString("fragment");
mTabPosition = bundle.getInt("tab");
} catch (Exception ex) {
}
mViewPager = (ViewPager) findViewById(R.id.pager);
mTabsAdapter = new TabsAdapter(this, getSupportActionBar(), mViewPager);
mTabsAdapter.addTab(tab1, FragmentStackSupport.CountingFragment.class);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ECLAIR) {
mTabsAdapter.addTab(tab2, FragmentStackSupport.CountingFragment.class);
mTabsAdapter.addTab(tab3, FragmentStackSupport.CountingFragment.class);
mTabsAdapter.addTab(tab4, FragmentStackSupport.CountingFragment.class);
} else {
if (!fragment.contains("details")) {
mTabsAdapter.addTab(tab2, LoaderCursorSupport.CursorLoaderListFragment.class);
} else {
mTabsAdapter.addTab(tab2, ExampleFragment.class);
}
mTabsAdapter.addTab(tab3, LoaderCustomSupport.AppListFragment.class);
mTabsAdapter.addTab(tab4, LoaderThrottleSupport.ThrottledLoaderListFragment.class);
}
if (savedInstanceState != null) {
getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt("index"));
}
if (mTabPosition > -1) {
mTabsAdapter.setPrimaryItem(mTabPosition);
mActivity = this;
}
}
Inside this Class there's a TabsAdapter
public static class TabsAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<String> mTabs = new ArrayList<String>();
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (mTabPosition > -1 && mTabRefreshed) {
int tabPosition = tab.getPosition();
if (mTabPosition != tabPosition) {
if (mPreviousActivity != null) {
mPreviousActivity.finish();
mTabRefreshed = false;
mPreviousActivity = null;
mTabPosition = -1;
Intent intent = new Intent();
intent.setClass(mContext, ActionBarTabsPager.class);
intent.putExtra("fragment", "home");
intent.putExtra("tab", tabPosition);
mActivity.startActivity(intent);
mActivity.finish();
}
}
}
mViewPager.setCurrentItem(tab.getPosition());
}
Can this be done simpler? Or should I just give up on having Tabs together with fragment history? This was done before Android 3.0 with ActivityGroups and Activities, but it seems this can't be done with fragments.
I found the other good example of the same implementation in hear... https://github.com/UweTrottmann/SeriesGuide
In this example under package com.battlelancer.seriesguide.ui
you can find UpcomingRecentActivity.java, and UpcomingFragment.java
and layout upcoming_multipan.xml
this example works for me...
I got one problem while adding different content for detail-fragment the different tabs, it gives me class-cast-exception
so i implemented a common detalFragment class and created separate layout in onCreateView method
but the only one problem i found is layout is not changing on tab switched, may be need to do it by implementing some listener
I'll tell you when i found the answer.

Categories

Resources