Thursday 28 March 2013

Android: Wobble Effect Animation

res/anim/wobble.xml


<?xml version="1.0" encoding="UTF-8"?>


<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false">

   <scale
          android:interpolator="@android:anim/accelerate_decelerate_interpolator"
          android:fromXScale="0.0"
          android:toXScale="1.2"
          android:fromYScale="0.0"
          android:toYScale="1.2"
          android:pivotX="50%"
          android:pivotY="50%"
          android:fillAfter="false"
          android:duration="400" />
   <set android:interpolator="@android:anim/decelerate_interpolator">
      <scale
             android:fromXScale="1.2"
             android:toXScale="0.8"
             android:fromYScale="1.2"
             android:toYScale="0.8"
             android:pivotX="50%"
             android:pivotY="50%"
             android:startOffset="400"
             android:duration="400"
             android:fillBefore="false" />
      <!-- <rotate
             android:fromDegrees="0"
             android:toDegrees="360"
             android:toYScale="0.0"
             android:pivotX="50%"
             android:pivotY="50%"
             android:startOffset="700"
             android:duration="400" />
          -->
   </set>
</set>


ImageView iv1=(ImageView)findViewById(R.id.imageviewid);


Animation shake = AnimationUtils.loadAnimation(BuzzBoardMain.this, R.anim.wobble);

shake.reset();
shake.setFillAfter(true);
iv1.startAnimation(shake);

Wednesday 20 March 2013

Android: Toggle Switch like iphone



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

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="100dp" >

        <SlidingDrawer
            android:id="@+id/slidingDrawer1"
            android:layout_width="154dp"
            android:layout_height="54dp"
            android:background="@drawable/ios_retina_toggle_on_full"
            android:content="@+id/content"
            android:handle="@+id/handle"
            android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/handle"
                android:layout_width="54dp"
                android:layout_height="54dp"
                android:background="#00000000"
                android:src="@drawable/ios_retina_toggle_button" />
            <ImageView
                android:id="@+id/content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ios_retina_toggle_off" />

        </SlidingDrawer>

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ios_retina_toggle_frame" />

    </FrameLayout>

</LinearLayout>
ios_retina_toggle_frame

 ios_retina_toggle_button
ios_retina_toggle_on_full


ios_retina_toggle_off



Android: Alert dialog with Animation

 /res/values/styles.xml


<resources>

    <style name="AppTheme" parent="android:Theme.Light" />

    <style name="DialogAnimation">
        <item name="android:windowEnterAnimation">@anim/slide_up</item>
        <item name="android:windowExitAnimation">@anim/slide_down</item>
    </style>
</resources>

/res/layout/dialoglayout.xml.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>
    <Button
        android:id="@+id/dismiss"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dismiss"/>

</LinearLayout>


/res/anim/slide_up.xml.


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

    <translate
        android:duration="300"
        android:fromYDelta="100%p"
        android:toYDelta="0" />

</set>

/res/anim/slide_down.xml.


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

    <translate
        android:duration="300"
        android:fromYDelta="0"
        android:toYDelta="100%p" />

</set>

MainLayout

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

    <Button
        android:id="@+id/opendialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="@dimen/padding_medium"
        android:text="Open Dialog"
        tools:context=".MainActivity" />

</RelativeLayout>

MainActivity.java

package com.example.animationdialog;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.app.Dialog;

public class MainActivity extends Activity {

 Button btnOpenDialog;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnOpenDialog = (Button)findViewById(R.id.opendialog);
        btnOpenDialog.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    openDialog();
   }});
    }

    private void openDialog(){
     final Dialog dialog = new Dialog(MainActivity.this);
     dialog.setTitle("Animation Dialog");
     dialog.setContentView(R.layout.dialoglayout);
     dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
     Button btnDismiss = (Button)dialog.getWindow().findViewById(R.id.dismiss);
     
     btnDismiss.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    dialog.dismiss();
   }});
     
     dialog.show();
    }
    
}

Sunday 17 March 2013

Android: How to get iPhone-style overscrolling in ListView


BounceListViewActivity.java

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import com.example.bouncelistview.BounceListView;

