Inflate ile Dynamic Form

Onur Sınırtaş
Etiya
Published in
3 min readAug 24, 2017

--

Inflate ile Dynamic Form

Merhaba Arkadaşlar ,

Bu yazımda inflate ile dynamic form oluşturmayı anlatacağım.

İlk olarak şöyle yapalım. İnflate edilecek dosyaları oluşturalım.

Texview oluşturmak için dosya adı :

create_textview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/form_background"
android:orientation="vertical">

<TextView
android:id="@+id/editTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>

Edittext oluşturmak için dosya adı :

create_edittext.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/form_background"
android:orientation="vertical">

<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/TextLabel">

<EditText
android:id="@+id/editTextStr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:maxLines="1"
android:singleLine="true"
android:textSize="15sp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>

Datepicker oluşturmak için dosya adı :

create_datepicker.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/form_background"
android:orientation="vertical">

<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/TextLabel">

<EditText
android:id="@+id/editTextDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:focusable="false"
android:maxLines="1"
android:singleLine="true"
android:textSize="15sp" />

</android.support.design.widget.TextInputLayout>

</LinearLayout>

Spinner oluşturmak için dosya adı :

create_spinner.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/form_background"
android:orientation="vertical">

<fr.ganfra.materialspinner.MaterialSpinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp" />
</LinearLayout>

Button oluşturmak için dosya adı :

create_button.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/form_background"
android:orientation="vertical">

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>

Inflate edilecek dosyalarımızı hazırladıktan sonra java dosyamızı ve xml dosyasını hazırlıyoruz. Java dosyası için CreateDynamic adı verildi ve xml de activity_create_dynamic oluşturuldu.

activity_create_dynamic.xml dosyası aşağıdaki kodlardan oluşur.

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.test.onursinirtas.dynamic_form.CreateDynamic">

<ScrollView
android:id="@+id/scrollCreate"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:id="@+id/lyCreateForm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">

</LinearLayout>
</LinearLayout>
</ScrollView>

</LinearLayout>

Xml dosyasını oluşturduktan sonra java dosyası için kodlamamız gereken kodları yazmaya başlayalım.

İlk olarak ekrandaki dinamik oluşturacağız sayısını belirlemek ve onları sırayla ekrana basmak için Integer tipinde viewCount diye değişken oluşturduk.İlk değeri 0 veriyoruz. Sonra List tipinde View oluşturup bunun adına itemsView adını verdik. Inflate edilenleri ekranda göstermek için kullandık.

Integer viewCount = 0;List<View> itemsViev = new ArrayList<>();

activity_create_dynamic.xml dosyası içinde bulunan LinearLayout buluyoruz.

LayoutInflater layoutInflater = getLayoutInflater();
LinearLayout lyCharInflate = (LinearLayout) findViewById(R.id.lyCreateForm);
lyCharInflate.removeAllViews();

LinearLayout bulduktan sonra sırayla inflate için hazırlanan dosyaları ekleyebiliriz.

TextView

itemsView.add(layoutInflater.inflate(create_textview, lyCharInflate, false)); // itemsView sırayla inflate dosyasını ekleme işlemi yapıyoruz.
//İnflate edilen dosyanın içindeki Text View bulup setText özelliğine değerini veriyoruz.
final TextView editTextViewStr = (TextView) itemsView.get(viewCount).findViewById(R.id.editTextView);
editTextViewStr.setText(getResources().getString(R.string.textView));
lyCharInflate.addView(itemsView.get(viewCount)); //Burda da itemsView ekleme yapmıştır.Şimdi lyCharInflate addView ekranda gösterilmesini sağlıyoruz.
viewCount++;

EditText

itemsView.add(layoutInflater.inflate(create_edittext, lyCharInflate, false));
final EditText editTextStr = (EditText) itemsView.get(viewCount).findViewById(R.id.editTextStr);
final TextInputLayout textInputLayout = (TextInputLayout) itemsView.get(viewCount).findViewById(R.id.textInputLayout);
textInputLayout.setHint(getResources().getString(R.string.editTextInput));
editTextStr.setText(getResources().getString(R.string.editText));
lyCharInflate.addView(itemsView.get(viewCount));
viewCount++;

Spinner

itemsView.add(layoutInflater.inflate(create_spinner, lyCharInflate, false));
final MaterialSpinner materialSpinner = (MaterialSpinner) itemsView.get(viewCount).findViewById(R.id.spinner);
materialSpinner.setHint(getResources().getString(R.string.materialSpinner));

ArrayList<String> stringList = new ArrayList<>();
stringList.add("Türkiye");
stringList.add("Almanya");
stringList.add("Avusturya");

ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, stringList);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
materialSpinner.setAdapter(spinnerAdapter);


lyCharInflate.addView(itemsView.get(viewCount));
viewCount++;

DatePicker

itemsView.add(layoutInflater.inflate(create_datepicker, lyCharInflate, false));
final EditText editTextDate = (EditText) itemsView.get(viewCount).findViewById(R.id.editTextDate);
final TextInputLayout textInputLayoutDate = (TextInputLayout) itemsView.get(viewCount).findViewById(R.id.textInputLayoutDate);
textInputLayoutDate.setHint(getResources().getString(R.string.textInputLayoutDate));
final Calendar myCalendar = Calendar.getInstance();


final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String myFormat = "dd/MM/yyyy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

editTextDate.setText(sdf.format(myCalendar.getTime()));
}

};

editTextDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(context, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});

lyCharInflate.addView(itemsView.get(viewCount));
viewCount++;

Button

itemsView.add(layoutInflater.inflate(create_button, lyCharInflate, false));
final Button button = (Button) itemsView.get(viewCount).findViewById(R.id.button);
button.setText(getString(R.string.button));
lyCharInflate.addView(itemsView.get(viewCount));
viewCount++;

Uygulamanın örnek kodlarına Onur Sınırtaş github hesabından erişebilirsiniz.

Yararlı olması dileğiyle…

--

--