Android : Edit text validation for android

on Tuesday, August 12, 2014


I am developing one android application. In it on Button click one dialog would diplay which contains Some Edittexts according to database table column count. and if data type is integer or text then that edittext should allow only integer or text respectively. if i type character for integer type edit text then one error shuold display in popup. but all i want is at run time and if all is ok then record should be insert into table. else record insertion would be stop. please help... here is my code :



Button insert=(Button)new Button(DisplayTable_Grid.this);
insert.setText(Html.fromHtml("<font size=10>Insert</font>"));
//insert.setBackgroundColor(Color.rgb(3,12,90));
insert.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
final AlertDialog.Builder alertDialog=new AlertDialog.Builder(DisplayTable_Grid.this);
final LinearLayout linearlayout=new LinearLayout(DisplayTable_Grid.this);
linearlayout.setOrientation(LinearLayout.VERTICAL);

sqlcon.open();

getColumnNames=sqlcon.getColumnNames(DisaplayTables_list.getTableNameFromListView);
Cursor crs=sqlcon.readColumnCnt(DisaplayTables_list.getTableNameFromListView);
int count=crs.getColumnCount();

for(cfor=0;cfor<count;cfor++)
{ //add edittext to arralist
enter_text=new EditText(DisplayTable_Grid.this);
allEds.add(enter_text);
getarraylist=arraylist.get(cfor).toString();

String datatype_text="{datatype=text}";
String datatype_integer="{datatype=integer}";
if(getarraylist.equals(datatype_text))
{
//allEds.get(cfor).setInputType(InputType.TYPE_CLASS_TEXT);
enter_text.setInputType(InputType.TYPE_CLASS_TEXT);
}
else if(getarraylist.equals(datatype_integer))
{
try
{
int convert=Integer.parseInt(enter_text.getText().toString());
}
catch(NumberFormatException e)
{
//enter_text.setInputType(InputType.TYPE_CLASS_NUMBER);

if(enter_text.getText().toString().contains("a")||enter_text.getText().toString().contains("a"))
{
enter_text.setError("char not allowed");
}
else
{

}
}
/*enter_text.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if(!hasFocus)
{ //enter_text.setError("charachter not allowed");
enter_text.setError("char not allowed");
}
}
}); */

//Toast.makeText(getApplicationContext(), "integer",Toast.LENGTH_LONG).show();
//allEds.get(cfor).setError("charachter not allowed");
//enter_text.setInputType(InputType.TYPE_CLASS_NUMBER);
}
else
{
Toast.makeText(getApplicationContext(), "no datatype found",Toast.LENGTH_LONG).show(); }

set_txt=new TextView(DisplayTable_Grid.this);
set_txt.setText(getColumnNames.get(cfor));
set_txt.setTextSize(15);
textViewlst.add(set_txt);
linearlayout.addView(set_txt);
linearlayout.addView(enter_text);


}
sqlcon.close();

alertDialog.setPositiveButton("Insert", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1)
{
if(allEds.size()==0)
{Toast.makeText(getApplicationContext(), "value not found",Toast.LENGTH_LONG).show();}
else
{
for(int i=0; i < allEds.size(); i++)
{ storeEditextText.add(allEds.get(i).getText().toString());
System.out.println("show gettext: "+storeEditextText.get(i).toString());
}

if(storeEditextText.size()==0)
{Toast.makeText(getApplicationContext(), "Text is empty",Toast.LENGTH_LONG).show();}
else
{
sqlcon.open();

try
{sqlcon.insert(DisaplayTables_list.getTableNameFromListView,storeEditextText);}
catch(Exception e)
{Toast.makeText(getApplicationContext(),"emp_id is Primary key", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(),"column value already exists",Toast.LENGTH_LONG).show();}

sqlcon.close();

System.out.println("size of storeEdittext="+storeEditextText.size());
System.out.println("size of Edit Texts: "+allEds.size());

}
Intent i=new Intent(getApplicationContext(),DisplayTable_Grid.class);
startActivity(i);
}
}


});

0 comments:

Post a Comment