Friday, December 4, 2015

How to implement search function in Android

public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

        helper = new DBhelper_D(this);
        db = helper.getReadableDatabase();
        String keyword = cs.toString().trim();
        String whereClause = "name like ?";
        String[] whereArgs = new String[]{
                keyword + "%",
        };

        Cursor c = db.query(helper.TABLE, null, whereClause, whereArgs,
                null, null, null);

        adapter = new SimpleCursorAdapter(
                this,
                R.layout.guest_row_dd,
                c,
                new String[]{DBhelper_D.C_NAME, DBhelper_D.C_COUNT, DBhelper_D.C_INVITE},

                new int[]{R.id.guestrowname, R.id.guestrowcount, R.id.guestrowinvite});

        guestList.setAdapter(adapter);

    }

Wednesday, November 11, 2015

How to open PDF document in Android

public void openpdf() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDF";

        File file = new File(path, "guest.pdf");

        intent.setDataAndType(Uri.fromFile(file), "application/pdf");
        startActivity(intent);
    }

Tuesday, October 13, 2015

How to create table in SQLite database

public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE + "(" + C_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT," + C_NAME + " text,"
                + C_SIDE + " text," + C_INVITE + " text," + C_COUNT + " text,"
                + C_ATTEND + " text," + C_ALCOHOL + " text )");
}

Tuesday, September 29, 2015

How to validate email address in Android

private boolean isValidEmail(String mail) {
        String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(mail);
        return matcher.matches();
    }