How to make a custom Data Picker like Date Picker in Android - android

How to make a custom Data Picker like Date Picker in Android so that I can get data (int data) by custom input or by using the buttons (- & +) so that it become easy for user to enter larger value !!
Has anyone any idea how to implement this?
please provide example, code snippet if possible.
I want to make like this
left - Button
middle - EditText
right - Button
I want Like this image--
This is the example of Date Picker in Android:-

then try this link
try on text changed listener as described in the link to update the variable whenever user changes the edit text, then variable will hold the changed value and so you can use your method to change values of that variable

Design a layout as you want with two buttons and one edittext in middle
Then give one button addition funtionality and other substraction functionality
Follow little code snippet, provided below
addButton.setOnClickListener(new OnClickListener(){
public void onClick(){
int valueInEditText = Integer.parseInt(etReference.getText().toString());
etReference.setText(String.valueOf(valueInEditText + 1));
}
});
Similaryly, do it for substration button.
Note: Do validation if you don't want negative values.

You might want to use the NumberPicker which will allow you to select any value.
It's not the same look but it might work for you:
https://android--examples.blogspot.com/2015/05/how-to-use-numberpicker-in-android.html

After reading through various posts, examples, articles and Android documentation I found solution to my own question, It can be achieved by two ways:
First by using NumberPicker
Secondly by adding TextChangedListener to the EditText
Here is the code how I achieved it :
int noOfCups = 2;
EditText editTextNoOfCups;
editTextNoOfCups = (EditText) findViewById(R.id.edit_text_no_of_cups);
editTextNoOfCups.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() != 0) {
Log.e("pss", String.valueOf(s));
noOfCups = Integer.parseInt(String.valueOf(s));
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
Here is the link to official Android documentation :
TextWatcher | Android Developers

Related

Android AutoCompleteTextView is overlapping while typing suggestion list

I am trying to implement HashTag and mention by using AutocompleteTextview. It is working fine.
Depends on the entered keyword I am setting adapter in AutocompleteTextview. For that, I am using TextWatcher. It is showing the result. Till this working fine. But when I am typing the suggestion list is overlapping.
In the image, black Shadow is because of multiple pages. The number of pages is increasing when text in AutocompleteTextview is changing.
autoCompleteTextview.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().startsWith("#")) {
setupTagUserAutocomplete(data);
}
else if (s.toString().startsWith("#")) {
setupHashAutocomplete(list);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
How to avoid this?
You do not have to use TextWatcher. Using TextWatcher will create a new suggestion list every time the text is changed.
You have to use an adapter for that purpose.
That is the reason it is creating so many suggestion-lists.
You have to create a custom adapter since there is a custom requirement because of the two conditions ( # and #).
Here is a link to an AutoCompleteTextView tutorial for you to go through. I hope you find this one useful.
https://www.studytonight.com/android/autocomplete-textview
In the above tutorial refer to the steps, but instead of an ArrayAdapter create a custom one. Inside that adapter, you can have that list according to the text.

Lag after setting text in OnTextChanged()

Below is an example that replicates my lagging problem. Once I set the text on the EditTextView it takes at least 1.5 seconds for the user to be allowed to input another character.
amountEditText.addTextChangedListener(new TextWatcher() {
#Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override public void afterTextChanged(Editable s) {}
#Override public void onTextChanged(CharSequence s, int start, int before, int count) {
String amount = s.toString();
if( !amount.equals(current )) {
amountEditText.removeTextChangedListener(this);
amountEditText.setText(s);
Selection.setSelection(amountEditText.getText(), amountEditText.getText().length());
amountEditText.addTextChangedListener(this);
}
}
});
I've searched around and have not found a solution.
I identified that the issue was coming from the textView.setText() call.
The solution was to not use setText(), instead use the Editable that is provided to you in the onTextChanged callback.
I tried to use the Editable before, however i couldn't get it working with inputs such as "$12,000".
This was due to having InputFilters still attached to the Editable.
Regards,
Scott.
It lags because you remove and then readd your listener.
You seems to be trying to select the text inside the EditText. To do so, simply set editText.setSelectAllOnFocus(true); or android:selectAllOnFocus="true" in xml. Then remove the entire TextChangedListener from your code.
According to me it is most probably because of trying to remove (amountEditText.removeTextChangedListener(this);) and add (amountEditText.addTextChangedListener(this);) the Text Change Listener every time. I would recommend you to replace your code without those adding and removal.
Hope this helped.

android: EditText input format

In my app I have to format the input of two EditTexts like:
1234 4567 67: Ten digits that grouped by four. (The space is automatically, not inserted by user)
11/14: Four digits that separated by '/'. (The '/' is inserted automatically)
I don't know how to do it. Please help:
Put a listener on the edit text as afterTextChanged.
Get the number of digits by using the length() function.
Once you get the number of digits, you can extract each digit and then insert space of '/' at the appropriate place.
len=editText.getText.toString().length();
then you can do the appropriate change by checking the length.
num=Integer.parseInt(editText.getText.toString());
temp=num;
if(len>=10)
{
A:
if((len-4)>0)
{
for(i=0;i<(len-4);i++)
{
temp=temp/10; //we get the first 4 digits
}
editText.setText(temp+" "); //place letters and add space
temp=num%(10^(len-4)); //get the num without the first-4 letters
len=len-4; //modify length
goto A; //repeat again
}
editText.setText(temp); //add the last remaining letters
}
else if(len==4)
{
temp=num;
temp=temp%100; //store the last 2 digits
num=num/10; //get the first 2 digits
editText.setText(num+"/"+temp);
}
i havnt tried this but i think this will work.
Hope this will help. :)
I can think of two ways of achieving it:
Use addTextChangedListener:
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// Do your tricks here
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
Create custom Edittexts
This link wont do what you are looking for, but will give you an idea how to create custom EditText.
Use "onKeyListener" to get the input event.
EditText OnKeyDown
Then check for correct input and count the digits. Add the whitespace/slash in your code.
Sample code:
if (editText.getText.toString().length() % 4 == 0) editText.setText(editText.getText.toString() + " ");
Didn't try it by myself, but this would be the way i would try. In addition i would check for numeric input too.

