Pages

Powered By Blogger

Saturday, 23 March 2013

Android for DatePickerDialog, TimePickerDialog and CalendarView widget

Android for simple example of DatePickerDialog, TimePickerDialog and CalendarView widget
 
MainActivity.java

public class MainActivity extends Activity {
     private EditText mDateDisplay;
    private Button mPickDate;

    private EditText mDateDisplay2;
    private Button mPickDate2;

    private int mYear;
    private int mMonth;
    private int mDay;

    private EditText mTimeDisplay;
    private Button mPickTime;

    private int mHour;
    private int mMinute;

    static final int DATE_DIALOG_ID = 0;
    static final int CALENDAR_VIEW_ID = 1;
    static final int TIME_DIALOG_ID = 2;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;

    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // capture our View elements
        mDateDisplay = (EditText) findViewById(R.id.dateDisplay);
        mPickDate = (Button) findViewById(R.id.pickDate);
        mDateDisplay2 = (EditText) findViewById(R.id.dateDisplay2);
        mPickDate2 = (Button) findViewById(R.id.pickDate2);
        mTimeDisplay = (EditText) findViewById(R.id.timeDisplay);
        mPickTime = (Button) findViewById(R.id.pickTime);
 
        // add a click listener to the select a date button
        mPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        // add a click listener to the select a calendar date button
        mPickDate2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, MyCalendar.class);
                startActivityForResult(intent,CALENDAR_VIEW_ID);
            }
        });

        // add a click listener to the button
        mPickTime.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);
            }
        });
        // get the current date and time
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);

        // display the current date
        displayDate();
        displayCalendarViewDate();

        // display the current time
        displayTime();
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        switch(requestCode) {
        case CALENDAR_VIEW_ID:
            if (resultCode == RESULT_OK) {

                Bundle bundle = data.getExtras();

                mDateDisplay2 = (EditText) findViewById(R.id.dateDisplay2);
                mDateDisplay2.setText(bundle.getString("dateSelected"));
                break;
            }
        }

    }
     // updates the date in the EditText
    private void displayDate() {
        mDateDisplay.setText(
                new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("/")
                .append(mDay).append("/")
                .append(mYear).append(" "));
    }

    // updates the date in the EditText
    private void displayCalendarViewDate() {
        mDateDisplay2.setText(
                new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("/")
                .append(mDay).append("/")
                .append(mYear).append(" "));
    }

    // updates the time we display in the EditText
    private void displayTime() {
        mTimeDisplay.setText(
                new StringBuilder()
                .append(pad(mHour)).append(":")
                .append(pad(mMinute)));
    }

    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

    // the callback received when the user "sets" the date in the dialog
    private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year,
                int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            displayDate();
        }
    };

    // the callback received when the user "sets" the time in the dialog
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            mHour = hourOfDay;
            mMinute = minute;
            displayTime();
        }
    };

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                    mDateSetListener,
                    mYear, mMonth, mDay);
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this,
                    mTimeSetListener, mHour, mMinute, false);

        }
        return null;
    } 
}

MyCalendar.java

public class MyCalendar extends Activity {
     private int mYear;
    private int mMonth;
    private int mDay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CalendarView calendar = new CalendarView(this);
        calendar.setOnDateChangeListener(mDateSetListener);
        setContentView(calendar);

    }

    // the callback received when the user "sets" the date in the dialog
    private CalendarView.OnDateChangeListener mDateSetListener =
        new CalendarView.OnDateChangeListener() {
         public void onSelectedDayChange(CalendarView view, int year,
                int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            String selectedDate = new StringBuilder().append(mMonth + 1).append("/").append(mDay).append("/")
            .append(mYear).append(" ").toString();

            Bundle b = new Bundle();
            b.putString("dateSelected", selectedDate);

            //Add the set of extended data to the intent and start it
            Intent intent = new Intent();
            intent.putExtras(b);
            setResult(RESULT_OK,intent);     
            finish();
        }
    };

}

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:orientation="horizontal" android:gravity="center" android:id="@+id/layout1">
    <TableLayout android:layout_height="match_parent"
        android:layout_width="wrap_content" android:id="@+id/tableLayout1">
        <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:textSize="25sp"
                android:id="@+id/dateText" android:text="Android Dialog Date:" />
            <EditText android:text="EditText" android:layout_height="wrap_content"
                android:id="@+id/dateDisplay" android:layout_width="wrap_content"
                android:inputType="date" android:textStyle="bold" android:enabled="false" />
            <Button android:layout_width="wrap_content" android:id="@+id/pickDate"
                android:textSize="12sp" android:text="Change Date"
                android:layout_height="wrap_content" />
        </TableRow>
        <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:textSize="25sp"
                android:id="@+id/dateText" android:text="Calendar View Date:" />
            <EditText android:text="EditText" android:layout_height="wrap_content"
                android:id="@+id/dateDisplay2" android:layout_width="wrap_content"
                android:inputType="date" android:textStyle="bold" android:enabled="false" />
            <Button android:layout_width="wrap_content" android:id="@+id/pickDate2"
                android:textSize="12sp" android:text="Change Date"
                android:layout_height="wrap_content" />
        </TableRow>
        <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:textSize="25sp"
                android:id="@+id/dateText" android:text="Android Dialog Time:" />
            <EditText android:text="EditText" android:layout_height="wrap_content"
                android:id="@+id/timeDisplay" android:layout_width="wrap_content"
                android:inputType="date" android:textStyle="bold" android:enabled="false" />
            <Button android:layout_width="wrap_content" android:id="@+id/pickTime"
                android:textSize="12sp" android:text="Change Time"
                android:layout_height="wrap_content" />
        </TableRow>
    </TableLayout>
 
