How to hide a dynamically created EditText box in android? - 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.

Related

setting edit text visible each time button is pressed

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

create edittext and radiobutton dynamically

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

android working with Views

In my activity I have the following views
TextView player1;
TextView player2;
TextView player3;
TextView player4;
EditText player1name;
EditText player2name;
EditText player3name;
EditText player4name;
Each of the TextView's has the onclick listener applied to it. and so fires the OnClick function.
When we get to the onClick this is what i am currently doing:
#Override
public void onClick(View v) {
//the v variable is the clicked textview, in this case "player1"
//hide the textview and show the resultant edittext
v.setVisibility(View.GONE);
player1name.setVisibility(View.VISIBLE);
//set focus on edit text and when focus is lost hide it and set the textview text
player1name.requestFocus();
imm.showSoftInput(player1name, InputMethodManager.SHOW_FORCED);
player1name.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View y, boolean x) {
v.setVisibility(View.VISIBLE);
player1name.setVisibility(View.GONE);
imm.hideSoftInputFromWindow(player1name.getWindowToken(), 0);
String name = player1name.getText().toString();
if (name.equals("")) {
v.setText("Player Name1");
} else {
v.setText(name);
}
}
});
}
However with this solution I will need to duplicate this code and change the view names for player2 - player2name, player3 - player3name etc
i can obviously grab the clicked TextView via v, however what i cant seem to do is grab its corresponding EditText.
i had thought of doing this:
View test = v + "name";
//then i replace all references to player1name with the test variable
but it doesnt work it wants me to convert View test; into a string
any suggestions?
EDIT: made it easier to understand my question
View test = v + "name";
will give a compile error. Because "v" is not a string type. and also even if it was String, test is not. This line is pretty wrong.
There a few options to achieve what you want,
You can use hashmap
Declare a global field for hashmap
private final HashMap<Integer,EditText> map = new HashMap<Integer,EditText>();
and in onCreate method put your textview id as key, and put your edittext variables in value.
player1name = (EditText) findViewById(R.id.player1name);
map.put(R.id.textView1, player1name);
// for the rest
in onClick method
EditText e = map.get(v.getId());
Then replace them with "e"
e.requestFocus(); //example
Will you please state your problem clearly? Currently, your language is very ambiguous and I can not figure out, exactly what are you looking for. It will help us to know your problem and in turn solve it.

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