Showing posts with label ics. Show all posts
Showing posts with label ics. Show all posts

Sunday, 27 January 2013

Ics Time Picker Example in Android

Download the following Libraries from the url :  https://github.com/SimonVT . You will find list of libraries and download it which is circled in the below image.


Ensure you have added number picker library to the time picker libray.

 And you should add this time picker libary to your project

activity_main.xml

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

    <Button
        android:id="@+id/time_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="43dp"
        android:layout_marginTop="34dp"
        android:text="Set Time" />

</RelativeLayout>

MainActivity.java


package com.rajeshvijayakumar.timepicker;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import net.simonvt.app.TimePickerDialog;
import net.simonvt.widget.TimePicker;

import java.util.Calendar;

public class MainActivity extends Activity implements OnClickListener {

    private Button mTimeButton;

    private Calendar mCalen;
    private int hourOfDay;
    private int minute;
    private int ampm;

    private static final int Time_PICKER_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTimeButton = (Button) findViewById(R.id.time_button);
        mCalen = Calendar.getInstance();
        hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
        minute = mCalen.get(Calendar.MINUTE);
        ampm = mCalen.get(Calendar.AM_PM);
        mTimeButton.setOnClickListener(this);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case Time_PICKER_ID:
                return new TimePickerDialog(this, TimePickerListener,
                        hourOfDay, minute, false);
        }
        return null;
    }

    private TimePickerDialog.OnTimeSetListener TimePickerListener =
            new TimePickerDialog.OnTimeSetListener() {

                // while dialog box is closed, below method is called.
                public void onTimeSet(TimePicker view, int hour, int minute) {

                    mCalen.set(Calendar.HOUR_OF_DAY, hour);
                    mCalen.set(Calendar.MINUTE, minute);

                    int hour12format = mCalen.get(Calendar.HOUR);
                    hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
                    minute = mCalen.get(Calendar.MINUTE);
                    ampm = mCalen.get(Calendar.AM_PM);
                    String ampmStr = (ampm == 0) ? "AM" : "PM";
                    // Set the Time String in Button
                    mTimeButton.setText(hour12format + " : " + minute + " / " + ampmStr);
                }
            };

    @Override
    public void onClick(View v) {
        showDialog(Time_PICKER_ID);
    }
}


styles.xml

    <style name="SampleTheme" parent="android:Theme">
        <item name="numberPickerUpButtonStyle">@style/NPWidget.Holo.ImageButton.NumberPickerUpButton</item>
        <item name="numberPickerDownButtonStyle">@style/NPWidget.Holo.ImageButton.NumberPickerDownButton</item>
        <item name="numberPickerInputTextStyle">@style/NPWidget.Holo.EditText.NumberPickerInputText</item>
        <item name="numberPickerStyle">@style/NPWidget.Holo.NumberPicker</item>
    </style>



In Manifest.xml give android:theme="@style/SampleTheme"

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/SampleTheme" >
        <activity
            android:name=".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>




Output :














Saturday, 26 January 2013

Ics Date Picker Example in Android

Download the following Libraries from the url :  https://github.com/SimonVT . You will find list of libraries and download it which is circled in the below image.

Ensure you have added calendarview and number picker library to the date picker libray.
 And your project should add this date picker library as follows :

activity_dialog.xml

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

    <Button
        android:id="@+id/date_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="43dp"
        android:layout_marginTop="34dp"
        android:text="Set Date" />

</RelativeLayout>

IcsDatePickerActivity.java


package com.rajeshvijayakumar.datepicker;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import net.simonvt.app.DatePickerDialog;
import net.simonvt.widget.DatePicker;

import java.util.Calendar;


public class MainActivity extends Activity implements OnClickListener {

    private Button mDateButton;

    private Calendar mCalen;
    private int day;
    private int month;
    private int year;

