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

Thursday, 8 August 2013

Using the SeekBar Control in Android Apps

In this tutorial we will work through the process of using the SeekBar control in an Android app. The SeekBar control allows users to select a numerical amount using a slider control. Using the SeekBar is straightforward, you can add one to an XML layout file and retrieve the user input in your Java Activity classes.

To demonstrate the process of using the SeekBar, we will add one to a Dialog control launched on clicking a user interface item. Here is the end result:

Add the SeekBar to an XML layout in your app as follows:
<SeekBar
android:id="@+id/level_seek"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
To include this in a basic layout you could use the following, saved as "level_layout.xml" for use with the below Java code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView android:id="@+id/level_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="5dp"
android:textStyle="bold"
android:text="100%"/>
<SeekBar android:id="@+id/level_seek"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
<Button android:id="@+id/level_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"/>
</LinearLayout>
The user selection can be retrieved when they click the button and the TextView can update as they move the slider control, to reflect the current amount selected, which in this tutorial will be a percentage value.

Now in an Android Activity class, you can set the initial value and retrieve the chosen user value. To use it in a Dialog, which is a typical usage of the slider control in many application UIs, add the following to a click listener onClick block in an Activity class for a button or other control you want the SeekBar to appear for:
final Dialog levelDialog = new Dialog(this);
levelDialog.setTitle("Select level:");
levelDialog.setContentView(R.layout.level_layout);
Retrieve references to the TextView and SeekBar items in the layout defined above:
final TextView levelTxt = (TextView)levelDialog.findViewById(R.id.level_txt);
final SeekBar levelSeek = (SeekBar)levelDialog.findViewById(R.id.level_seek);
Set the maximum value for the SeekBar:
levelSeek.setMax(100);
levelSeek.setProgress(100);
We are using 100 as the maximum since the SeekBar is going to set a percentage. Next set up a listener for changes on the SeekBar control:
levelSeek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
//change to progress
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
levelTxt.setText(Integer.toString(progress)+"%");
}
//methods to implement but not necessary to amend
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
You can of course implement the start and stop tracking methods to tailor user interaction with the SeekBar in your app. All we do here is update the TextView with the current level as the user slides the control up or down. Next retrieve a reference to the "OK" button we added to the layout:
Button okBtn = (Button)levelDialog.findViewById(R.id.level_ok);
Set up a click listener for the button:
okBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//respond to level
int chosenLevel = levelSeek.getProgress();
levelDialog.dismiss();
}
});
In this onClick method you can add whatever code you need to respond to the user's chosen level using the retrieved integer value. The method also dismisses the Dialog. Finally, still inside the onClick method, show the Dialog:
levelDialog.show();
Your app should now have the ability to let users set levels using this intuitive control, which almost all smartphone users are well accustomed to.

Further Resources:
Android Developers: SeekBar Class
TechRepublic: Android's SeekBar your way

Monday, 29 July 2013

Android: Defining Animation in Java on User Interaction

In this tutorial we will demonstrate using a set of animations on an Android view, with the animation prompted by user interaction. We will include an Image View in the app layout, starting the animations when the user clicks it. We will make two animations run concurrently using an Animation Set. Rather than defining the animations in XML as resources, we will implement the details in Java. I've written about some of the other approaches to animation in Android for Mobiletuts+, see these more detailed guides:
This tutorial indicates some of the alternative options you can choose from when implementing animations in your Android projects.

Setup Layout and Drawables

In your app layout, you can use any view you want to animate, for example, the following Image View:

The background drawable can be any shape you like, for example the following, saved in the app drawables as "spin_pic.xml":




The shape is a circle with a linear gradient in it. You can include it in any layout you choose.

Java Activity

In your Java Activity class, set the layout as normal. Your class will need the following imports:
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
Retrieve a reference to the view you want to animate in onCreate:
ImageView spinBtn = (ImageView)findViewById(R.id.spinView);
Set the class up to act as click listener for the Image View:
spinBtn.setOnClickListener(this);
For this your Activity class must implement the OnClickListener interface as follows, but with your own class name:
public class YourActivity extends Activity implements OnClickListener
Add an onClick method to the class and check for the Image View:
public void onClick(View v){
if(v.getId()==R.id.spinView){
//respond to click
}
}
Alter the code if your view has a different ID. Inside the conditional block you can start the animation.

Animate

Now you can animate your view when it is clicked. We will be spinning and fading the view at the same time. In the if block you created in onClick for the view you want to animate, first create a Rotate Animation:
RotateAnimation rotateAnim = new RotateAnimation
 (0f, 360f, 
 Animation.RELATIVE_TO_SELF, 0.5f,
 Animation.RELATIVE_TO_SELF, 0.5f);
The first two parameters represent the number of degrees to rotate the view from and to - we will rotate it one full circle. The second two pairs of parameters represent the pivot points to rotate on, with the first pair representing the X pivot value type then the value and the second two doing the same for the Y value - this code defines the centre of the view on both axes.

Next define an Alpha Animation to fade the view out as it rotates:
AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.0f);
We will fade the view from full alpha to invisible. Next create an Animation Set so that we can apply both animations to the same view:
AnimationSet animSet = new AnimationSet(true);
The true parameter specifies that the same interpolator should be used for all animations in the set. Now specify the animation set properties:
animSet.setInterpolator(new AccelerateInterpolator());
animSet.setDuration(3000);
animSet.setRepeatCount(0);
animSet.setFillAfter(true);
You could set the properties for the animations individually - setting them on the set applies them to all animations within it. We will accelerate the animation, let it elapse over 3000 milliseconds, only carry it out once and retain the end state rather than letting the view jump back to its original state after the animation is complete, which is the default behaviour.
Add the two animations to the set:
animSet.addAnimation(rotateAnim);
animSet.addAnimation(alphaAnim);
Finally, start the animation on the view:
v.startAnimation(animSet);
Run your animation to see the result:
The view spins and fades on clicking, eventually disappearing completely.

Resources

The official Android Developer Guide runs through the various options you have in animating application elements: Animation and Graphics