Friday, 2 August 2013

Android E-Mail & Mobile Number Validation

Hi!


  Today i am going to show How to validate e-mail id & mobile number in android. Because some struggle with validation as specially for e-mail..,

   Validation of email id & Mobile number in android is similar to validation in java.


You can Download my jar at Here and can add into your project

or


  •     create a bean class to  forward your input to validate


package ra_jEmail_Mob_Validation;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RValidation {
String r_email, r_mobil_no;
boolean is_mob_number = false, is_email = false;

        //this is the default format for email validation 

String regEx = "[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}" + ")+";

// empty constructor for call the object

public RValidation() {
super();
}

// create getter & setter for parameters 

public String getR_email() {
return r_email;
}

public void setR_email(String raj_email) {
this.r_email = raj_email;
}

public String getR_mobil_no() {
return r_mobil_no;
}

public void setR_mobil_no(String raj_mobil_no) {
this.r_mobil_no = raj_mobil_no;
}

//method to validate you mobile number, if it is valid it will return true
public boolean isIs_mob_number() {
// mobile number should be 10 digit
Pattern pattern = Pattern.compile("\\d{10}");
Matcher matchr = pattern.matcher(this.getR_mobil_no().trim());
if (matchr.matches()) {
is_mob_number = true;
}
return is_mob_number;
}

// method to validate you e-mail id, if it is valid it will return true
public boolean isIs_email() {
Matcher matcherObj = Pattern.compile(regEx).matcher(
this.getR_email().trim());
if (matcherObj.matches()) {
is_email = true;
}
return is_email;
}

}



  • Here Pass the value for validation from your Action class by on click listener
public class Register extends Activity {


EditText et_r_name,et_r_mob,et_r_email,r_pass;
//create reference for our class
RValidation r_valid;
boolean sucess_regist=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Button reg=(Button) findViewById(R.id.r_regist);
et_r_name=(EditText) findViewById(R.id.r_name);
et_r_mob=(EditText) findViewById(R.id.r_mob);
et_r_email=(EditText) findViewById(R.id.r_email);
r_pass=(EditText) findViewById(R.id.r_pass);

// create a new object for our reference

r_valid=new RValidation();

reg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!et_r_name.getText().toString().equals(""))
{
// pass the mobile number to through our setter methid
r_valid.setR_mobil_no(et_r_mob.getText().toString());
// if it is true move to next step
if(r_valid.isIs_mob_number())
{
//pass the e-mile id to through our setter methid
r_valid.setR_email(et_r_email.getText().toString());
//if it is true move to next step
if(r_valid.isIs_email())
{
if(!r_pass.getText().toString().equals(""))
{
AlertDialog.Builder ad= new AlertDialog.Builder(new ContextThemeWrapper(Register.this, R.style.popup_theme));
ad.setTitle("Success");
ad.setMessage("You Have Login Successfully..,");
ad.setInverseBackgroundForced(false);
ad.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});  
a ad.show();
}
else
{
r_pass.setError(Html.fromHtml("<font color='green'>Enter Your Password</font>"));
}
}
else
{
et_r_email.setError(Html.fromHtml("<font color='green'>Enter Your Valid E-mail address</font>"));
}
}
else
{
et_r_mob.setError(Html.fromHtml("<font color='green'>Mobile Number Should Be 10 Digit</font>"));
}
}
else
{
et_r_name.setError(Html.fromHtml("<font color='green'>Enter Your Name</font>"));
//
}
}
});
}
}


finally i have set some error messages and add some html tag for my customization..,

Thank You..,

Please Leave Your Comment..,



Have A Happy Day..,

Custom Horizontal(ListView) Dynamic TextView Without using Adopter & with On Click function

Hi!

   I was faced a problem to generate dynamic horizontal list view in fragment.I search the help with Google too. But their is no more help to fulfill my need.

The Problems are :

  • Their is no default horizontal list view in Android (the default list view is vertical),
  • Even though we implement the customize Horizontal list view, It is too heard to implementing with Fragment,
  • Implementing the "on click" Listener on the dynamic items too.