</LinearLayout>

mycalendar.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_width="wrap_content"
    android:layout_height="match_parent" android:id="@+id/frameLayout1"
    <CalendarView android:id="@+id/calendarView1"
        android:layout_width="wrap_content" android:layout_height="wrap_content" />
</FrameLayout>

  Output:


 
 

Friday, 22 March 2013

How to create Accordion in android

Accordion simple Example

MainActivity.java

public class MainActivity extends ListActivity implements OnClickListener
{
public OnLongClickListener longClickListner;
LinearLayout panel1,panel2,panel3,panel4,panel5;
TextView text1,text2,text3,text4,text5;
View openLayout;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
panel1 = (LinearLayout) findViewById(R.id.panel1);
panel2 = (LinearLayout) findViewById(R.id.panel2);
panel3 = (LinearLayout) findViewById(R.id.panel3);
panel4 = (LinearLayout) findViewById(R.id.panel4);
panel5 = (LinearLayout) findViewById(R.id.panel5);
//panel1.setVisibility(View.VISIBLE);
//panel1.setVisibility(View.VISIBLE);
//Log.v(“CZ”,”height at first …” + panel1.getMeasuredHeight());
text1 = (TextView) findViewById(R.id.text1);
text2 = (TextView) findViewById(R.id.text2);
text3 = (TextView) findViewById(R.id.text3);
text4 = (TextView) findViewById(R.id.text4);
text5 = (TextView) findViewById(R.id.text5);
text1.setOnClickListener(this);
text2.setOnClickListener(this);
text3.setOnClickListener(this);
text4.setOnClickListener(this);
text5.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
hideOthers(v);
}
private void hideThemAll()
{
if(openLayout == null) return;
if(openLayout == panel1)
panel1.startAnimation(new ScaleAnimToHide(1.0f, 1.0f, 1.0f, 0.0f, 500, panel1, true));
if(openLayout == panel2)
panel2.startAnimation(new ScaleAnimToHide(1.0f, 1.0f, 1.0f, 0.0f, 500, panel2, true));
if(openLayout == panel3)
panel3.startAnimation(new ScaleAnimToHide(1.0f, 1.0f, 1.0f, 0.0f, 500, panel3, true));
if(openLayout == panel4)
panel4.startAnimation(new ScaleAnimToHide(1.0f, 1.0f, 1.0f, 0.0f, 500, panel4, true));
if(openLayout == panel5)
panel5.startAnimation(new ScaleAnimToHide(1.0f, 1.0f, 1.0f, 0.0f, 500, panel5, true));
}
private void hideOthers(View layoutView)
{
{
int v;
if(layoutView.getId() == R.id.text1)
{
v = panel1.getVisibility();
if(v != View.VISIBLE)
{
panel1.setVisibility(View.VISIBLE);
Log.v(“CZ”,”height…” + panel1.getHeight());
}
//panel1.setVisibility(View.GONE);
//Log.v(“CZ”,”again height…” + panel1.getHeight());
hideThemAll();
if(v != View.VISIBLE)
{
panel1.startAnimation(new ScaleAnimToShow(1.0f, 1.0f, 1.0f, 0.0f, 500, panel1, true));
}
}
else if(layoutView.getId() == R.id.text2)
{
v = panel2.getVisibility();
hideThemAll();
if(v != View.VISIBLE)
{
panel2.startAnimation(new ScaleAnimToShow(1.0f, 1.0f, 1.0f, 0.0f, 500, panel2, true));
}
}
else if(layoutView.getId() == R.id.text3)
{
v = panel3.getVisibility();
hideThemAll();
if(v != View.VISIBLE)
{
panel3.startAnimation(new ScaleAnimToShow(1.0f, 1.0f, 1.0f, 0.0f, 500, panel3, true));
}
}
else if(layoutView.getId() == R.id.text4)
{
v = panel4.getVisibility();
hideThemAll();
if(v != View.VISIBLE)
{
panel4.startAnimation(new ScaleAnimToShow(1.0f, 1.0f, 1.0f, 0.0f, 500, panel4, true));
}
}
else if(layoutView.getId() == R.id.text5)
{
v = panel5.getVisibility();
hideThemAll();
if(v != View.VISIBLE)
{
panel5.startAnimation(new ScaleAnimToShow(1.0f, 1.0f, 1.0f, 0.0f, 500, panel5, true));
}
}
}
}
public class ScaleAnimToHide extends ScaleAnimation
{
       private View mView;
       private LayoutParams mLayoutParams;
       private int mMarginBottomFromY, mMarginBottomToY;
       private boolean mVanishAfter = false;
       public ScaleAnimToHide(float fromX, float toX, float fromY, float toY, int duration, View view,boolean vanishAfter)
       {
           super(fromX, toX, fromY, toY);
           setDuration(duration);
           openLayout = null;
           mView = view;
           mVanishAfter = vanishAfter;
           mLayoutParams = (LayoutParams) view.getLayoutParams();
           int height = mView.getHeight();
           mMarginBottomFromY = (int) (height * fromY) + mLayoutParams.bottomMargin – height;
           mMarginBottomToY = (int) (0 – ((height * toY) + mLayoutParams.bottomMargin)) – height;
       
           Log.v(“CZ”,”height…” + height + ” , mMarginBottomFromY….” + mMarginBottomFromY  + ” , mMarginBottomToY..” +mMarginBottomToY);
       }
       @Override
       protected void applyTransformation(float interpolatedTime, Transformation t)
       {
           super.applyTransformation(interpolatedTime, t);
           if (interpolatedTime < 1.0f)
           {
               int newMarginBottom = mMarginBottomFromY + (int) ((mMarginBottomToY – mMarginBottomFromY) * interpolatedTime);
               mLayoutParams.setMargins(mLayoutParams.leftMargin, mLayoutParams.topMargin,mLayoutParams.rightMargin, newMarginBottom);
               mView.getParent().requestLayout();
               //Log.v(“CZ”,”newMarginBottom…” + newMarginBottom + ” , mLayoutParams.topMargin…” + mLayoutParams.topMargin);
           }
           else if (mVanishAfter)
           {
               mView.setVisibility(View.GONE);
           }
       }
}
public class ScaleAnimToShow extends ScaleAnimation
{
       private View mView;
       private LayoutParams mLayoutParams;
       private int mMarginBottomFromY, mMarginBottomToY;
       private boolean mVanishAfter = false;
       public ScaleAnimToShow(float toX, float fromX, float toY, float fromY, int duration, View view,boolean vanishAfter)
       {
           super(fromX, toX, fromY, toY);
           openLayout = view;
           setDuration(duration);
           mView = view;
           mVanishAfter = vanishAfter;
           mLayoutParams = (LayoutParams) view.getLayoutParams();
           mView.setVisibility(View.VISIBLE);
           int height = mView.getHeight();
           //mMarginBottomFromY = (int) (height * fromY) + mLayoutParams.bottomMargin + height;
           //mMarginBottomToY = (int) (0 – ((height * toY) + mLayoutParams.bottomMargin)) + height;
       
           mMarginBottomFromY = 0;
           mMarginBottomToY = height;
       
           Log.v(“CZ”,”……………..height…” + height + ” , mMarginBottomFromY….” + mMarginBottomFromY  + ” , mMarginBottomToY..” +mMarginBottomToY);
       }
       @Override
       protected void applyTransformation(float interpolatedTime, Transformation t)
       {
           super.applyTransformation(interpolatedTime, t);
           if (interpolatedTime < 1.0f)
           {
               int newMarginBottom = (int) ((mMarginBottomToY – mMarginBottomFromY) * interpolatedTime) – mMarginBottomToY;
               mLayoutParams.setMargins(mLayoutParams.leftMargin, mLayoutParams.topMargin,mLayoutParams.rightMargin, newMarginBottom);
               mView.getParent().requestLayout();
               //Log.v(“CZ”,”newMarginBottom…” + newMarginBottom + ” , mLayoutParams.topMargin…” + mLayoutParams.topMargin);
           }
       }
}
}

