I am trying to set up the action bar in an Activity which extends fragment:
public class ItemDescriptionActivity extends FragmentActivity
and this is the fragment:
public class ImageSliderFragment extends Fragment
How can I set it up, I cannot extend AppCompatActivity, thanks for your help
use AppCompatActivity . AppCompatActivity extends FragmentActivity so you can you fragments inside AppCompatActivity.
The class AppCompatActivity is a child class of FragmentActivity. so you can use AppCompatActivity instead of FragmentActivity.
Related
How can I define a different Statusbar and Actionbar color for each fragment ?
At the moment.
How it should look.
First of all, I would highly recommend you to migrate to new approach - Toolbar. It is much more flexible and you can customize it as plain View.
About your question.
You can just get ActionBar object and setBackground programatically.
Here is short example
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR IN HEX 0xFFFF6666 for instance"));
I will show how would I implement this. This is more about architecture and patterns.
Use some base class for Fragment it would be better to have base class for Activity as well. Lets consider
public class BaseFragment extends Fragment
And you Activity class in which your fragment lives.
public class MainActivity extends Activity
And you have to define responsibilities of Activity in this case and create interfaces
In your case to work with ActionBar
Create interface
public interface ActionBarProvider {
void setActionBarColor(ColorDrawable color);
}
Make your activity implement this interface
public class MainActivity extends Activity implements ActionBarProvider {
public void setActionBarColo(ColorDrawable color) {
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(color));
}
}
And finally in BaseFragment in onAttach
public void onAttach(Context context) {
super.onAttach(context);
mActionBarProvider = (ActionBarProvider) context;
}
Make mActionBarProvider variable protected and make each fragment extend BaseFragment and you can change action bar color from any fragment like this mActionBarProvider.setActionBarColor(new ColorDrawable());
Hope this helps.
I have a new android project with a navigation drawer. Now i want to add a new base class and inherit it in the navigation drawer fragment class. But it already extends Fragment. So how do I inherit the base class in the navigation drawer fragment?
Multiple inheritance does not exit in Java. Essentially what you have to do is make the base class inherit from Fragment and then you can extend your navigation drawer Fragment from the base class.
public class BaseFragment extends Fragment {
...
}
public class NavigationDrawerFragment extends BaseFragment {
...
}
I am using SherlockFragmentActivity with two fragments.
I want to call methods defined in the fragments from my SherlockFragmentActivity.
Fragment1 extends Fragment{
protected void resetSettings(){
//my code
}
}
and the activity
Main extends SherlockFragmentActivity{
//here i want to call resetSettings()
}
I have implemented:
public class CategoryListFragment extends ListFragment
(Categories) Tab1
public class CrimeListFragment extends ListFragment
(List of crimes) Tab2
-- If you click on the tab it returns ALL crimes.
public class CrimeFragment extends Fragment
(Individual Crime) Tab 3
-- If you click on the tab it returns first crime.
And the activity that hosts all of the tabs:
public class TabsFragmentActivity extends FragmentActivity implements TabHost.OnTabChangeListener
What I want to implement is an onListItemClick on (Tab 1) "Categories" that passes the ID or string to Tab 2 so that only the crimes of that category are displayed.
This is already described in the official android documentation.
please follow the link and read about it there: https://developer.android.com/training/basics/fragments/communicating.html
In my application i have tabs but now i need to add Sherlock Actionbar i know that I need use extends SherlockActivity but early I had FragmentActivity how to fix it ?
public class MainActivity extends SherlockActivity {
Now I have error here beacuse i don't have FragmentActivity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
Error:
The method getSupportFragmentManager() is undefined for the type MainActivity
There is SherlockFragmentActivity, I believe, you can extend from that