Friday, January 18, 2013

Rounded corners around any view


There two way set round corner around any view(for example linearlayout)
1. first create a 9-patch image that having corners.
this is easiest way to put corners around linearlayout
must make sure the outside 1-pixel area is completely empty except for the black lines.
2. second method is by drawable xml, see below sample
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
   <stroke android:width="2dp" android:color="#FF00FF00" />
    <solid android:color="#DEE2EE" />
    <padding android:left="7dp" android:top="7dp"
            android:right="7dp" android:bottom="7dp" />
    <corners android:radius="15dp" />
</shape>
see output of this

Friday, January 11, 2013

TimePickerDialog - 15 Minutes Interval


Default TimePickerDialog having 1 minute interval changes, how make this by 15 or 30 minutes.
in a very simple way, just create a customize TimePickerDialog class and override the onTimeChanged method of dialog
see below sample code
public class TimePickerDialogs extends TimePickerDialog{

 public TimePickerDialogs(Context arg0, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) {
  super(arg0, callBack, hourOfDay, minute, is24HourView);
  // TODO Auto-generated constructor stub
 }

 @Override
 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
  // TODO Auto-generated method stub
  //super.onTimeChanged(arg0, arg1, arg2);
  if (mIgnoreEvent)
            return;
        if (minute%TIME_PICKER_INTERVAL!=0){
            int minuteFloor=minute-(minute%TIME_PICKER_INTERVAL);
            minute=minuteFloor + (minute==minuteFloor+1 ? TIME_PICKER_INTERVAL : 0);
            if (minute==60)
                minute=0;
            mIgnoreEvent=true;
            view.setCurrentMinute(minute);
            mIgnoreEvent=false;
        }
 }

 private final int TIME_PICKER_INTERVAL=15;
 private boolean mIgnoreEvent=false;
 
}
and called this class by this line of code in your activity class
new TimePickerDialogs(this, new TimePickerDialog.OnTimeSetListener(){

  @Override
  public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
  // TODO Auto-generated method stub
  StringBuilder output = new StringBuilder().append(selectedHour).append(":").append(selectedMinute);
         System.out.println(output.toString());
                }}, hour, minute, true).show();

Thursday, January 10, 2013

Video Streaming in Customize Android Video View


How play video streaming like YouTube rtsp, 3GP, MP4 over http streaming in android video view ?
before start, you must know which video format file supported by android see this link media format
for streaming need to access internet so need internet permission, add below line in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
set streaming url in video view by using setVideoURI method, see below
//youtube video rtsp url
videoviewer.setVideoURI(Uri.parse("rtsp://v4.cache1.c.youtube.com/CiILENy73wIaGQmC00ZlwwIDOxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"));
videoviewer.requestFocus();
videoviewer.setKeepScreenOn(true);
videoviewer.setOnErrorListener(this);
videoviewer.setOnPreparedListener(this);
setOnPreparedListener is use for prepare mediaplayer, that used inside video view, and start buffering video for buffering we override setOnBufferingUpdateListener method of mediaplayer
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
   // show updated information about the buffering progress
   @Override
   public void onBufferingUpdate(MediaPlayer mp, int percent) {
    Log.d(this.getClass().getName(), "percent: " + percent);
   }
 });
for start play video used start() method and for stop using stopPlayback().
Whole source of this post available at My github Repository VideoStreaming

Wednesday, January 9, 2013

Logout and Exit from previous activity in android


Mostly of time, We facing a challenge in android when we creating session base application, in which we need to save login credentials through out application and after click on logout need to finish all previous activities those are using session values.
for save login credential in application used SharedPreferences, SQLite database or external file writing in SD card and more info see Android Storage guide
and in case of logout just remove all values from the SharedPreferences and redirect to login screen but when am click back from android mobile button, last activity restore. How prevent this,?
just add some code of lines in onRestart function in activity class at which you want to check session keys,
see below code of lines
@Override
 protected void onRestart() {
  // TODO Auto-generated method stub
  super.onRestart();
  SharedPreferences pref = context.getApplicationContext().getSharedPreferences("samplekey", Context.MODE_PRIVATE);
  if (pref.getString("<login key name>", "").length()==0) {
      finish();  
  }
 }
whenever you click back button from android mobile , every time onRestart method of previous activity/screen class called and checking about session,
if session not exist than it's finish the activity otherwise open that screen/activity

Catch Home button pressed in Android


searching about how Catch Home button pressed in Android, lot of blogs, technics and method implemented by different- different developer but not sccess for this home button catching... some of the blogs and post says you can't catch Home button click in android due to security and user safety .
finally i found and implement a method to detect home button click and notify user for that see below code samples
for saving info in android am using SharedPreferences so a Session class structure to save all info

public class Session {

 private SharedPreferences pref;
 private Editor editor;
 private String KEY = "sampletest";

 public Session(Context context) {
  // TODO Auto-generated constructor stub
  pref = context.getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
  editor = pref.edit();
 }

 public void savePreferenceData(String key, String value) {
  editor.putString(key, value);
  editor.commit();
 }

 public String getPreferenceData(String key) {
  return pref.getString(key, "");
 }
 
 public void clearall(){
  editor.clear();
  editor.commit();
 }
 
}


now create a common class structure that having technics to detects Home button clicks
public class Common extends Activity{

 private Session presession;
 private Boolean startactivitys;
 private Boolean UserLeaveHints;
 

 @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  startactivitys = false;
  UserLeaveHints = false;
  System.out.println(this+" onResume ");
 }

 
 @Override
 public void startActivity(Intent intent) {
  // TODO Auto-generated method stub
  super.startActivity(intent);
  System.out.println(this+" startActivity ");
  startactivitys = true;
  presession = new Session(this);
  presession.savePreferenceData("onBackPressed", String.valueOf(false));
 }

 @Override
 protected void onStop() {
  // TODO Auto-generated method stub
  super.onStop();
  System.out.println(this+" onStop ");
  presession = new Session(this);
  boolean temp = presession.getPreferenceData("onBackPressed")!=null ? 
    Boolean.parseBoolean(presession.getPreferenceData("onBackPressed")):false;
  System.out.println("previous backpress "+temp);
  if(temp && UserLeaveHints){
   Toast.makeText(this, "app leaving", Toast.LENGTH_SHORT).show();
   System.out.println("++++++++++++++app leaving++++++++++++++++");
  }
  if(!startactivitys && UserLeaveHints){
   Toast.makeText(this, "app leaving", Toast.LENGTH_SHORT).show();
   System.out.println("++++++++++++++app leaving++++++++++++++++");
  }
 }

 @Override
 protected void onUserLeaveHint() {
  // TODO Auto-generated method stub
  super.onUserLeaveHint();
  System.out.println(this+" onUserLeaveHint ");
  UserLeaveHints = true;
 }

 @Override
 public void onBackPressed() {
  // TODO Auto-generated method stub
  super.onBackPressed();
  System.out.println(this+" onBackPressed ");
  presession = new Session(this);
  presession.savePreferenceData("onBackPressed", String.valueOf(true));
 }

 
}
see onStop function having Toast notification, that's show notification to user when user click on home and leaving application safely
One Question arising in mind how use this in your project ?
just simple extent this common class with your activity class instead of extending with activity .. and create Session as mention .
now you test this and provide feedback