main.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical” android:layout_width=”fill_parent” android:background=”#ffffff”
android:layout_height=”fill_parent”>
<LinearLayout android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:orientation=”vertical” android:layout_gravity=”center”>
<TextView android:text=”Location” android:id=”@+id/text1″ android:padding=”5dip” android:background=”#005555″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
<LinearLayout android:id=”@+id/panel1″ android:visibility=”gone”  android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”wrap_content”>
<TextView android:layout_margin=”2dip” android:text=”Item1″ android:padding=”5dip” android:background=”#777777″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
<TextView android:layout_margin=”2dip” android:text=”Item2″ android:padding=”5dip” android:background=”#777777″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
<TextView android:layout_margin=”2dip” android:text=”Item3″ android:padding=”5dip” android:background=”#777777″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
</LinearLayout>
    </LinearLayout>
    <LinearLayout android:layout_marginTop=”2dip” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:orientation=”vertical” android:layout_gravity=”center”>
<TextView android:text=”Symptons” android:id=”@+id/text2″ android:padding=”5dip” android:background=”#005555″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
<LinearLayout android:id=”@+id/panel2″ android:visibility=”gone” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”wrap_content”>
<TextView android:layout_margin=”2dip” android:text=”Item1″ android:padding=”5dip” android:background=”#777777″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
<TextView android:layout_margin=”2dip” android:text=”Item2″ android:padding=”5dip” android:background=”#777777″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
<TextView android:layout_margin=”2dip” android:text=”Item3″ android:padding=”5dip” android:background=”#777777″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
</LinearLayout>
    </LinearLayout>
    <LinearLayout android:layout_marginTop=”2dip” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:orientation=”vertical” android:layout_gravity=”center”>
