Getting text from EditText without using XML id - android

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.

Related

Assign ID to added EditText Android

Hy, I wanna ask something about dynamic EditText that's added by button click.
I have another scanner Button to scan qr code, get the value and set the value in each EditText added. To set the text I sure need to know the id of each EditText. So how do I assign id to each EditText or there's another way to work around with it.
Inside my add-button onclick
public void addView(View view){
LinearLayout li = (LinearLayout) findViewById(R.id.etMsisdn);
edt = new EditText(this);
edt.setId(0);
li.addView(edt);
}
Thank you very much
Hope to helpful
String strname = "task" + Integer.toString(i);
EditText editText = new EditText(this);
editTextsMap.put(strname, editText);
You can set a tag for your dynamically created EditText with calling setTag(Object tag) method. Then, you can simply find it with calling findViewWithTag() method.
// Dynamically create EditText
EditText editText = new EditText(this);
editText.setTag("editText");
// Find it via tag
EditText editText = (EditText) findViewWithTag("editText");

How to store a value from a vector and then display it on an editText in android?

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());
}
});

How to reference object when add view by programatically?

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>);
}
});

How to find out if an EditText has been created?

In a dialog layout, I create a second EditText programmatically depending on user choices. When I want to return the inputs, I need to know if a second EditText has been created or not. And I don't understand how to make this check. My statement if (edittextTwo != null) is always null, even when the second EditText has been displayed and entered text into.
Here are the methods which create the second EditText and return their inputs:
if (edittextTwo != null)
private void displayASecondEdittext(String title) {
ViewGroup layout = (ViewGroup) findViewById(R.id.layout_editdialog);
TextView titleTwo = new TextView(this);
titleTwo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
titleTwo.setText(title);
layout.addView(titleTwo);
EditText edittextTwo = new EditText(this);
edittextTwo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.addView(edittextTwo);
}//END displayASecondInputfield
private void returnResult() {
Intent resultIntent = new Intent(this, EditDialog.class);
resultIntent.putExtra(EDITONE, edittextOne.getText().toString());
if (edittextTwo != null) {
resultIntent.putExtra(EDITTWO, edittextTwo.getText().toString());
Log.v(TAG, "edittextTwo ="+edittextTwo.getText().toString());
}
setResult(Activity.RESULT_OK, resultIntent);
finish();
}//END returnResult
move EditText edittextTwo; out side of your method. put it here:
public class myActivity extends Activity
EditText edittextTwo = null;
And then, in your DisplayASecondEditText(), do this instead of what you have:
edittextTwo = new EditText(this);
This will make your declaration global, and allow all all methods to access the variable.
I don't understand how your code above even compiles, since edittextTwo is a local variable in displayASecondEdittext. Make it a member variable in your activity and it should work.

dynamic create editbox in android?

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);

Categories

Resources