I'm binding data between android fragment and ViewModel in kotlin. Compiler throws
error: cannot find symbolimport >frydzej.yerbet.databinding.FragmentCollectionBindingImpl
I have tried to change gradle version to 3.2.1 and databinding compiler version to 3.2.1. But with same result.
My Android Studio version is 3.3 RC 3
This is my project gradle buildscript
buildscript {
ext.kotlin_version = '1.3.11'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0-rc03'
classpath "org.jetbrains.kotlin:kotlin-gradle-
plugin:$kotlin_version"
classpath 'android.arch.navigation:navigation-safe-args-gradle-
plugin:1.0.0-alpha09'
}
}
and app gradle
dataBinding{
enabled = true
}
dependencies {
def lifecycle_version = "2.0.0"
def room_version = "2.0.0"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
implementation 'com.google.android.material:material:1.1.0-alpha02'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha09"
implementation "android.arch.navigation:navigation-ui-ktx:1.0.0-alpha09"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation 'com.cuneytayyildiz:onboarder:1.0.3'
implementation 'com.android.support:design:28.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
testImplementation 'junit:junit:4.12'
debugImplementation 'com.amitshekhar.android:debug-db:1.0.4'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
fragment_collection.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="viewModel" type="frydzej.yerbet.viewModels.CollectionViewModel"/>
</data>
<FrameLayout
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".Views.CollectionFragment">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_item_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/ic_add"
android:onClick="#{() -> viewModel.addFabClicked()}"
android:layout_margin="16dp"
app:fabSize="normal"/>
</FrameLayout>
</layout>
and the Fragment view code:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding: FragmentCollectionBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_collection, container, false)
val view: View = binding.root
val viewModel: CollectionViewModel = ViewModelProviders.of(this).get(CollectionViewModel::class.java)
binding.viewModel = viewModel
return view
}
In MainActivity databinding there is either
ActivityMainBinding
and
ActivityMainBindingImpl,
but in CollectionFragment only
FragmentCollectionBinding
is created
I had the same issue:
The solution eventually was that I misspelled one of the values in my fragments XML file.
Take a look at the android:onclick parameter with an additional () I mistakenly added
WRONG
<Button
android:id="#+id/end_game_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="#string/end_game"
android:theme="#style/SkipButton"
android:onClick="#{() -> gameViewModel.onGameFinished()()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline" />
RIGHT
<Button
android:id="#+id/end_game_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="#string/end_game"
android:theme="#style/SkipButton"
android:onClick="#{() -> gameViewModel.onGameFinished()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline" />
I will advise you check through your fragment's XML file for any typo or misspelled variable names and correct them. This solved my issue.
These errors only get caught at compile-time and points to the generated databinding files, making it hard to debug.
Okay, I found solution. My ViewModel constructor had one parameter and I had to create custom ViewModelProviderFactory in order to work. It wasn't Android Studio fault.
Code before:
val viewModel: CollectionViewModel = ViewModelProviders.of(this).get(CollectionViewModel::class.java)
Code after:
val viewModel: CollectionViewModel = ViewModelProviders.of(this, CollectionViewModelFactory(activity!!.application)).get(CollectionViewModel::class.java)
where
activity!!.application
is argument to my ViewModel
Related
I am using the following in android studio
View binding
Navigation
The bottom menu when clicked the app does not go to new destination
Here is the code:
App level build.gradle file:
buildscript {
repositories {
google()
}
dependencies {
def nav_version = "2.5.3"
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
}
}
plugins {
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
id 'com.google.dagger.hilt.android' version "2.44" apply false
}
Module level build.gradle file:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id "com.google.dagger.hilt.android"
id "androidx.navigation.safeargs.kotlin"
}
android {
namespace 'com.example.newsapiclient'
compileSdk 32
defaultConfig {
applicationId "com.example.newsapiclient"
minSdk 26
targetSdk 32
versionCode 1
versionName "1.0"
buildConfigField("String","API_KEY", MY_KEY)
buildConfigField("String","BASE_URL",MY_URL)
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding = true
}
}
dependencies {
def lifecycle_version = "2.5.1"
def nav_version = "2.5.3"
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.code.gson:gson:2.10.1'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
// ViewModel
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")
// ViewModel utilities for Compose
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version")
// LiveData
implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
implementation("com.google.dagger:hilt-android:2.44")
kapt("com.google.dagger:hilt-android-compiler:2.44")
implementation("androidx.navigation:navigation-fragment-ktx:$nav_version")
implementation("androidx.navigation:navigation-ui-ktx:$nav_version")
implementation 'com.github.bumptech.glide:glide:4.14.2'
kapt 'com.github.bumptech.glide:compiler:4.14.2'
testImplementation 'junit:junit:4.13.2'
testImplementation "com.squareup.okhttp3:mockwebserver:4.10.0"
testImplementation "com.google.truth:truth:1.1.3"
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
navigation xml file (nav_graph.xml):
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="#+id/nav_graph"
app:startDestination="#id/newsFragment">
<fragment
android:id="#+id/newsFragment"
android:name="com.example.newsapiclient.NewsFragment"
android:label="fragment_news"
tools:layout="#layout/fragment_news" >
<action
android:id="#+id/action_newsFragment_to_infoFragment"
app:destination="#id/infoFragment" />
</fragment>
<fragment
android:id="#+id/saveFragment"
android:name="com.example.newsapiclient.SaveFragment"
android:label="fragment_save"
tools:layout="#layout/fragment_save" >
<action
android:id="#+id/action_saveFragment_to_infoFragment"
app:destination="#id/infoFragment" />
</fragment>
<fragment
android:id="#+id/infoFragment"
android:name="com.example.newsapiclient.InfoFragment"
android:label="fragment_info"
tools:layout="#layout/fragment_info" />
</navigation>
activity_main.xml:
class MainActivity : AppCompatActivity() {
private lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
val navController = navHostFragment.navController
}
}
bottom navigation menu (bottom_menu.xml)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/newsFragment"
android:icon="#drawable/ic_baseline_live_tv_24"
android:title="News Headlines" />
<item
android:id="#+id/saveFragment"
android:icon="#drawable/ic_baseline_save_24"
android:title="Save News" />
</menu>
When I click on the bottom menu bottom, the app does not display the second destination in navigation graph whichhas id fragment_save.
You missing code to link bottom navigation with nav controller.
Add below code in onCreate method of your MainActivity.
val navView: BottomNavigationView = findViewById(R.id.bottom_nav_view)
navView.setupWithNavController(navController)
I am a Android Studio starter and I want to create a Recycle View for practical. First, I have followed this video to study ( https://www.youtube.com/watch?v=j29YnF-mPd8 ) but at the end, I noticed an "Unresolved reference: recyclerView" error. How I can solve it ?
package com.example.recycleview
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
private var layoutManager: RecyclerView.LayoutManager?=null
private var adapter:RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
layoutManager=LinearLayoutManager(this)
recyclerView.layoutManager=layoutManager //here got problem
adapter=RecyclerViewAdapter()
recyclerView.adapter=adapter //and here got problem
}
}
Below are my build gradle :
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.recycleview"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation fileTree(dir: 'libs',includes: ['*.jar'])
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
// For control over item selection of both touch and mouse driven selection
implementation 'androidx.recyclerview:recyclerview-selection:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
}
My main activity XML file :
<?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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Thx for giving solution to help me !
You have to declare it before using it, just add:
val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
inside onCreate()
When you are calling
recyclerView.layoutManager=layoutManager //here got problem
adapter=RecyclerViewAdapter()
recyclerView.adapter=adapter //and here got problem
You are using the recyclerView variable without having it declared and initialized first.
Initialize it before using it with a call like this:
val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager=layoutManager //here got problem
adapter=RecyclerViewAdapter()
recyclerView.adapter=adapter //and here got problem
how can i use lottie animation in custom progress dialog.
i know how implement custom progress bar with lottie but i want progress dialog.
i have my animation in json format. but i dont know how make custom progress dialog.
in gradle i write this line
implementation 'com.airbnb.android:lottie:3.4.4'
and new class
import android.app.ProgressDialog;
import android.content.Context;
import android.view.animation.Animation;
public class MyProgressDialog extends ProgressDialog {
public MyProgressDialog(Context context) {
super(context,R.style.NewDialog);
// TODO Auto-generated constructor stub
}
}
i found this video but it is coded by kotlin. i want use java
https://www.youtube.com/watch?v=ZcR6AMNIagU
i found solution
1- make one xml layout for new design. "loading.json" is json format of animation in asset folder.
<?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"
android:orientation="vertical"
android:layout_width="100dp"
android:layout_height="100dp"
>
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/progressAnimationView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:lottie_autoPlay="true"
app:lottie_fileName="loading.json"
app:lottie_loop="true" />
</androidx.constraintlayout.widget.ConstraintLayout>
2-create new java class
public class lottiedialogfragment extends Dialog {
public lottiedialogfragment(Context context) {
super(context);
WindowManager.LayoutParams wlmp = getWindow().getAttributes();
wlmp.gravity = Gravity.CENTER_HORIZONTAL;
getWindow().setAttributes(wlmp);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
setTitle(null);
setCancelable(false);
setOnCancelListener(null);
View view = LayoutInflater.from(context).inflate(
R.layout.dialog_lottie, null);
setContentView(view);
}
}
3-call dialog in main activity
final lottiedialogfragment lottie=new lottiedialogfragment(this);
lottie.show();
You can create it easily using the LottieDialog library
Code will be like this
LottieDialog dialog = new LottieDialog(this)
.setAnimation(R.raw.ripple)
.setAutoPlayAnimation(true)
.setDialogHeightPercentage(.2f)
.setAnimationRepeatCount(LottieDialog.INFINITE)
.setMessage("Loading...")
.setMessageColor(orangeColor);
dialog.show();
Github Repo for examples and documentation : LottieDialog
End result will be like this
I add Navigation Drawer Activity to my project in Android Studio. I got this problem:
This operation requires the libraries androidx.navigation:navigation-fragment:+, androidx.navigation:navigation-ui:+.Problem: Inconsistencies in the existing project dependencies found.Version incompatibility between:- androidx.appcompat:appcompat:1.1.0#aarand:- androidx.appcompat:appcompat:1.1.0#aarWith the dependency:- androidx.annotation:*:1.1.0versus:- androidx.annotation:*:2.0.0The project may not compile after adding these libraries.Would you like to add them anyway?
I clicked OK and when I install the app in my phone everything is OK except that the fragments do not switch when you select another item on the menu. I don't think this is normal because I have seen tutorials and when you added it automaticaly works without any changes.
Anyone know where the problem is?
My gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.example.opencvapplication"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation project(path: ':java')
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.material:material:1.2.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.navigation:navigation-fragment:2.3.0'
implementation 'androidx.navigation:navigation-ui:2.3.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.ankuryadav.lib:volleylib:1.0.3'
implementation 'com.loopj.android:android-async-http:1.4.9'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.42.6'
implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.42.6'
}
And the Activity
public class MainActivity2 extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity2, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
Found the solution: Why doesn't the navigation drawer activity work?
The activity_main.xml it's created incorrectly because the part should be after DrawerLayout.
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
I post this because it wasn´t my mistake, it's an error from Android Studio template, so I hope that if someone see this doesn't waste as much time as I did.
I have changed the app theme from Theme.appcompat to Theme.MaterialComponents
error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rohindh.kcthappytummy, PID: 16582
android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating
class <unknown>
Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at
com.rohindh.kcthappytummy.Adapter.AddedtRecyclerAdapter.createpopup(AddedtRecyclerAdapter.java:70)
at
com.rohindh.kcthappytummy.Adapter.AddedtRecyclerAdapter.access$000(AddedtRecyclerAdapter.java:26)
at
com.rohindh.kcthappytummy.Adapter.AddedtRecyclerAdapter$1.onClick(AddedtRecyclerAdapter.java:62)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
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.textfield.TextInputLayout.<init>(TextInputLayout.java:458)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:417)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at
com.rohindh.kcthappytummy.Adapter.AddedtRecyclerAdapter.createpopup(AddedtRecyclerAdapter.java:70)
at
com.rohindh.kcthappytummy.Adapter.AddedtRecyclerAdapter.access$000(AddedtRecyclerAdapter.java:26)
at
com.rohindh.kcthappytummy.Adapter.AddedtRecyclerAdapter$1.onClick(AddedtRecyclerAdapter.java:62)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
When user clicks the edit button, an Alert dialog window should appear but I don't know what I'm missing
Adapter code
public class AddedtRecyclerAdapter extends RecyclerView.Adapter<AddedtRecyclerAdapter.ViewHolder> {
private Context context;
private ArrayList<Dishmodel> dishmodellist;
//pop elements and requirement
private AlertDialog.Builder build;
private AlertDialog dialog;
private EditText nameedt,desedt,pricedt;
private ImageButton closebtn;
private Button addbtn;
private ProgressBar progressBar;
public AddedtRecyclerAdapter(Context context, ArrayList<Dishmodel> dishmodellist) {
this.context = context;
this.dishmodellist = dishmodellist;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.dish_list_item,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull AddedtRecyclerAdapter.ViewHolder holder, final int position) {
final Dishmodel dish= dishmodellist.get(position);
holder.name.setText(dish.getName());
holder.price.setText(String.valueOf(dish.getPrice()));
holder.descirption.setText(dish.getDescription());
//edit btn clicked then create a pop window and send the current array with is position
holder.edtbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createpopup(dish,position);
}
});
}
private void createpopup(Dishmodel dish, int position) {
build = new AlertDialog.Builder(context);
View view = LayoutInflater.from(context).inflate(R.layout.popup,null);
nameedt = view.findViewById(R.id.itemname);
desedt = view.findViewById(R.id.itemdes);
pricedt = view.findViewById(R.id.itemprice);
addbtn = view.findViewById(R.id.itemaddbtn);
closebtn = view.findViewById(R.id.closebtn);
progressBar = view.findViewById(R.id.addprogress);
build.setView(view);
dialog = build.create();
dialog.show();
}
#Override
public int getItemCount() {
return dishmodellist.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name,descirption,price;
ImageButton edtbtn,deletebtn;
public ViewHolder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.dishname);
descirption = itemView.findViewById(R.id.dishdes);
price = itemView.findViewById(R.id.dishprice);
edtbtn = itemView.findViewById(R.id.editbtn);
deletebtn = itemView.findViewById(R.id.deletebtn);}
}
error in 26 line in xml ie material.TextInputLayout
xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="380dp" android:paddingHorizontal="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/closeicon"
android:id="#+id/closebtn"
android:layout_alignParentEnd="true"
android:backgroundTint="#color/appcolor"
/>
<ProgressBar
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/addprogress"
android:visibility="invisible"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/outlinedTextField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Item Name"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/itemname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto"
android:maxLines="1"
android:inputType="textPersonName" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/outlinedTextField1"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:hint="Item Description"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/itemdes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="5"
android:fontFamily="#font/roboto"
android:inputType="textMultiLine" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/outlinedTextField2"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:hint="Item Price"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/itemprice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:fontFamily="#font/roboto"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:backgroundTint="#color/appcolor"
android:layout_marginTop="25dp"
android:id="#+id/itemaddbtn"
android:text="Add"/>
</LinearLayout>
Manifest code
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
Style
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowDisablePreview">false</item>
</style>
<style name="myDialog" parent="Theme.MaterialComponents.DayNight.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
dependencies
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-database:19.2.1'
implementation 'com.google.firebase:firebase-auth:19.3.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'}
I have referred to many solutions in stackoverflow but can't able to solve this issue. If anyone knows solution for this issue please help out with this.
Thanks in advance