I am successfully running the Activity, but I see the titlebar above the scrolltabs. I want the titlebar to be removed, when I try:
public class Iphone6Activity extends Activity
I get error Cannot resolve method 'getSupportFragmentManager()'
Below is my main Activity:
package com.hashmi.omar.vodafonestore;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class Iphone6Activity extends ActionBarActivity {
// Declaring Your View and Variables
ViewPager pager;
ViewPagerAdapter_ip6 adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Overview","Specifications", "Choose a Bundle"};
int Numboftabs =3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_iphone6);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles for the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter_ip6(getSupportFragmentManager(),Titles,Numboftabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.ip6pager);
pager.setAdapter(adapter);
// Assigning the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.ip6tabs);
// Setting the ViewPager for the SlidingTabsLayout
tabs.setViewPager(pager);
}
}
How can I remove actionbaractivity?
ActionBarActivity is deprecated, so I suggest first change it to AppCompatActivity:
public class Iphone6Activity extends AppCompatActivity
and in your styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
inside your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.getActionBar().setTitle("");
return super.onCreateOptionsMenu(menu);
}
in your manifest:
<activity
android:name="your activity"
android:configChanges="orientation|screenSize"
android:label="Label of your Activity HERE"
android:launchMode="singleTop">
...
choose one of them, in your manifest just set android:label to a empty String (if you choose manifest way)
Take a look at this question: Cannot call getSupportFragmentManager() from activity
Also, to remove title bar from your view, change the theme of your layout xml or your android manifest(this would change the theme of the entire application, you might have to use a toolbar if you want custom toolbars in activities) to something like Theme.AppCompat.Light.NoActionBar.
Lastly, ActionBarActivity extends Activity so you don't really need to change it.
Related
My application crashes and I get
Error :
setNavigationMode(int)' on a null object reference
Code :
package com.example.muhammad_adel.tabs;
import android.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("one"));
actionBar.addTab(actionBar.newTab().setText("two"));
actionBar.addTab(actionBar.newTab().setText("three"));
}
}
Change 1 : instead of import android.support.v4.app.ActionBar use import android.support.v7.app.ActionBar in your activity
Change 2 : change this actionBar = getActionBar(); to actionbar = getSupportActionBar();
Update:
You have to use PagerSlidingTabStrip because setNavigationMode is now deprecated.
It's been long time since ActionBar got deprecated.
Try to use Toolbar and SlidingTabs.
Please refer to this answer for same question. It will provide you enough guide for your query.
Android, Tabs without Actionbar
You can easily implement this and it will be easy for you to handle onClick() events for tabs.
I find the solution thanks for your help
First instead of import android.app.ActionBar => import android.support.v7.app.ActionBar
Second changing this actionBar = getActionBar(); to actionbar = getSupportActionBar();
Third you must implement TabListener
You should Try to change your parent style
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
Colleagues, MainActivity class is derived from FragmentActivity, and for some reason activity's onCreateOptionsMenu() is not getting called. I have the first breakpoint in onCreate(), which is getting triggered, and the second one in onCreateOptionsMenu(), which is not getting triggered.
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
private FragmentPagerAdapter m_fragmentPagerAdapter;
private ViewPager m_viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the primary sections of the activity.
m_fragmentPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
m_viewPager = (ViewPager) findViewById(R.id.container);
m_viewPager.setAdapter(m_fragmentPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu); // Inflate the menu; this adds items to the action bar if it is present.
return true;
}
}
Fragments in this app don't have their own menus. One menu belonging to the activity "covers" everything.
Theme is Holo.Light
What prevent onCreateOptionsMenu() from getting called? What am I missing?
Double-check that you have really set the app theme to Holo.Light. I can't explain what is causing the problem, but I was able to reproduce it. After experimenting with changes between FragmentActivity and AppCompatActivity, I accidentally ran with FragmentActivity and theme set to Theme.AppCompat.Light. This created the behavior you are seeing, with onCreateOptionsMenu() not called.
As a side issue, you should fix your onCreateOptionsMenu() to call through to super as noted in the documentation: "Deriving classes should always call through to the base implementation".
Try:
setHasOptionsMenu();//call it from onCreate(); or onViewCreated();
This method only used in fragment to tell the activity that this fragment has an option menu.
Add setHasOptionsMenu(true); in your onCreate() method.
I am working with this tutorial to teach myself tab fragments. When I paste and run the MainActivity I get this error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
at hss.fragmenttabstutorial.MainActivity.onCreate(MainActivity.java:27)
So I changed the Activity to ActionBarActivity, and changed the ActionBar to getSupportActionBar like many suggested. Now it won't build due to getSupportActionBar, stating "Incompatible types". What should I do?
Here's the Main code:
import android.app.Activity;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Fragment;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
// Declaring our tabs and the corresponding fragments.
ActionBar.Tab bmwTab, fordTab, toyotaTab;
Fragment bmwFragmentTab = new FragmentTab1();
Fragment toyotaFragmentTab = new FragmentTab2();
Fragment fordFragmentTab = new FragmentTab3();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Asking for the default ActionBar element that our platform supports.
ActionBar actionBar = getSupportActionBar();
// Screen handling while hiding ActionBar icon.
actionBar.setDisplayShowHomeEnabled(false);
// Screen handling while hiding Actionbar title.
actionBar.setDisplayShowTitleEnabled(false);
// Creating ActionBar tabs.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Setting custom tab icons.
bmwTab = actionBar.newTab().setText("Fragment1");
toyotaTab = actionBar.newTab().setText("Fragment2");
fordTab = actionBar.newTab().setText("Fragment3");
// Setting tab listeners.
bmwTab.setTabListener(new TabListener(bmwFragmentTab));
toyotaTab.setTabListener(new TabListener(toyotaFragmentTab));
fordTab.setTabListener(new TabListener(fordFragmentTab));
// Adding tabs to the ActionBar.
actionBar.addTab(bmwTab);
actionBar.addTab(toyotaTab);
actionBar.addTab(fordTab);
}
}
Instead of import android.app.ActionBar use android.support.v7.app.ActionBar.
This ensures compatibility with the rest of the support library including ActionBarActivity.
I was recommended to extend my Activity class from ActionBarActivity
Here is the previous code:
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
I wrote new application and followed advice.
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar =getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
If I use ACtionBarActivity instead of Activity, I get the following error on the phone, when I try to run it:
The method getSupportActionBar() is undefined for the type TaskActivity
Your class needs to extend from ActionBarActivity, rather than a plain Activity in order to use the getSupport*() methods.
Update [2015/04/23]: With the release of Android Support Library 22.1, you should now extend AppCompatActivity. Also, you no longer have to extend ActionBarActivity or AppCompatActivity, as you can now incorporate an AppCompatDelegate instance in any activity.
Here is another solution you could have used. It is working in my app.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
android.support.v7.app.ActionBar actionBar =getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_main)
Then you can get rid of that import for the one line ActionBar use.
If you are already extending from ActionBarActivity and you are trying to get the action bar from a fragment:
ActionBar mActionBar = (ActionBarActivity)getActivity()).getSupportActionBar();
Here is the answer of my question.
I've asked that again with some remarks.
How to add support libraries?
If you are extending from an AppCompatActivity and are trying to get the ActionBar from the Fragment, you can do this:
ActionBar mActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
You have to change the extends Activity to extends
AppCompactActivity then try set and getSupportActionBar()
Can you set the ActionBar before you set the Contient View? This order would be better:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar =getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
I'm having some difficulties getting a PreferenceFragment to load using XML.
It keeps throwing a ClassCastException "PreferenceFragmentClass cannot be cast to android.support.v4.app.Fragment".
The code is intended to run on API14 and higher.
Here's my code:
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
Here's the code where SettingsFragment is being used in:
import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class FragmentsSetup extends FragmentActivity {
private ViewPager viewPager;
private TabsAdapter tabsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewPager = new ViewPager(this);
viewPager.setId(R.id.pager);
setContentView(viewPager);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tabsAdapter = new TabsAdapter(this, viewPager);
tabsAdapter.addTab(actionBar.newTab().setText(getString(R.string.forwarding_tab).toUpperCase()), ForwardingFragment.class, null);
tabsAdapter.addTab(actionBar.newTab().setText(getString(R.string.settings_tab).toUpperCase()), SettingsFragment.class, null);
}
}
Any tips would be greatly appreciated.
SettingsFragment inherits from PreferenceFragment. PreferenceFragment is from the native API Level 11 implementation of fragments. However, FragmentsSetup is inheriting from FragmentActivity, from the Android Support package's backport of fragments.
That combination will not work.
If you intend to support devices older than API Level 11, you cannot use PreferenceFragment. Also, I am uncertain if PreferenceFragment works outside of a PreferenceActivity (it might, but I have never tried that).
If you're developing for Android 3.0 (API level 11) and higher, you should use a PreferenceFragment to display your list of Preference objects.
Also you can add a PreferenceFragment to any activity—you don't need to use PreferenceActivity.
See documentation here:
https://developer.android.com/guide/topics/ui/settings.html?