Custom WeekView Calender in Android.

Today I want to share some important code. Today I want to share WeekViewCalender example in android.

Follow the Important steps:
1. create the Application weekcalenderView
2. select minimum sdk 15.
3. select Blank Activity and create MainActivity.java
4. finish

MainActivity.java

package program.androidheight.weekcalenderview;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity  implements OnClickListener{
    public String dateFormat, logInID;
    public String[] weekDays;
    public String[] NextPreWeekday;
    public String dateFormate;
    public String firstDayOfWeek;
    public String lastDayOfWeek;
    private TextView textViewServiceApp;
    private TextView textViewWeekCalender;
    private TextView textViewPrevDate;
    private TextView textViewDate;
    private TextView textViewNextDate;
    private TextView textViewSun;
    private TextView textViewMon;
    private TextView textViewTue;
    private TextView textViewWed;
    private TextView textViewThu;
    private TextView textViewFri;
    private TextView textViewSat;
    private ImageView nextMonth,previousMonth;

    public int weekDaysCount = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //textViewPrevDate = (TextView) findViewById(R.id.textViewPrevDate);
        textViewDate = (TextView) findViewById(R.id.currentMonth);
        //textViewNextDate = (TextView) findViewById(R.id.textViewNextDate);
        textViewSun = (TextView) findViewById(R.id.textViewSun);
        textViewMon = (TextView) findViewById(R.id.textViewMon);
        textViewTue = (TextView) findViewById(R.id.textViewTue);
        textViewWed = (TextView) findViewById(R.id.textViewWed);
        textViewThu = (TextView) findViewById(R.id.textViewThu);
        textViewFri = (TextView) findViewById(R.id.textViewFri);
        textViewSat = (TextView) findViewById(R.id.textViewSat);
        nextMonth = (ImageView) findViewById(R.id.nextMonth);
        previousMonth = (ImageView)findViewById(R.id.prevMonth);
        nextMonth.setOnClickListener(this);
        previousMonth.setOnClickListener(this);

        textViewSun.setOnClickListener(this);
        textViewMon.setOnClickListener(this);
        textViewTue.setOnClickListener(this);
        textViewWed.setOnClickListener(this);
        textViewThu.setOnClickListener(this);
        textViewFri.setOnClickListener(this);
        textViewSat.setOnClickListener(this);
        NextPreWeekday = getWeekDay();
        firstDayOfWeek = CommonMethod.convertWeekDays(NextPreWeekday[0]);
        lastDayOfWeek = CommonMethod.convertWeekDays(NextPreWeekday[6]);
        textViewDate.setText( CommonMethod.convertWeekDaysMonth(NextPreWeekday[6]));
        textViewSun.setText(CommonMethod.convertWeekDays(NextPreWeekday[0])
                + "\nSun");
        textViewMon.setText(CommonMethod.convertWeekDays(NextPreWeekday[1])
                + "\nMon");
        textViewTue.setText(CommonMethod.convertWeekDays(NextPreWeekday[2])
                + "\nTue");
        textViewWed.setText(CommonMethod.convertWeekDays(NextPreWeekday[3])
                + "\nWeb");
        textViewThu.setText(CommonMethod.convertWeekDays(NextPreWeekday[4])
                + "\nThu");
        textViewFri.setText(CommonMethod.convertWeekDays(NextPreWeekday[5])
                + "\nFri");
        textViewSat.setText(CommonMethod.convertWeekDays(NextPreWeekday[6])
                + "\nSat");
    }
    public String[] getWeekDay() {

        Calendar now = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String[] days = new String[7];
        int delta = -now.get(GregorianCalendar.DAY_OF_WEEK) + 1;
        now.add(Calendar.DAY_OF_MONTH, delta);
        for (int i = 0; i < 7; i++) {
            days[i] = format.format(now.getTime());
            now.add(Calendar.DAY_OF_MONTH, 1);
        }

        return days;

    }
    @SuppressLint("SimpleDateFormat")
    public String[] getWeekDayNext() {

        weekDaysCount++;
        Calendar now1 = Calendar.getInstance();
        Calendar now = (Calendar) now1.clone();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String[] days = new String[7];
        int delta = -now.get(GregorianCalendar.DAY_OF_WEEK) + 1;
        now.add(Calendar.WEEK_OF_YEAR, weekDaysCount);
        now.add(Calendar.DAY_OF_MONTH, delta);
        for (int i = 0; i < 7; i++) {
            days[i] = format.format(now.getTime());
            now.add(Calendar.DAY_OF_MONTH, 1);
        }

        return days;

    }

    @SuppressLint("SimpleDateFormat")
    public String[] getWeekDayPrev() {

        weekDaysCount--;
        Calendar now1 = Calendar.getInstance();
        Calendar now = (Calendar) now1.clone();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String[] days = new String[7];
        int delta = -now.get(GregorianCalendar.DAY_OF_WEEK) + 1;
        now.add(Calendar.WEEK_OF_YEAR, weekDaysCount);
        now.add(Calendar.DAY_OF_MONTH, delta);
        for (int i = 0; i < 7; i++) {
            days[i] = format.format(now.getTime());
            now.add(Calendar.DAY_OF_MONTH, 1);
        }

        return days;

    }
    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.nextMonth:
                NextPreWeekday = getWeekDayNext();
                firstDayOfWeek = CommonMethod.convertWeekDays(NextPreWeekday[0]);
                lastDayOfWeek = CommonMethod.convertWeekDays(NextPreWeekday[6]);
                textViewDate.setText(/*firstDayOfWeek + "-" + lastDayOfWeek + " "
+*/ CommonMethod.convertWeekDaysMonth(NextPreWeekday[6]));
                textViewSun.setText(CommonMethod.convertWeekDays(NextPreWeekday[0])
                        + "\nSun");
                textViewMon.setText(CommonMethod.convertWeekDays(NextPreWeekday[1])
                        + "\nMon");
                textViewTue.setText(CommonMethod.convertWeekDays(NextPreWeekday[2])
                        + "\nTue");
                textViewWed.setText(CommonMethod.convertWeekDays(NextPreWeekday[3])
                        + "\nWeb");
                textViewThu.setText(CommonMethod.convertWeekDays(NextPreWeekday[4])
                        + "\nThu");
                textViewFri.setText(CommonMethod.convertWeekDays(NextPreWeekday[5])
                        + "\nFri");
                textViewSat.setText(CommonMethod.convertWeekDays(NextPreWeekday[6])
                        + "\nSat");
                break;
            case R.id.prevMonth:
                NextPreWeekday = getWeekDayPrev();
                firstDayOfWeek = CommonMethod.convertWeekDays(NextPreWeekday[0]);
                lastDayOfWeek = CommonMethod.convertWeekDays(NextPreWeekday[6]);
                textViewDate.setText(/*firstDayOfWeek + "-" + lastDayOfWeek + " "
+*/ CommonMethod.convertWeekDaysMonth(NextPreWeekday[6]));
                textViewSun.setText(CommonMethod.convertWeekDays(NextPreWeekday[0])
                        + "\nSun");
                textViewMon.setText(CommonMethod.convertWeekDays(NextPreWeekday[1])
                        + "\nMon");
                textViewTue.setText(CommonMethod.convertWeekDays(NextPreWeekday[2])
                        + "\nTue");
                textViewWed.setText(CommonMethod.convertWeekDays(NextPreWeekday[3])
                        + "\nWeb");
                textViewThu.setText(CommonMethod.convertWeekDays(NextPreWeekday[4])
                        + "\nThu");
                textViewFri.setText(CommonMethod.convertWeekDays(NextPreWeekday[5])
                        + "\nFri");
                textViewSat.setText(CommonMethod.convertWeekDays(NextPreWeekday[6])
                        + "\nSat");


            case R.id.textViewSun:
                Toast.makeText(MainActivity.this,CommonMethod.convertFormattedDate(NextPreWeekday[0]) , Toast.LENGTH_LONG).show();

                break;
            case R.id.textViewMon:
                Toast.makeText(MainActivity.this,CommonMethod.convertFormattedDate(NextPreWeekday[1]) , Toast.LENGTH_LONG).show();

                break;
            case R.id.textViewTue:
                Toast.makeText(MainActivity.this,CommonMethod.convertFormattedDate(NextPreWeekday[2]) , Toast.LENGTH_LONG).show();

                break;
            case R.id.textViewWed:
                Toast.makeText(MainActivity.this,CommonMethod.convertFormattedDate(NextPreWeekday[3]) , Toast.LENGTH_LONG).show();

                break;
            case R.id.textViewThu:
                Toast.makeText(MainActivity.this,CommonMethod.convertFormattedDate(NextPreWeekday[4]) , Toast.LENGTH_LONG).show();

                break;
            case R.id.textViewFri:
                Toast.makeText(MainActivity.this,CommonMethod.convertFormattedDate(NextPreWeekday[5]) , Toast.LENGTH_LONG).show();

                break;
            case R.id.textViewSat:
                Toast.makeText(MainActivity.this,CommonMethod.convertFormattedDate(NextPreWeekday[6]) , Toast.LENGTH_LONG).show();

                break;
            default:
                break;
        }

    }

}



