create edittext and radiobutton dynamically - android

How can i create edittext and radiobutton dynamically so that either
i can write in edittext or can click the radiobutton but i should
not do the both, and then what ever i have selected or written the answer
i need to save.
i search for it but i did not get any appropriate answer , Please help me !
RadioButton toggleRadioButton;
RadioGroup editRadiogroup = new RadioGroup(this);
editRadiogroup.setOrientation(RadioGroup.HORIZONTAL);
LinearLayout editlayout=new LinearLayout(this);
editlayout.setOrientation(LinearLayout.HORIZONTAL);
try
{
for (int i = 0; i < answer.length; i++)
{
int id = Integer.parseInt(5+ "" + i);
// add edit text
EditText editText;
editText = new EditText(this);
editText.setMaxLines(1);
editText.setGravity(Gravity.LEFT);
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.setId(id);
editlayout.addView(editText, WrapWidth, WrapHeight);
TextView tv = new TextView(this);
tv.setTextColor(Color.BLACK);
tv.setTextSize(18);
tv.setText("Year" +" " +"(or)"+" ");
editlayout.addView(tv);
// add radio button
toggleRadioButton = new RadioButton(this);
toggleRadioButton.setChecked(false);
toggleRadioButton.setTextSize(15);
toggleRadioButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
editRadiogroup.addView(toggleRadioButton,WrapWidth1,WrapHeight);

Hi you can listen on changes on the Edit text:
How do listen EditText?
When you get text written in the edit text you can make the toggleRadiobutton dissapear with toggleRadiobutton.setVisibilty(View.INVISIBLE) or you disable it with toggleRadiobutton.setEnabled(false).
The same if you detect that there is no text in the edittext, you can re-enable the togglebutton.
With the toggleRadioButton you would take the same approach. You check the status of button in on OnClickListener and enable or disable the Edittext accordingly:
toggleRadioButton.setOnClickListener(new OnClickListener(View v){
if(((RadioButton)v).isChecked()){
edittext.setEnabled(false);
}else{
edittext.setEnabled(true);
}
});

Related

How can i set text to Multiple edit text in order of position in the Layout

I'm learning android , and now i want to know how to set text to multiple edittext by button click in order of position in the layout.So in i have buttons which set text to the edit text area in order of position . but how to do this in onclicklistener button's method. Or making the edittext requests the focus if previous one already contains text .
Hope this helps...
Implement a TextWatcher and add textwatcher to all of the EditText, use this inside the afterTextChanged method
for (int i=1; i<=7; i++){
for (int j = 0; j< linearLayout.getChildCount(); j++) {
if(linearLayout.getChildAt(j) instanceof EditText) {
EditText editText = (EditText) linearLayout.getChildAt(j);
EditText editText2 = (EditText) linearLayout.getChildAt(i);
if(editText.getId() == editText2.getId()) {
if(!editText.getText().toString().isEmpty()) {
//DO YOUR STUFF
}
}
}
}

Android get element group by id then select by tag

I have a table view in which i populate rows via xml like this
TableLayout table= (TableLayout) findViewById(R.id.tblLayoutPop);
for(int i=0;i<3;i++){
TableRow row = (TableRow) LayoutInflater.from(Popup.this).inflate(R.layout.row, null);
row.setTag(i);
((TextView)row.findViewById(R.id.header)).setText("Some Text");
EditText ed= (EditText) row.findViewById(R.id.editText);
ed.setTag(i);
Button add= (Button) row.findViewById(R.id.buttonAdd);
add.setTag(i);
Button remove= (Button) row.findViewById(R.id.buttonRemove);
remove.setTag(i);
table.addView(row);
}
And xml is here looks like
There are 3 such rows , now on click of add button i want to increment its particular edit text
For this i tried this , to select edit text first then get it by tag
public void add(View v){
String tag = v.getTag().toString();
Log.d("TAG", tag);
int position = Integer.parseInt(tag);
View eds= findViewById(R.id.editText);
EditText ed= (EditText) eds.findViewWithTag(position);
ed.setText(tag);
}
this doesnt work And I need a way to select corresponding edit text. How to do this
Any help is really appreciated
Thanks
Had a look at ur xml file
First thing change your edit text's tag to something different from buttons
like ed.setTag("ed"+i);
Then u can select edit text via tag from parent view of button. As u have already grouped buttons and edit text in linear layout.
Like this
EditText ed= (EditText) ((View) v.getParent()).findViewWithTag("ed"+tag);
This is untested but it may point you to the right direction
implement View.OnClickListener
in your loop set click event to your Button Views
for(int i=0;i<3;i++){
TableRow row = (TableRow) LayoutInflater.from(Popup.this).inflate(R.layout.row, null);
row.setTag(i);
((TextView)row.findViewById(R.id.header)).setText("Some Text");
EditText ed= (EditText) row.findViewById(R.id.editText);
ed.setTag(i);
Button add= (Button) row.findViewById(R.id.buttonAdd);
add.setTag(i);
add.setOnClickListener(this)
Button remove= (Button) row.findViewById(R.id.buttonRemove);
remove.setTag(i);
remove.setOnClickListener(this);
table.addView(row);
}
then in your onClick(View view) method, try this
#Override
public void onClick(View view) {
int viewId = view.getId();
if(viewId == R.id.buttonAdd && view.getTag().toString().equals("0")){
View parentView = view.getRootView();
EditText mEditText = (EditText)parentView.findViewWithTag("0");
int currentEditTextValue = Integer.parseInt(mEditText.getText().toString());
currentEditTextValue = currentEditTextValue + 1;
mEditText.setText(String.valueOf(currentEditTextValue));
}
// more conditions here ...
}
Hope it gives you more ideas. Do ask question if you have any.

How to hide a dynamically created EditText box in android?

I want to make an attendance plus tasking system. For this, I have used check-boxes and a linear layout. Linear layout is for adding edit-text box dynamically. When a check-box is checked, a new text field is to be visible and when it is unchecked, the text field has to be hidden. I have used part of code something like this:
public void onCheckBoxClicked(View v) {
boolean checked = ((CheckBox) v).isChecked();
EditText tv;
tv = new EditText(this);
tv.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT));
tv.setText(one.getText().toString()+ "'s task today?");
tv.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
tv.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
switch (v.getId()) {
case R.id.checkBox1:
if(checked) {
list.addView(tv);
Toast.makeText(getApplicationContext(), one.getText().toString() + " is present", Toast.LENGTH_SHORT).show();
}
if(!checked) {
tv.setVisibility(EditText.GONE); //problem exists here--is not executing
Toast.makeText(getApplicationContext(), one.getText().toString() + " is absent", Toast.LENGTH_SHORT).show();
}
break;
Now, I can easily get a edit-text field when I check the box but, on unchecking it, the edit-text field doesn't hide. How can I solve it?
This is because you are creating new EditText each time some checkBox clicked. You are trying to hide EditText which isn't even shown/added yet. Got it!!
Try making your EditText tv = new EditText(this); editText as class variable , create it in onCreate. And then you can hide/show when you need it.
it would look something like below code.
EditText tv;
onCreate(.......){
..............
tv = new EditText(this);
..............
}
and then use this tv in your onCheckBoxClicked method instead of creating one in this method.

List Of Text And Clickable

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 + ", ":
}

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