How to disable scrolling for CalendarView - Android Dev - java

I am currently refreshing my android developement skills and came across the CalendarView component. I wanted to disable the scrolling of the calendar and instead enable the arrows to browse through the calendar.
What I want to know is the following:
How can I disable the scrolling from the CalendarView? And why does Android Studio show me the arrow browse-version in the Designer-Mode but compiles and runs the scroll-version of the calendar?
I am running an Android VM, a "Galaxy Nexus" with API 22. I also posted the "build.gradle" further below.
What Android shows me/What I want:
What Android Studio compiles & runs:
Here some code:
// activity_main.java:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="0dp">
<CalendarView
android:id="#+id/calendarView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:isScrollContainer="false" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
// build.gradle
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.example.kalender003_emptyactivity"
minSdkVersion 16
targetSdkVersion 30
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 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Use MaterialComponents Theme, it will most probably solve the issue.
In your values/themes file replace the parent them with this,
parent="Theme.MaterialComponents.DayNight.DarkActionBar"
If you don't want Action Bar then ,
parent="Theme.MaterialComponents.DayNight.NoActionBar"
And if you only want to show Light Coloured Calendar view regardless of Devices's theme use,
parent="Theme.MaterialComponents.Light.NoActionBar"

Thanks to Vaibhav Goyal i came across the solution.
The API level I used was to low (API 22, Android 5.1). Changing that to API 23 (Android 6.0) solved my problem. Now it shows me the CalendarView with arrow-navigation instead of the scroll version!

yourCalenenderId.addOnItemTouchListener(object : RecyclerView.SimpleOnItemTouchListener() {
override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
return if (e.action == MotionEvent.ACTION_MOVE) {
true
} else {
super.onInterceptTouchEvent(rv, e)
}
}
})

Related

SafeArgs does not generate any class

I am trying to use Safe Args with the NavigationComponent. I have followed a tutorial, but I have problems because any class is generated automatically, so I can't pass any arguments because Classes (obviously) are not recognized
These are my Gradle file
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
def nav_version = "2.4.1"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
classpath 'com.android.tools.build:gradle:7.1.0'
classpath 'com.google.gms:google-services:4.3.10'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
id 'androidx.navigation.safeargs'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.example.frangela"
minSdk 21
targetSdk 31
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
}
buildFeatures {
viewBinding true
}
}
dependencies {
def nav_version = "2.4.1"
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
implementation 'com.google.firebase:firebase-database:20.0.3'
implementation 'com.google.firebase:firebase-common-ktx:20.0.0'
implementation 'com.google.android.gms:play-services-maps:18.0.1'
implementation 'com.google.android.gms:play-services-location:19.0.1'
implementation 'com.google.firebase:firebase-crashlytics-buildtools:2.8.1'
testImplementation 'junit:junit:'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation platform('com.google.firebase:firebase-bom:29.0.3')
implementation "androidx.recyclerview:recyclerview:1.2.1"
implementation 'com.firebaseui:firebase-ui-database:6.2.1'
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.android.gms:play-services-auth:20.1.0'
}
This is the xml code:
<?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/mobile_navigation"
app:startDestination="#+id/nav_home">
<fragment
android:id="#+id/nav_home"
android:name="com.example.frangela.ui.home.HomeFragment"
android:label="#string/menu_home"
tools:layout="#layout/fragment_home" />
<fragment
android:id="#+id/nav_gallery"
android:name="com.example.frangela.ui.gallery.GalleryFragment"
android:label="#string/menu_gallery"
tools:layout="#layout/fragment_gallery" />
<fragment
android:id="#+id/nav_slideshow"
android:name="com.example.frangela.ui.slideshow.InserisciSpesaFragment"
android:label="#string/menu_slideshow"
tools:layout="#layout/fragment_inserisci" >
<argument
android:name="descrizione"
app:argType="string" />
<argument
android:name="ammontare"
app:argType="float" />
</fragment>
<fragment
android:id="#+id/nav_stipendio"
android:name="com.example.frangela.ui.stipendio.InserisciStipendioFragment"
android:label="#string/menu_stipendio"
tools:layout="#layout/fragment_stipendio" />
<fragment
android:id="#+id/nav_resoconto"
android:name="com.example.frangela.ui.resocard.ResocardFragment"
android:label="#string/Resoconto"
tools:layout="#layout/fragment_resocard" >
<action
android:id="#+id/action_nav_resoconto_to_nav_slideshow"
app:destination="#id/nav_slideshow" />
</fragment>
</navigation>
After that I resync my project and rebuild, but nothing changes.
I tried to follow different tutorials and questions but none of them solved my problem. What am I missing?
Thanks in advance
After some days, I have found that the code has been generated, but in different folders, so that I cannot see them

Not able to use CoordinatorLayout in Android Studio

My problem is that I am not able to use CoordinatorLayout in Android Studio despite adding all the possible dependencies in my build.gradle file. The class is not instantinated.
Here's my build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.uiuxpractical4materialdesign"
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'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0'
}
activity_main.xml:
<?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.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
The Error:
java.lang.NoClassDefFoundError: androidx/coordinatorlayout/R$attr
at androidx.coordinatorlayout.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:211)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:403)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:186)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:144)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:309)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:418)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:429)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:333)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:863)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:837)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:323)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:394)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:200)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:572)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$5(RenderTask.java:698)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Due to this, the class is not instantinated and I get something like this:-
Also, here a small picture of the error too:-
Thanks for everyone helping out there to solve this issue.
Please also inform if you need anything else.
try to add this line in your dependencies in build.gradle
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.android.support:design:29.0.2'
don't worry about the difference between com.android..... and androidx.... This implementation androidx. automatically migrate the implementation if your android studio is higher than 3.2.
you can check this link.https://stackoverflow.com/a/58619780/10075771 it will help you understand also the situation :)
Hoping i help you in little way :)
try adding below dependencies
implementation 'com.android.support:appcompat-v7:23.1.1'
implementation 'com.android.support:design:23.1.1'
Hope this will work for you.

Theme Error - how to fix?

Tried Many Solution But No Helped
Ref : Failed to find style 'coordinatorLayoutStyle' in current theme
But not Helped
Render Problem:
Failed to find style 'coordinatorLayoutStyle' in current theme
Tip: Try to refresh the layout.
build.gradle file of my project is :
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
module:app
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "*****"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:design:28.0.0-alpha1'
}
main Activity XML code
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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">
<android.support.design.bottomappbar.BottomAppBar
android:id="#+id/bar"
style="#style/Widget.MaterialComponents.BottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:navigationIcon="#drawable/ic_menu"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:fabAttached="true"
app:backgroundTint="#color/colorPrimary"
app:fabCradleVerticalOffset="12dp"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="#id/bar"/>
</android.support.design.widget.CoordinatorLayout>
Why I am Not changing compiled SDK version 28 to 27
bcz I want to use Bottom app bar
Bottom App Bar Material Design
App Bars:Bottom- Material Design
This is a bug in 28.0.0 and the only fix(workaround) is adding this to your Build.gradle:
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == "com.android.support") {
if (!requested.name.startsWith("multidex")) {
details.useVersion "27.1.1"
}
}
}
}
Which somehow, this bypasses the issue and uses 27 support library for that. Otherwise, you may wanna update your Android Studio to canary channel version or using backward support library like 27.1.1 or etc.
This error was occurred in your Rendering Layout which is a part of Studio Not in any files or Library
Try adding "Base" before Theme in styles.xml as show : -
"Base.Theme.AppCompat.Light.DarkActionBar"
Please restart Android Studio by selecting the menu option "File" →
"Invalidate Caches / Restart"
Workarounds: Try rendering with M preview using AS v1.3 preview OR
use FrameLayout for designing and change to
android.support.design.widget.CoordinatorLayout while debugging.

AdMob - Ad not displayed - Error 2

I am building an android application that has two versions.One with Ad. and one with out Ad. . I've successfully integrated the AdMob in the application. but the Ads are not getting displayed on the screen.
Here's my
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.myapp"
minSdkVersion 9
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors{
free{
applicationId "com.myapp"
buildConfigField "boolean","IS_PAID_VERSION", "false"
}
paid{
applicationId "com.myapp.paid"
buildConfigField "boolean","IS_PAID_VERSION", "true"
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.google.android.gms:play-services-ads:8.4.0'
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
tools:context="com.myapp.activities.AdTestActivity">
<com.google.android.gms.ads.AdView
android:id="#+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/ad_unit_id" />
</RelativeLayout>
AdTestActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.myapp.R;
public class AdTestActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ad_test);
final AdView adView = (AdView) findViewById(R.id.ad_view);
final AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("A144A6A159A94CC77CE70EC1A874B93A") //My test device 1
.addTestDevice("8477FA9B6C105C84897FC2717E19A548") //My test device 2
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
adView.loadAd(adRequest);
}
}
The ad is no getting displayed on the screen, and the logcat says...
I/Ads: Starting ad request.
02-12 00:24:41.381 15472-15493/com.myapp W/Ads: There was a problem getting an ad response. ErrorCode: 2
02-12 00:24:41.382 15472-15472/com.myapp I/Ads: Scheduling ad refresh 60000 milliseconds from now.
02-12 00:24:41.382 15472-15472/com.myapp W/Ads: Failed to load ad: 2
PS: I've a good internet connection and valid AdUnitId. I don't have any kind of AdBlock applications in my device. I've also read all SO threads about this problem, but none of them worked for me.
What am i doing wrong here ? :(
I had the same issue and I fixed it by updating Google Play Services to the latest version on my mobile. So maybe try updating it from Play Store.

No resource found that matches the name - Android Studio

I just downloaded Android Studio and have created a new project.
Right off the bat, I'm getting two errors:
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Button.Colored'.
Here is my gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.test.testapp"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
}
and my installed packages:
What is messed up here? Thanks
In your gradle, compileSdkVersion is 22 while your support library version is 23. The two should match.
See Error retrieving parent for item: No resource found that matches the given name after upgrading to AppCompat v23
I also had same problem now. I tried below steps and worked perfectly.
In .gradle change like this
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-v13:23.0.1'
}
You'll get error in style file change it as below
<style name="AppTheme">
<!-- Customize your theme here. -->
</style>
modify your main_menu.xml
<menu 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" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" android:showAsAction="never" />
</menu>
In MainActivity remove AppCompatActivity like
MainActivity extends Activity

Categories