Learn Python


Click to Learn Python3

Tuesday, August 28, 2012

Seek Bar or Progress Bar or Slide Bar or Slider

This bar will be like this, the name for this bar is seek bar



In the xml file we need to mention like this to call the seek bar


xml
<SeekBar
android:id="@+id/seekbar"
android:layout_width="fill_parent"   
android:layout_height="wrap_content"   
android:layout_margin="10px"  
/>  


The java class file will be like this


Seek.java
package com.android.seek;

import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class Seek extends Activity {
    /** Called when the activity is first created. */
  AudioManager audioManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final TextView reading = (TextView) findViewById(R.id.reading);
        SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar);
        seekBar.setMax(100);
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
   
   @Override
   public void onStopTrackingTouch(SeekBar seekBar) {
   }
   
   @Override
   public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
   }
   
   @Override
   public void onProgressChanged(SeekBar seekBar, int progress,
     boolean fromUser) {
    reading.setText("Processing "+progress+"% ");
   }
  });
        }
}



The code is fulling checked so you can run the code after downloading it itself.

You can download the full source code


Download source


Have a good day.

Monday, November 21, 2011

Running a service in background on Android


Here the code is generate to run some process in background through service. For example we are running the time in background and displaying it in the UI. The service coding will be in a seperate class MyService.java


MyService.java
package com.services.demo;

import java.util.Date;
import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; public class MyService extends Service { public static final String BROADCAST_ACTION = "com.services.demo.ServiceDemoActivity"; private final Handler handler = new Handler(); Intent intent;

int counter = 0; @Override public void onCreate() { // Called on service created intent = new Intent(BROADCAST_ACTION); } @Override

public void onDestroy() { // Called on service stopped }

@Override public void onStart(Intent intent, int startid) { int i = 0; while (i < 101) { if (i > 100) { this.onDestroy(); } else { counter = i; i++; handler.removeCallbacks(sendUpdatesToUI); handler.postDelayed(sendUpdatesToUI, 1 * 1000); // 1 sec }

} } private Runnable sendUpdatesToUI = new Runnable() { public void run() { DisplayLoggingInfo(); handler.postDelayed(this, 1 * 1000); // 1 sec }

}; private void DisplayLoggingInfo() { intent.putExtra("time", new Date().toLocaleString()); intent.putExtra("counter", String.valueOf(counter)); sendBroadcast(intent); }

public static boolean isRunning() { return true; } @Override

public IBinder onBind(Intent intent) { return null; } }

So for starting the service and stopping the service we will be using


Java
startService(new Intent(ServiceDemoActivity.this, MyService.class));
stopService(new Intent(ServiceDemoActivity.this, MyService.class));

For updating the UI we need to use the broadcast service. In that BroadcastReceiver we will update the UI

Java
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateDate(intent);
}

}; private void updateDate(Intent intent) { String time = intent.getStringExtra("time"); TextView date = (TextView) findViewById(R.id.date); date.setText(time); }
In the MyService.java the method DisplayLoggingInfo() will send the broadcast to the activity to update the UI, the UI xml code is
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/hello"
 android:id="@+id/notification"
 />

 <TextView
 android:layout_width="fill_parent" android:id="@+id/date"
 android:layout_height="wrap_content" android:text="" android:gravity="center" android:textSize="20sp" android:padding="20dp"/>
 <Button android:id="@+id/stop" android:text="Stop Service" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>

If we are opening the application on the first time means, it will be like this

and if we go out and come back to the page means, it will be showing a message like this

and if we stop the service means, it will be showing a message like this



You can download the full source code



Download source


Have a good day.