I have JSON data and would like to add them to EditText
JSON
{
"Data":[
{
"Name":"benz",
"Display":"slk200",
"Value":"1"
},
{
"Name":"bmw",
"Display":"z4",
"Value":"2"
},
{
"Name":"toyota",
"Display":"supra",
"Value":"3"
},
{
"Name":"honda",
"Display":"civic",
"Value":"4"
}
]
}
Java
LinearLayout placeHolder = (LinearLayout) findViewById(R.id.place_holder);
for (Data f : dataList) {
EditText ed = new EditText(context);
ed.setHint(f.display);
placeHolder.addView(ed);
}
If I would like to reference the EditText object after click some button
What should I do?
EditText inherits from TextView, which inherits from View. This means, you can use the view.setId(int) to assign an id to the dynamic EditText view.
You can then call findViewById(EditText id you gave it) to get the EditText view.
For example, assigning an id:
EditText ed = new EditText(context);
ed.setId(99);
And to reference the new EditText view:
EditText tmpEd = (EditText) findViewById(99);
tmpEd.setText("a test string");
Parsing Json request,
String jsonString;
JSONObject reader = new JSONObject(jsonString);
JSONObject dataJSON = reader.getJSONObject("Data");
displayText = dataJSON.getString("Display");
EditText ed = new EditText(context);
ed.setHint(displayText );
Following links might b useful to u
1- TutorailzPoint-JsonParsing
2- AndroidHive-Json
for the part on the button, you can implement an onClickListener.
If you have already created a button in your layout.
Reference it with:
Button b1 = (Button)findViewById(R.id.<buttonid>);
Then you can create an onClickListener for the button, which listens for a click.
b1.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
EditText e1 = (EditText) findViewById(R.id.<edittextid>);
//get records
e1.setText(<next record>);
}
});
Related
I have 2 TextView, and 1 Button. I want to be able to click the button and to save the text on the first TextView and show the value on the second TextView.
I've tried this code:
public void buttonOnClick(){
Button submit = (Button) findViewById(R.id.submit);
editName = (EditText) findViewById(R.id.name);
editEmail = (EditText) findViewById(R.id.email);
textout = (TextView) findViewById(R.id.outputText);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textout.setText(editName.getText()+"\n"+editEmail.getText());
}
});
}
But I get an error on 'textout'. When I clock the red light buble, it says 'create a local variable', field text'.
Try this
public void onClick(View v) {
textout.setText(editName.getText().toString()+"\n"+editEmail.getText().toString());
}
Your problem stimulates from the fact that EditText getText() returns Editable Object. It's not a String and you can't concat 2 Editable Objects.
You have 2 Options:
textout.setText(editName.getText().toString() + "\n" + editEmail.getText().toString());
And Second you can use SpanableStringBuilder
SpannableStringBuilder sb = new SpannableStringBuilder(editName.getText());
sb.append("\n").append(editEmail.getText());
The Second options allows you also to decorate the Text and save you the Need to build a String which is (maybe) better.
I am programmatically creating an EditText field whose input value I need to read after. But when I try to read the text inside my OnClickListener, it says that textNewProdPrice is accessed within inner class and should be declared final. How do I get around this? This is what I have so far:
EditText textNewProdPrice = new EditText(this);
textNewProdPrice.setTextColor(Color.WHITE);
textNewProdPrice.setHint("New Value");
textNewProdPrice.setTextSize(25);
textNewProdPrice.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
Button buttonPriceChange = new Button (this);
buttonPriceChange.setClickable(true);
buttonPriceChange.setBackgroundResource(R.drawable.buttonmain);
buttonPriceChange.setTextColor(Color.WHITE);
buttonPriceChange.setText("Change");
buttonPriceChange.setTextSize(25);
prodDetails.addView(textNewProdPrice);
prodDetails.addView(buttonPriceChange);
buttonPriceChange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newProductPrice = textNewProdPrice.getText().toString();
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTS_PRICE, newProductPrice);
dataBase.update(TABLE_NAME_PRODUCTS, values, "_id='" + currentProductID + "'");
Intent intent = new Intent(products_details.this, products_details.class);
intent.putExtra("currentProduct", currentProductID);
startActivity(intent);
finish();
}
});
Make the EditText final
final EditText textNewProdPrice = new EditText(this);
or if you wish to refer it out of the block the use setId()
Documentation setId():
Sets the identifier for this view. The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number.
EditText textNewProdPrice = new EditText(this);
textNewProdPrice.setId(1001); //Your any random id
Just declare your EditText final, as suggested:
final EditText textNewProdPrice = new EditText(this);
Doing an ID lookup for the EditText would be a waste since you already have a reference to it.
I have a situation where there are 3 editText fields created dynamically and those values should be stored in vector and should be displayed again after i click on button "ADD" after three fields are added?
Any help will be appreciated, thanks in advance.
You should set OnClickListener
final EditText editText1 = new EditText(context);
final EditText editText2 = new EditText(context);
final EditText editText3 = new EditText(context);
final Vector<String> vector = new Vector<String>();
// Adding editTextes to layout code here....
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vector.add(editText1.getText());
vector.add(editText2.getText());
vector.add(editText3.getText());
}
});
I have a program that has a text input and a button. When I type something into the input and press the button I want that String to be added to a String Arraylist and have that Arraylist displayed in a TextView. Right now I have a method:
public void addString(View view)
{
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
choices.add(message);
}
"edit-choices" is the name of the text input and "choices" is the name of the array list. First of all am I doing this correctly? Second, how to I get the text view to display the contents of "choices". Right now my TextView id is just textView1
Please keep in mind that it is not the best way to show list items in a TextView. You can do this using a ListView. Anyhow, see pseudo code below (didn't test that in Eclipse, however, it should show how it is basically going to work):
public class YourActivity extends Activity {
Vector<String> choices = new Vector<String>();
public void onCreate(Bundle ....) {
(Button) myButton = (Button) findViewById(R.id.button);
myButton.setOnClickListener(new OnClickListener() {
#Override
public boolean button.onClick() {
addString();
TextView textView = (TextView) findViewById(R.id.text_view);
String listRepresentation = "";
for (String choice : choices)
if ("".equals(listRepresentation))
listRepresentation = choice; else
listRepresentation = ", " +choice;
textView.setText(listRepresentation );
}
});
}
public void addString(View view)
{
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
choices.add(message);
}
}
So simply assign an OnClickListener to your button that does what you need.
The question is how you want the Text to be displayed...
Either like a list view or just as a normal text.
If you want to show the text as a normal text in the text view you can simply do something like this.
for(String msg : choices)
{
textView1.setText(textView1.getText()+msg);
}
If you want the choices to be displayed in list view you need to set an adapter to the list view using the choices that you have.
First of all am I doing this correctly?
If it works for you, sure. I would maybe cache the EditText so you don't have to "find" it every time you want to access it's content.
Your only "problem" here is, that a TextView has no method that accepts a List<String>. So, you'll need to make a single string out of your list of strings.
You can simply iterate over the list and con-cat them together:
StringBuilder b = new StringBuilder();
for (String s : choices){
b.append(s+"\n");
}
textview.setText(b.toString());
This will simply build one string from all the items in your list, adding line-breaks after every item.
You'll need to set your TextView's android:inputType-attribute to textMultiLine, so it will actually show you multiple lines.
I want to create a Editbox dynamically when i click a add button and i also want to get the values typed in that Editbox when i click in save button.
Please Help me. Regards. Augustine
Try out this:
LinearLayout mLinearLayout = new LinearLayout(this);
mLinearLayout = (LinearLayout)findViewById(R.id.mylinearlayout);
Button lButton = (Button)findViewById(R.id.mybtnid);
lButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
EditText lEditText = new EditText(this);
lEditText.SetText("Text Here");
mLinearLayout.addView(lEditText);
}
}
To get the values typed into the EditText, you need to additionally set an identifier for the view.
lEditText.setId(2); //you can use any integer ID
Then, you can retrieve the text inside the OnClickListener of the save button as:
EditText lEditText = (EditText)findViewById(2);
String txt = lEditText.getText().toString();
To create an edit text dynamically or programmatically:
EditText ed = new EditText(context);
Set whatever parameters you want to set for this edit text and then add this in your view:
view.addView(ed);
OR
view.addView(ed, layoutParams);
you can create EditText with the following code inside your Activity
EditText _edit = new EditText(this);
Then to add this to you activity layout you have to get the particular layout by it id
For Ex.
LinearLayout linear = (LinearLayout)findViewById(R.id.linear);
then simple add this EditText object to the LinearLauout by using the following code..
linear.addView(_edit);