Menus

Thursday 18 August 2016

Android Programming Tips


INDEX
Converting a string to an integer on Android
Converting a int to an string on Android
Converting a string to an double on Android
Converting a long to an string on Android
Decimal Format
Android Date Format Change
Set the focus on EditText Programmatically
Check If EditText is empty
Preventing going back to the previous activity
Set max-length of EditText programmatically with other InputFilter
String comparison - Android




Converting a string to an integer on Android


int in = Integer.valueOf(et.getText().toString());
//or
int in2 = new Integer(et.getText().toString());


Converting a int to an string o android



int tmpInt = 10;
String tmpStr10 = String.valueOf(tmpInt);

int tmpInt = 10;
String tmpStr10 = Integer.toString(tmpInt);

Converting a string to an double on Android

try this:
double d= Double.parseDouble(yourString);



Converting a long to an string on Android


String strLong = Long.toString(longNumber);


Decimal Format


new DecimalFormat("##,##,##0").format(amount);

Displaying Currency in Indian Numbering Format


Examples:

amount.setText(new DecimalFormat("####0.00").format(amount_item).toString());

or

total.setText(new DecimalFormat("####0.00").format(Double.parseDouble(txtCount.getText().toString()) * amount_item));

Android Date Format Change


SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());

The date format in sqlite should be of following format:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD 

For more details, have a look: http://www.sqlite.org/lang_datefunc.html


set the focus on EditText Programmatically


editText = (EditText)findViewById(R.id.myTextViewId); 
editText.requestFocus();



Check If EditText is empty



if(txtNumber.getText().toString().trim().length() == 0){
    Toast.makeText(getApplicationContext(),"Number cannot be blank!" ,Toast.LENGTH_SHORT).show();
    txtNumber.requestFocus();
    return;
}

use the TextUtils class like this :
if(TextUtils.isEmpty(strUserName)) {
    Toast.makeText(this, "plz enter your name ", Toast.LENGTH_SHORT).show();
    return;
}


This example check If EditText is empty and value must be a graterthan zero


if(txtCount.getText().toString().trim().length() == 0 || Integer.parseInt(txtCount.getText().toString()) <1){
    Toast.makeText(getApplicationContext(),"Count cannot be blank!" ,Toast.LENGTH_SHORT).show();
    txtCount.setText("");
    txtCount.requestFocus();
    return;
}





Preventing going back to the previous activity


Following solution can be pretty useful in the usual login / main activity scenario or implementing a blocking screen.
To minimize the app rather than going back to previous activity, you can override onBackPressed() like this:

@Override
public void onBackPressed() {
    moveTaskToBack(true);
}

Another Solutions
LoginActivity on successful login starts the Main activity and this time because the user is logged on, the Main activity will continue its normal course. LoginActivity is declared as following in the manifest file:
<activity android:name="LoginScreen" android:label="@string/app_name"
    android:noHistory="true" android:excludeFromRecents="true">
</activity>
Setting noHistory and excludeFromRecents to true for LoginActivity means that the user cant return to this activity using back button.


Set max-length of EditText programmatically with other InputFilter

Use InputFilter set text length
Limit the character input in an EditText , EditText in XML layout gives us android:maxLength
Try this
txtNumber.setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});
function
public void setEditTextMaxLength(int length) {
    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(length);
    edt_text.setFilters(FilterArray);
}
EditText et = new EditText(this);
int maxLength = 3;
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
et.setFilters(FilterArray);
public void setEditTextMaxLength(final EditText editText, int length) {
    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(length);
    editText.setFilters(FilterArray);
}


String comparison - Android


Check equals

if(item=="ALL"){
    ..........
    ..........
}


Android string comparison for use equal() method to compare the value of the objects.

if(gender.equals("Male"))
 salutation ="Mr.";
if(gender.equals("Female"))
 salutation ="Ms.";

string1.equals(string2)
// returns true if both strings are equal

string1.compareTo(string2)
// returns 0 if both strings are equal




No comments:

Post a Comment