how to get multiple edittext value from child view - android

I have one main layout with one LinearLayout. In this LinearLayout I inflates xml layouts that contain a form with multiple edittext fields(4 EditText) in it. every time i click on add button xml layouts that contain a form with multiple edittext fields(4 EditText) add in linearlayout.
On first attempt I show only one form. There is button for adding more forms. Suppose If user clicks on "Add" button two times then user have three total forms. I successfully get all these three forms.
i am set Tag child view so easy to get one bye one edittext value
but my problem is when i click on save button all edittext data
get and add in array ..
Button buttonAdd = (Button) findViewById(R.id.pdBtnAddDoc);
buttonAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myRoot = (LinearLayout) findViewById(R.id.linearLayoutAdd);
LayoutInflater inflater = (LayoutInflater)PrimaryDoctorFragment.this.getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
childView = inflater.inflate(R.layout.fragment_primary_doc_add,myRoot);
// childView.setId(tag++);
childView.setTag(tag++);
}
});
here is my save button:-
btnSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for (int i = 0; i < tag; i++) {
System.out.println("i:- " + i);
View aView = myRoot.findViewById(i);
EditText name = (EditText) aView
.findViewById(R.id.childEtName);
Log.d("Gettext :- ", name.getText().toString());
}
}
});
on save button all four text field data get and add in array..

Related

how to create loops in xml file in android

I'm new to Android Programming.
I want to implement p: data table tag functionality as of prime faces in android.
I want a table layout in XML through which I can iterate over a list of values from bean along with a checkbox for each row.
Using checkbox listener I want that particular row to be editable and vice versa. and that row should contain text views which allow the user to edit the text and also add row functionality which add an empty row to table to allow a user to enter values and save it.
can anyone suggest how can I proceed?
Thank you.
You have to manage all this in your Java class.
Here is a triggerless example for dynamically added checkboxes.
for(int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText("I'm dynamic!");
ll.addView(cb);
}
Here is a simple example. Simply put the event listener on the desired checkbox and do the processing according to what you want.
public class MainActivity extends AppCompatActivity {
private CheckBox checkBox1;
private TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv_1);
tv1.setText("This is an example");
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(checkBox1.isChecked()){
tv1.setText("Hi i am check");
tv1.setTextColor(Color.RED);
}else {
tv1.setText("Hi i am not checked");
tv1.setTextColor(Color.BLUE);
}
}
});
}
}

Enabling/Disabling all buttons in a Grid Layout

I am trying to create a word game, where all the letters in a word will be added to a grid layout and presented to the user. The user then has to form the word by clicking the letter buttons. The buttons will be disabled once pressed. If the user decides to reset the game, the same grid layout has to be presented again to the user with all the buttons enabled. Is there any function that can be used to set all the buttons as Clickable? Below is the code, I used.
for(final String letter : shuffled_word){
final Button button = new Button(this);
button.setText(letter);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button.setClickable(false);
}
});
this.game_grid.addView(button);
}
int id = getResources().getIdentifier("button"+i, "id", getActivity().getPackageName());
Button b=(Button) view.findViewById(id);
and then b.setClickable(falsE)

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 get "View" clicked in ListView not "Complete Row" :: android

We make listener for Items(Rows) click listener in ListView. How to get know, in a particular row a specific View get clicked?
R1 --> TextView1 | TextView2
R2 --> TextView1 | TextView2
I can get which row is clicked. but i also want to know which View of the row get clicked.
You need to set position as tag to textView, and then onClick you can receive its tag.
So basicly you set textView's staff on adapter's getView method
textView.setTag(position);
textView.setOnClickListener(listener);
And then you'll have click listener like this
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
int position = Integer.parseInt(v.getTag().toString());
// Do whatever you like...
}
};
You can add specific click handlers to the textViews:
<TextView
android:onClick="clickHandler">
Then in your activity:
public void clickHandler(View v) {
// obtain parent row
LinearLayout parentView = (LinearLayout) v.getParent();
// with the parentView you can get all other views as well:
TextView textView1 = (TextView) parentRow.getChildAt(0);
}
I do it like this:
assign onClickListener to whatever can be clicked
I assign tag to it to identify v.setTag(R.id.itemId, item.id);
In onclick I do:
#Override
public void onClick(View v) {
final int id = (Integer) v.getTag(R.id.itemId);
}

How to remove a Button added programatically

I am adding buttons to a linear layout at runtime and need to add the functionality of removing them if the user desires. At the moment I have a button which opens a popup with a list consisting of the text for each button added. If possible, would I be able to have each onItemClick delete the corresponding button? If not, what would be the best way to remove a specific button?
Here is the code for adding buttons:
private void addButton(){
LinearLayout lL = (LinearLayout) findViewById(R.id.requirement_linear);
lL.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
p.setMargins(0,2,0,0);
Button b = new Button(this);
b.setBackgroundResource(R.drawable.blue_button);
b.setOnClickListener(openRequirement);
b.setTextColor(Color.parseColor("#FFFFFF"));
String button_text = (index + 2) + ". " + requirement_list.get(index + 1).getName();
b.setText(button_text);
requirements_text.add(button_text);// requirements_text is an arraylist<string> which stores the text so I can display them in my popup to delete them.
index ++;
lL.addView(b,p);
}
You can use removeView() on your LinearLayout and pass your button. You can identify the button on the OnClick(View view) callback, the view there is the button.
as requested, here is an example.
final LinearLayout lL = (LinearLayout) findViewById(R.id.requirement_linear);
Button b = new Button(this);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View button) {
lL.removeView(button);
}
});
lL.addView(b);
Alternatively you can use removeViewAt() to remove a child view by index. You can use the index of your 'list of text of buttons'. Assuming its a listview, you can try this.
lview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
lL.removeViewAt(position);
}
});
here is how i hide and show the buttons
Button b = new Button(this);
b.setVisibility(View.GONE); // this will hide the button and it will now show
b.setVisibility(View.VISIBLE); // this will show the button if it was hidden before
Enjoy!

Categories

Resources