Thursday, May 19, 2011

Android - Full Screen App

By default an Android app has a title bar and allows the user to pull down the task bar while your app is open.


The following code allows you to remove the title bar and hide the task bar. This would be your onCreate method in your activity.

 public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        //No title   
        requestWindowFeature(Window.FEATURE_NO_TITLE);   
        //Full screen (no task bar)  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);   
        setContentView(R.layout.main);  
 }  

Result:

Monday, May 16, 2011

Android - ImageView scaleType Samples

I found it super frustrating when testing out the different scale types for an ImageView. The Graphical Layout in Eclipse currently does not consistently show an accurate representation of an image in an ImageView when changing the scaleType. The only way to know what the image was going to look like was to run the app and then decide if the current scaleType was what I was looking for.

Here is a table showing samples for all scale types for the ImageView.

scaleType fill_parent x fill_parent 200dp x 200dp 200dp x 200dp
with Overlay
matrix



fitXY



fitStart



fitCenter



fitEnd



center



centerCrop



centerInside




The column labeled "fill_parent x fill_parent" represents the following layout:
 <?xml version="1.0" encoding="utf-8"?>  
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent">  
   <ImageView android:layout_width="fill_parent"  
               android:layout_height="fill_parent"  
               android:src="@drawable/eureka"  
               android:scaleType="matrix">  
   </ImageView>  
 </FrameLayout>  

The column labeled "200dp x 200dp" represents the following layout:
 <?xml version="1.0" encoding="utf-8"?>  
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent">  
   <ImageView android:layout_width="200dp"  
               android:layout_height="200dp"  
               android:layout_gravity="center"  
               android:src="@drawable/eureka"  
               android:scaleType="matrix">  
   </ImageView>  
 </FrameLayout>  

Finally, the column labeled "200dp x 200dp with Overlay" represents the last layout with a 200dp by 200dp rectangle centered and overlaying the ImageView.

For more info on scaleType and each type's definition check out ImageView.ScaleType

Android – Styling the ProgressDialog Bar

This is the default gray and yellow progress bar:








I want my progress bar to look like this:










Steps to accomplish this:


1) Create a layer list xml file in your drawable folder to hold the shapes and gradients for each part of the progress bar.


my_progress_bar.xml
1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
3:  <item android:id="@android:id/background">  
4:    <shape>  
5:      <corners android:radius="5dip" />  
6:      <gradient  
7:          android:startColor="#69A0C6"  
8:          android:endColor="#446F8D"  
9:          android:angle="270"/>  
10:    </shape>  
11:  </item>  
12:  <item android:id="@android:id/secondaryProgress">  
13:    <clip>  
14:      <shape>  
15:        <corners android:radius="5dip" />  
16:        <gradient  
17:          android:startColor="#B963BA"  
18:          android:endColor="#A05AA1"  
19:          android:angle="270" />  
20:      </shape>  
21:    </clip>  
22:  </item>  
23:  <item android:id="@android:id/progress">  
24:    <clip>  
25:      <shape>  
26:        <corners  
27:          android:radius="5dip" />  
28:        <gradient  
29:          android:startColor="#911793"  
30:          android:endColor="#5B115C"  
31:          android:angle="270" />  
32:      </shape>  
33:    </clip>  
34:  </item>  
35:  </layer-list>  
In this example the blue color is the background and the purple is the progress.


2) Set the progress drawable in your Java code.

 dialog.setProgressDrawable(c.getResources().getDrawable(R.drawable.my_progress_bar));