Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CardView example not working

I'm trying the two new features in android L version,

  • RecyclerView
  • CardView

Well the RecyclerView got working fine for me.But when I tried to execute the CardView.It throws an error and I was not able to find whats the error is!

MainActivity.java

public class MainActivity extends Activity {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] myDataset ={"Uday","Harihar","chetan","ravi","harish","Rahul","satish","vikaram","ravikiran","harish2"};

 mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

    // improve performance if you know that changes in content
    // do not change the size of the RecyclerView
    mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // specify an adapter (see also next example)
   mAdapter = new MyAdapter(myDataset);
    mRecyclerView.setAdapter(mAdapter); 

}

MyAdapter.java

package com.hrh.material;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private String[] mDataset;

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView mTextView;
    public ViewHolder(View v) {
        super(v);
        mTextView = (TextView)v.findViewById(R.id.text);
    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
    mDataset = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
                           .inflate(R.layout.activity_card_view, parent, false);
    // set the view's size, margins, paddings and layout parameters

    ViewHolder vh = new ViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    holder.mTextView.setText(mDataset[position]);

}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return mDataset.length;
}


}

activity_card_view.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
>

      <android.support.v7.widget.CardView
        android:id="@+id/cardview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:elevation="100dp"
        card_view:cardCornerRadius="8dp"
        >

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

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

in the layout it shows that "Resource id 0x7f040001 is not of type STYLE (instead anim)".

enter image description here

like image 606
Uday Avatar asked Aug 28 '26 07:08

Uday


1 Answers

I resolve the issue following this steps:

  1. Removing the android-support-v7-appcompat, and cardview libs projects from my IDE (Eclipse in my case).
  2. Update Android SDK Tools and Android Support Library from Android SDK Manager.
  3. Restart your IDE (Eclipse).
  4. Import the android-support-v7-appcompat, and cardview libs projects.

And make sure you added xmlns:card_view at the top of your your xml file:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:card_view="http://schemas.android.com/apk/res-auto"

 <android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp">

And thats it , hope it helps.

like image 136
Kokusho Avatar answered Aug 29 '26 22:08

Kokusho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!