Pages

Saturday, February 15, 2014

How to get screen dimensions in android



There is Display class to get the display of screen in android.

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
//getSize()  method is introduced in API level 13.
int width = size.x;
int height = size.y;

Before API  level 13 below method are used.

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); //this method is deprecated now
int height = display.getHeight();//this method is deprecated now



Wednesday, February 12, 2014

Example to show Animated GIF image in Android

Android  doesnot support GIF image simply you should use WebView  or other thing to run gif image.
I use WebView to implement the GIF image .


First create a GIFView by extending webview.

public class GIFView extends WebView {

public static final String HTML_FORMAT = "<html><body style=\"text-align: center;  vertical-align:                       right;background-color: transparent;\"><img src = \"%s\" /></body></html>";

public GIFView(Context context, String fileUrl) throws IOException {
  super(context);


final String html = String.format(HTML_FORMAT, fileUrl);

setBackgroundColor(Color.TRANSPARENT);
loadDataWithBaseURL("", html, "text/html", "UTF-8", "");

}

}


Create your activity class here.

public class MainActivity extends Activity {

private WebView webView;
// private ProgressBar pd;
private LinearLayout llProgress;

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


      llProgress = (LinearLayout) findViewById(R.id.ll_progress);
try {

//                give your gif image name here(example.gif).
GIFView gif = new GIFView(this,
"file:///android_asset/example.gif");
llProgress.addView(gif);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


here is your   activity_main   file

<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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/ll_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" >
    </LinearLayout>

</RelativeLayout>


Tuesday, February 11, 2014

Example to create Round Shape image bitmap android



public static Bitmap getRoundedShape(Bitmap scaleBitmapImage,
int targetWidth, int targetHeight, Context context) {
// TODO Auto-generated method stub
// int targetWidth = width;
// int targetHeight = height;

targetHeight = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, targetHeight, (context)
.getResources().getDisplayMetrics());
targetWidth = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, targetWidth, (context)
.getResources().getDisplayMetrics());

Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth) / 2, ((float) targetHeight) / 2,
(Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
Path.Direction.CCW);
Log.d("", "In process: width" + targetWidth + " height" + targetHeight);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()), new Rect(0, 0, targetWidth,
targetHeight), null);
return targetBitmap;
}

Android ViewPager Example


This is simple example for a viewpager in android.First add "android-support-v4.jar"  library in your libs folder.than create an activity and add this code.


public class MainActivity extends Activity implements OnClickListener {

private ViewPager viewPager;
private TextSwipping pagerAdapter;
private ArrayList<String> alText = new ArrayList<String>();
private Context context;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.welcomepage);

alText.add("Hi");
alText.add("This");

alText.add("is");

alText.add("Manu");

viewPager = (ViewPager) findViewById(R.id.welcome_text_pager);
pagerAdapter = new TextSwipping(alText, this);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(0);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrollStateChanged(int pos) {
// TODO Auto-generated method stub

if (viewPager.getCurrentItem() == 0) {
//                           set your conditions here
} else if (viewPager.getCurrentItem() == 1) {
//                           set your conditions here
} else if (viewPager.getCurrentItem() == 2) {
//                           set your conditions here
} else if (viewPager.getCurrentItem() == 3) {
//                           set your conditions here
}

}
});

}

}



add this viewpager in your welcomepage.xml  file



<android.support.v4.view.ViewPager
            android:id="@+id/welcome_text_pager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />





and create Adapter for the text displaying in your viewpager.



public class TextSwipping extends PagerAdapter {

ArrayList<String> alText;
Context context;

public TextSwipping(ArrayList<String> alText, Context context) {
this.alText = alText;
this.context = context;

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// View view= inflater.inflate(R.layout.textswitcher, null);


// I am using a   dynamic  text view  you could use
//a layout using inflater or use a fragment as your necessity.
TextView tvTextSwitcher = new TextView(context);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
tvTextSwitcher.setTextColor(Color.parseColor("#ff78ff"));
tvTextSwitcher.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20);
tvTextSwitcher.setLayoutParams(lp);
tvTextSwitcher.setGravity(Gravity.CENTER_HORIZONTAL);

tvTextSwitcher.setText("  " + alText.get(position));
tvTextSwitcher.setTypeface(WidgetProperties
.setTextTypefaceRegular(context));

((ViewPager) container).addView(tvTextSwitcher);

return tvTextSwitcher;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
return null;
}
}