Sunday, 28 August 2011

How to Set EditText Cursor position in Android


Below Code is Set cursor to Starting in EditText:

        EditText editText = (EditText)findViewById(R.id.edittext_id);
        editText.setSelection(0);

Below Code is Set cursor to end of the EditText:

        EditText editText = (EditText)findViewById(R.id.edittext_id);
        editText.setSelection(editText.getText().length());

Below Code is Set cursor after some 2th Character position :

   EditText editText = (EditText)findViewById(R.id.edittext_id);
   editText.setSelection(2);


Here we done this way to set cursor position to end of the text after 
updating the text of EditText programmatically here, editText is EditText 
Now cursor will be appear after"Updated New Text" String

EditText editText = (EditText)findViewById(R.id.edittext_id);
editText.setText("Updated New Text");
int position = editText.getText().length();
Editable editObj= editText.getText();
Selection.setSelection(editObj, position);


Wednesday, 24 August 2011

How to Set EditText Text start Letter as capital letter?

In Android Layout using an specific inputType Attribute in EditText for automatically capitalizing the first letter

<EditText
android:id="@+id/editText_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textCapSentences"
>
</EditText>


Tuesday, 23 August 2011

Date picker example

You can use “android.widget.DatePicker” class to render a date picker component to select day, month and year in a pre-defined user interface in android

In this tutorial, you'll create a DatePickerDialog, which presents the date picker in a floating dialog box at the press of a button. When the date is set by the user, a TextView will update with the new date.

 xml(date_picker_display.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"
android:layout_gravity="center_horizontal" >
<TextView
android:id="@+id/Date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Selected Date "
android:textSize="30sp" />
<TextView
android:id="@+id/displayDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/Date"
android:text="Date will appear Here"
android:textSize="30sp" />
<Button
android:id="@+id/pickDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/displayDate"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Pick a date using Date Picker" />
</RelativeLayout>


Activity(DatePickerDisplay.class)

package com.androidsurya.datepicker;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class DatePickerDisplay extends Activity {
private TextView pDisplayDate;
private Button pPickDate;
private int Year;
private int Month;
private int Day;
/**
* This integer will uniquely define the dialog to be used for displaying
* date picker.
*/
static final int DATE_DIALOG_ID = 007;


/** Callback received when the user "picks" a date in the dialog */
private DatePickerDialog.OnDateSetListener pDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Year = year;
Month = monthOfYear;
Day = dayOfMonth;
updateDisplay();
displayToast();
}
};
/** Updates the date in the TextView */
private void updateDisplay() {
pDisplayDate.setText(new StringBuilder()
// Month is 0 based so add 1
.append(Month + 1).append("/").append(Day).append("/")
.append(Year).append(" "));
}
/** Displays a notification when the date is updated */
private void displayToast() {
Toast.makeText(
this,
new StringBuilder().append("Date choosen is ").append(
pDisplayDate.getText()), Toast.LENGTH_SHORT).show();
}


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.date_picker_display);
/** Capture our View elements */
pDisplayDate = (TextView) findViewById(R.id.displayDate);
pPickDate = (Button) findViewById(R.id.pickDate);


/** Listener for click event of the button */
pPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
/** Get the current date */
final Calendar cal = Calendar.getInstance();
Year = cal.get(Calendar.YEAR);
Month = cal.get(Calendar.MONTH);
Day = cal.get(Calendar.DAY_OF_MONTH);
/** Display the current date in the TextView */
updateDisplay();
}
/** Create a new dialog for date picker */
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, pDateSetListener, Year, Month,
Day);
}
return null;
}
}

Register Activity in Android Manifest file(AndroidManifest.xml)

<activity
android:name="com.androidsurya.datepicker.DatePickerDisplay"
android:label="@string/app_name" >

Output Screenshort
 










For More information about Date Picker : Android Developers site

Friday, 19 August 2011

How to setCancelable to Activity as Dialog in Android

Some Times we don't need to cancle alert when user click on outside dialog. At this time use below code in your activity oncreate() method after setContentView()

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demolayout);
                //Dialog should not be canceled 
this.setFinishOnTouchOutside(false);
}

Thursday, 18 August 2011

How to Set Activity as Dialog in android

Simply use this Below Code to Set Activity as Dialog.
Using android:theme="@android:style/Theme.Dialog"
                                          (OR)  
 android:theme="@android:style/Theme.Holo.Dialog"

attribute in activity tag we can define our activity appear as a dialog.

<activity
            android:name="com.androidsurya.DialogActivity"
            android:theme="@android:style/Theme.Dialog" >

Wednesday, 17 August 2011

Rating Bar in Android

In Android, you can use “android.widget.RatingBar” to display rating bar component in stars icon. The user is able to touch, click on the stars or drag to set the rating value easily.
Here we I will show you how to use XML to display a rating bar, few textviews When user click/drag on the rating bar’s star, the selected rating value will be displayed in the textview.

Here rating bar contains many configurable values. In this case, the rating bar contains 5 stars, each increase 1.0 value, so, it contains the minimum of 1.0 (1 star) and maximum value of 5.0 (5 stars). In addition, it made the 1nd star (1.0) selected by default. 

layout(ratingBar.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:id="@+id/RateMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please Rate Me"
android:textAppearance="?android:attr/textAppearanceMedium" />

<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="1.0"
android:stepSize="0.5" />

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textesult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result is : "
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/txtRatingValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>

</LinearLayout>

Activity (ShowRatingbarActivity.class):

Inside activity “onCreate()” method, attach a listener on rating bar, fire when rating value is changed.

package com.androidsurya.ratingbarwidget;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

public class ShowRatingbarActivity extends Activity {

    private RatingBar ratingBar;
    private TextView txtRatingValue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ratingbar);
        addListenerOnRatingBar();

    }

    public void addListenerOnRatingBar() {

        ratingBar = (RatingBar) findViewById(R.id.ratingBar);
        txtRatingValue = (TextView) findViewById(R.id.txtRatingValue);

        // if rating value is changed and display the current rating value in the result (textview)
        // automatically
        ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
            public void onRatingChanged(RatingBar ratingBar, float rating,
                    boolean fromUser) {

                txtRatingValue.setText(String.valueOf(rating));

            }
        });
    }

}

Manifest file(AndroidManifest)

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidsurya.ratingbarwidget"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ShowRatingbarActivity"
            android:label="@string/title_activity_ratingbar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
 Output Screenshot:

 




Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...