Associate EditText with a TextView for labeling purposes

Hi I am learning Android programming and have run into an issue that I couldn't get a clear answer to through researching.
I have a TextView which serves as a label for my EditText. I have a method which checks if the EditText is an empty String. If the string is empty I want to be able to get a reference to the TextView that corresponds to that EditText in order to make a toast saying something like "please enter a value for ".
I've looked into getLabelFor/setLabelFor but is there a way to do this in the layout XML?
What is best practice for this type of functionality.
You're describing a functionally that is build in to EditText. There is a special field you can define in xml called hint, which is the recommended way to label an EditText rather than a nearby TextView. Additionally, EditText has a method called setError() (link). If the user attempts to hit a submit button, for example, you can check to see if the EditText is empty and if so, call setError().
I wonder if the following is the thing that you need
TextWatcher inputTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().equals("")) {
textView.setText("please enter a value for ..");
} else {
textView.setText("<the textedit is not empty>");
}
}
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(inputTextWatcher);

Android EditText boxes

I want to cause the focus of one edit text box to move to another on editting (meaning you can only type on letter before it automatically moves on to the next edit text).
It's the "on edit" that I can't get my head around. Can anyone help me out with a simple example? Theres a lot I need to implement it into, so just a basic understanding should set the ball rolling ^_^
I do not really recommend this. With soft keyboards and multiple languages, what exactly is "one letter"? After all, a soft keyboard might enter in an entire word, like it or not.
CommonsWare makes an excellent point: you can't prevent the user from adding more characters to the EditText box, however you can listen to what's changed and act on that. Here's how to:
EditText editbox = (EditText) findViewById(R.id.MyEditBoxName);
editbox.addTextChangedListener(new TextWatcher()
{
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
public void afterTextChanged(Editable s)
{
// Test s for length, request focus for the next edit.
// editbox2.requestFocus();
}
});
Be careful not to get yourself into an infinite loop changing the editbox, any changes you make will cause these methods to be called again recursively.

Categories

Resources