I have no idea of programming. I would like a very informative answer so i can fully understand what this class is about. I found this"implements and proxies the necessary calls
* to be used with AppCompat."
But i don't no either what is AppCompat.
PreferenceActivity :
It is the base class for an activity to show a hieracrchy of preferences to the user.
Example : To enable / disable notification , data sync frequency, etc.,
AppCompatActivity :
It is the base class for activities that use the support library for action bar features.
You can add an ActionBar to your Activity when running on API level 7 or higher by extending this class for your activity and setting the activity theme to Theme.AppCompact or a similar theme.
Example: Provides Material color themes, widget tinting, appbar support, etc.,
AppCompatPreferenceActivity :
It is an abstract class, which extends PreferenceActivity.
When you create a new preference activity from the menu, android studio itself add this class.
Example : Create SettingActivity from menu, it will extend from AppCompatPreferenceActivity.
This class has more useful methods (inflate, set / get support actionbar etc).
Example :To add the toolbar, inflate the toolbar and set it. Only minimal lines of code .
Hope this helps you !!!
Related
I am new to Android development. I want every activity has a same customized popup menu on the top right. I use a LinearLayout in xxmenu.xml and include it in each activity's layout xml files. The display is OK. As the OnClickListener should be same, I don't want to implement the OnClick functions in every activity's Java file. How can I do this?
I know Android has inherent menu solution via OptionsMenu, but I don't know if it can be customized or how (e.g., I want it on topright corner, with different backgrounds, each item has an image icon followed by texts, one-side shadow, etc.)
Make an abstract BaseActivity class that implements onCreateOptionsMenu and onOptionsItemSelected for your common menu and menu items. Then make all your other activities extend from that one.
First, the solution is to define a base class (e.g., BaseActivity) to extend Activity, and the actual activities using this menu to extend BaseActivity (e.g., MainActivity extends BaseActivity).
Second, use a separate xml to define the menu layout and include it in other layouts. A simple way to bind the listeners is to use android:onClick="toggleMenu".
Third, implement public void toggleMenu(View v) in BaseActivity.
We can also call the views and do the bindings in Java code. But I met a code sequence problem that if I get the clickable view via findViewById in BaseActivity's onCreate(), it returns null, and the binding fails. So the finding of the views and following bindings should be done in the child classes (e.g., MainActivity), after calling super.onCreate().
I am building an app with a drawer layout similar to the Android Facebook app. I am wondering what the best method for architecture is. Should I have a main activity which is responsible for the action bar, and then have it use fragments to display the content of each menu item, or should I be using one activity to manage the action bar, and then have each menu item kick off entirely separate activities?
I could also imagine building multiple activities, which each have to manage the action bar. This option seems the worst.
You have two architecture options here
MainActivity with Fragments
ParentActivity that handles drawer and lots of Activities that extends this Activity.
I have tried both in different projects and found some things worth sharing.
For me The MainActivity that handles drawer and then using Fragments to fill the display is the best.
You will need to handle callbacks from specific Fragments in your MainActivity and redirect them to the specific Fragment they came from. This is mainly if you use Interfaces in objects lower in the Arcitecture chain since you sometimes need to pass down Activity to certain objects. This generates more code that are not as generic as one might want in top level architecture node.
If you are using a ParentActivity and extending it for each ChildActivity, you can write all specific code in the child, meaning that the toplevel ParentActivity will almost only have generic code.
If you are using the ParentActivity with ChildActivities and you are switching between Activities, you fill get the graphic when an Activity closes and the next opens every time a user switches between navigation objects. If you use Fragments this wont happen as the Fragment will be switched in the background. The user will also experience that the navigation drawer will be closed and recreated each time he clicks on an item there.
Its also unnessecary to recreate the navigation drawer with each click on an item. This is a minus for the ParentActivity approach.
With the ParentActivity approach you will also have to keep track of how the backbutton should function, this will be autoaticly handled for you with Fragments. Also when starting new Activities you have to choose if a new Activity should be created or if the old should be killed etc.
Just my 5c, hopes it helps :)
The best way is to use one Activity with one Fragment per section/view.
Take a look at the design documentation.
Also see the Tutorial and Sample Application. It's fairly straight-forward.
You will have one activity which manages ActionBar, Drawer (ListView!) and Fragment.
Every time it clicks an item in the ListView it updates the fragment with the new view.
If you use different Activities then you should use intents with a very bad effect, use a different activity only if needed (if it's totally unrelated to the current activity maybe?)
Official documentation: http://developer.android.com/training/implementing-navigation/nav-drawer.html
If you got any problem in creating this, online you can found more tutorials but the official is very great.
You should have the activity holding the actionbar & drawer
When using a drawer you should not start new activities from within the drawer but fragment instead
Good post & video about it: https://plus.google.com/u/0/+RomanNurik/posts/3nMVVQzUTjG
another good read: http://www.androiduipatterns.com/2013/05/the-new-navigation-drawer-pattern.html
And finally this is a must see also (check the slides or the video): https://plus.google.com/u/0/+NickButcher/posts/1jeyV2n1ZpM
This is a relatively general question that I have regarding to Android development.
In the Android application, I am using SlidingMenu library. Imagine the activity I am trying to implement has a Navigation Drawer (from Sliding Menu Library) and action bar tabs with View Pager and contains different fragments.
In order to have the navigation drawer in the activity, I had to inherit the application from SlidingActivity like this:
public class ActivityMain extends SlidingActivity implements TabListener {
However, to make View Pager work in this activity, I will need to use Make a FragmentPagerAdapter instance, and if I want to use it, it requires the activity extend the Fragment Activity.
My Activity already extended SlidingActivity, so there is no way to extend another super class. I am not sure what will be a proper way to solve this conflict. When I was working on the Android app, I have saw some other cases that different components in one activity requires to extend from different super class. What will be the general solution to such problem?
Thank you
If you look at the package of SlidingMenu here, they have an activity called SlidingFragmentActivity. Extend this activity instead of SlidingActivity gives you everything you need.
You can wrap your Activities in a SlidingMenu by constructing it programmatically new SlidingMenu(Context context) and then calling SlidingMenu.attachToActivity(Activity activity, SlidingMenu.SLIDING_WINDOW | SlidingMenu.SLIDING_CONTENT). SLIDING_WINDOW will include the Title/ActionBar in the content section of the SlidingMenu, while SLIDING_CONTENT does not. You can check it out in the example app AttachExample Activity.
from https://github.com/jfeinstein10/SlidingMenu
I have just started developing an Android app and have no experience at all. I have read a lot about Activities / Fragments / Widgets, but don't seem to find a clear answer to my question which is:
Can I create the Action Bar for the app as a fragment so whenever I change an activity I will simply call the one action bar (i.e. the one fragment)? I intend to develop a dynamic UI to create fragments for individual option and thought that it would be easy to have a general Action Bar appearing on all pages.
When you want to customize the ActionBar in all your Activities, the first step is to create a custom Theme in XML.
In this theme, you can customize nearly everything
Please refer to this excellent blog post: http://android-developers.blogspot.be/2011/04/customizing-action-bar.html
Using a Fragment for the ActionBar would be crazy!
If you want to add some code programatically in all your Activities, simply extends a custom Activity, for instance MyCustomActivity, that extends Activity.
public class MyCustomActivityextends Activity{
In this class, you can use getActionBar() and tweak it according to your needs
If you wanna have one ActionBar for all your Activities use inheritance. Create an Activity which simply handles the ActionBar like you wanna have and make it the Superclass like this.
public class ActionBarActivity extends Activity{
public void onCreate(... ) {
ActionBar actionBar = getActionBar();
// + some other method calls of your choice
}
public onCreateOptionsMenu(Menu menu){
// create your actionbaritems here
}
public boolean onOptionsItemSelected(MenuItem item) {
// handle your click events for the items here
}
}
Now you can use this Activity for all your Activities with inheritance:
public class MyActivity extends ActionBarActivity{
...
}
With this setup you are free to use Fragments as you like.
Keep in mind that every time you call a new Activity the callbacks of the super class will be invoked.
I think what you're thinking of is this:
You want your action bar to be the same on every screen, and only need to program it once.
The approach I use, is that the actionbar live in a root activity, containing a single viewpager. And all of the screens that the user interacts with are Fragments in that view pager.
If you create a blank android project in eclipse, and select actiobar with tabs, the project will set this up for you and you can see how that works.
In most cases this would be an unnecessary complication. Once you get started, you will probably find that just a few simple lines of code will create an "identical" Action Bar in each activity .Themes etc can be added later. Better questions at this stage might be should I use Action Bar Sherlock to enable better support of old devices ? and you should think about the overall structure of your app e.g activities / fragment activities / fragments / tabs so that you are able to get something working quickly that can be readilly extended as you develop you full solution.
I'm using Google's new tools to start an application that has the ability to switch between three tabs.
This is great, but lacks support for older devices.
1. I added ABS with the support library to the application.
2. I changed public class MainActivity extends FragmentActivity implements ActionBar.TabListener { to public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {
3. I'm still left with a ton of errors, and I don't even know if this will work properly on older devices. Does anyone have any tips on how to implement sliding tabs that are compatible with 2.x and up?
Update:
I'm stuck on step 6 of alextsc's answer
I tried this wizard once and I think I threw away the generated code completely when I implemented this exact pattern with ActionBarSherlock, so I suggest you start with a normal "Blank" activity from scratch. Here is a small step-by-step guide. Not all steps are completely described, but you should find enough documentation with the keywords to get started.
1) Add ActionBarSherlock to your project (obviously)
2) Create a new activity that extends SherlockFragmentActivity and set a proper abs theme
You should have a blank activity with an action bar at this point.
3) Change the layout and include a ViewPager that fills the viewport
4) Write your fragments (or placeholders for now) and the adapter for the ViewPager, wire these together
There are a lot of tutorials out there that explain everything neccessary here, e.g. this blog post.
This should give you an activity with an action bar and a swipable layout. You can swipe between your fragments now.
5) Add action bar tabs and attach a blank tab listener to them
Example:
actionBar = getSupportActionBar();
sampleTab = actionBar.newTab()
.setText(R.string.title)
.setTag(TABTAG_SAMPLE)
.setTabListener(tabListener);
actionBar.addTab(sampleTab);
Make sure that you give each of your tabs an individual tag (a string const is fine). This will be used to identify which tab is clicked in a second. Also make sure that you keep the created tab instances in a class variable. You will need them later on. Repeat the above snippet for each tab. You can use a normal TabListener, but I recomment using the SimpleTabListener since you only need to override one method later.
Now you should have an activity with the action bar, swipeable fragments and (non-functional) tabs.
6) Fill the tab listener and connect it to the viewpager
private SimpleTabListener tabListener = new SimpleTabListener() {
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
final String tag = (String) tab.getTag();
if (TABTAG_SAMPLE.equals(tag)) {
viewPager.setCurrentItem(INDEX_SAMPLE);
} else if (TABTAG_SECONDTAB.equals(tag)) {
viewPager.setCurrentItem(INDEX_SECONDFRAGMENT);
}
}
};
This should be straightforward. You listen for a tab select event, check which tab is selected via the saved tag and call the viewpagers setCurrentItem() method with the index of the fragment that is associated with a certain tab.
Now you should be able to select a fragment via a tab as well as swipe around. You will note that swiping to a certain fragment wont sync the tabs accordingly, they wont get selected properly yet.
7) Attach an OnPageChangeListener to your ViewPager and select tabs accordingly
Again, you can use a SimpleOnPageChangeListener here as well instead of the interface. Short example:
private SimpleOnPageChangeListener onPageChangeListener
= new SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
switch (position) {
case INDEX_SAMPLE:
actionBar.selectTab(sampleTab);
break;
case INDEX_SECONDFRAGMENT:
actionBar.selectTab(secondTab);
break;
}
};
};
This should also be self-explanatory. You watch for a swipe action that changes the displayed fragment, check it's index and select the appropriate tab. You see here why you had to keep the tab instances from step 5 around, you need them to select a tab.
Everything should work now.
The exact same process worked for me. I removed all the imports and then pressed ctrl+shift+o and selected the compatibility classes. It worked absoulutely fine. see the post here.
I would recommend taking a look at ViewPagerIndicator it works with ActionBarSherlock and the compatibility library, I currently have an app using this for swiping tabs on 2.2 and up.
I have implemented very complicated application using actionbar sherlock. It works fine on 2.2 to ICS devices I have tested. I also used tabs and everything works fine. I changed it later to navigation list because there should be 6 tabs.
Try demos included with actionbar to start with.
This errors are probably some classpath problems. You need to add sherlock as lib project and
support library must be included in both sherlock lib project and your project. Also check if both support libraries must have same versions.
I cannot add comments so this is my comment to ViewPagerIndicator. I used to it but when I started to use loaders to load data and fragmentadapter sometimes there were weird issues when new data came, orientation changed... But to use tabs with actionbarsherlock ViewPagerIndicator is not necessary.