    private static final int DATE_PICKER_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDateButton = (Button) findViewById(R.id.date_button);
        mCalen = Calendar.getInstance();
        day = mCalen.get(Calendar.DAY_OF_MONTH);
        month = mCalen.get(Calendar.MONTH);
        year = mCalen.get(Calendar.YEAR);
        mDateButton.setOnClickListener(this);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case DATE_PICKER_ID:
                return new DatePickerDialog(this, datePickerListener,
                        year, month, day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener datePickerListener =
            new DatePickerDialog.OnDateSetListener() {

                // while dialog box is closed, below method is called.
                public void onDateSet(DatePicker view, int selectedYear,
                        int selectedMonth, int selectedDay) {
                    year = selectedYear;
                    month = selectedMonth;
                    day = selectedDay;

                    // Set the Date String in Button
                    mDateButton.setText(day + " / " + (month + 1) + " / " + year);
                }
            };

    @Override
    public void onClick(View v) {

        showDialog(DATE_PICKER_ID);

    }
}

styles.xml


    <style name="SampleTheme" parent="@android:style/Theme">
        <item name="calendarViewStyle">@style/Widget.Holo.CalendarView</item>
        <item name="datePickerStyle">@style/Widget.Holo.DatePicker</item>
        <item name="numberPickerUpButtonStyle">@style/NPWidget.Holo.ImageButton.NumberPickerUpButton</item>
        <item name="numberPickerDownButtonStyle">@style/NPWidget.Holo.ImageButton.NumberPickerDownButton</item>
        <item name="numberPickerInputTextStyle">@style/NPWidget.Holo.EditText.NumberPickerInputText</item>
        <item name="numberPickerStyle">@style/NPWidget.Holo.NumberPicker</item>
    </style>

In Manifest.xml  give android:theme="@style/SampleTheme"

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/SampleTheme" >
        <activity
            android:name="com.rajeshvijayakumar.datepicker.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>

 Output :








Source Code : Download this example Here



Sunday, 20 January 2013

Action Bar Sherlock : Creating Action Menu Items in ICS Action Bar in Android

Add ActionBarSherlock Library Project to your Application Project, Here is a Link for this

MainActivity.java

package com.rajeshvijayakumar.actionmenuitem;

import android.os.Bundle;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Menu;

public class MainActivity extends SherlockActivity {

    private MenuItem mGoItem;
    private MenuItem mClearItem;
   
    private static final int GO_ITEM_ID = 1;
    private static final int CLEAR_ITEM_ID = 2;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        mGoItem = menu.add(0, GO_ITEM_ID, 0, null);
        mGoItem.setIcon(R.drawable.abs__ic_go).setShowAsAction(
                MenuItem.SHOW_AS_ACTION_ALWAYS);
        mClearItem = menu.add(0, CLEAR_ITEM_ID, 0, null);
        mClearItem.setIcon(R.drawable.abs__ic_clear).setShowAsAction(
                MenuItem.SHOW_AS_ACTION_ALWAYS);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()) {
        case GO_ITEM_ID :
            Toast.makeText(MainActivity.this, "You have Pressed 'Go' Menu Item", Toast.LENGTH_LONG).show();
            return true;
        case CLEAR_ITEM_ID :
            Toast.makeText(MainActivity.this, "You have Pressed 'Clear' Menu Item", Toast.LENGTH_LONG).show();
            return true;
        }
       
        return false;
    }   
}

Output :



Source Code : Download this Example with Library Here

ActionBar Sherlock : Drop down Ics Spinner and applying Style in android


 Add ActionBarSherlock Library Project to your Application Project, Here is a Link for this


Project Structure :


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="fill_parent" >

<com.actionbarsherlock.internal.widget.IcsSpinner
        android:id="@+id/ics_spinner"
        style="@style/spinner_style"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" />

</RelativeLayout>

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

        <TextView
            android:id="@+id/name_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imageView1"
            android:layout_marginLeft="17dp"
            android:layout_toRightOf="@+id/imageView1"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="14dp"
            android:src="@drawable/abs__ic_clear_normal" />

    </RelativeLayout>

styles.xml

