Recycleview of cardview is not working.How can I fix it? - android

I am a beginner trying to learn android developement.I try to make a recyclerview using below cardview.But when I try to run It's not working.So,how can I fix it?
The cardview code below:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView 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:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="10dp"
android:id="#+id/list_layout"
android:background="#color/white"
android:layout_marginBottom="10dp"
>
<LinearLayout
android:layout_width="2000dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ratio_button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do the work"
android:textSize="20sp"
android:id="#+id/task"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/delete"
android:backgroundTint="#color/red"
android:text="Delete"
android:visibility="invisible"
/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>

implementation 'com.android.support:cardview-v7:28.0.0'
After that, Let's try this:
androidx.cardview.widget.CardView

your question is not clear! but after trying your XML file, I believe you're missing material dependencies in your app level Gradle file
implementation 'com.google.android.material:material:1.2.1'
MaterialCardView is part of this library and you should include it in your app's dependencies before using it.

Related

Android CardView layout broken pre-API 28

I have view layout using a CardView that is supposed to look like this
And it looks fine on devices running Android 9 and above (API 28+). However, on anything older it looks to be broken:
The layout is as follows:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="125dp"
android:layout_height="40dp"
android:layout_marginEnd="8dp"
app:cardCornerRadius="165dp"
app:cardElevation="8dp"
app:contentPadding="8dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal"
>
<android.support.v7.widget.CardView
android:layout_width="24dp"
android:layout_height="24dp"
app:cardCornerRadius="16dp"
app:cardElevation="0dp"
>
<ImageView
android:id="#+id/tab_icon"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_gravity="center"
tools:src="#tools:sample/avatars"
/>
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/tab_title"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_marginStart="4dp"
android:layout_marginEnd="6dp"
android:ellipsize="end"
android:gravity="center_vertical"
android:maxLines="1"
android:paddingBottom="2dp"
android:textColor="#android:color/black"
android:textSize="14sp"
tools:text="Sample User"
/>
<ImageView
android:id="#+id/tab_close"
android:layout_width="14dp"
android:layout_height="14dp"
android:src="#drawable/btn_close"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
I'm using the 'com.android.support:cardview-v7:28.0.0' dependency.
It seems the issue was the value set for cardCornerRadius. Setting it to 20dp got me the desired border-radius without breaking the layout.
You can use SDP - a scalable size unit
dependencies {
implementation 'com.intuit.sdp:sdp-android:1.0.6'
}
FYI
<android.support.v7.widget.CardView
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="125dp"
android:layout_height="40dp"
android:layout_marginEnd="8dp"
app:cardCornerRadius="165dp"
app:cardElevation="8dp"
app:contentPadding="8dp"
>
Leads to inconsistency. Different layouts end up hard-coding different font sizes and margins, making the app appear less polished.
TRY
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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="#dimen/_125sdp"
android:layout_height="#dimen/_35sdp"
android:layout_marginEnd="#dimen/_5sdp"
app:cardCornerRadius="#dimen/_150sdp"
app:cardElevation="#dimen/_5sdp"
app:contentPadding="#dimen/_5sdp"
>
.........
<android.support.v7.widget.CardView
android:layout_width="#dimen/_22sdp"
android:layout_height="#dimen/_22sdp"
app:cardCornerRadius="#dimen/_12sdp"
app:cardElevation="0dp"
>
NOTE
Switch to AndroidX.
AndroidX replaces the original support library APIs with packages in
the androidx namespace. Only the package and Maven artifact names
changed; class, method, and field names did not change.
Like
<androidx.cardview.widget.CardView

Smooth scrolling in scrollView

