Tuesday, December 9, 2014

Dynamic Controls Creation in Android with validation of Child Views



Previous post with out validation

http://android-pratap.blogspot.in/2014/11/dynamic-controls-creation-in-android.html



DynamicFieldsActivity.java


package com.pratap.allsampleprojects;

import android.animation.LayoutTransition;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class DynamicFieldsActivity extends Activity {

EditText textIn;
Button buttonAdd;
LinearLayout container;
Button buttonShowAll;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamicfields);
textIn = (EditText) findViewById(R.id.textin);
buttonAdd = (Button) findViewById(R.id.add);
container = (LinearLayout) findViewById(R.id.container);

buttonAdd.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

int val = Integer.parseInt(textIn.getText().toString());

for (int c = 1; c <= val; c++) {

LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(
R.layout.dynamicrow, null);

container.addView(addView, 0);

}

}
});

LayoutTransition transition = new LayoutTransition();
container.setLayoutTransition(transition);

buttonShowAll = (Button) findViewById(R.id.showall);
buttonShowAll.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

String showallPrompt = "";

int childCount = container.getChildCount();
showallPrompt += "childCount: " + childCount + "\n\n";

for (int c = 0; c < childCount; c++) {
View childView = container.getChildAt(c);

final EditText etText = (EditText) (childView
.findViewById(R.id.etname));

EditText etAge = (EditText) (childView
.findViewById(R.id.etAge));

RadioGroup radioSexGroup = (RadioGroup) childView
.findViewById(R.id.radioSex);

int selectedId = radioSexGroup.getCheckedRadioButtonId();

RadioButton radioSexButton = (RadioButton) childView
.findViewById(selectedId);

String name = (String) (etText.getText().toString());

String age = (String) (etAge.getText().toString());

String gender = (String) (radioSexButton.getText()
.toString());

etText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable edt) {
if (etText.getText().length() > 0) {
etText.setError(null);
}
}

@Override
public void beforeTextChanged(CharSequence s,
int start, int count, int after) {
// TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub

}
});

if (etText.getText().toString().length() == 0) {

etText.setError("Enter Name");

} else {

}

if (age.equals("")) {

etAge.setError("Enter age");

}

if (gender.equals("")) {

radioSexButton.setError("Enter Gender");

}

String childTextViewText = "";

childTextViewText += (String) (etText.getText().toString());

childTextViewText += (String) (etAge.getText().toString());

childTextViewText += (String) (radioSexButton.getText()
.toString());

showallPrompt += c + ": " + childTextViewText + "\n";
}

Toast.makeText(DynamicFieldsActivity.this, showallPrompt,
Toast.LENGTH_LONG).show();
}
});
}
}


No comments:

Post a Comment