public class BounceListViewActivity extends Activity {

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

BounceListView mBounceLv = (BounceListView) findViewById(R.id.list);
mBounceLv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, getData()));
}

private List<String> getData() {

List<String> data = new ArrayList<String>();
data.add("abc");
data.add("dsdsd");
data.add("dsds");
data.add("dff");
data.add("hghg");
data.add("jhj");
data.add("kjk");
data.add("abrerec");
data.add("afdfbc");
data.add("fdf");
data.add("abdsdc");
data.add("sds");
data.add("wewe");
data.add("sfsf");
data.add("dfggrfg");
data.add("fdfdvdvd");
data.add("vdvdv");
data.add("abdvdvc");
data.add("abdvdvc");
data.add("dvdvdv");
data.add("bbbfb");
data.add("avdvdvbc");
data.add("vdvdv");
return data;
}

}

BounceListView.java 


import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.ListView;

public class BounceListView extends ListView {
private static final int MAX_Y_OVERSCROLL_DISTANCE = 200;

private Context mContext;
private int mMaxYOverscrollDistance;

public BounceListView(Context context) {
super(context);
mContext = context;
initBounceListView();
}

public BounceListView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
initBounceListView();
}

public BounceListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
initBounceListView();
}

private void initBounceListView() {
// get the density of the screen and do some maths with it on the max
// overscroll distance
// variable so that you get similar behaviors no matter what the screen
// size

final DisplayMetrics metrics = mContext.getResources()
.getDisplayMetrics();
final float density = metrics.density;

mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE);
}

@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
int scrollY, int scrollRangeX, int scrollRangeY,
int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
// This is where the magic happens, we have replaced the incoming
// maxOverScrollY with our own custom variable mMaxYOverscrollDistance;
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY,
scrollRangeX, scrollRangeY, maxOverScrollX,
mMaxYOverscrollDistance, isTouchEvent);
}

}

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />

    <com.example.bouncelistview.BounceListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>



Thursday 14 March 2013

Android: Change the Alpha value of button.



To set the Alpha value

Drawable d = getResources().getDrawable(R.drawable.Imagename);
d.setAlpha(50);
buttonId.setBackgroundDrawable(d);


You can set alpha level 0 to 255
o means transparent and 255 means opaque.
to restore the Original button.
Drawable d = getResources().getDrawable(R.drawable.Imagename);
d.setAlpha(255);

buttonId.setBackgroundDrawable(d);

Android: Shadow for Layout


<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- Bottom 2dp Shadow -->

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#BDBDBD" />

            <corners android:radius="7dp" />

        </shape>
    </item>

    <!-- White Top color -->

    <item android:bottom="3px">
        <shape android:shape="rectangle" >
            <solid android:color="#DADCE8" />

            <corners android:radius="7dp" />

        </shape>
    </item>

</layer-list>

Wednesday 13 March 2013

Android: app restarted after lock screen starts


 In AndroidManifest.xml, 
android:configChanges="keyboardHidden|orientation"

Tuesday 12 March 2013

Android: How can I know that my WebView is loaded 100%


mWebView.setWebViewClient(new WebViewClient() {

   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
       if (!loadingFinished) {
          redirect = true;
       }

   loadingFinished = false;
   webView.loadUrl(urlNewString);
   return true;
   }

   @Override
   public void onPageStarted(WebView view, String url) {
        loadingFinished = false;
        //SHOW LOADING IF IT ISNT ALREADY VISIBLE  
    }

   @Override
   public void onPageFinished(WebView view, String url) {
       if(!redirect){
          loadingFinished = true;
       }

       if(loadingFinished && !redirect){
         //HIDE LOADING IT HAS FINISHED
       } else{
          redirect = false; 
       }

    }
});

Thursday 7 March 2013

Android: height and width for layout based on device screen size



display = getWindowManager().getDefaultDisplay();
Constants.deviceWidth = display.getWidth();
Constants.deviceHeight = display.getHeight();



LinearLayout.LayoutParams paramssample = new LinearLayout.LayoutParams(
Constants.deviceWidth, 60 * Constants.deviceHeight / 100);
mBottomSlider.setLayoutParams(paramssample);