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.
Related
public class Test extends Fragment {
EditText subject,bodybd;
TextView buttonpass;
private long mLastClickTime = 0;
TabHost tHost;
public static Test newInstance() {
Test fragment = new Test();
return fragment;
}
ImageView my_loc_btn;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
setTitleFragment("Email to GSD");
View rootView = inflater.inflate(R.layout.store_location, container, false);
my_loc_btn=(ImageView) rootView.findViewById(R.id.my_loc_btn);
my_loc_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment newFragment = new MyLocation();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.tabcontent, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
// ///// set tab content////
Resources ressources = getResources();
TabHost tabHost = (TabHost) rootView.findViewById(android.R.id.tabhost);
tabHost.setup();
// List tab
Intent intentAndroid = new Intent().setClass(getActivity(),NewMapShow.class);
TabHost.TabSpec tabSpecAndroid = tabHost
.newTabSpec("Android")
.setIndicator("Android",getResources().getDrawable(R.drawable.nicon))
.setContent(intentAndroid);
// map tab
Intent intentApple = new Intent().setClass(getActivity(), MyLocation.class);
TabHost.TabSpec tabSpecApple = tabHost
.newTabSpec("Apple")
.setIndicator("Apple",getResources().getDrawable(R.drawable.nicon))
.setContent(intentApple);
tabHost.addTab(tabSpecApple);
tabHost.addTab(tabSpecAndroid);
/** Defining Tab Change Listener event. This is invoked when tab is changed */
TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
}
};
tabHost.setOnTabChangedListener(tabChangeListener);
tabHost.setCurrentTab(2);
/////////end of tabfragment//////////////
return rootView;
}
protected void setTitleFragment(String strTitle){
Toolbar mToolbar = (Toolbar) ((AppCompatActivity)getActivity()).findViewById(R.id.toolbar);
TextView txtTitle =((TextView)mToolbar.findViewById(R.id.toolbar_title));
txtTitle.setText(strTitle);
}
}
and my logcat message:
Process: com.nahid.com.gsdambassadorpractice, PID: 22380
java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
at android.widget.TabHost.setup(TabHost.java:132)
at com.nahid.com.gsdambassadorpractice.fragment.Test.onCreateView(Test.java:85)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
now i want to know how can i run tabactivity in a fragment.Also how to change fragment on tab click.Above code is crushing the application.Please help me how can i solve this problem.
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'm trying to create a custom TabHost. And I want to call the setTabSelectionListener() method of TabWidget.
But I have the following error:
TabWidget.OnTabSelectionChanged cannot be resolved to a type
Here is my test code:
public class CustomTabHost {
public CustomTabHost(Activity activity) {
TabWidget tab_widget = (TabWidget) activity.findViewById(R.id.tab_widget);
tab_widget.setTabSelectionListener(new TabWidget.OnTabSelectionChanged() {
public void onTabSelectionChanged(int tabIndex, boolean clicked) {
}
});
}
}
I'm using Eclipse and Android 2.2.
Thanks in advance!
Instead of using the setTabSelectionListener why dont you use the .OnchangeTabListener as shown : How to use TabHost.OnTabChangeListener in android?
public class ListFragment extends Fragment implements OnTabChangeListener{
View v;
TabHost tabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.main_menu_libraries, container, false);
tabHost=(TabHost) v.findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("Tab 1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("About",getResources().getDrawable(R.drawable.android));
TabSpec spec2=tabHost.newTabSpec("Tab 2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("Blog",getResources().getDrawable(R.drawable.android));
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.setOnTabChangedListener(this);
return v;
}
#Override
public void onTabChanged(String arg0) {
if (tabHost.getCurrentTab()==0)
{
//your code here
}
if (tabHost.getCurrentTab()==1)
{
//your code here
}
Also in the part where it says //your code here ... try calling a new class which extends Fragment and inflate the particular fragment with the menu !
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.
in my app after the SplashScreen i am calling a Tabactivity.
In tha tab activity, from the first tab i am switched over to a another activity called as Float, which is not related to the TabActivity. From this activity when a condition becomes True i want to show the third tab in the TabBar. How to open the third tab from the tab activity.
Following is the code of my Tabactivity class
public class MainTabBar extends TabActivity
{
TabHost tabHost;
Intent intent;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.maintab);
addTab1(Display.class);
addTab2(History.class);
addTab3(Capture .class);
addTab4(AboutUs.class);
}
private void addTab1( Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("Tab1");
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.hometab, getTabWidget(), false);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
private void addTab2( Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("Tab2");
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.macstab, getTabWidget(), false);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
private void addTab3( Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("Tab3");
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.abouttab, getTabWidget(), false);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
private void addTab4( Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("Tab4");
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.contacttab, getTabWidget(), false);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
Is it to the above thing using a flag or any other easy way, pls suggest me friends, i am very new to android
You can use the tab index to show the third tab
tabHost.setCurrentTab(2);