Friday 15 June 2012

Put & (Ampercend) sign in Android XML file


eg.
android:text="Rock & Roll Dance"

Set Any type of font in your application


Download .ttf file of the font which you like


Go to computer font (I’ve windows 7) and copy shruti font.
Paste it in android assets folder.
Use it in TextView.


Following is a code that can help you:


TextView detailText = (TextView) findViewById(R.id.detailText);
Typeface font = Typeface.createFromAsset(getAssets(), “SHRUTI.TTF”);
detailText.setTypeface(font);

Get Height and Weidth in android


int width,height;
Display display = getWindowManager().getDefaultDisplay();
        width = display.getWidth();
        height = display.getHeight();

CHECK EMAIL VALIDATIONS IN ANDROID


This is a simple example showing email validation in ANDROID.
This example uses regex for email validation.
Create a button and an edittext in your main.xml file and try this code.




import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class EmailValidationDemo extends Activity   {

    EditText TF;
    public Button checkButton;

    public final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
              "[a-zA-Z0-9+._%-+]{1,256}" +
              "@" +
              "[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" +
              "(" +
              "." +
              "[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" +
              ")+"
          );
     @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

       TF=(EditText) findViewById(R.id.TF);
       checkButton=(Button) findViewById(R.id.checkButton);

        checkButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
               String email=TF.getText().toString();
               if(checkEmail(email))
                  Toast.makeText(EmailValidationDemo.this,"Valid Email Addresss", Toast.LENGTH_SHORT).show();
               else
                  Toast.makeText(EmailValidationDemo.this,"Invalid Email Addresss", Toast.LENGTH_SHORT).show();
        }
        });
    }
    private boolean checkEmail(String email) {
        return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
    }
}



Refresh button in Android




btn_Refresh.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
// REFRESH CODE
RefreshPageActivity.this.startActivity(getIntent());
}
});

Delete SPACE from EditText(User cant add space in beginning in edittext if user presses space)


Step-1: Add "implements TextWatcher" after Activity
eg. public class AddMoreWebsite extends Activity implements TextWatcher {


Step-2:Now Put Cursor on Red line error and Add UnImplemented methods of TextWatcher.
Eg. it Will add following Methods


public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String result = s.toString().replaceAll(" ", "");
if (!s.toString().equals(result)) {
et_AddMoreSite.setText(result);
et_AddMoreSite.setSelection(result.length());


}
}


public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub


}


public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub


}


Step-3: Now Add following lines in afterTextChanged(...)


String result = s.toString().replaceAll(" ", "");
if (!s.toString().equals(result)) {
et_AddMoreSite.setText(result);
et_AddMoreSite.setSelection(result.length());




Step-4: Now Put this line in onCreate() after your editText declaration.


et_AddMoreSite.addTextChangedListener(this);




Ref. Link: http://stackoverflow.com/questions/9757991/how-to-delete-instantly-space-from-an-edittext-if-a-user-presses-the-space


Enjoy.....

Awake Screen(Screen On) when starting activity




First import this file
import android.view.WindowManager.LayoutParams;


call this method whenever you need to On the screen
 private void unlockScreen() {
        Window window = this.getWindow();
        window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
        window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
    }