Skip to main content

Posts

Showing posts from May, 2013

Download a file to your android device from a remote server with a Custom ProgressBar

First we will create a simple layout with a button that will download a file on it’s onClick event . This is the contents of main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical" >     <Button         android:id="@+id/btnDownload"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentRight="true"         android:layout_alignParentTop="true"         android:layout_marginRight="66dp"         android:onClick="downloadFile"         android:text="Download File" />     <TextView         android:id="@+id/tv1"         android:layout_width="wrap_conte

Android Custom Dialog Example

1) Create XML in \res\drawable and named it bgdialog.xml . <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle" >     <gradient         android:angle="90"         android:endColor="#ffffff"         android:startColor="#ffffff" />     <padding         android:bottom="10dp"         android:left="10dp"         android:right="10dp"         android:top="10dp" />     <stroke         android:width="2dp"         android:color="#000000" />     <corners android:radius="10dp" /> </shape> 2) In main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height=&q

How to animate Visibility/Invisibility view in Android?

 This post is related to show animation on any view in android.  1) Simple Android Animation on View GONE/VISIBLE If you just want a better transition view on GONE / VISIBLE rather than the default Android show/hide, I came across  some simple trick to make a simple animation using TranslateAnimation class. *Note: For a better transitioning, you can use animationlistener , and setVisibility onAnimationStart() or onAnimationEnd(). // View to be animate (I use ImageView as example here) ImageView imageView = (ImageView)findViewById(R.id.imageview); Below are the methods to set view from VISIBLE to GONE // To animate view slide out from left to right public void slideToRight(View view){ TranslateAnimation animate = new TranslateAnimation( 0 ,view.getWidth(), 0 , 0 ); animate.setDuration( 500 ); animate.setFillAfter(true); view.startAnimation(animate); view.setVisibility(View.GONE); } // To animate view slide out from right to left public void s

How can i change background Color of Option Menu?

The below code is working fine 2.3.6(test on device and emulator). @Override     public boolean onCreateOptionsMenu(Menu menu) {         MenuInflater inflater = getMenuInflater();         inflater.inflate(R.menu.exit_menu, menu);         setMenuBackground();         return true;     } protected void setMenuBackground()     {         getLayoutInflater().setFactory(new Factory() {              @Override                         public View onCreateView(final String name, final Context context,                                 final AttributeSet attrs) {                             if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {                                 try { // Ask our inflater to create the view                                     final LayoutInflater f = getLayoutInflater();                                     final View[] view = new View[1];                                     try {