<TextView android:text=”Location” android:id=”@+id/text3″ android:padding=”5dip” android:background=”#005555″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
<LinearLayout android:id=”@+id/panel3″ android:visibility=”gone” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”wrap_content”>
<TextView android:layout_margin=”2dip” android:text=”Item1″ android:padding=”5dip” android:background=”#777777″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
<TextView android:layout_margin=”2dip” android:text=”Item2″ android:padding=”5dip” android:background=”#777777″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
<TextView android:layout_margin=”2dip” android:text=”Item3″ android:padding=”5dip” android:background=”#777777″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
</LinearLayout>
    </LinearLayout>
    <LinearLayout android:layout_marginTop=”2dip” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:orientation=”vertical” android:layout_gravity=”center”>
<TextView android:text=”Character” android:id=”@+id/text4″ android:padding=”5dip” android:background=”#005555″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
<LinearLayout android:id=”@+id/panel4″ android:visibility=”gone” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”wrap_content”>
<TextView android:layout_margin=”2dip” android:text=”Item1″ android:padding=”5dip” android:background=”#777777″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
<TextView android:layout_margin=”2dip” android:text=”Item2″ android:padding=”5dip” android:background=”#777777″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
<TextView android:layout_margin=”2dip” android:text=”Item3″ android:padding=”5dip” android:background=”#777777″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
</LinearLayout>
    </LinearLayout>
    <LinearLayout android:layout_marginTop=”2dip” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:orientation=”vertical” android:layout_gravity=”center”>
<TextView android:text=”Factors” android:id=”@+id/text5″ android:padding=”5dip” android:background=”#005555″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
<LinearLayout android:id=”@+id/panel5″ android:visibility=”gone” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”wrap_content”>
<TextView android:layout_margin=”2dip” android:text=”Item1″ android:padding=”5dip” android:background=”#777777″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
<TextView android:layout_margin=”2dip” android:text=”Item2″ android:padding=”5dip” android:background=”#777777″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
<TextView android:layout_margin=”2dip” android:text=”Item3″ android:padding=”5dip” android:background=”#777777″  android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
</LinearLayout>
    </LinearLayout>
<ListView android:id=”@android:id/list” android:layout_width=”fill_parent” android:visibility=”gone”
android:layout_height=”340dip”  android:drawSelectorOnTop=”false”  android:dividerHeight=”0px”/>
</LinearLayout>

Output: