Skip to content

Instantly share code, notes, and snippets.

@bratawisnu
Last active January 13, 2021 09:44
Show Gist options
  • Save bratawisnu/0bcf88f76f49b769433ee82fa5fe6407 to your computer and use it in GitHub Desktop.
Save bratawisnu/0bcf88f76f49b769433ee82fa5fe6407 to your computer and use it in GitHub Desktop.
Dynamically Add and Remove Views in Android including dialog form and retrieve data
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fancy="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay">
<mehdi.sakout.fancybuttons.FancyButton
android:id="@+id/btn_back"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:paddingLeft="0dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
fancy:fb_borderColor="@color/transparent"
fancy:fb_borderWidth="1dp"
fancy:fb_defaultColor="@color/transparent"
fancy:fb_focusColor="@color/transparent"
fancy:fb_fontIconResource="&#xf060;"
fancy:fb_fontIconSize="18sp"
fancy:fb_iconPosition="left"
fancy:fb_text=""
fancy:fb_textColor="@color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SURVEY"
android:textColor="@color/white"
android:textSize="22sp"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:textStyle="normal" />
<ImageView
android:visibility="gone"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginRight="0dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:src="@drawable/logo" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_below="@id/appbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginBottom="5dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Identifikasi Responden" />
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_responden"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:paddingBottom="5dp"
android:visibility="visible"
app:hintTextAppearance="@style/TextLabel">
<EditText
android:id="@+id/et_responden"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukkan Nama Responden"
android:inputType="textEmailAddress"
android:singleLine="true"
android:textColor="@color/textColor" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginBottom="5dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Identifikasi Usia" />
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_usia"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:paddingBottom="5dp"
android:visibility="visible"
app:hintTextAppearance="@style/TextLabel">
<EditText
android:id="@+id/et_usia"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Masukkan Usia"
android:inputType="textEmailAddress"
android:singleLine="true"
android:textColor="@color/textColor" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
<mehdi.sakout.fancybuttons.FancyButton
android:id="@+id/btn_continue"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
fancy:fb_borderColor="@color/white"
fancy:fb_borderWidth="0dp"
fancy:fb_defaultColor="@color/colorPrimary"
fancy:fb_focusColor="@color/colorPrimaryDark"
fancy:fb_iconPosition="left"
fancy:fb_radius="0dp"
fancy:fb_text="Selanjutnya"
fancy:fb_textColor="@color/white" />
</RelativeLayout>
public class dinamicFieldActivity extends AppCompatActivity {
Activity activity;
private LinearLayout parentLinearLayout;
String res;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parent_dinamic_field);
activity = this;
parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout);
Button btnSimpan = findViewById(R.id.btnSimpan);
btnSimpan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = parentLinearLayout.getChildCount() - 1;
for (int i = 0; i < count; i++) {
final View row = parentLinearLayout.getChildAt(i);
TextView editTextResult = (TextView) row.findViewById(R.id.strValue);
String result = editTextResult.getText().toString();
Utilities.ShowLog("result field", result);
}
}
});
}
public void onAddField(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}
public void onDelete(View v) {
parentLinearLayout.removeView((View) v.getParent());
}
public void Popup(View v) {
Init(activity, (View) v.getParent());
}
// TODO: POPUP INPUT
public void Init(Context context, final View view) {
Context _context = context;
final Dialog _dialog = new Dialog(_context);
_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
_dialog.setContentView(R.layout.dialogfield);
FancyButton button = (FancyButton) _dialog.findViewById(R.id.btn_continue);
final EditText etResponden = (EditText) _dialog.findViewById(R.id.et_responden);
final EditText etUsia = (EditText) _dialog.findViewById(R.id.et_usia);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
JSONObject jObj = new JSONObject().put("nama", etResponden.getText().toString()).put("usia", etUsia.getText().toString());
String nama = jObj.optString("nama");
res = jObj.toString();
TextView resultJson = view.findViewById(R.id.strValue);
resultJson.setText(jObj.toString());
TextView label = view.findViewById(R.id.label);
label.setText(etResponden.getText().toString());
} catch (JSONException e) {
e.printStackTrace();
}
_dialog.hide();
}
});
_dialog.show();
_dialog.setCancelable(true);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fancy="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/strValue"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:onClick="Popup"
android:textSize="22sp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:gravity="center_vertical"
android:editable="false"
android:clickable="true"
android:inputType="text"/>
<mehdi.sakout.fancybuttons.FancyButton
android:id="@+id/delete_button"
android:onClick="onDelete"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_gravity="center"
android:paddingLeft="0dp"
android:paddingTop="0dp"
android:paddingRight="0dp"
android:paddingBottom="0dp"
fancy:fb_borderColor="@color/white"
fancy:fb_borderWidth="0dp"
fancy:fb_defaultColor="@color/transparent"
fancy:fb_focusColor="@color/transparent"
fancy:fb_fontIconResource="&#xf058;"
fancy:fb_iconPosition="left"
fancy:fb_radius="0dp"
fancy:fb_text=""
fancy:fb_textColor="@color/delete" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fancy="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp">
<Button
android:id="@+id/btnSimpan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Simpan"
android:layout_alignParentBottom="true"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fancy="http://schemas.android.com/apk/res-auto"
android:id="@+id/parent_linear_layout"
android:layout_above="@id/btnSimpan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/strValue"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:onClick="Popup"
android:textSize="22sp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:gravity="center_vertical"
android:editable="false"
android:clickable="true"
android:inputType="text"/>
<mehdi.sakout.fancybuttons.FancyButton
android:id="@+id/delete_button"
android:onClick="onDelete"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_gravity="center"
android:paddingLeft="0dp"
android:paddingTop="0dp"
android:paddingRight="0dp"
android:paddingBottom="0dp"
fancy:fb_borderColor="@color/white"
fancy:fb_borderWidth="0dp"
fancy:fb_defaultColor="@color/transparent"
fancy:fb_focusColor="@color/transparent"
fancy:fb_fontIconResource="&#xf058;"
fancy:fb_iconPosition="left"
fancy:fb_radius="0dp"
fancy:fb_text=""
fancy:fb_textColor="@color/delete" />
</LinearLayout>
<Button
android:id="@+id/add_field_button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#555"
android:onClick="onAddField"
android:paddingLeft="5dp"
android:text="Add Field"
android:textColor="#FFF" />
</LinearLayout>
</RelativeLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment