Skip to content

Instantly share code, notes, and snippets.

@czak
Last active April 25, 2023 17:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czak/df2e8ebd1e19d9d85dc6ddb36b04350b to your computer and use it in GitHub Desktop.
Save czak/df2e8ebd1e19d9d85dc6ddb36b04350b to your computer and use it in GitHub Desktop.
Complete source of a simple VerticalGridFragment example
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/pizza_grid_fragment"
android:name="pl.czak.tvtalk.leanbackminimal.PizzaGridFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pl.czak.tvtalk.leanbackminimal">
<uses-feature
android:name="android.software.leanback"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".PizzaGridActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "pl.czak.tvtalk.leanbackminimal"
minSdkVersion 23
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:recyclerview-v7:24.0.0'
compile 'com.android.support:leanback-v17:24.0.0'
}
package pl.czak.tvtalk.leanbackminimal;
import android.app.Activity;
import android.os.Bundle;
public class PizzaGridActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pizza);
}
}
package pl.czak.tvtalk.leanbackminimal;
import android.content.Context;
import android.os.Bundle;
import android.support.v17.leanback.app.VerticalGridFragment;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
import android.support.v17.leanback.widget.ImageCardView;
import android.support.v17.leanback.widget.Presenter;
import android.support.v17.leanback.widget.VerticalGridPresenter;
import android.view.ViewGroup;
public class PizzaGridFragment extends VerticalGridFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Title");
VerticalGridPresenter gridPresenter = new VerticalGridPresenter();
gridPresenter.setNumberOfColumns(4);
setGridPresenter(gridPresenter);
ArrayObjectAdapter adapter = new ArrayObjectAdapter(new PizzaCardPresenter());
setAdapter(adapter);
adapter.add("Pizza 1");
adapter.add("Pizza 2");
adapter.add("Pizza 3");
adapter.add("Pizza 4");
adapter.add("Pizza 5");
adapter.add("Pizza 6 with a very long title which will not fit");
}
private class PizzaCardPresenter extends Presenter {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
return new ViewHolder(new ImageCardView(parent.getContext()));
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, Object item) {
ImageCardView view = (ImageCardView) viewHolder.view;
Context context = view.getContext();
String title = (String) item;
view.setTitleText(title);
view.setContentText(null);
view.setMainImage(context.getDrawable(R.drawable.pizza));
}
@Override
public void onUnbindViewHolder(ViewHolder viewHolder) {
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PizzaCardStyle" parent="Widget.Leanback.ImageCardViewStyle">
<item name="lbImageCardViewType">Title</item>
<item name="cardType">infoOver</item>
</style>
<style name="PizzaCardImageStyle" parent="Widget.Leanback.ImageCardView.ImageStyle">
<item name="android:layout_width">200dp</item>
<item name="android:layout_height">200dp</item>
</style>
<style name="PizzaCardTitleStyle" parent="Widget.Leanback.ImageCardView.TitleStyle">
<item name="android:textAlignment">textEnd</item>
<!--<item name="android:gravity">center</item>-->
</style>
<style name="AppTheme" parent="@style/Theme.Leanback">
<item name="android:windowBackground">@drawable/background_pizza</item>
<item name="imageCardViewStyle">@style/PizzaCardStyle</item>
<item name="imageCardViewImageStyle">@style/PizzaCardImageStyle</item>
<item name="imageCardViewTitleStyle">@style/PizzaCardTitleStyle</item>
</style>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment