Friday, 7 June 2013

Toggle Button Android


Toggle Button - UI Tutorials Series 

Hi Guys I decide to make all simple and complex UI control/view Tutorials for newbies :)
So here is our first example.
Toggle Button in Android
This is just the simple example for implementing Toggle Button in android application
Create Simple Layout as
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="55dp"
        android:textOn="On"
        android:textOff="Off" />


</RelativeLayout>

and Here is Our Favorite MainActivity.java :)

public class MainActivity extends Activity {
       private ToggleButton onOffTBtn;

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

              onOffTBtn = (ToggleButton) findViewById(R.id.toggleButton1);

              onOffTBtn.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View v) {
                           // TODO Auto-generated method stub
                           if (onOffTBtn.isChecked()) {
                                  Toast.makeText(MainActivity.this, "On", 1).show();
                           } else {
                                  Toast.makeText(MainActivity.this, "Off", 1).show();
                           }

                     }
              });
       }

}


you can download example from here 
Happy Coddddddding :)

Thursday, 6 June 2013

Android Alarm Manager to Start Service every hour

 Start Service every hour


     Hi Guys  Today we will create a service which will start after every hour ,
So we can use such service to fetch  or updated data from web to our app.

Here is simple example.


Service Started

Service Running Background



Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name and other things.

Create simple Layout 
activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/stopBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="155dp"
        android:text="Stop" />

    <Button
        android:id="@+id/startBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/stopBtn"
        android:layout_alignLeft="@+id/stopBtn"
        android:layout_marginBottom="41dp"
        android:text="start" />

</RelativeLayout>


In MainActivity.java  copy Paste  following  code 


public class MainActivity extends Activity {

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

              // Start service using AlarmManager

              Calendar cal = Calendar.getInstance();
              cal.add(Calendar.SECOND, 10);
             
              Intent intent = new Intent(this, TestService.class);
      
              PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
             
              AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
              //for 30 mint 60*60*1000
              alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                           60*60*1000, pintent);

              Button startBtn = (Button) findViewById(R.id.startBtn);
              startBtn.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View v) {
                           // TODO Auto-generated method stub
                           startService(new Intent(getBaseContext(), TestService.class));
                     }
              });

              Button stopBtn = (Button) findViewById(R.id.stopBtn);
              stopBtn.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View v) {
                           // TODO Auto-generated method stub
                           stopService(new Intent(getBaseContext(), TestService.class));
                     }
              });

       }


}


and here is your Service code 

TestService.java




public class TestService extends Service {

       @Override
       public IBinder onBind(Intent intent) {
              // TODO: Return the communication channel to the service.
              throw new UnsupportedOperationException("Not yet implemented");
       }

       @Override
       public void onCreate() {
              // TODO Auto-generated method stub

              Toast.makeText(getApplicationContext(), "Service Created", 1).show();
              super.onCreate();
       }

       @Override
       public void onDestroy() {
              // TODO Auto-generated method stub
              Toast.makeText(getApplicationContext(), "Service Destroy", 1).show();
              super.onDestroy();
       }

       @Override
       public int onStartCommand(Intent intent, int flags, int startId) {
              // TODO Auto-generated method stub
              Toast.makeText(getApplicationContext(), "Service Running ", 1).show();
              return super.onStartCommand(intent, flags, startId);
       }


}


one more thing to be done is that you will have to declare the service in the manifest to make it functional.


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

    <uses-sdk
        android:minSdkVersion="8"
        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.example.alarmservice.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>

        <service            android:name="com.example.alarmservice.TestService"            android:enabled="true"            android:exported="true" >        </service>

    </application>

</manifest>

you can download complete example here, 


Happy Codding :)





Include tag in android



      Hi guys Today we will see include tag of android xml

This is one of the best thing that we can reuse existing xml file in other xml.
so here is good example,

home_page.xml

just create xml as bleow Assume that this top_layout.xml  is a  top bar of each page of app. 

top_layout.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="wrap_content"
    android:background="@drawable/genericbar_background_tile" >

    <Button
        android:id="@+id/BackBtn"
        style="@style/K_style"
        android:layout_alignParentLeft="true"
        android:text="@string/backBtn" />

    <Button
        android:id="@+id/HomeBtn"
        style="@style/K_style"
        android:layout_alignParentRight="true"
        android:text="@string/HomeStr" />

    <TextView
        android:id="@+id/titleTv"
        style="@style/K_style"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/titleStr"
        android:textAppearance="?android:attr/textAppearanceMedium" />


</RelativeLayout>





To include this layout in our hompage or any page just add the
    <include layout="@layout/ layoutname"/>



home_page.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" >

    <include layout="@layout/top_lyaout"/>
  
    <LinearLayout
        android:id="@+id/TopLNLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/MidleLNLaoyout"
        android:layout_centerHorizontal="true" >

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

        <Button
            android:id="@+id/TopRightBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/MidleLNLaoyout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >

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

        <Button
            android:id="@+id/MidRightBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/BottonLNLaout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/MidleLNLaoyout"
        android:layout_centerHorizontal="true" >

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

        <Button
            android:id="@+id/BottomRightBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>


</RelativeLayout>

So final screen will be look like this as top_Layout.xml included in home-page xml. 
this can be reuse in any number of screens.

                               


I hope It might be help full,
Happy Codding :)