Friday, January 28, 2011

Android Date Picker

ANDROID DATE PICKER
DOWNLOAD SOURCE CODE
1.when ever you click datepicker ..
2.dialog window showing.you change date but cancel button u click.
3.again click the datepicker it showing date..last what u change that date only displaying.

If u don't want this issue..u have to add this code in your activity.

 @Override
        protected void onPrepareDialog(int id, Dialog dialog) {
            switch (id) {
              
                case ID_DATEPICKER:
                    ((DatePickerDialog) dialog).updateDate(myYear, myMonth, myDay);
                    break;
            }
        }  

Source code:
Activity Code:
public class DatePicker extends Activity {
    private int myYear, myMonth, myDay;
    static final int ID_DATEPICKER = 0;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button datePickerButton = (Button)findViewById(R.id.datepickerbutton);
        datePickerButton.setOnClickListener(datePickerButtonOnClickListener);
    }
    private Button.OnClickListener datePickerButtonOnClickListener
        = new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final Calendar c = Calendar.getInstance();
                myYear = c.get(Calendar.YEAR);
                myMonth = c.get(Calendar.MONTH);
                myDay = c.get(Calendar.DAY_OF_MONTH);
                showDialog(ID_DATEPICKER);
            }
    };
    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        switch(id){
            case ID_DATEPICKER:
                Toast.makeText(DatePicker.this,
                        "- onCreateDialog -",
                        Toast.LENGTH_LONG).show();
                return new DatePickerDialog(this,
                        myDateSetListener,
                        myYear, myMonth, myDay);
            default:
                return null;
        }
    }
     @Override
        protected void onPrepareDialog(int id, Dialog dialog) {
            switch (id) {
              
                case ID_DATEPICKER:
                    ((DatePickerDialog) dialog).updateDate(myYear, myMonth, myDay);
                    break;
            }
        }   

    private DatePickerDialog.OnDateSetListener myDateSetListener
        = new DatePickerDialog.OnDateSetListener(){
           
            @Override
            public void onDateSet(android.widget.DatePicker  view, int year,
                    int monthOfYear, int dayOfMonth) {
                // TODO Auto-generated method stub
                String date = "Year: " + String.valueOf(year) + "\n"
                + "Month: " + String.valueOf(monthOfYear+1) + "\n"
                + "Day: " + String.valueOf(dayOfMonth);
            Toast.makeText(DatePicker.this, date,
                    Toast.LENGTH_LONG).show();
               
            }
    };
}


Android Menu Creation

Android Menu Creation.
DOWNLOAD SOURCE CODE
If you click menu button it will be displaying menu in android.

source code:
MenuActivity:

public class ManuActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        ManuStructure menuStr = new ManuStructure();
        menuStr.createOptionsMenu(menu, ManuStructure.MENU1_PAGE);
    return super.onCreateOptionsMenu(menu);
    }   
}



MenuStructure:

public class ManuStructure {
    private static final int MENU1= Menu.FIRST;
    private static final int MENU2 = Menu.FIRST+1;
    private static final int MENU3 = Menu.FIRST+2;
   
    public static final int MENU1_PAGE = 1;
    public static final int MENU2_PAGE = 2;
    public static final int MENU3_PAGE = 3;
    public void createOptionsMenu(Menu menu, int fromPage )
    {       
        switch (fromPage) {
        case MENU1_PAGE :
            menu.add(0,MENU1 , 0,"MENU 1").setIcon(R.drawable.icon);
            menu.add(0, MENU2, 0,"MENU 2").setIcon(R.drawable.icon);
            menu.add(0, MENU3, 0,"MENU 3").setIcon(R.drawable.icon);
           
            break;
        }
    }
}

Tuesday, January 25, 2011

Android Orientation configuration.

Android Orientation configuration.
      

You have to add this android:configChanges="orientation" line in Androidmanifest file.

 <activity android:name=".NewActivity"
            android:label="Dashboard" android:configChanges="orientation"
            android:theme="@android:style/Theme.NoTitleBar" />.

here after not coming this  android.internal.policy.impl.PhoneWindow .error.working fine





Thursday, January 13, 2011

Android Tutorial Free Download PDF.

Executing a HTTP POST Request with HttpClient

 DOWNLOAD SOURCE CODE

Executing A HTTP POST Request with HttpClient.

 public void postData() { 
        // Create a new HttpClient and Post Header 
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new HttpPost("http://192.168.2.175:8080/script.php"); 
     
        try { 
            // Add your data 
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
            nameValuePairs.add(new BasicNameValuePair("id", "2")); 
           nameValuePairs.add(new BasicNameValuePair("stringdata", "Vijayakumar!")); 
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
   
           // Execute HTTP Post Request 
           HttpResponse response = httpclient.execute(httppost); 
            
       } catch (ClientProtocolException e) { 
           // TODO Auto-generated catch block 
       } catch (IOException e) { 
           // TODO Auto-generated catch block 
       } 
   }

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...