Android: Swipeable Tabs Using ViewPager and Fragments

How many more types juicy tabs can we have? As many as we want! It’s time to take our tabs to the next level and make the views swipeable. Fingers at the ready!

Continue reading Android: Swipeable Tabs Using ViewPager and Fragments

Advertisements

Android: Styling a Tab Layout with FragmentTabHost and Fragments

Well since TabActivity is now depreciated, I figured it was time to update my FragmentTabHost with some style. I will be using some of the same framework from my previous entry about FragmentTabHost. Let’s get down to it!

Continue reading Android: Styling a Tab Layout with FragmentTabHost and Fragments

Android: Creating a Tab Layout with FragmentTabHost and Fragments

I wrote a post about how to create and style the TabHost component in Android. Not surprising they have recently depreciated this widget in favor FragmentTabHost so I figured I’d run through the same examples as I did with TabHost just to document it. Most of this is taken from the SDK examples.

 

Continue reading Android: Creating a Tab Layout with FragmentTabHost and Fragments

Android: Creating a Tab Layout With TabHost and TabWidget

Android Tabhost Example

Straight out of the box! This example uses the built in tabhost and tabwidget in a project. In this example we will be placing the all three tab’s content in one layout and not in separate layouts. To customize the tabs: Android: Styling the Tabs in a TabWidget.

Continue reading Android: Creating a Tab Layout With TabHost and TabWidget

Android: A Basic Alert Dialog Created Programmatically

In order for this to work, an AlertDialog must extend some kind of Activity.

final Context context = this;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

// set title
alertDialogBuilder.setTitle("Your Title");

// set dialog message
alertDialogBuilder
	.setMessage("Click yes to exit!")
	.setCancelable(false)
	.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog,int id) {
			// if this button is clicked, close
			// current activity
			//MainActivity.this.finish();
		}
	})
	.setNegativeButton("No",new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog,int id) {
			// if this button is clicked, just close
			// the dialog box and do nothing
			dialog.cancel();
		}
});

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();

Android: Animation Drawable Using Animation List

Adding an animation to your project is easy in android.
Note: animating large images can crash your app and cause it to force close.

MainActivity.java

public class MainActivity extends Activity{

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

    @Override
    public void onWindowFocusChanged(boolean hasFocus){
        if(hasFocus){
            ImageView imageView = (ImageView) findViewById(R.id.imageConfirmOrderGift);
            AnimationDrawable animation = (AnimationDrawable) imageView.getDrawable();
            animation.start();
        }
    }
}

res/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@anim/animate_image" />

</RelativeLayout>

res/anim/animate_image.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/selected" 
    android:oneshot="false" 
    android:visible="true">

    <item android:drawable="@drawable/image1" android:duration="50"/>
    <item android:drawable="@drawable/image2" android:duration="50"/>
    <item android:drawable="@drawable/image3" android:duration="50"/>
    <item android:drawable="@drawable/image4" android:duration="50"/>
    <item android:drawable="@drawable/image5" android:duration="50"/>

</animation-list>

Android: Repeating Background Image

Android - repeating background image

To be used as a background drawable.

Create a jpg or a png with a pattern and save it in your drawable-hdpi/ldpi/mdpi/xdpi folders with the name bg_image.jpg (or png).

Then create a new file in the drawable folder named “bg_pattern.xml”. Paste the code below and then apply it to your the background of your layout/object (linearLayout, relativeLayout etc).

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/back_image" 
    android:tileMode="repeat" />

To make it the applications background (not just an activity):
res/styles.xml

<style name="AppTheme" parent="android:Theme.Light" >
    <item name="android:windowBackground">@drawable/app_bg_pattern</item>
</style>