Monday, 2 June 2014

Fragment Transitions

 MainActivity.java

public class MainActivity extends Activity {
   
    private Fragment mLandingAilsesFrag;
    private Fragment mDetailsFragment;
    private boolean mIsDetailsFragmentAdded;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_fragment);
        getActionBar().setHomeButtonEnabled(true);
        mLandingAilsesFrag = new CardFragement();
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, mLandingAilsesFrag).commit();
       
    }
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {

        int itemId = item.getItemId();
        switch (itemId) {
        case android.R.id.home:
       if(mIsDetailsFragmentAdded){
          removeDetailsFragement();
} else {
addDetailsFragment();
}
            break;

        }

        return true;
    }
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            if(mIsDetailsFragmentAdded){
                removeDetailsFragement();
                return true; 
            }else {
                return super.onKeyUp(keyCode, event); 
            }
           
        }
        return super.onKeyUp(keyCode, event);
    }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void addDetailsFragment() {
        mIsDetailsFragmentAdded = true;
        mDetailsFragment = new DetailsCardViewFragment();
        FragmentManager fragmentManager = getFragmentManager(); 
        FragmentTransaction transaction = fragmentManager.beginTransaction();    
        transaction.setCustomAnimations(R.animator.slide_in_left,   R.animator.slide_out_right);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.add(R.id.content_frame, mDetailsFragment);
        transaction.commit();
    }
   
    public void removeDetailsFragement() {
        if (mIsDetailsFragmentAdded) {
            mIsDetailsFragmentAdded = false;
            FragmentManager fragmentManager = getFragmentManager();
            //fm.beginTransaction().remove(mDetailsFragment).commit();
          
            FragmentTransaction transaction = fragmentManager.beginTransaction();   
            transaction.setCustomAnimations(R.animator.slide_in_left,   R.animator.slide_out_right);
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            transaction.remove(mDetailsFragment);
            transaction.commit();
        }
    }
}

activity_main_fragment.xml

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_farment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.googlenowcard.CustomFrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </FrameLayout>

CardFragement.java

  public class CardFragement extends Fragment{ 
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_main, null);
 //inflate the layout and return the View.
    return  view;
}
  
}
activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_bottom_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text= "fragmentOne">
    </TextView>
</RelativeLayout>

DetailsCardViewFragment:

  public class DetailsCardViewFragment extends Fragment{ 
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_details, null);
 //inflate the layout and return the View.
    return  view;
}
  
}

activity_details.xml:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_bottom_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text= "fragmentOne">
    </TextView>
</RelativeLayout>


slide_in_left.xml:
create one xml file in res/animator folder.

<?xml version="1.0" encoding="utf-8"?>
<set>
  <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="x"
    android:valueType="floatType"
    android:valueFrom="1280"
    android:valueTo="0"
    android:duration="500"/>
</set>

slide_out_right.xml:
create one xml file in res/animator folder.

<?xml version="1.0" encoding="utf-8"?>
<set>
  <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="x"
    android:valueType="floatType"
    android:valueFrom="0"
    android:valueTo="1280"
    android:duration="500"/>
</set> 



Note: for th alternative clicks details fragment added and removed and fragment comes fromt the right side.

showing popupwindow at view android

1. copy this popupmenu.xml in your res/menu folder

 <menu xmlns:android="http://schemas.android.com/apk/res/android">
  <group android:id="@+id/group_popupmenu">
      <item android:id="@+id/menu1"
          android:title="item one"/>
      <item android:id="@+id/menu2"
          android:title="item two"/>
      <item android:id="@+id/menu3"
          android:title="item three"/>
  </group>
</menu>


2. copy this function in your code where you want show the menu.


  private void showPopupMenu(View v){
        PopupMenu popupMenu = new PopupMenu(mContext, v);
           popupMenu.getMenuInflater().inflate(R.menu.popupmenu, popupMenu.getMenu());
        
           popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
       
        @Override
        public boolean onMenuItemClick(MenuItem item) {
         Toast.makeText(mContext,
           item.toString(),
           Toast.LENGTH_LONG).show();
         return true;
        }
       });
        
           popupMenu.show();
       }


3. from the clicke listner call this function as show below.

 ex: menuId.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                showPopupMenu(v);
            }
        });

that's it.

Friday, 19 April 2013

How to run monkeyrunner?


If you want to run monkeyrunner from anywhere within the command line, you need to add the path of the monkeyrunner directory(\tools ) to your system PATH variable.

in ubuntu follow the steps below.

1. open the ternminal

2. changed to to the direcotory

  android-sdk-linux/platform-tools$