I am new to android and I have created a simple block of code which has a scrollView and an image. I want this scrollview to scroll smoothly. What do I have to do for that ? I have gone through many materials over internet, but all of those use hard terminologies which I'm not able to understand. Can someone please help me ? Thanks.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/top_string"
android:textColor="#android:color/white"
android:textSize="16dp"
android:textScaleX="1.5"
/>
<ImageView
android:id="#+id/img"
android:layout_width="300dp"
android:layout_height="150dp"
android:src="#drawable/dg_truck_main"
android:layout_gravity="center_horizontal"
android:background="#android:color/holo_blue_light"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_below="#id/text_view"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/top_string2"
android:textColor="#android:color/white"
android:textSize="16dp"
android:textScaleX="1.5"
android:layout_marginTop="50dp"
android:layout_below="#id/img"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
You have added ScrollView inside RelativeLayout, what I suggest is that you should place RelativeLayout inside ScrollView tag. Please go through below code,
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/top_string"
android:textColor="#android:color/white"
android:textSize="16dp"
android:textScaleX="1.5"
/>
<ImageView
android:id="#+id/img"
android:layout_width="300dp"
android:layout_height="150dp"
android:src="#drawable/dg_truck_main"
android:layout_gravity="center_horizontal"
android:background="#android:color/holo_blue_light"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_below="#id/text_view"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/top_string2"
android:textColor="#android:color/white"
android:textSize="16dp"
android:textScaleX="1.5"
android:layout_marginTop="50dp"
android:layout_below="#id/img"
/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
Okay I got my answer. Since I was new to android, I thought that "smooth scroll" is something else and we have to integrate it into our project. But a scrollView already gives smooth scrolling and so we do not need to write any extra code for that.
//to make the scrollview faster
//add the below line in xml
android:fillViewport="true"
//also add the following line to your java class
// to make the scroll view faster
scrollView.fullScroll(View.FOCUS_DOWN);
scrollView.setSmoothScrollingEnabled(true);
I had a hard time resolving this issue. Finally found a solution very simple and very smooth outcome. In your recycler view set nested scrolling to false.
Two ways to achieve it.
Through xml file -
android:nestedScrollingEnabled="false"
Through java code -
recyclerView.isNestedScrollingEnabled = false

android.support.v7.widget.AppCompatTextview Failed to instataniate

i am having problem, failed to instantiate one or more classes.Following classes cannot be instantiate .
android.support.v7.widget.AppCompatTextview
There it is my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/iv_comment_owner_display"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/facebook" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/tv_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Username"
android:textColor="#android:color/black"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text=""
android:textColor="#android:color/black" />
</LinearLayout>
</LinearLayout>
AppCompatTextView was added in appcompat-v7:22.1.0
Please check you appcompat version. You have to update your gradle file with this.
clean/Rebuild project from Android Studio,it might help.
If it doesn't try to invalidate caches and restart android studio.
If you have not done,
Try adding the dependencies in your build.gradle(Module:App)
please check the version according to your app
dependencies {
compile 'com.android.support:design:25.3.1'
}
Try putting this code in your layout
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello world"
android:textSize="18dp"
/>
because i see no AppCompatTextView in your layout.

class file for android.support.v4.widget.TintableCompoundButton not found

