Can get the actionBar reference - android

This is the code for my activity.
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBar;
public class MainActivity extends FragmentActivity {
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getActionBar();
}
}
The problem is that the getActionBar() is red lined (Type mismatch: cannot convert from android.app.ActionBar to android.support.v7.app.ActionBar) but if you see the imports it is intended to be of support library.
Well I just want to have a an actionbar reference as I am working with swiping tabs and which belong to support library.
I mean can I get a reference to the actionBar for support library as I can have for non-support i.e. ActionBar ab = getActionBar(); ?

Related

ActionBar didn't work?

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">

Tab Fragment Tutorial failing on build

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.

Start activity without ActionBar and add it in the onCreate event

I'm using the AppCompat/ActionBarCompat library and I need to create a custom ActionBar. I need to open the activity without an ActionBar and enable it only when I add the custom view. How can I do this?
PS: I need to define the activity to not use an ActionBar through the AndroidManifest.xml and my application minimum API level is 10.
.hide() the action bar and then .show() the action bar when your ready for it
import android.support.v7.app.ActionBar;
...
private ActionBar mActionbar;
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActionbar = getSupportActionBar();
mActionbar.hide()
}
...
somewhere when something cool happens
mActionbar.show();

getSupportActionBar() The method getSupportActionBar() is undefined for the type TaskActivity. Why?

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);
}

PreferenceFragment can't load from XML (ClassCastException)

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?

Categories

Resources