Friday 21 June 2013

Date Picker and Set any specific date using Calender for Android


Current Date and set any specific date from Calender

In This project I am going to show you how date picker works. You can get the current date and
also you can pick any specific date as well.

In main.xml specifying two text view, one is for current date and another is for specific date that you select from calender. After clicking on button, one dialog will appear for calender, you can choose any specific date.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Today Date:"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/tvCurrentDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

      <Button
        android:id="@+id/btnChangeDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Date" />
   
       <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your Selected Date:" />

    <TextView
        android:id="@+id/tvselectedDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <DatePicker
        android:id="@+id/dpResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


CalenderViewActivity.java
public class CalenderViewActivity extends Activity {

 private TextView tvDisplayTodayDate,tvModifiedDate;
 private DatePicker dtPicker;
 private Button btnChangeDate;
 private int year;
 private int month;
 private int day;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.main);
  setCurrentDate();
  btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
  tvModifiedDate = (TextView)findViewById(R.id.tvselectedDate);
  btnChangeDate.setOnClickListener(new OnClickListener() {
 
   @Override
   public void onClick(View v) {

    showDialog(101);
   }

  });
 }

 private DatePickerDialog.OnDateSetListener dpListener = new DatePickerDialog.OnDateSetListener() {
                           public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) {
                                  year = selectedYear;
                                  month = selectedMonth;
                                  day = selectedDay;
                         // Displaying selected date into textview
                            tvModifiedDate.setText(day+"-"+(month + 1)+"-"+year);
                         // Showing selected date into datepicker
                         //dtPicker.init(year, month, day, null);

                       }
      };
      protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 101:
       // set date picker as current date
       return new DatePickerDialog(this, dpListener, year, month,day);
    }
    return null;
   }

 public void setCurrentDate() {
 
  tvDisplayTodayDate = (TextView) findViewById(R.id.tvCurrentDate);
  dtPicker = (DatePicker) findViewById(R.id.dpResult);

  final Calendar c = Calendar.getInstance();
  year = c.get(Calendar.YEAR);
  month = c.get(Calendar.MONTH);
  day = c.get(Calendar.DAY_OF_MONTH);
  //Displaying today date into textview
  tvDisplayTodayDate.setText(day+"-"+(month + 1)+"-"+year);
  // Showing current date into datepicker
  dtPicker.init(year, month, day, null);

 }
}





Wednesday 5 June 2013

Android Layout Inflator

Android Layout Inflator


Layout-Inflater class is used to instantiate layout XML file into its corresponding View objects.
In other words, it takes as input an XML file and builds the View objects from it.
So we will make a simple application for Android to more understand about layout inflater.
Step 1: Make a New Project.
Step 2: Go to res/main.xml, provide some specific-id for linearlayout.
main.xml
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/main_layout_id">
</LinearLayout>
Step 3: Go to res - layout and make a new xml called "layout_item.xml" . Now you must have 2 xml files: main.xml and layout_item.xml
Step 4: We will make a TextView in the xml created before. This will be the view that we want to inflate (add) to the main.xml . Put the code below in the layout_item.xml
layout_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:id="@+id/layout_item_id">
 
    <TextView android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Hello, this is the inflated text :D"
              android:layout_gravity="center"
              android:gravity="center_horizontal"
              android:id="@+id/text_item_id"/>
 
</LinearLayout>

Step 5: Now in your main activity you will have to call the main.xml with findViewById because it is the parent that will host our view.
 LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout_id);
Step 6: At this step we have to create a view to inflate the layout_item.xml.In the inflate method we have to say first WHAT we want to inflate and then WHERE we want to inflate our view. We use the "false"attribute because this way any further layout changes we make to the loaded view,will take effect. If we have set it to "true" it would return the root object again, which would prevent further layout changes to the loaded object (unless we can find it using findViewById).  
View view = getLayoutInflater().inflate(R.layout.layout_item, mainLayout,false);
Step 7: And the last step, add the view to the layout
mainLayout.addView(view);
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
 
public class MyActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
 
        //here we set the main layout
        // this is the parrent layout...here we will add the view we created
        setContentView(R.layout.main);
 
        //call the main layout from xml
        LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout_id);
 
        //create a view to inflate the layout_item (the xml with the textView created before)
        View view = getLayoutInflater().inflate(R.layout.layout_item, mainLayout,false);
 
        //add the view to the main layout
        mainLayout.addView(view);
    }
}