3.   ./adb -d shell monkey -p com.neem23.fishwrap -v 20000


   here : com.neem23.fishwrap is my app package.

        20000 no of inputs.

         -d means device.

Monday, 15 April 2013

Customization of sendIntent android

Action send intent will pickup all the installed application and shown in a popup.

if we want customize this popup. we can get the the individual installed app and 
 we can filter the popup list.

example: 


    /**
     * this method will give all the installed applications
     */
    public void getInstalledPackages() {
       List<ResolveInfo>   activities = context.getPackageManager().queryIntentActivities(
                sendIntent, 0);

        final Object a[] = activities.toArray();
        for (int i = 0; i < activities.size(); i++) {

            Drawable appicon;
           //gets the app name
            String temp = ((ResolveInfo) a[i]).activityInfo.applicationInfo
                    .loadLabel(context.getPackageManager()).toString();
//gets the app icon
            appicon = ((ResolveInfo) a[i]).activityInfo.applicationInfo
                    .loadIcon(context.getPackageManager());
//saving in array list.
            listmessages.add(temp);
            currentDisp.add((ResolveInfo) a[i]);
            drawbles.add(appicon);


        }

    }


//inflating the custom layou

      dialog = new Dialog(context, R.style.Theme_Dialog_Translucent);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.shownetworksdialogue);
         listview = (ListView)dialog.findViewById(R.id.networklist);
        TextView okbuton = (TextView)dialog.findViewById(R.id.shownetworkok);
        TextView dialougeTitle = (TextView)dialog.findViewById(R.id.dialogtitle);


        listview.setAdapter(new CustomAdapter());


CustomAdapter class:



  
    public class CustomAdapter extends BaseAdapter {
      
        public int getCount() {
            return listmessages.size();
        }

    
        public Object getItem(int arg0) {
            return arg0;
        }

  
        public long getItemId(int arg0) {
            return arg0;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
           
                       Holder holderView = null;
            if (convertView == null) {
                holderView = new Holder();
                convertView = inflater.inflate(R.layout.networklist, null);
 
                holderView.network = (TextView) convertView
                        .findViewById(R.id.gmail);
                holderView.launcheicon=(ImageView) convertView.findViewById(R.id.launchericon);
      

               
                convertView.setTag(holderView);
            } else {
                holderView = (Holder) convertView.getTag();
            }

            holderView.launcheicon.setImageDrawable(drawbles.get(position));
holderView.network.setText(listmessages.get(position));               

            
            return convertView;
        }

    }

//holder class

    private class Holder {
     
        TextView network;
        RelativeLayout viewinfolayout,viewinfomainsiblinglayout;
        ImageView launcheicon;

    }



networklist xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:facebook="http://schemas.android.com/apk/res-auto"
    android:id="@+id/popupbg"
    android:layout_width="fill_parent"
    android:layout_height="48dip" >

    android:orientation="horizontal"

    <ImageView
        android:id="@+id/launchericon"
        android:layout_width="42dp"
        android:layout_height="42dp"
   />
      

    <TextView
        android:id="@+id/gmail"
        style="@style/textviewwrapcontent"
    
        android:text="@string/gmail"
        android:textStyle="bold" />
</LinearLayout>



shownetworksdialogue xml:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="48dp"
    android:layout_marginBottom="72dp"
    android:background="@color/lightblack"
    android:orientation="vertical" >
    <include android:id="@+id/showpopop" layout="@layout/popupheading"/>
    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_above="@+id/bellwolay"
                  android:layout_below="@+id/showpopop"
                   >
    <ListView android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:id="@+id/networklist"></ListView>
    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentBottom="true"
                   android:id="@+id/bellwolay"
                  android:orientation="vertical">
    <View android:layout_width="fill_parent"
  
          android:layout_height="1dp"
          android:background="@color/thinkbule"/>
    <TextView
              style="@style/popupbottom"
              android:id="@+id/shownetworkok"
         
              android:text="@string/cancel"
              android:gravity="center"/>
    </LinearLayout>

</RelativeLayout>

 

how to use handlers

 A handler will be executed on the thread in which it is created.

    There are two uses of Handler
(1) to schedule messages and runnables to be executed as some point in the future

(2) to enqueue an action to be performed on a different thread than your own. 

sample code:

    private Runnable pageChangingTask = new Runnable() {
       
        @Override
        public void run() {
          //executable code write here
           
        }
    };






//this line of code will remove any of the pending task in queue.

handler.removeCallbacks(pageChangingTask);




//postDelayed method takes two arguements one runnable object and 
//  time in milliseconds.





                handler.postDelayed(pageChangingTask, 500);