Action bar as fragment

I was just starting to develop an Android app and had no experience. I read a lot about actions / snippets / widgets, but didn't seem to find a clear answer to my question, which:

Can I create an action bar for an application as a fragment, so whenever I change an action, I just call up one action panel (i.e. one fragment)? I intend to develop a dynamic interface for creating snippets for a single option and thought it would be easy to create a common action bar on all pages.

+4
source share
4 answers

If you want to customize the ActionBar in all your actions, the first step is to create a custom theme in XML.

In this thread you can configure almost everything

Please refer to this wonderful blog post: http://android-developers.blogspot.be/2011/04/customizing-action-bar.html

Using a snippet for an ActionBar will be crazy!

If you want to add some code programmatically in all your actions, it simply extends user activity, for example MyCustomActivity, which extends Activity.

public class MyCustomActivityextends Activity{ 

In this class you can use getActionBar () and customize it according to your needs

+2
source

If you want one ActionBar to use inheritance for all your actions. Create an action that simply handles the ActionBar as you want, and make it a 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 action for all your inheritance actions:

 public class MyActivity extends ActionBarActivity{ ... } 

With this setting, you can use fragments of your choice.

Keep in mind that every time you invoke a new action, superclass callbacks are called.

0
source

I think you are thinking about this: You want your action bar to be the same on every screen, and you only need to program it once. The approach I use is that the action bar lives in a root activity containing one viewer. And all the screens the user interacts with are fragments in this view.

If you create an empty android project in eclipse and select tabbed actiobar, the project will install this for you and you will see how it works.

0
source

In most cases, this would be an unnecessary complication. Once you get started, you'll probably find that only a few simple lines of code will create an β€œidentical” action bar in every action. Themes, etc. May be added later. At this stage, it is better to ask questions, if I should use Sherlock to support better support for older devices? and you should think about the general structure of your application, for example, activities / activity fragments / fragments / tabs so that you can quickly get something that can be easily expanded as you develop your complete solution.

0
source

Source: https://habr.com/ru/post/1489588/


All Articles