Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Friday, 10 May 2013

Android Action Bar

So this is a post mostly for myself so I dont lose some links but if you are interested in the Android ActionBar style then some of the code that I will post here may be useful for you.

First off a couple of links that I have found to be useful. In no particular order:


  • http://www.vogella.com/articles/AndroidActionBar/article.html
  • http://wptrafficanalyzer.in/blog/adding-custom-action-view-to-action-bar-in-android/
  • http://wptrafficanalyzer.in/blog/creating-custom-action-provider-in-action-bar/
  • http://actionbarsherlock.com/

As background the ActionBar is a consisten title bar that can be added to an android application. The most common and basic form is to simply have an icon and a title to be used to brand your application. Recently I have had a need to do a little more than basic branding so I have had to do a lot of research. 

One thing I should note is that the ActionBar is only available in Android 3.0 (sdk 11 I think) and above. Below that and you will have to either make everything custom yourself (been there, done that, did not like it) or you can use the ActionBarSherlock library which gives you all the ActionBar functionality all the way back to sdk 7.

As my application requires compatibility all the way back to at least Gingerbread (I am aiming for Froyo) I have implemented it with the sherlock library. The only different in the following code is that instead of calling getSupportActionBar() you would call getActionBar(). At least thats the only difference I have found so far.

So the google blog entry here gives a decent tutorial on how to customize the look for the action bar but for my purposes I wanted to be able to deal with things a little more dynamically. I really wanted to be able to add custom icons, buttons, and text which would change based on what the user was currently looking at. For example I have an initial landing page where I may want to have the users name displayed to let them know who is logged in. I may also want to have a button that would open a new fragment to compose an email at which point I would like the text to change to say "Compose".

So for starters, getting access to the ActionBar is a simple call:
ActionBar actionBar = this.getSupportActionBar();
or 
ActionBar actionBar = this.getActionBar(); // iof you arent using Sherlock

So in order to change the text of the actin bar I can simply call 
actionBar.setTitle("TITLE"); // replace the default applicaiton name with this text

The action bar also supports a subtitle which is nice although I probably wont use it much.
actionBar.setSubtitle("subtitle");// the subtitle sits below the title and is smaller font

By default the ActionBar will use your applicaiton icon in the top left corner of the action bar, but for my purposes I wanted to use that space for a different button. Specifically I wanted to use it as a menu button that would slide open a menu ont he left side of the screen similar to the way that facebook does it. Sliding the menu in is a blog entry to itself but to change the image is fairly simple. All you have to do is pass in a drawable or resource integer to the setLogo function. This can be easily achieved using styles as in the google blog but I wanted to be able to change the icon based on what the user was doing. Perhaps on the main page I would like my app icon but on the compose page I want it to switch to be a mail icon.

actionBar.setLogo(R.drawable.ic_action_menu_dark);// set the drawable
actionBar.setDisplayUseLogoEnabled(true);// this toggles between displaying app icon and custom logo


Now while changing the icon is great I also want to be able to click on it to perform actions. Android does suppor a "home as up" functionality in which the logo can be clicked on to perform an action but the drawback is that it adds a back arrow < icon beside your icon when you do this. I have yet to figure out how to get rid of that :(

// to make the "home" button clickable I have to set it as home up enables. unfortunately this adds an < indicating that you can go back which I
// dont want.
actionBar.setDisplayHomeAsUpEnabled(true);

Now the background of the ActionBar is pretty borring by default so of course you will want to set the background which is easy :)


Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
actionBar.setBackgroundDrawable(d);


The drawable passed in will be stretched over the full action bar so you want to make sure your drawable is the correct size for the given screen.

The final and probably the most important feature that I figured out was the addition of a custom view to the action bar. This custom view replaces the title but leaves the logo and any added menu items.


actionBar.setCustomView(R.layout.actionbar_view);
EditText search = (EditText) actionBar.getCustomView().findViewById(R.id.searchfield);
search.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
Toast.makeText(MainActivity.this, "Search triggered", Toast.LENGTH_LONG).show();
return false;
}
});
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);

One thing that I should add when it comes to menu items added is that most of the tutorials that I read simply assumed your phone didn't have a hardware menu button in which case your menu xml files will be added to the action bar. If you are on a device that does have a menu hardware key the menu items aren't added to the action bar. To ensure that it is added you have to add a few lines

MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.actionbar, menu);


Again I am using the sherlock support library but it should work if you just called getMenuInflator() instead

~(' ')~


Sunday, 3 February 2013

Project DIYSales

So since I have been working in the mobile dev area for the past year I have been interested in making my own app and putting it on the market. For a long time I couldn't think of what to make as it seemed like everything was already made. That's one of the problems with having such an open development policy I find.

Thankfully Talia came to the rescue. Talia does craft shows. Talia is very fastidious about recording what sold and for how much. Talia was doing everything with paper and pencil. Talia is a computer science PhD candidate... Paper and pencil just would not do. So I decided to make an app to track her sales and her stock.

Now you may be thinking "Didn't you say everything was already done?" and you would be right. There are many sales and inventory apps on the market but after looking through many of them with Talia we decided that they were all clunky and bad. Thus project DIYSales was born.

Requirements

  1. Provide basic inventory features
  2. Provide sales features
  3. Provide reporting features

Inventory Features
An inventory item will consist of the following pieces of information
  1. Product name. A short product name that can be used in the generation of scanning codes such as QR codes. This must be unique.
  2. Product description. This is an optional field and consists of a more verbose description of the product.
  3. Product availability (number in stock)
  4. Product price
  5. Product image. To allow for better selection in the sales features an product image or icon can be assigned to the product
The inventory must be able to support the following operations:
  1. add a new product
    1. Provide all the above information. In order to provide the icon support the app will integrate with 2 other applications. 
      1. the first application will be the camera app to take a picture of the product.
      2. The second application will be the image gallery application to allow for selecting of previously taken pictures or images that have been uploaded to the phone from another source. (I.E. if you take images of  your products with a studio camera)
    2. The ability to create QR codes based off of the product name and send that QR code via email (or other sharing application). This is easily implemented through the use of the Barcode app which already contains all abilities to generate QR codes and share them
  2. edit a product
    1. Update all information including the product name.
    2. This includes regenerating the barcode
    3. This includes updating the stock counter
    4. Part of the update feature will be to remove the product from the system completely
  3. Browse all products with an easy visual indicator of the number of items in stock
Sales Features
The sales features include the following
  1. add an item to the sales basket by scanning a bar code or QR code
  2. add an item to the sales basket by selecting the items from a list of all currently in stock products
  3. remove an item from the current basket
  4. remove all items from the current basket
  5. checkout a basket and save the transaction details for reports later.
  6. Provide a facility to allow for haggling and bartering by allowing either a discount percentage or a fixed amount discount
  7. provide the current total in the basket
  8. automatically match items in the basket against saved "deals" to provide an automatic discount
In addition to the basic sales features a "Saved Deals" ability will be included. This will allow you to automatically provide a discount based on the items selected. An example of this would be if you buy 5 items you get an automatic 25% off or $10.00 off 

Reports Features
The reports features will consist of selecting from several reports that can be compiled based on sales and inventory. The specific reports are currently undetermined as specific requirements wont be finalized until the next trade show but the following are potential reports.
  1. Total sales report. This report would give totals for all the sales that have occurred divided by day. It could also include total discounts for each day.
  2. Sales numbers by product. This could consist of all products that have been sold and the total of each product and could be used to help determine which products are more popular than others.

To date I have spent several hours working on it and have created a basic layout along with the initial database structures. I am currently working on the inventory features so I hope to have those complete by the end of February
~(' ')~