Thursday, November 20, 2014

Dynamic Controls Creation in Android


DynamicFieldsActivity.java

import android.animation.LayoutTransition;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
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);


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



dynamicfields.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="10dp"
android:padding="5dp" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Add" />

<EditText
android:id="@+id/textin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/add"
android:inputType="number" />
</RelativeLayout>

<Button
android:id="@+id/showall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Data in Fieldsl" />

<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>

</LinearLayout>


dynamicrow.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal"
android:padding="3dp"
>

<EditText
android:id="@+id/etname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="Name"
android:inputType="textPersonName" >

<requestFocus />
</EditText>

<EditText
android:id="@+id/etAge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Age"
android:inputType="number" >
</EditText>

<RadioGroup
android:id="@+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5dp">

<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="M"
/>

<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="F"
/>
</RadioGroup>

</LinearLayout>





ScreenShots






No comments:

Post a Comment