Tuesday, 9 July 2013

Fragment In Android Example In seven easy step

Android introduced fragments in Android 3.0 (API level 11) before create such type of project first put this line in your manifest file 


<uses-sdk  android:minSdkVersion="11"    android:targetSdkVersion="16" /> or more API 

Note:- Nothing else configuration for that 

For that demo we need device configuration like 




In that demo application we use List Fragment Activity for display List view in one fragment and on click over the particular index of list display the detail left portion 

Step -1 Create Project in Android 
Step -2 put the following code in main.xml

<LinearLayout 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" >
<fragment 
    android:id="@+id/list"
    android:layout_width="200dp"  
    android:layout_height="wrap_content"
    android:layout_margin="?android:attr/actionBarSize"
    class="com.example.demofrag.ListActivityDemo"
    />
 <fragment 
     android:id="@+id/detail"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     class="com.example.demofrag.DetailFragment"
     />
</LinearLayout>


Step:3 put following code in MainActivity.java


package com.example.demofrag;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}



}


Step-4 create ListFragment.java class 



package com.example.demofrag;

import android.annotation.SuppressLint;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

@SuppressLint("NewApi")
public class ListActivityDemo extends ListFragment {

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String data =(String)getListView().getItemAtPosition(position);
   DetailFragment detailFragment = (DetailFragment)getFragmentManager().findFragmentById(R.id.detail);
   if(detailFragment!=null && detailFragment.isInLayout()){
    detailFragment.setText(data);
   }
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
 String[] arrray = new String[]{"JAVA","JEE","JSE","ANDROID"};
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1,arrray);
      setListAdapter(adapter);
}

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
  
}


Step 5 create DeatilFragment.java

package com.example.demofrag;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

@SuppressLint("NewApi")
public class DetailFragment extends Fragment{

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.frag_tree, container ,false);
return view;

}
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.captain);
view.setText(item);

  
}

Step:6 Create frag_one.xml layout



<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    

</fragment>

Step 7: Create frag_three.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/captain"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginTop="20dip"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

</LinearLayout>



for more Detail check below  
http://developer.android.com/training/basics/fragments/creating.html

Tuesday, 25 June 2013

Slide Show in Android



Step:- 1 Create Activity in your project
public class MainActivity extends Activity {
 ImageView  slide; //image view form layout
 Button playslide; //on click start play slide show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

        playslide = (Button)findViewById(R.id.playslide);
playslide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
   //first create array of all resource 
final int[] image = new int[]{R.drawable.audio_pause,R.drawable.audio_play,R.drawable.audio_replay,R.drawable.audio_seek,R.drawable.audio_stop};
 //create handel for update the User interface 
         final Handler handler = new Handler();
// create those can set the image to image view
final Runnable runnable = new Runnable() {@Override public void run() {slide.setImageResource(image[currentIndex% image.length]);currentIndex++;}};
//Create timer class for refresh image after some time iterval      
Timer task = new Timer();
         task.scheduleAtFixedRate(new TimerTask() {@Override public void run() {handler.post(runnable);}}, 0,4000);
}
});
}
}
Step: 2 create layout to display the image

<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"
    >

 <ImageView
       android:id="@+id/slide"
       android:layout_below="@+id/playslide"
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:paddingTop="100dp"
       android:src="@drawable/ic_launcher"
       android:layout_centerInParent="true"
       />
</RelativeLayout>



Monday, 21 January 2013

Singleton Deign Pattern

Step by Step Singleton Deign Pattern

 Step:-1 Create class can't access by out side the with help of constructor (so avoid the creation of object )
              so as below

      class DemoSingleton {
 
        private DemoSingleton(){
   
          }
    }
       
Step :-2 Then quick question in mind  how we get the object that class or use it . so create a class type variable of DemoSingleton type with private and static

   class DemoSingleton{
 
   private static DemoSingleton demosingleton;

   private DemoSingleton(){

  }

 }

Step :-3 Then create method who will create and check object is not more then on copy.

class DemoSingleton {

 private static DemoSingleton demosingleton;

  private DemoSingleton(){
}

public static DemoSingleton getDemoSingleton(){
     
   if(demosingleton== null){
   demosingleton=new DemoSingleton();
  }
 
return demosingleton;
}

}

Step:4 But it is not food full in case of multiple threading so we can modify some code

class DemoSingleton {

 private static DemoSingleton demosingleton;

  private DemoSingleton(){
}

public static DemoSingleton getDemoSingleton(){
 
    synchronized (DemoSingleton.class) {
if (demoSingleton == null) {
   if(demosingleton== null){
   demosingleton=new DemoSingleton();
  }
  }
}
return demosingleton;
}

}