Using seterror method for validating edittext in android - java

I have two edittexts for username and password ,I use the seterror method to show an error when both of them are empty.The problem is when the error message pops up in the second field(the password field) , part of the message is missing.This bug is there only on older devices.How do i ensure that the error does not happen in older devices.
My code:
public class SignInPage extends Activity {
EditText txtusername,txtpassword;
Button btnlogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signinpage);
txtusername=(EditText) findViewById(R.id.txtusername);
txtpassword=(EditText) findViewById(R.id.txtpassword);
btnlogin=(Button) findViewById(R.id.btnlogin);
btnlogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(txtusername.getText().toString().trim().equals(""))
{
txtusername.setError("Username is mandatory");
txtusername.requestFocus();
}
if(txtpassword.getText().toString().trim().equals(""))
{
txtpassword.setError("Password is mandatory");
txtpassword.requestFocus();
}
else
{
Toast.makeText(getApplicationContext(),"Checking with server",Toast.LENGTH_LONG).show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Try this it might help you
fname = FirstName.getText().toString().trim();
if(fname.isEmpty()) //&& fname.matches("[a-zA-Z ]+"))
{
FirstName.requestFocus();
FirstName.setError(Html.fromHtml("<font color='red'>Please enter the First name</font>"));
}
TextWatcher textWatcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{ }
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{ }
#Override
public void afterTextChanged(Editable arg0)
{
if(arg0.toString().isEmpty())
{
return;
}
if (FirstName.getText() == arg0) {
Validate(FirstName);
FirstName.addTextChangedListener(textWatcher);
public void Validate(EditText et) {
et.setError(null);
}

Related

how to get dictionary meaning from direct to web android

this is my main activity code.
this is kind of jumble solver Program.
Jumble helper is create to solve unjumble word.
When you enter any Unordered word it will gives correct dictionary word.
currently when I tap on any word it text converted in to voice (TTS API) instead of I want to open dictionary meaning.
now I want to add dictionary meaning from web dictionary..
how can I add word meaning??
note that I will currently call data from unix word dic.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt_char = (EditText) findViewById(R.id.editText);
wordListView = (ListView) findViewById(R.id.word_listview);
wordListView.setOnItemClickListener(this);
tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int i) {
if (i != TextToSpeech.ERROR) {
tts.setLanguage(Locale.ENGLISH);
}
}
});
copyDBFileOnlyOnce();
}
public void viewOnClick(View button) {
switch (button.getId()) {
case R.id.btnGet:
String word = edt_char.getText().toString();
if (!"".equalsIgnoreCase(word) && ResourceUtil.supportedWordFormat(word)) {
new DataDictionaryTask(this, wordListView).execute(AlphabetToPrime.calcPrimeProduct(word.toLowerCase()));
} else {
((View)(wordListView.getParent())).setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Please enter a-z characters only", Toast.LENGTH_LONG).show();
}
hideSoftKeyboard(this);
break;
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String spk = (String)parent.getItemAtPosition(position);
Log.d("Selected text:", spk);
tts.speak(spk, TextToSpeech.QUEUE_FLUSH, null);
}
private void copyDBFileOnlyOnce()
{
prefs = getSharedPreferences("jumblehelper_pref", MODE_PRIVATE);
if (prefs.getBoolean("firstrun", true)) {
new AssetLoader(this).execute("jumblehelper.db3");
prefs.edit().putBoolean("firstrun", false).commit();
}
}
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
}

EditText search item in listview doesn't work

Hi guys i'm implementing a edittext in my listview to filter the items. I'm following this example, but nothing happen. It doesn't work and when i try to filter it doesn't do what i want.. This is my part of code of edittext:
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setTextFilterEnabled(true);
lv.setAdapter(listadaptor);
inputSearch.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.listadaptor.getFilter().filter(cs);
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
};
My listview is a list that show all application installed in the device.. I have this to load the list:
private class LoadApplications extends AsyncTask<Void, Void, Void> {
public ProgressDialog progress = null;
#Override
protected Void doInBackground(Void... params) {
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
listadaptor = new ApplicationAdapter(MainActivity.this,R.layout.snippet_list_row, applist);
return null;
}
#Override
protected void onCancelled() {
super.onCancelled();
}
protected void onDestroy() {
if(progress!=null)
if(progress.isShowing()){
progress.dismiss();
}
}
#Override
protected void onPostExecute(Void result) {
setListAdapter(listadaptor);
progress.dismiss();
super.onPostExecute(result);
}
#Override
protected void onPreExecute() {
progress = ProgressDialog.show(MainActivity.this, null,
"Caricamento applicazioni in corso...");
super.onPreExecute();
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
so listadaptor is my adapter.. Someone can help me? Thanks
Try this one ..
ArrayList<String> type_name_copy = new ArrayList<String>();
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable theWatchedText) {
ArrayList<String> type_name_filter = new ArrayList<String>();
String text = theWatchedText.toString();
for (int i = 0; i < array.size(); i++) {
if ((array.get(i).toLowerCase()).contains(text
.toLowerCase())) {
type_name_filter.add(array.get(i));
}
}
type_name_copy = type_name_filter;
listUpdate(type_name_copy);
}
});
}
public void listUpdate(ArrayList<String> data) {
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data));
}
}
I suggest you to use AutocompleteTextView which is Edittext with in-built Search functionality in android.

