Splash Screen Android Example Code

In this post you can learn on how to set up a splash screen in android before entering the home screen with a few seconds timeout which gives a professional rich UI design user experience.

HomeScreen Activity Java File

public class HomeScreen extends Activity {
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homescreen); 
         
    } 
     
    @Override
    protected void onDestroy() {
         
        super.onDestroy();
         
    }
}


Splashscreen activity Java file

public class SplashScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);
        
         
//Sleep for 5 seconds
        Thread background = new Thread() {
            public void run() {
                 
                try {
                    sleep(5000);
                     
                    Intent i=new Intent(getBaseContext(),HomeScreen.class);
                    startActivity(i);
                     
                    //remove the activity
                    finish();
                     
                } catch (Exception e) {
                 
                }
            }
        };
         
        // starting the thread
        background.start();
             
    }
     
    @Override
    protected void onDestroy() {
       
        super.onDestroy();
         
    }
}

Android ToggleButton Source Code Example

In this post you can learn on how to work with ToggleButton in android. Toggle button means giving two different functionalities in a single button and examples like turn loudspeaker on / off. The next example is mute/unmute the current call, etc... These are some of the examples of ToggleButton in android. And also you can change the image of the ToggleButton too in android.

Layout.xml File

<ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButtonExample" >
android:textOn="On"
android:textOff="Off"
</ToggleButton>

On activity java file under onCreate make use of the below function to work with ToggleButton.

tb = (ToggleButton) findViewById(R.id.toggleButton1);
        tb.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 if (tb.isChecked()) {

                        //corresponding code units
                    } else {

                        //corresponding code units
                    }
            }
        });

ImageButton Change Image OnClick XML

In this post you can learn on how to change the image of the ImageButton OnClick in android. This is very simple and you can use a simple XML file to make this functionality happen.

changeimage.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused"
          android:state_focused="true" />
    <item android:drawable="@drawable/button_normal" />

</selector>

changeimage is an xml file that we must create inside drawable folder where "button_pressed", "button_focused" and "button_normal" are images under drawable folder respectively.

layout.xml

Make a reference of the above XML file in the button declaration to make it effect.

<Button
        android:id="@+id/imagebutton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:background="@drawable/changeimage" />

Swipe Tabs Android Source Code Example

Hi All,

In this post I am going to explain all about swipe tabs in android with an awesome example. You all might have used facebook in your android phones right. In that you might have noticed a swipe tabs view layout for post page, notifications, etc... Likewise you can design your swipeable tabs in android which brings high user experience and ease to use and navigation and high technological one. Just have a glance on the source code examples below. Here in this example you can see a screen with two swipeable tabs songs and videos. Very easy to build swipe tabs in android and enjoy with the example below.

1. Android Manifest XML file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.swipetabs"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.android.swipetabs.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>

2. Android main activity layout file xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


</android.support.v4.view.ViewPager>


3. Songs activity layout file xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="songs"
        android:textSize="20dp"
        android:layout_centerInParent="true"/>
    


</RelativeLayout>


4. Videos activity layout file xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="videos"
        android:textSize="20dp"
        android:layout_centerInParent="true"/>
    

</RelativeLayout>



5. MainActivity Java file

package com.android.swipetabs;

import com.android.swipetabs.adapter;

import com.android.swipetabs.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {

private ViewPager pager;
private adapter adapter;
private ActionBar action;
private String[] tabs = { "songs", "videos" };

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

pager = (ViewPager) findViewById(R.id.pager);
action = getActionBar();
adapter = new adapter(getSupportFragmentManager());

pager.setAdapter(adapter);
action.setHomeButtonEnabled(false);
action.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

for (String tab_name : tabs) {
action.addTab(action.newTab().setText(tab_name)
.setTabListener(this));
}
//on swipe tabs functionality
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

@Override
public void onPageSelected(int position) {

action.setSelectedNavigationItem(position);
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}

@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
pager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

}



6. Adapter java file

package com.android.swipetabs;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class adapter extends FragmentPagerAdapter {

public adapter(FragmentManager fgmt) {
super(fgmt);
// TODO Auto-generated constructor stub
}


@Override
public Fragment getItem(int index) {

switch (index) {
case 0:
// Games fragment activity
return new songs();
case 1:
// Movies fragment activity
return new videos();
}

return null;
}

@Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}

}




7. songs java file

package com.android.swipetabs;

import com.android.swipetabs.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class songs extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.songs, container, false);
return rootView;
}
}


8. Videos java file

package com.android.swipetabs;

import com.android.swipetabs.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class videos extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.videos, container, false);
return rootView;
}

}

FlashLight Android Source Code Example

Guys, welcome you all here. Here you can find the source code example of flashlight in android. Code units are below and its very simple to design it. Just learn it and try to implement your own awesome flashlight application and launch it in the google play with adding innovations in it. By developing an awesome fantabulous flashlight of paid version you can become a millionaire soon with the high amount of downloads that you get.

1. Android Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myflashever.android"
    android:versionCode="1"
    android:versionName="2.0" >

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name="com.myflashever.android.flashlight"
            android:screenOrientation="portrait" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2. Layout file - main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ToggleButton
        android:id="@+id/flashlight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textOn="Turn On"
        android:textOff="Turn Off"
        android:background="@drawable/change" />


</RelativeLayout>


2.1 @drawable/change
change.xml - for changing the ToggleButton image on click.


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/ooo"
          android:state_checked="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/lll"
        android:state_checked="false"/>
 </selector>


3. flashlight.java file

package com.myflashever.android;

import com.myflashever.android.R;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;

public class flashlight extends Activity {

private boolean isLightOn = false;

private Camera cam;

private ToggleButton button;

@Override
protected void onStop() {
super.onStop();

if (cam != null) {
cam.release();
}
}

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

button = (ToggleButton) findViewById(R.id.flashlight);

Context context = this;
PackageManager pm = context.getPackageManager();

if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return;
}

cam = Camera.open();
final Parameters p = cam.getParameters();

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

if (isLightOn) {

p.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(p);
cam.stopPreview();
isLightOn = false;

} else {

p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
isLightOn = true;

}

}
});

}

}



Incredible FlashLight

Please find below a sample innovative free flashlight app in the market.
https://play.google.com/store/apps/details?id=com.myflashever.android&hl=en

My android apps in the Market

Incredible Bible

After learning android programming and writing blogs on the same, I got some good experience in the development and now I developed my first android application very simple and great and have launched it in the google play market and I released it as a paid version -Incredible Bible -(https://play.google.com/store/apps/details?id=com.udhaya.android).

Guys, It is great to learn android programming and its very great to make money with tech, non tech, games, books, etc... and al stuff from the android market. Praise the Lord.

Incredible Bible - My First Android App in the Market - https://play.google.com/store/apps/details?id=com.udhaya.android. Kindly download and make prayers to the Lord instantly and accordingly. God bless you.
My Profile