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.
Related
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!
Im trying to add a tabhost to my application but i cant figure why this error 'cannot resolve constructor intent' is showing, here's my activity code:
public class SixthFragment extends Fragment {
public static final String TAG = "sixth";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sixthfragment, container, false);
TabHost tabHost = (TabHost) view.findViewById(R.id.tabHost);
TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,MainActivity.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this,Tab2Activity.class));
tab3.setIndicator("Tab3");
tab3.setContent(new Intent(this,Tab3Activity.class));
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
some help would be apreciated, thanks for your time.
The first parameter to the Intent constructor that you are trying to use takes a Context. Fragment is not a Context. Use getActivity() instead of this.
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);
}
});
I really need to access the images of a tab at runtime for scaling.
Therefore I made a custom tab layout with an imageview and a textview.
But if i want to add the custom tab to my tabhost I a "did you forget to call 'public void setup(localactivitymanager activitygroup)'" exception.
Thx in advance for any solutions ;D
Ps. I cannot use
spec = tabHost.newTabSpec("2").setIndicator(res.getString(R.string.tabname2),
res.getDrawable(R.drawable.tabimage2));
tabHost.addTab(spec, FragmentTwo.class, null);
because i need to scale the image before adding.
Here is my class:
public class MyTabActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabmanager_layout);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
{
mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#3b6c8d")); //unselected
}
}
private void addCustomTab(Context context,String labelId, Drawable drawable, Class<?> c, FragmentTabHost fth ) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_customtab, null);
ImageView image = (ImageView) view.findViewById(R.id.icon);
TextView text = (TextView) view.findViewById(R.id.tabtitle);
image.setImageDrawable(drawable);
text.setText(labelId);
TabHost.TabSpec spec = fth.newTabSpec(labelId);
Intent i = new Intent(this,c);
spec.setContent(i);
spec.setIndicator(view);
fth.addTab(spec);
}
You need to use FragmentTabHost's custom addTab() which will also take your Fragment class as parameter.
Replace
TabHost.TabSpec spec = fth.newTabSpec(labelId);
Intent i = new Intent(this,c);
spec.setContent(i);
spec.setIndicator(view);
with
TabHost.TabSpec spec = fth.newTabSpec(labelId);
spec.setIndicator(view);
fth.addTab(spec, c, null);
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);