Radio Group as Circular Page Indicator

I am trying to use RadioGroup as a CircularPageIndicator. The problem is with the RadioGroup.OnCheckedChangeListener. It seems to call it self when onPageSelected is called. However I want it to work vice versa i.e. when I select a radioButton it should change a view based on the location of the Array index being provided. However it seems as if onCheckedChange is never called when I change a check on the radio button and so onCheckedChange never triggers.
public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener, OnPageChangeListener {
ViewPager mImagePager;
ImagePagerAdapter mPagerAdapter;
RadioGroup mPageIndicator;
boolean swipeChange = false;
int[] mRadioButtonIds = new int[] { R.id.radio0, R.id.radio1, R.id.radio2, R.id.radio3, R.id.radio4 };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initComponents();
mPageIndicator.setOnCheckedChangeListener(this);
mImagePager.setAdapter(mPagerAdapter);
mImagePager.setOnPageChangeListener(this);
}
/**
*
*/
private void initComponents() {
mPageIndicator = (RadioGroup) findViewById(R.id.radioGroup1);
mImagePager = (ViewPager) findViewById(R.id.imgPager);
mPagerAdapter = new ImagePagerAdapter();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageSelected(int pSelectedPagePosition) {
swipeChange = true;
mPageIndicator.check(mRadioButtonIds[pSelectedPagePosition]);
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (!swipeChange) {
int itemPosition = Arrays.asList(mRadioButtonIds).indexOf(checkedId);
mImagePager.setCurrentItem(itemPosition, true);
swipeChange = false;
}
}
}
Note : I don't want to use any third party code/lib to create a custom Circular Page Indicator.
Can any one please point a mistake here. Is there a reason for the RadioGroup not to work?
I forgot to take a look at the documentation!
http://developer.android.com/guide/topics/ui/controls/radiobutton.html

Getting Data from EditText, RadioButton and Spinner

I search on many websites and many tutorials how can I get the data from editText and radioGroup and spinner DIRECTLY When the user input the data
I tried with this:
MyEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
{case1.setName(CaseName.getText().toString());
}
return true;
}
return false;
}
});
but it didn't take the data, I just use button to make that as shown below:
Test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.cancel1:
try
{
// 1-Name
case1.setName(CaseName.getText().toString());
}catch(Exception e){
}
break;
}
}});
Can anyone explain what shall I use to save the data in Object(case1) directly when the user input it ?
editext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// s is your text .
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
})
for Radio button
checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks, depending on whether it's now checked
if (((CheckBox) v).isChecked()) {
Toast.makeText(HelloFormStuff.this, "Selected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(HelloFormStuff.this, "Not selected", Toast.LENGTH_SHORT).show();
}
}
});

Android: Broken menu button when text used

I have defined a standard onCreateOptionsMenu. The menu button works fine when my EditText box is empty. But when the EditText box has data in it, the menu button doesnt work. Any clue? Please help, i have no clue how to solve this. Thanks!
public class TipCalc extends Activity {
private EditText total;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
total = (EditText)findViewById(R.id.EditText01);
total.setOnKeyListener(mKeyListener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
private OnKeyListener mKeyListener = new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP) {
if((v.getId() == R.id.EditText01
&& (total.getText().toString().length() > 0) {
calculate();
return true;
}
}
return false;
}
}
I dont know if that would be correct but create setOnClickListener rather than OnKeyListener
something like this
TextView total = (TextView) findViewById(R.id.EditText01);
total.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
//do some stuff
}
});

Categories

Resources