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
<!-- card_layout.xml --> <!-- This XML layout defines a simple card layout with a text view. --> < LinearLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:padding = "24dp" > <!-- This TextView displays the text "Lazyroar" with mentioned properties --> < TextView android:id = "@+id/textViewCard" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:textSize = "18sp" android:textColor = "#0F9D58" android:text = "Lazyroar" /> </ LinearLayout > |
Step 3:
In “activity_main.xml” add the “RecyclerView” widget .
XML
<!-- activity_main.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); // Create a CardScrollView widget mCardScrollView = new CardScrollView( this ); // Set it as the content view of your activity setContentView(mCardScrollView); // Create a CardScrollAdapter to // supply cards to the CardScrollView class MyCardAdapter extends CardScrollAdapter { @Override public int getCount() { // Return the number of cards // that you want to display return 2 ; } @Override public CardBuilder getCard( int position) { // Create a CardBuilder object for // each card that you want to display if (position == 0 ) { return new CardBuilder( this ).setText( "Card 1" ); } else if (position == 1 ) { return new CardBuilder( this ).setText( "Card 2" ); } else { // Return null if the position is out of range return null ; } } } // Set the CardScrollView's adapter // to the CardScrollAdapter that you created 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> { // This field stores the list of // cards that the adapter will display. private List<String> cardData; // This constructor initializes the adapter // with the specified list of cards. public CardAdapter(List<String> cardData) { this .cardData = cardData; } // This method inflates the layout for a // card view and returns a new CardViewHolder object. @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); } // This method binds the data for a card view // to the corresponding CardViewHolder object. @Override public void onBindViewHolder(CardViewHolder holder, int position) { String cardText = cardData.get(position); holder.textViewCard.setText(cardText); } // This method returns the number of cards in the list. @Override public int getItemCount() { return cardData.size(); } // This static class defines a ViewHolder object that // stores references to the views in a card view. public static class CardViewHolder extends RecyclerView.ViewHolder { TextView textViewCard; // This constructor initializes the ViewHolder object // with the references to the views in a card view. 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.