<resources>
    <style name="spinner_style">
        <item name="android:dropDownWidth">fill_parent</item>
        <item name="android:showDividers">beginning|middle|end</item>
        <item name="android:alignmentMode">alignBounds</item>
        <item name="android:dividerHeight">2dp</item>
        <item name="android:scrollbars">none</item>
        <item name="android:scrollbarAlwaysDrawVerticalTrack">false</item>
        <item name="android:scrollbarTrackVertical">@android:color/transparent</item>
        <item name="android:dropDownSelector">@android:color/white</item>
        <item name="android:requiresFadingEdge">none</item>
    </style>
  </resources>

MainActivity.java

package com.rajeshvijayakumar.icsspinner;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.internal.widget.IcsAdapterView;
import com.actionbarsherlock.internal.widget.IcsAdapterView.OnItemSelectedListener;
import com.actionbarsherlock.internal.widget.IcsSpinner;

public class MainActivity extends SherlockActivity implements OnItemSelectedListener {

    private IcsSpinner mIcsSpinner;
    private IcsAdapter mAdapter;
    private String[] mNameList = { "Akash", "Akshay", "Mahesh", "Mrithula",
            "Rajesh", "Sonika" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mIcsSpinner = (IcsSpinner) findViewById(R.id.ics_spinner);
        mAdapter = new IcsAdapter(MainActivity.this, R.layout.drop_down_view, mNameList);
        mIcsSpinner.setAdapter(mAdapter);
        mIcsSpinner.setOnItemSelectedListener(this);
    }

    public class IcsAdapter extends ArrayAdapter<String> {

        String[] nameList = new String[mNameList.length];
       
        public IcsAdapter(Context context, int textViewResourceId,
                String[] objects) {
            super(context, textViewResourceId, objects);
            this.nameList = objects;
        }

        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        @Override
        public void setNotifyOnChange(boolean notifyOnChange) {
            super.setNotifyOnChange(notifyOnChange);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView,
                ViewGroup parent) {

            LayoutInflater inflater = getLayoutInflater();
            View row = inflater.inflate(R.layout.drop_down_view, parent, false);
            TextView spinnerText = (TextView) row.findViewById(R.id.name_view);
            if (position < mNameList.length) {
                String cateName = mNameList[position];
                spinnerText.setText(cateName);
            }
            return row;
        }
    }

    @Override
    public void onItemSelected(IcsAdapterView<?> spinAdapView, View view,
            int position, long id) {
        Toast.makeText(MainActivity.this, mNameList[position], Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected(IcsAdapterView<?> parent) {
    }
}

In AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock" >
        <activity
            android:name=".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>

Output :






Source Code : Download this Example with Library Here

Wednesday, 9 January 2013

ICS / Jelly bean pager adapter with page title strip back ported to Android 2.0


If your eclipse's ADT plugin  is not updated to ICS / jellybean, then this example will be very useful to you :

Project Structure :


main.xml

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

    <!--
    This title strip will display the currently visible page title, as well as the page
    titles for adjacent pages.
    -->

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />

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


MainActivity.java

 package com.rajeshvijayakumar.pageradapterdemo;

public class MainActivity extends FragmentActivity {

    SectionsPagerAdapter mSectionsPagerAdapter;

    ViewPager mViewPager;

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

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }


    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
           
            // getItem is called to instantiate the fragment for the given page.
            // Return a DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase();
            case 1:
                return getString(R.string.title_section2).toUpperCase();
            case 2:
                return getString(R.string.title_section3).toUpperCase();
            }
            return null;
        }
    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Create a new TextView and set its text to the fragment's section
            // number argument value.
            TextView textView = new TextView(getActivity());
            textView.setGravity(Gravity.CENTER);
            textView.setText(Integer.toString(getArguments().getInt(
                    ARG_SECTION_NUMBER)));
            return textView;
        }
    }

}

In strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">PagerAdapterDemo</string>
    <string name="title_section3">Section 3</string>
    <string name="title_section2">Section 2</string>
    <string name="title_section1">Section 1</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>

</resources>

 In Manifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.rajeshvijayakumar.pageradapterdemo.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>

In styles.xml

<resources>

    <style name="AppBaseTheme" parent="android:Theme.Light">
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
    </style>

</resources>
Output :