Google Glass CardScrollView Example in Android
Last Updated :
28 Aug, 2023
In this article, we will learn how to create a Google Glass CardScrollView in Android. It is a view that displays cards, or children’s views, that scroll horizontally. A custom RecyclerView is used to display a list of cards that users may scroll through when creating a card scroll view. Here is a detailed instruction.
Step-by-Step Implementation
Step 1:
Create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio as the programming language.
Step 2:
In your project’s res/layout folder, create a new XML layout file called “card_layout.xml” for the card item. Here, we’ll create a simple card layout with a TextView to display some text.
XML
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:padding = "24dp" >
< TextView
android:id = "@+id/textViewCard"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:textSize = "18sp"
android:textColor = "#0F9D58"
android:text = "GeeksforGeeks" />
</ LinearLayout >
|
Step 3:
In “activity_main.xml” add the “RecyclerView” widget .
XML
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical" >
< androidx.recyclerview.widget.RecyclerView
android:id = "@+id/recyclerViewCards"
android:layout_width = "match_parent"
android:layout_height = "wrap_content" />
</ LinearLayout >
|
Step 4:
In the MainActivity.java file , create a LinearLayoutManager with a horizontal orientation and a RecyclerView with the custom adapter in your activity or fragment.
Java
import android.app.Activity;
import android.os.Bundle;
import com.google.android.glass.widget.CardScrollView;
import com.google.android.glass.widget.CardBuilder;
public class MainActivity extends Activity {
private CardScrollView mCardScrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
mCardScrollView = new CardScrollView( this );
setContentView(mCardScrollView);
class MyCardAdapter extends CardScrollAdapter {
@Override
public int getCount() {
return 2 ;
}
@Override
public CardBuilder getCard( int position) {
if (position == 0 ) {
return new CardBuilder( this ).setText( "Card 1" );
} else if (position == 1 ) {
return new CardBuilder( this ).setText( "Card 2" );
} else {
return null ;
}
}
}
mCardScrollView.setAdapter( new MyCardAdapter());
}
}
|
This will set up the LinearLayoutManager.
Step 5:
Create a RecyclerView.Adapter class named “CardAdapter.java” to fill the RecyclerView with the card items.
Java
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {
private List<String> cardData;
public CardAdapter(List<String> cardData) {
this .cardData = cardData;
}
@Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout, parent, false );
return new CardViewHolder(view);
}
@Override
public void onBindViewHolder(CardViewHolder holder, int position) {
String cardText = cardData.get(position);
holder.textViewCard.setText(cardText);
}
@Override
public int getItemCount() {
return cardData.size();
}
public static class CardViewHolder extends RecyclerView.ViewHolder {
TextView textViewCard;
public CardViewHolder(View itemView) {
super (itemView);
textViewCard = itemView.findViewById(R.id.textViewCard);
}
}
}
|
Above code will implement the adapter.
These steps are required to create a CardScrollView for google glass. We’ve successfully built an Android Google Glass CardScrollView. The cards will appear when the user taps on the Google Glass touchpad. The associated object will then be called after the user selects one of the cards. It is important to note that Google Glass production has been halted,but utilizing Android for different devices is worth knowing.
Share your thoughts in the comments
Please Login to comment...