Archives for 8 Sep,2010

You are browsing the site archives by date.

Android: Creating a custom Adapter for GridView (ButtonAdapter)

Background
Adapters are great, it’s a fact. After you get over the initial learning curve you will realise you love them (almost as much as a six sided companion). This is my attempt at a casual explanation of how to create your own custom adapter, in this example we will create a ButtonAdapter similar to something you might see in a soundboard (yawn).

Example of a grid adapter in use

Example of a grid adapter in use

Advantages of an adapter

  • Dynamic – Can expand to any number of elements rather than statically coding each individual view.
  • Elegant – Makes your code petite and quite clear to understand once you get over the initial difficulty
  • Beautiful – Now you don’t have to control how many items there are in rows or columns, android will automatically fill up the screen in the best way possible. This also means you don’t need to redesign your application for horizontal and vertical orientations.

Creating a ButtonAdapter

So we’re going to dive straight into the deep end and create our own ButtonAdapter class that extends the BaseAdapter class. If you are doing this in eclipse you can write the first line and then it will offer to autocreate (implement) the missing methods for you (if you highlight the error). This code goes inside your Activity in your java file but not inside your oncreate method.

public class ButtonAdapter extends BaseAdapter {
 private Context mContext;

 // Gets the context so it can be used later
 public ButtonAdapter(Context c) {
  mContext = c;
 }

 // Total number of things contained within the adapter
 public int getCount() {
  return filenames.length;
 }

  // Require for structure, not really used in my code.
 public Object getItem(int position) {
  return null;
 }

 // Require for structure, not really used in my code. Can
 // be used to get the id of an item in the adapter for 
 // manual control. 
 public long getItemId(int position) {
  return position;
 }

 public View getView(int position, 
                           View convertView, ViewGroup parent) {
  Button btn;
  if (convertView == null) {  
   // if it's not recycled, initialize some attributes
   btn = new Button(mContext);
   btn.setLayoutParams(new GridView.LayoutParams(100, 55));
   btn.setPadding(8, 8, 8, 8);
   } 
  else {
   btn = (Button) convertView;
  }
  exus
  btn.setText(filesnames[position]); 
  // filenames is an array of strings
  btn.setTextColor(Color.WHITE);
  btn.setBackgroundResource(R.drawable.button);
  btn.setId(position);

  return btn;
 }
}

So the important methods are getCount and getView. getCount returns the number of objects (in our case buttons) that will be needed in this adapter. getView returns an object (again a button in our case) so that it can be used.

Both these functions reference an array that I have referered to as filenames this is a string array (String[]) which looks something like the following:

public String[] filesnames = { 
			"File 1", 
			"File 2",
			"Roflcopters"
			};

Creating an OnClickListener

You can add the following to your getView method to setup a new onclick listener for your buttons so that they can react to button presses.

  // Set the onclicklistener so that pressing the button fires an event
  // We will need to implement this onclicklistner.
  btn.setOnClickListener(new MyOnClickListener(position));

For this to work we need to implement our own OnClickListner which I have named MyOnClickListener (for lack of a better name) this is the same as a normal onclick listner except we pass an integer so that we can tell which button called our onClick method (you could get the id from the view passed, but this method is useful when expanding your program later on).


class MyOnClickListener implements OnClickListener
{
 private final int position;

 public MyOnClickListener(int position)
 {
  this.position = position;
 }

 public void onClick(View v)
 {
  // Preform a function based on the position
  someFunction(this.position)
 }
}

Implementing this adapter

Now implementing the adapater is very simple, add a few imports and load a grid view from an xml file. Then we simply set the gridview’s adapter to be a new ButtonAdapter and it will automatically do the rest for us.

// You will need the following imports

import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

// In your oncreate (or where ever you want to create your gridview)
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ButtonAdapter(this));

And hopefully your all done, you could then go on to add a context menu(menu on long press) to your buttons. If you have any questions or want something explaining a bit better, just ask!

Read More

Nexus Revamped Pro Live Wallpaper 1.1.15 update (Exciting features update)

Nexus Revamped Pro has just been updated to version 1.1.15 and with that come lots of exciting new features as well as many bug fixes!

So Whats new?

