i am new to Android and i might have a silly / dumb Question. I have got an Activity where i want to dynamically create Multiple Input Fields. The amount of Input Fields is defined by the User.
Because the Input is Styled and Consists of 2 Elements and didnt want to Create these Elements every Time because of Elements have got multiple Parameters that are the same every time. Thats why i created a XML File just for this two Elements which i would like to use in my Programm to Create the Inputs.
View_input.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayoutTableName"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="10dp"
android:hint="@string/create_table_name"
android:inputType="text"
app:boxStrokeColor="@color/colorAccent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:placeholderText="@string/create_table_table_name_placeholder"
app:startIconTintMode="src_in">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/textInputName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:text="" />
</com.google.android.material.textfield.TextInputLayout>
</merge>
My Target XML (activity_create_table_column_names.xml) looks like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.createtable.CreateTableColumnNamesActivity">
<LinearLayout
android:id="@+id/columnNameInputContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="start"
android:orientation="vertical"
android:padding="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/create_table_column_names"
android:textStyle="bold" />
<!-- HERE -->
</LinearLayout>
Where i wrote <!-- HERE -->
i want all my Input-Fields to be.
I started with just displaying one Input:
private fun initLayout() {
val container = findViewById<LinearLayout>(R.id.columnNameInputContainer)
val inflater = applicationContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view = inflater.inflate(R.layout.view_input, LinearLayout(this))
container.addView(view)
}
But i got the Following Exception:
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243)
at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:217)
at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:145)
at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:115)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:458)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:417)
My Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
...
</style>
</resources>
What am i doing wrong? Is this even the correct way i should programm something like this?
Edit: Android.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="de.hsos.ma.adhocdb">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:appComponentFactory="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:appComponentFactory">
<activity
android:name=".ui.createtable.CreateTableColumnNamesActivity"
android:parentActivityName=".ui.homescreen.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ui.createtable.CreateTableActivity"
android:parentActivityName=".ui.homescreen.MainActivity" />
<activity
android:name=".ui.homescreen.TestTableActivity"
android:parentActivityName=".ui.homescreen.MainActivity">
<!--
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
-->
</activity>
<activity
android:name=".ui.TableShow.TableShowActivity"
android:parentActivityName=".ui.homescreen.MainActivity" />
<activity android:name=".ui.tablelist.LayoutTableListItem" />
<activity android:name=".ui.homescreen.MainActivity">
</activity>
</application>
</manifest>
found the solution ,get your inflator from activity not application just modify your initLayout()
like this
// here is the fix
val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With