Atlas91
Atlas91

Reputation: 5904

Trouble importing android.support.v7.widget.CardView into Eclipse

I use Eclipse and I'm trying to create an application using the new support-library-v7:21.+ from Lollipop.

After all i still got the invalid R declaration. I restarted Eclipse and then, after re-importing the library, seem went! I created a Tolbar and a NavigationDrawer as well without problem - it works.

Now, i would like to put a CardView in my ListView items:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
   >
    <!-- A CardView that contains a TextView -->
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="200dp"
        android:layout_height="200dp"
        card_view:cardCornerRadius="4dp">
    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/codename"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/versione"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/link"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/timestamp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    </android.support.v7.widget.CardView>
</LinearLayout>

well, first error: No resource identifier found for attribute 'cardCornerRadius'. I tried to delete the attribute, restart the application but I get a the following crash:

java.lang.RuntimeException: Binary XML file line #2: You must supply a layout_width attribute.

I don't understand what the problem is.

Upvotes: 14

Views: 18066

Answers (6)

Jorgesys
Jorgesys

Reputation: 126523

Inside your build.gradle file (inside dependencies section) add the last versions for support library and cardView support library:

dependencies {
    ...
    ...
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:cardview-v7:23.0.1'
}

Upvotes: 0

SilentKnight
SilentKnight

Reputation: 14021

It is obvious that you didn't import the CardView support project as your main project's library. To import CardView library, you need to do this:
1, first, import CardView support library into you Eclipse. The process should be like this: File-->Import-->Android-->Existing Android Code into Eclipse-->Browse-->.../sdk/extras/android/support/v7/cardview-->OK-->Finish. Then you will see project of android-support-v7-cardview in your Eclipse in which all resource and jar is.
2, add android-support-v7-cardview into your main project as a library. Just like this: right-click your project-->Properties-->Android-->Add, and select android-support-v7-cardview into your project.

And then, rebuild your project. Errors about CardView in your project will be finished.

Upvotes: 0

Akanksha Rathore
Akanksha Rathore

Reputation: 3623

We need to do both the things add android.support.v7.widget.CardView as a library project and also add + check it's jar file in java build path

Go to File -> Import -> Existing Android code into workspace --> Browse (Go to sdk/extras/android/support/v7/cardview) --> Click ok --> Click Finish

Right click on cardview project --> Properties --> Android(Left Pane) --> Enable isLibrary (tick the checkbox) --> Apply --> ok

Right click on your project --> Properties --> Android(Left pane) --> Add (under library) --> cardview --> apply --> ok

right click on your project again --> build path --> configure build path -->under libraries-->add jar-->expand cardview-->expand libs-->select android.support.v7.widget.CardView.jar

under order and export-->check android.support.v7.widget.CardView.jar-->click

Hope it will work fine.

Upvotes: 0

shkschneider
shkschneider

Reputation: 18243

Adding the library

Gradle

compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:cardview-v7:22.0.0'

Eclipse

Using android.support.v7.widget.CardView in my project (Eclipse)

Proper LinearLayout

As the error said, a LinearLayout needs a layout_width and a layout_heighth. Always.

Upvotes: 20

Maddy
Maddy

Reputation: 923

I had the same issue.

Tried adding Eclipse->properies->Android->Libraries->Add = CardView is not there. Tried Eclipse->properies->JavaBuildPath->Libraries->Add Jars->(select from CardView->libs) did not work.

what worked: cardview->project.properites, add android.library=true if missing.

Upvotes: 0

Udit Khandelwal
Udit Khandelwal

Reputation: 326

I have outlined the steps that worked for me as an answer to this question. It should work.

Upvotes: 0

Related Questions