I try and found the solution for the problem and happy to share with developers.

ArrayList for my business class


ArrayList<CatagoryB> clist;

return the view for the list and execute the Async class

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_list_of_category,container);
clist = new ArrayList<CatagoryB>();
new CatagoryList().execute();

return view;
}

store the list item into the array list

clist.add(new CatagoryB(lcid, lcname));


initialize the dynamic text view 

final TextView myid[] = new TextView[clist.size()];
final TextView mynm[] = new TextView[clist.size()];

initialize the layout too

LinearLayout lout = (LinearLayout) getActivity().findViewById(
R.id.catlout);

create dynamic object for the textview

myid[i] = new TextView(getActivity());
mynm[i] = new TextView(getActivity());

set it on layout 

myid[i].setLayoutParams(new LayoutParams(0, 0));

assign the parameters

myid[i].setText(clist.get(i).getCid());
mynm[i].setText(clist.get(i).getCname());

assign the id

mynm[i].setId(i);
myid[i].setId(i);
set onclick listener

mynm[i].setOnClickListener(new OnClickListener()

can get the relevent item by id

int iid = v.getId();
String actual_id = clist.get(iid).getCid();

Here is over view of my code


//@SuppressLint("NewApi")
@SuppressLint("ResourceAsColor")
public class CategoryList extends Fragment {

private CategoryToSubCategory connecter;
String Cat_Url = "onsite link for your database query";
ArrayList<CatagoryB> clist;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_list_of_category,
container);
clist = new ArrayList<CatagoryB>();
new CatagoryList().execute();
return view;
}

public interface CategoryToSubCategory {
public void List_SubCategory(int item);
}

@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
if (activity instanceof CategoryToSubCategory) {
connecter = (CategoryToSubCategory) activity;
} else {
throw new ClassCastException();
}
}

class CatagoryList extends AsyncTask<String, String, String> {

@Override
protected String doInBackground(String... arg0) {
JSONParser jparse = new JSONParser();
List<NameValuePair> pair = new ArrayList<NameValuePair>();
String JSon = jparse.makeHttpRequest(Cat_Url, "GET", pair);
try {
JSONArray jaray = new JSONArray(JSon);
if (jaray != null) {
Log.d("JSon", JSon);
for (int i = 0; i < jaray.length(); i++) {
JSONObject j = jaray.getJSONObject(i);
String lcid = j.getString("categorykey");
String lcname = j.getString("stockcategoryname");
clist.add(new CatagoryB(lcid, lcname));
}
}

} catch (Exception e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
final TextView myid[] = new TextView[clist.size()];
final TextView mynm[] = new TextView[clist.size()];
LinearLayout lout = (LinearLayout) getActivity().findViewById(
R.id.catlout);
for (int i = 0; i < clist.size(); i++) {
myid[i] = new TextView(getActivity());
mynm[i] = new TextView(getActivity());
myid[i].setLayoutParams(new LayoutParams(0, 0));
mynm[i].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
myid[i].setTextSize(30);
mynm[i].setTextSize(30);
mynm[i].setPadding(5, 1, 5, 1);
myid[i].setTextColor(Color.WHITE);
mynm[i].setBackgroundResource(R.drawable.b);
mynm[i].setTextColor(Color.WHITE);
myid[i].setText(clist.get(i).getCid());
mynm[i].setId(i);
myid[i].setId(i);
mynm[i].setText(clist.get(i).getCname());
lout.addView(myid[i]);
lout.addView(mynm[i]);
mynm[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int iid = v.getId();
String actual_id = clist.get(iid).getCid();
 connecter.List_SubCategory(Integer.valueOf(actual_id));
}
});
}
}

}
}


Layout xml file for the Layout


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:widget="http://schemas.android.com/apk/res-auto"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/hesdbg" >

        <LinearLayout
            android:id="@+id/catlout"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:orientation="horizontal" >
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>



Please Leave Your Comment..,