This is a massive update and I have added many features, I have tonnes more ideas for things to include next. Please send me an email if there is anything you’d like or if you spot any problems! Enjoy!

  • Added: EXPERIMENTAL Parallax option to have background scrolling effect and modify its magnitude
  • Added: EXPERIMENTAL Rotation option to rotate the entire foreground (Physics > Rotation)
  • Added: Darkness slider to all color dialogs, you now use the darkness slide.
  • Added: Choice between 1, 2 and 4 custom particle colors, so you don’t have to bother filling them in if you only want 1 or 2 colors
  • Added: EXPERIMENTAL to scale the size of the live wallpaper (Physics > scaling)
  • Removed: Tint colors on non-image backgrounds, they are pointless now you can select all possible colors and are hence disabled for performance benefits
  • Removed: Alpha slider from some color dialogs, you should now use particle saturation to change the alpha
  • Moved: Touch interaction is now in the Reactions menu.
  • Improved: Particle saturation can now be set for the head and the tail individually
  • Improved: Changed the look of the particles should be more similar to the original wallpaper
  • Improved: Changed some default values (Speed deviation, Particle saturation)
  • Improved: Background color now uses a color dialog
  • Improved: Made changelog text smaller
  • Improved: How particle saturation works, it now affects the particle and trail properly
  • Improved: Custom background images are now resampled to use less memory, should cause less force closes and lower memory usage. Also added a protection in to alter user when there was a memory error rather than force closing
  • Improved: Custom particle colors selection now shows the color next to each option. Makes it a bit nicer I think 🙂
  • Bug fix: Background tints now work properly on backgrounds / custom backgrounds, and are also more efficient
  • Bug fix: Density is more consistent now, not reliant on particle speed
  • Bug fix: Tails didn’t always decay to completely transparent
  • Known bug: Disabling static foreground will cause particles to “fall” off the grid 🙁
  • Known bug: Large rotation values can cause particles to spawn on screen
  • Known bug: Licencing server issues when data connection not avaliable

Some are marked as experimental as I’ve tested them, and my beta testers have had a play but still need to be tried and tested, go wild! and let me know of any problems.

Walkthrough of some of the new and the more interesting settings

Custom Particle Colors

Follow this like a story board, left to right top to bottom. Shows the process of selecting custom colors

Follow this like a story board, left to right top to bottom. Shows the process of selecting custom colors

Custom Backgrounds

Setting a custom background image

Setting a custom background image

Battery Reactions
The live wallpaper has the ability to react to the battery status either changing the particle colors or the speed of the particles.

Option to change colors based on battery level. (Right to left, top to bottom: 100%, 75%, 50%, 25%, 0% battery capacity)

Option to change colors based on battery level. (Right to left, top to bottom: 100%, 75%, 50%, 25%, 0% battery capacity)

Scale (Experimental)
A few users asked if they could have an option to scale the wallpaper to larger sizes, so here you go!

Nexus Revamped Pro 1.1.15 scaling settings and examples

Nexus Revamped Pro 1.1.15 scaling settings and examples

Saturation

Change the saturation of the particles heads and tails

Change the saturation of the particles heads and tails

Parallax (Experimental)
Parallax or the scrolling of the background at a different rate to the foreground is a difficult effect to demonstrate well in a photo so please wait for the video to see a preview of this.

Rotation (Experimental)
Rotate the grid of the foreground, particles will no longer be aligned to the grid but I think it looks kinda funky.

Nexus Revamped Pro 1.1.15 rotation setting

Nexus Revamped Pro 1.1.15 rotation setting


Nexus Revamped Pro 1.1.15 rotation

Nexus Revamped Pro 1.1.15 rotation

Changelog
Now you can know what’s up.

Changelog, no more wondering what I've been doing!

Changelog, no more wondering what I've been doing!

Restore default settings
Safety button to lose all settings.

Don't worry about experimenting with settings you can always reset

Don't worry about experimenting with settings you can always reset

Video
I am still planning on posting a video soon (when I get some free time) to demonstrate the app as it has improved significantly since the first video.

Android Market Links
Either click the following android market links (in android phone) or search the market for nexus revamped.
Pro Version
Nexus Revamped Pro Live Wallpaper (com.stealthcopter.nexusrevampedpro)

qr

Free Version

So the free version is a few versions behind the pro version and some of the fancier features are missing, I am constantly improving both so don’t worry if you don’t want to shell out £0.99 for an app.

Nexus Revamped Live Wallpaper (com.stealthcopter.nexusrevamped)

Download link

qr

Future
I have plans and ideas for the future of this application, please complete the poll below to indicate what you’d like to see in this app or leave feedback (comment) if you have any.

What would you like to be added next to nexus revamped pro?

View Results

Loading ... Loading ...
Read More