I want to Create a CheckBox, My compile
sdk version is 23,
Target version is 23,
build tool version is 23.0.0,
in gradle I'hv added
compile 'com.android.support:support-v4:23.2.0',
compile 'com.android.support:appcompat-v7:23.2.0'
both and in menifest given the app theme as android:theme="#style/Theme.AppCompat.Light.NoActionBar" and no specific theme to Activity. when I run my code its throwing
InflateException line number #23 at xml file, problem while inflating kind of
Then I decided to convery a simple ChcekBox to android.support.v7.widget.AppCompatCheckBox but on the xml file Preview its showing Rendering Problems The following classes could not be instantiated:
- android.support.v7.widget.AppCompatCheckBox
Exception Details java.lang.ClassNotFoundException: android.support.v4.widget.TintableCompoundButton
and if i try to run it it gives me class file for android.support.v4.widget.TintableCompoundButton not found.
So, Any one faced this issue?
<?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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<LinearLayout
android:id="#+id/lnv_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rlv_header"
android:orientation="vertical">
<android.support.v7.widget.AppCompatCheckBox
android:id="#+id/confirmCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:gravity="start"
android:text="#string/personal_confirm_text"
android:textColor="#color/black"
android:textSize="14sp" />
</LinearLayout>
<RelativeLayout
android:id="#+id/rlv_next"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="40dp"
android:background="#color/crabfx_light_gray"
android:visibility="gone">
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_alignParentTop="true"
android:background="#color/crabfx_green" />
<TextView
android:id="#+id/tv_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:drawablePadding="10dp"
android:drawableRight="#drawable/discloser"
android:gravity="center_vertical"
android:text="#string/lbl_next"
android:textColor="#color/black"
android:textSize="18sp" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:background="#color/crabfx_green" />
</RelativeLayout>
</RelativeLayout>
and below is the theme to Application tag in menifest
android:theme="#style/Theme.AppCompat.Light.NoActionBar"

Android Percent Support Library Blank Screen on Actual Device

I tried using the Percent Support Library as detailed here and here. It works nicely in the Android Studio designer, but when I try to install my app into my phone, nothing is showing but an empty screen. What might the problem be?
Edit:
I've added the following lines in my gradle file dependencies.
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:support-v4:23.0.0'
compile 'com.android.support:design:23.0.0'
compile 'com.android.support:percent:23.0.0'
And here is my fragment layout:
<RelativeLayout 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="org.atpms.fairfare.ReportFragment"
android:id="#+id/fragment_report" >
<ProgressBar android:id="#+id/report_progress" style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_marginBottom="8dp" android:visibility="gone"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<RelativeLayout
android:id="#+id/report_form"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/reportButton"
android:gravity="center">
<ImageView
android:id="#+id/report_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="#drawable/report"
android:adjustViewBounds="true" />
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#+id/report_background"
android:layout_alignBottom="#+id/report_background"
android:layout_alignLeft="#+id/report_background"
android:layout_alignStart="#+id/report_background"
android:layout_alignRight="#+id/report_background"
android:layout_alignEnd="#+id/report_background">
<EditText
android:id="#+id/plateNumberField"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_widthPercent="58%"
app:layout_heightPercent="14%"
app:layout_marginTopPercent="20%"
app:layout_marginLeftPercent="36%"
android:textColor="#color/plate_number"
android:background="#null"
android:gravity="center"
android:textSize="24sp"
tools:ignore="TextFields"/>
<ImageView
android:id="#+id/meterPictureButton"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_widthPercent="12%"
app:layout_heightPercent="15.5%"
app:layout_marginTopPercent="38%"
app:layout_marginLeftPercent="84%"
android:src="#drawable/camera" />
<ImageView
android:id="#+id/meterPicture"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_widthPercent="97.2%"
app:layout_heightPercent="42%"
app:layout_marginTopPercent="55.8%"
app:layout_marginLeftPercent="1.6%"
android:scaleType="fitXY"
tools:ignore="ContentDescription" />
</android.support.percent.PercentRelativeLayout>
</RelativeLayout>
<Button
android:id="#+id/reportButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/report"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#drawable/report_button"
android:textSize="20sp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textColor="#color/button_text"
android:textAllCaps="false"
android:gravity="center"
tools:ignore="UnusedAttribute" />
</RelativeLayout>
</RelativeLayout>
Update:
Ok, I've inflated the exact same layout from an Activity using setContentView, and it works fine. It seems that the problem only arises when I try to use the PercentRelativeLayout in a Fragment layout. It appears that LayoutInflater isn't correctly inflating a layout with PercentRelativeLayout elements, but I'm still unsure and continuing to investigate.
It's not a very nice solution, but I solved this problem by just getting rid of the fragment, and making it a full-blown activity.

Categories

Resources