I have one Button, and on every click of that button I want to create new EditText Dynamically.
Now my question is , I have one style in style.xml for EditText. Now how can I apply that style to this dynamically created EditText?
My Code for EditText is:
List<EditText> allEdsLabReport ;
String[] edsLabReport;
EditText LabReportNm;
AddMoreLabReport.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
LabReportNm = new EditText(CaseReport.this);
allEdsLabReport.add(LabReportNm);
TableLayout tbl2 = (TableLayout)findViewById(R.id.TableLayoutLabReport);
TableRow tr1 = new TableRow(CaseReport.this);
tr1.addView(LabReportNm);
tbl2.addView(tr1);
}
});
new EditText(new ContextThemeWrapper(CaseReport.this, R.style.my_style));
LabReportNm.setTextAppearance(getApplicationContext(), R.style.style.xml)
editText.setTextAppearance(getApplicationContext(), R.drawable.demo.xml);
Related
Actually I want that each time I press the button 2 edit text is set visible,and same thing should happen each time the button is pressed.
Basically whenever user presses the button 2 edittext should appear(any loop concept?)
Please suggest.ThankYou :)
Just add a click listener to the button, and change the visibility of editText into it :
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editTextName.setVisible(true);
}
});
Not sure if i got your question right but you can use android:setvisibility=gone in xml editext fields and then in your button onclick use
edittext.setVisibility(View.VISIBLE);
to make the edittext fields visible.
Just set the initial visibility in the code or in the xml to GONE
and then add onClickListener
android:visibility="gone"
//or
btn2.setVisibility(View.GONE)
btn2.setOnClickListener(new View.OnClickListener(){
e#Override
public void onClick(View view) {
editText.setVisible(true);
}
});
You want to populate an edittext each time a button is pressed ?
Create a layout for your edittext:
public EditText createEditText() {
final LayoutParams lparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
final EditText edittext = new EditText(this); Editext.setLayoutParams(lparams);
return edittext; }
And then add edittexts to your layout:
rl.addView(createEditText());
If I understand correctly, you want to set edit text to visible on the press of a button. This can be done by following steps:
In your Main Class:
Create 2 new EditText variable:
EditText myEditText1;
EditText myEditText2;
Create a new method to be called on button click:
void buttonClick(View view){
//Get References
myEditText1 = (EditText) findViewById(R.id.first_edit_text);
myEditText2 = (EditText) findViewById(R.id.second_edit_text);
//Set visible
myEditText1.setVisibility(View.VISIBLE);
myEditText2.setVisibility(View.VISIBLE);
//Set edit texts to empty string to reset ("Recreation")
myEditText1.setText("");
myEditText2.setText("");
}
In your xml:
Add the onClick attribute to your Button:
android:onClick="buttonClick";
Add the id to your EditTexts:
android:id="#+id/first_edit_text"
android:id="#+id/second_edit_text"
Now, Whenever the button is pressed, the Edit Text becomes visible, no loop is required. And if you also want to be hidden before pressing button, add:
android:visibility="invisible"
Sources: setVisibility, onClick
i have a problem with my application. Basically what it does, is when i click a button, it creates 3 fields (2 editText and 1 spinner) on a scrollView. The thing works well, the only problem that im having, is related with the style, the activity bgColor is white(as the rest of the app) but, when i create elements programmatically, these elements doesnt have the look of the rest of my app. The editTexts are white with white letters (impossible to read since my bgColor is white as well) and its the same thing with the spinner. What can i do? Here is a snipet of code so you can see what im doing here.
public class AddIngredients extends Activity {
public int Count = 0;
public String[] spinnerArray = {"Gr", "kg", "Cups", "ml", "L", "oz"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addingredients);
final TableLayout lm = (TableLayout) findViewById(R.id.TableMain);
TableLayout.LayoutParams params = new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button addMore = (Button)findViewById(R.id.addmore);
addMore.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TableRow ll = new TableRow(getApplicationContext());
//ll.setOrientation(LinearLayout.HORIZONTAL);
EditText product = new EditText(getApplicationContext());
product.setHint(" Ingredient "+Count +" ");
// Create Button
EditText amount = new EditText(getApplicationContext());
// Give button an ID
amount.setId(Count);
amount.setHint("Quantity");
final Button btn2 = new Button(getApplicationContext());
btn2.setId(Count);
btn2.setText("Remove " + Count);
Spinner spinner = new Spinner(getApplicationContext());
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
ll.addView(product);
ll.addView(amount);
ll.addView(spinner);
lm.addView(ll);
Count = Count + 1;
I know my XML is working well because if i create the 3 views on my xml, they look great. PD: Thx in advance for any help! Greetings.
you can use
amount.setTextColor(Color.BLACK);
to set colour of text to black or any other colour
same can be used for spinner
Here's how I set the colors of my edit text lines and other theme-related android views.
http://android-holo-colors.com/
I just picked the color and views I wanted, then unzipped them in my res folder, then set the theme according the android tutorials.
I recommend backing up your res folder first, in case you don't like the results.
Garret
I had a error in my code. When creating the fields, i was using
(getApplicationContext());. I fixed it using MyApplicationName.this.
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 want to ask how to make a list of text that we can tap in each of the text and then get the selected text to editText.
I just added the screenshot
http://i.stack.imgur.com/ddZSg.png
I have been searching it since yesterday, but I can not find exact solution. I also tried with listview, but I don't know how it's possible with horizontal, and flow list item.
I am also new in android. But I can think of the logic what you want. You can try this out if you want.
First of all, you can make your list of texts EditText by list of buttons Buttons
You can dynamically add as many Buttons with their respective text as you want to show.
set their corresponding onClickListener
In their onClickListener , create an object of the EditText you are using to add texts.
Store the value of EditText into a String Variable first.
Add the text of the clicked Button to the variable.
and now again set the text in EditText with the variable you created to store the values.
Your task will be done.
Try referring this code.
and change the code accordingly as your needs.
// Adding EditText and a button in a new linear layout and then adding
// the new linearLayout to the main layout
String[] valuesToBeAdded={"A","B","C","D"};
String selectedValues=null;
LinearLayout mainLayout=(LinearLayout) findViewById(R.id.mainLayout);
LinearLayout localLayout = new LinearLayout(context);
localLayout.setOrientation(LinearLayout.VERTICAL);
localLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
EditText editText=new EditText(context);
editText.setText(selectedValues);
editText.setId(5000);
localLayout.addView(editText);
for(int i=0;i<valuesToBeAdded.length();i++){
Button button = new Button(context);
button.setText(R.string.scanDocument);
button.setId(i);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText ed=(EditText) findViewById(5000);
selectedValues=ed.getText();
selectedValues=selectedValues +" " + this.getText();
ed.setText(selectedValues);
}
});
localLayout.addView(button);
}
mainLayout.addView(localLayout);
Thank You
Could you not make as many buttons as you need and then in the button_Click method for all buttons add:
Buttonwithtext_Click(object sender, EventArgs e)
{
editTextBox.Text = editTextBox.Text + Buttonwithtext.text + ", ":
}
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);