I have three Edit Text on my activity and i will like to add each value to array if the Edit Text not empty.
Here is what i want:
EditText 1, EditText 2, EditText 3
public static ArrayList<String> arrayValue = new ArrayList<String>();
public static ArrayList<String> arrayTitleValue = new ArrayList<String>();
if edittext 1 not empty then
arrayTitleValue.add("Text Box Value 1")
arrayValue.add(EditText 1.getText.tostring)
if edittext 2 not empty then
Add edittext 2 to variable string e.g arrayValue.add(EditText 2.getText.tostring)
arrayTitleValue.add("Text Box Value 2")
if edittext 3 not empty then
Add edittext 3 to variable string e.g arrayValue.add(EditText 3.getText.tostring)
then finally: print the result this way:
System.out.println("Title Result: "+arrayTitleValue+" Array Value: "+arrayValue);
so at the end of the day i want my result to look like this:
Result value for arrayValue= Result1(edit text 1 value),Result2(edit text 2 value),Result3(edit text 3 value)
Result value for arrayTitleValue= ResultTile1("Title 1"),ResultTitle2("Title 2"),ResultTitle3("Title3")
i just want very fast way to achieve this task any help is welcome
You first have to give an id to each of your EditText in your layout file (.xml)
android:id="#+id/edittext_1"
After in your activity file (.java) you get an EditText object corresponding to each EditText :
EditText editText1 = (EditText) findViewById(R.id.edittext_1);
Then you check for your EditText to be filled and if it is, add its value to the List :
if(!editText1.getText().toString().equals("")){
arrayValue.add(editText1.getText().toString();
arrayTitleValue.add("Text Box Value 1");
}
Related
I have textview where user are asked to enter some information and that information is uploaded in Firebase Data Structure and then is Displayed on another activity
Here is the code I'm using to getText from Textview
etAuthor = (EditText) findViewById(com.nepalpolice.bookbazaar.R.id.editText1);
String bauthor = etAuthor.getText().toString();
and it does job pretty well.
and it is added to firebase.
But what if I want to add predefined Text like
Author:getText()
Here I have added author.
and This will be upudated on Database as well and will Displayed to user
instead of Consider Author is
J. K. Rowling
It will show
Author:J.K Rowling
Any help is appreciated.
Thanks in advance.
you can concatenate the desired string, in you case:
String bauthor ="Author:"+etAuthor.getText().toString();
String bauthor = "Auther : "+etAuthor.getText().toString(); //use this, '+' use for concatenate
#Bir Nepali, you have to just append while getting the text.
etAuthor = (EditText) findViewById(com.nepalpolice.bookbazaar.R.id.editText1);
String bauthor = "Author:" + etAuthor.getText().toString();
Now you can populate this bauhtor in the view.
How can I code the following:
For example, I have one EditText in which I insert a word (or words) like "abcdawa". I have another EditText in which I enter the search element. Let's say I want to search "a".
When I click a Button the indexes of the word in the first EditText which equals the search element should be shown in a TextView. Expected output: 1, 5, 7
Consider that you have 2 edittext with id edText_1 & edText_2, and a button
Now in on click of your button call following,
setOnClickListener{
String query = edText_2.getText().toString();
char[] dataArray = edText_1.getText().toString().toCharArray();
for (int i = 0; i < dataArray.length; i++) {
if (query.equalsIgnoreCase(String.valueOf(dataArray[i]))) {
textView.append(" " + i);
}
}
}
I need to set one of my EditText to enter three alpha value and four digits Ex:- ABC1234. So I need to change automatically input type from Text to phone when user typed three alphas. I came up with following code and but its not change to phone input type and text type is remains for any text length.
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
String text = editText.getText().toString();
if(text.length()==1 || text.length()==2 || text.isEmpty()){
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
System.out.println("Length Text type : " + text.length());
}else{
editText.setRawInputType(InputType.TYPE_CLASS_PHONE);
System.out.println("Length phone type : " + text.length());
}
}
What I have done wrong.
setRawInputType() is usually used when you initialize the view, in a constructor of a custom view or in onCreate() method of an activity.
In your case you have to call setInputType() change the mode of the soft keyboard.
I would like to take some variables, listed here;
double dBench = (((Math.floor((dWeight*.664)/10))*10)-10)+dPush;
double dFlies = (dBench)*.6;
double dCurls = (dWeight*dPull)*.25;
double dLats = ((Math.floor((dWeight*.5)/10))*10);
System.out.println(dBench);
System.out.println(dFlies);
System.out.println(dCurls);
System.out.println(dLats);
But how do I get the println()'s to print to a textview or something? Thanks.
You do not have to do println to textview. TextView has method called setText and getText. setText can set text to TextView and getText can get text from textview. Look here for more info.
Android Tutorial for TextView
For double,
textView = (TextView)findViewById(R.id.myTextView);
textView.setText(String.valueOf(Your double value));
Use the textview's set text method
textView = (TextView)findViewById(R.id.myTextView);
textView.setText("This text will be placed in the textView");
For a double value it will look like this
textView = (TextView)findViewById(R.id.myTextView);
textView.setText(String.valueOf(double_variable);
In case you want to print a value in textView:
TextiView textView = (TextView) findViewById(R.id.textViewId);
textView.setText(...see below...);
Integer: Integer.valueOf(integerVariable).toString()
the same can be done with Long, Double etc. Here is analogic example for Double:
double a = some value;
String toShow = Double.valueOf(a).toString();
textView.setText(toShow);
If you want to check those values (debug) you can use Log class.
Log.d("VALUE", Integer.valueOf(integerVariable).toString());
In this case the value will be shown in LogCat (tag VALUE). You can filter messages typing tag:VALUE
Integer, Long, Double are wrapper classes for primitives and those classes have toString() method which you can use to pass String into the TextView.
If you want to see the value which is stored into variables you can use log cat commands to print them into console as follows:
Log.v("Title" , variable_name);
or for numeric value you can use
textView = (TextView)findViewById(R.id.myTextView);
textView.setText(""+Your double value);
I know that you can get a normal text like this:
EditText input = (EditText) findViewById(R.id.input);
String value = input.getText().toString();
but now I need a to an input like this: 0xFFFFFF (a Hex-Color), an Integer... But value is only a String and I don't know how to convert it... I'm using view.setBackgroundColor(color);
Thanks a lot!
you can use the Color.parseColor(colorString); function to convert your string to a color int