2. CommonMethod.java file contain all important method like 
public static String convertWeekDays(String date) is to convert date to week date.


CommonMethod .java

package program.androidheight.weekcalenderview;

/**
 * Created by PRABHU on 11-08-2015.
 */
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class CommonMethod {

    public static String convertWeekDays(String date) {
        String formattedDate = null;
        try {
            SimpleDateFormat originalFormat = new SimpleDateFormat(
                    "yyyy-MM-dd", Locale.ENGLISH);
            SimpleDateFormat targetFormat = new SimpleDateFormat("dd");
            Date date12 = originalFormat.parse(date);
            formattedDate = targetFormat.format(date12);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return formattedDate;

    }



    public static String convertWeekDaysMonth(String date)
    {
        String formattedDate = null;
        try
        {
            SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd" , Locale.ENGLISH);
            SimpleDateFormat targetFormat = new SimpleDateFormat("MM yyyy");
            Date date12 = originalFormat.parse(date);
            formattedDate = targetFormat.format(date12);
        } catch (Exception e)
        {
            e.printStackTrace();
        }

        return formattedDate;

    }
    public static String convertFormattedDate (String date)
    {
        String formattedDate = null;
        try
        {
            SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd" , Locale.ENGLISH);

            Date date12 = originalFormat.parse(date);

            formattedDate = originalFormat.format(date12);
        } catch (Exception e)
        {
            e.printStackTrace();
        }

        return formattedDate;

    }

}





3. create a layout for Calender in activity_main.xml

activity_main.xml

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

    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/buttonlayout"
        android:layout_width="fill_parent"
        android:layout_height="60sp"
        android:background="@drawable/header"
        android:gravity="left|top"
        android:height="60sp"
        android:orientation="horizontal" >

        <!-- <Button
            android:id="@+id/settings"
            android:layout_width="54sp"
            android:layout_height="60sp"
            android:background="@drawable/meenu" /> -->

        <ImageView
            android:id="@+id/prevMonth"
            android:layout_width="20sp"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_marginLeft="10sp"
            android:src="@drawable/calendar_left_arrow_selector" >
        </ImageView>

        <TextView
            android:id="@+id/currentMonth"
            android:layout_width="fill_parent"
            android:layout_height="60sp"
            android:layout_weight="0.6"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#FFFFFF" >
        </TextView>

        <ImageView
            android:id="@+id/nextMonth"
            android:layout_width="20sp"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_marginRight="10sp"
            android:src="@drawable/calendar_right_arrow_selector" >
        </ImageView>

        <!-- <Button
            android:id="@+id/addEvent"
            android:layout_width="54sp"
            android:layout_height="60sp"
            android:background="@drawable/plus" /> -->
    </LinearLayout>

    <!-- <Button
        android:id="@+id/selectedDayMonthYear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/calendar_top_header"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" >
    </Button> -->

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="*"
        >

        <TableRow
            android:id="@+id/result_tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#d8d8d0" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:text="  " />

            <TextView
                android:id="@+id/textViewSun"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:gravity="center"
                android:text="Sun"
                android:textColor="#000" />

            <TextView
                android:id="@+id/textViewMon"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:gravity="center"
                android:text="Mon"
                android:textColor="#000" />

            <TextView
                android:id="@+id/textViewTue"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:gravity="center"
                android:text="Tue"
                android:textColor="#000" />

            <TextView
                android:id="@+id/textViewWed"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:gravity="center"
                android:text="Wed"
                android:textColor="#000" />

            <TextView
                android:id="@+id/textViewThu"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:gravity="center"
                android:text="Thu"
                android:textColor="#000" />

            <TextView
                android:id="@+id/textViewFri"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:gravity="center"
                android:text="Fri"
                android:textColor="#000" />

            <TextView
                android:id="@+id/textViewSat"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:gravity="center"
                android:text="Sat"
                android:textColor="#000" />
        </TableRow>
    </TableLayout>

</LinearLayout>

4. Drawer folder contains  some important images and important xml file that help to use the design the layout.

cal_left_arrow.xml

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:drawable="@drawable/cal_left_arrow_off" /> <!-- pressed -->
    <item
        android:state_focused="true"
        android:drawable="@drawable/cal_left_arrow_on" /> <!-- focused -->
    <item
        android:drawable="@drawable/cal_left_arrow_on" /> <!-- default -->
</selector>

calendar_left_arrow_selector.xml
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:drawable="@drawable/cal_left_arrow_off" /> <!-- pressed -->
    <item
        android:state_focused="true"
        android:drawable="@drawable/cal_left_arrow_on" /> <!-- focused -->
    <item
        android:drawable="@drawable/cal_left_arrow_on" /> <!-- default -->
</selector>

calendar_right_arrow_selector.xml

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:drawable="@drawable/cal_right_arrow_off" /> <!-- pressed -->
    <item
        android:state_focused="true"
        android:drawable="@drawable/cal_right_arrow_on" /> <!-- focused -->
    <item
        android:drawable="@drawable/cal_right_arrow_on" /> <!-- default -->
</selector>


OUTPUT




Download code from GitHub
SHARE

About prabhakar jha

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment