Breaking News
Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Wednesday, 18 February 2015

Android SeekBar example

SeekBar:
            Android SeekBar is the extension of ProgressBar. SeekBar allows the user to change the value using touch event/draggable thumb/left right arrow keys. The user can increase the value by dragging the thumb right   or by pressing the right arrow key. Similarly the user can decrease the value by dragging the thumb left or by pressing the left arrow key.
How to create:
SeekBar can be created using <SeekBar> tag in the layout xml file. For example;

<SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="26dp"/>

Important properties of SeekBar:
By default the SeekBar takes maximum value of 100. This max value can be changed using android:max="10"attribute in the layout xml file.
android:progress="5"  attribute is used to set the initial progress value. So if we set the progress attribute to 5, then during initial loading the SeekBar thumb will start at 5.

How to add listener to notify the changes:
SeekBar.OnSeekBarChangeListener is used as a callback that notifies clients when the progress level has been changed. This can be used for both user initiated changes as well as for programmatic chagnes.

seekBarInstance.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {...} method is used to notify the user actions/changes in the SeekBar.

We need to implement three abstract methods here;
1. public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {...}
    This listener method will be invoked if any change is made in the SeekBar
2. public void onStartTrackingTouch(SeekBar seekBar) {...}
    This listener method will be invoked at the start of user's touch event.
3. public void onStopTrackingTouch(SeekBar seekBar) {...}
    This listener method will be invoked at the end of user touch event.

Example application:
In this example application, I am just creating the SeekBar and displaying its current value in the TextView.

Android layout xml file: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="26dp"
        android:max="10"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/seekBar1"
        android:layout_marginLeft="29dp"
        android:layout_marginTop="14dp" />

</RelativeLayout>


Activity class: MainActivity.java


package com.example.seekbar;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

/**
 * Android SeekBar example
 * @author Prabu Dhanapal
 * @version 1.0
 * @since SEP 02 2013
 * 
 */
public class MainActivity extends Activity {
 
 private SeekBar seekBar;
 private TextView textView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  seekBar = (SeekBar) findViewById(R.id.seekBar1);
  textView = (TextView) findViewById(R.id.textView1);
  // Initialize the textview with '0'
  textView.setText(seekBar.getProgress() + "/" + seekBar.getMax());
  seekBar.setOnSeekBarChangeListener(
                new OnSeekBarChangeListener() {
    int progress = 0;
        @Override
      public void onProgressChanged(SeekBar seekBar, 
                                            int progresValue, boolean fromUser) {
        progress = progresValue;
      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
        // Do something here, 
                      //if you want to do anything at the start of
        // touching the seekbar
      }

      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
        // Display the value in textview
        textView.setText(progress + "/" + seekBar.getMax());
      }
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; 
                //this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}


Output screenshots: 

Seekbar


Seekbar


Seekbar


Source code of this application:
SeekBar.zip
Read More

Friday, 14 March 2014

Android Compass Code Example

Today I’m going to share a working code to make a very simple compass application for your android device.
 
Some android device (like Huawei Y300 and Lenovo P700i) does not have full support of motions sensors so this code will not work for them.



Files Needed

You need to create your own compass image. For this example, I’m using a stock photo. Your image must be a PNG with transparent background, do not use this jpg file I used.


img_compass
Add caption

Let’s Code

Here’s our MainActivity.java
01package com.example.compassapp;
02
03import android.app.Activity;
04import android.hardware.Sensor;
05import android.hardware.SensorEvent;
06import android.hardware.SensorEventListener;
07import android.hardware.SensorManager;
08import android.os.Bundle;
09import android.view.animation.Animation;
10import android.view.animation.RotateAnimation;
11import android.widget.ImageView;
12import android.widget.TextView;
13
14public class MainActivity extends Activity implements SensorEventListener {
15
16    // define the display assembly compass picture
17    private ImageView image;
18
19    // record the compass picture angle turned
20    private float currentDegree = 0f;
21
22    // device sensor manager
23    private SensorManager mSensorManager;
24
25    TextView tvHeading;
26
27    @Override
28    protected void onCreate(Bundle savedInstanceState) {
29        super.onCreate(savedInstanceState);
30        setContentView(R.layout.activity_main);
31
32        //
33        image = (ImageView) findViewById(R.id.main_iv);
34
35        // TextView that will tell the user what degree is he heading
36        tvHeading = (TextView) findViewById(R.id.tvHeading);
37
38        // initialize your android device sensor capabilities
39        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
40    }
41
42    @Override
43    protected void onResume() {
44        super.onResume();
45
46        // for the system's orientation sensor registered listeners
47        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
48                SensorManager.SENSOR_DELAY_GAME);
49    }
50
51    @Override
52    protected void onPause() {
53        super.onPause();
54
55        // to stop the listener and save battery
56        mSensorManager.unregisterListener(this);
57    }
58
59    @Override
60    public void onSensorChanged(SensorEvent event) {
61
62        // get the angle around the z-axis rotated
63        float degree = Math.round(event.values[0]);
64
65        tvHeading.setText("Heading: " + Float.toString(degree) + " degrees");
66
67        // create a rotation animation (reverse turn degree degrees)
68        RotateAnimation ra = new RotateAnimation(
69                currentDegree,
70                -degree,
71                Animation.RELATIVE_TO_SELF, 0.5f,
72                Animation.RELATIVE_TO_SELF,
73                0.5f);
74
75        // how long the animation will take place
76        ra.setDuration(210);
77
78        // set the animation after the end of the reservation status
79        ra.setFillAfter(true);
80
81        // Start the animation
82        image.startAnimation(ra);
83        currentDegree = -degree;
84
85    }
86
87    @Override
88    public void onAccuracyChanged(Sensor sensor, int accuracy) {
89        // not in use
90    }
91}
Our layout file activity_main.xml
01<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02    android:layout_width="match_parent"
03    android:layout_height="match_parent"
04    android:background="#fff" >
05
06    <TextView
07        android:id="@+id/tvHeading"
08        android:layout_width="wrap_content"
09        android:layout_height="wrap_content"
10        android:layout_centerHorizontal="true"
11        android:layout_marginBottom="40dp"
12        android:layout_marginTop="20dp"
13        android:text="Heading: 0.0" />
14
15    <ImageView
16        android:id="@+id/imageViewCompass"
17        android:layout_width="wrap_content"
18        android:layout_height="wrap_content"
19        android:layout_below="@+id/tvHeading"
20        android:layout_centerHorizontal="true"
21        android:src="@drawable/img_compass" />
22
23</RelativeLayout>

Source Code Download

You can download this sample project here: CompassApp.zip

Some notes

My app orientation is locked to portrait mode. There are no special permissions in the Manifest file.
Read More
Subscribe
Labels
Popular Posts

Subscribe Via Email

About Us

THIS IS ANDROID AND JAVA FREE TUTORIAL AND ALL PROVIDE SOLUTION IN ANDROID AND JAVA INTERVIEW QUE AND ANS IS AVAILABLE IN MY BLOG AND ANY QUERY PLEASE CONTACT ME GO NOW CONTACT US PAGE.

Total Pageviews

© Android and Java solution All rights reserved | Designed By Fireandroids