Pages

Friday 29 March 2013

Android switch fragment with animation


Following is simple code to animate fragment when switching between fragment

CategoryFragment newFragment = new CategoryFragment();

   Bundle args = new Bundle();
   newFragment.setArguments(args);

   FragmentTransaction transaction = getSupportFragmentManager()
     .beginTransaction();

   // Replace whatever is in the fragment_container view with this
   // fragment,
   // and add the transaction to the back stack so the user can
   // navigate back
   transaction.setCustomAnimations(R.anim.left_to_right,
     R.anim.right_to_left);
   transaction.replace(R.id.fragment_container, newFragment);
   transaction.addToBackStack(null);

   // Commit the transaction
   transaction.commit();

Put Following xml to res/anim folder
left_to_right.xml



    




left_to_right.xml



    



Android load fragment from backstack

For example my current fragment is catetegory fragment . It was loaded from option Fragment. now click on button i want to load option Fragment from backstack then following code may help.

btnBack.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    getActivity().getSupportFragmentManager().popBackStack();
    getActivity().getSupportFragmentManager().beginTransaction()
      .remove(CategoryFragment.this).commit();
   }
  });

Monday 18 March 2013

Android - Set marker to exact position using custom Overlay in MapView

There is a method in the ItemizedOverlay class called: boundCenterBottom(Drawable), which does the setBounds part. There's also a boundCenter(Drawable) method.

Just replace this code in your custom Itemized Overlay constructor :

public CustomItemizedOverlay(Drawable defaultMarker, MapView mapView) {
       super(boundCenterBottom(defaultMarker), mapView);
}

Thursday 14 March 2013

Android portrait mode video recording orientation proble,

In Android In portrait mode you need to add following to your camera object and recording object before calling prepare function.

check following function.
we assume you create surface object and camera and other required object


protected void startRecording() throws IOException {
  
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
   Log.i(TAG, ">=GINGERBREAD");
   openFrontCamera();
  }

  if (mCamera == null) {
   Log.i(TAG, "CAMERA NULL");
   mCamera = Camera.open();
  }
  
  int camera_oriantation = 90; // for portrait mode
  
  mCamera.setDisplayOrientation(camera_oriantation);

  Date date = new Date();
  filename = "rec" + date.toString().replace(" ", "_").replace(":", "_")
    + ".mp4";

  File dir = new File(General.MediaPath);
  if (!dir.exists()) {
   dir.mkdirs();
  }
  // create empty file it must use
  File file = new File(General.MediaPath, filename);

  mrec = new MediaRecorder();

  mCamera.lock();
  mCamera.unlock();

  // Please maintain sequence of following code.

  // If you change sequence it will not work.
  mrec.setCamera(mCamera);
  mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
  mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
  mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
  mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

  mrec.setOrientationHint(camera_oriantation);
  
  mrec.setPreviewDisplay(surfaceHolder.getSurface());
  mrec.setOutputFile(General.MediaPath + File.separator + filename);
  mrec.prepare();
  mrec.start();

 }

Android Generate the authentic hash key for Facebook

This is the Code of Generate the Actual Hash key of your Computer.
No Need to  use command prompt ,keytool , keystore.
Just paste this bellow code in your activity and Look at inside you Log.
There is one Log of  "Generated Hash Key".
Just Get it and use it simple...

Here is a quick code snippet :

private void generateHashKey() {
 try {
   PackageInfo info = getPackageManager().getPackageInfo(
     "taranor.taxicustomer.app", PackageManager.GET_SIGNATURES);
   for (Signature signature : info.signatures) {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(signature.toByteArray());
    Log.d("Generated Hash Key", Base64.encodeBytes(md.digest()));
   }
  } catch (NameNotFoundException e) {
   e.printStackTrace();
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  }
 }
Only one thing you need is the Base64.java to get this code is working.
I used the Following Link to get Base64.java.

Saturday 9 March 2013

Android - Button's different state effects using single image



Create an XML file for button selector :
buton_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/my_button" android:state_pressed="false"/>

    <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>

</selector>
Here my_button is an image in drawable folder and button_pressed is an XML file in drawable.
button_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <bitmap android:src="@drawable/my_button" />
    </item>
    <item>
        <color android:color="#60000000" />
        <!--
        <shape>
            <gradient
                android:endColor="#ff0000"
                android:startColor="#00ff00" />            
            <corners android:radius="3dp" />
        </shape>
        -->
    </item>
</layer-list>
Now set button's background as buton_selector.
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:background="@drawable/button_selector"        
        android:text="my botton" />

Note : We can also use shape,gradient,stroke (See commented part of button_pressed.xml) for rounded button backgrounds!

Thursday 7 March 2013

Android - How to generate Google Map API Key

RUN COMMAND PROMPT AS ADMINISTRATOR.

1.Locate your debug.keystore file
"C:\Users\QTM-19\.android\debug.keystore"

2.Locate keytool.exe in Java\bin folder
"C:\Program Files\Java\jre7\bin"

3.Generate MD5 key using this command in cmd :
C:\Program Files\Java\jre7\bin>keytool -v -list -alias androiddebugkey -keystore C:\Users\QTM-19\.android\debug.keystore -storepass android -keypass android
(remove "-v" if Java version < 7)

Certificate fingerprints:
          MD5:  AB:42:12:2A:80:FF:BE:DD:C3:57:38:80:87:CC:39:B3
          SHA1: 31:4D:1A:58:FF:01:14:79:75:E8:D0:1B:1D:1C:61:CF:7E:71:0D:62
          SHA256: AF:87:67:02:5D:EB:3D:03:80:9C:26:5E:C2:E7:96:67:19:17:66:26:A5:
89:7B:AA:8C:D2:E7:F0:27:42:E0:23
          Signature algorithm name: SHA256withRSA
        Version: 3



4.Get a key for the Google Maps Android API v1:
https://developers.google.com/maps/documentation/android/v1/maps-api-signup

5.To register for a Google Maps Android API v1 Key, follow these steps:
-->If you don't have a Google account, use the link on the page to set one up.
-->Paste the MD5 key
-->Click "Generate API Key"


Generated key:0kVayCORItzMtF85IkONrJY0ECuVYdTcAuUE4HQ


6.Add API KEY to your application's MapView objects:
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="EXAMPLE_MAPS_API_KEY_STRING" />

 7.Add <uses-library android:name="com.google.android.maps library" :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.package.name">
...
<application android:name="MyApplication" >
<uses-library android:name="com.google.android.maps" />
...
</application>

8.Sign your application with the certificate that corresponds to the Maps API Key referenced in your MapView elements.

==>FOR MORE INFO VISIT:
https://developers.google.com/maps/documentation/android/v1/mapkey

==>How to regenerate debug.keystore? Visit Here :
http://stackoverflow.com/questions/2194808/debug-certificate-expired-error-in-eclipse-android-plugins/2196397#2196397