Adding tabs dynamically in tabhost from database - xamarin.android

I try to add tabs in tabhost dynamically from data which I extract from database. But usually I get exceptions. I want to use this tabhost in activity.
This is my code :
Main.cs
[Activity (Label = "Main",Theme = "#style/Theme.Splash")]
public class Main : Activity
{
TabHost tabHost;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.activity_main);
tabHost = FindViewById<TabHost>(Resource.Id.tabHost1);
LocalActivityManager localActMgr = new LocalActivityManager(this, false);
localActMgr.DispatchCreate(bundle);
CSList<CategoriesArticlesConfig> categories = CategoriesArticlesConfig.List();
tabHost.Setup(localActMgr);
foreach (CategoriesArticlesConfig categorie in categories)
{
Intent i = new Intent();
i.SetFlags(ActivityFlags.NewTask);
i.PutExtra("CategorieID", categorie.CategorieArticlesConfigID);
i.SetClass(this, typeof(AfficheArticlesTab));
TabHost.TabSpec tab = tabHost.NewTabSpec("tab_"+categorie.CategorieArticlesConfigID);
tab.SetIndicator(""+categorie.Nom, Resources.GetDrawable(Android.Resource.Drawable.IcInputAdd));
tab.SetContent(i);
tabHost.AddTab(tab);
}
}
}
AfficheArticlesTab.cs
class AfficheArticlesTab : Activity
{
public AfficheArticlesTab() { }
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.activity_configuration);
GridView gridView = FindViewById<GridView>(Resource.Id.gv_item_articles_gridview_articles_tab_main_activity);
LayoutInflater layoutInflater = (LayoutInflater)GetSystemService(Context.LayoutInflaterService);
View view = layoutInflater.Inflate(Resource.Layout.gridview_articles_tab, null);
int CategorieID = Intent.Extras.GetInt("CategorieID");
CategoriesArticlesConfig categorie = CategoriesArticlesConfig.ReadFirst("CategorieArticlesID="+CategorieID);
gridView.Adapter = new ListArticleConfigAdapter(this,categorie.ArticlesConfig.ToList<ArticlesConfig>());
}
}
activity_main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px"
android:background="#drawable/background_simple"
android:padding="5dp">
<FrameLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/frameLayout1"
android:layout_margin="5dp"
android:layout_weight="0.5">
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout11">
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout12"
android:layout_weight="4">
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout13"
android:layout_marginTop="14.0dp"
android:layout_marginLeft="8.0dp">
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout18">
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/tv_num_ticket_activity_main" />
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/tv_num_caissier_activity_main"
android:layout_marginLeft="10dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout14">
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout15"
android:layout_weight="0.1">
<EditText
android:inputType="numberDecimal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ed_zone_main_activity"
android:layout_weight="0.16"
android:layout_marginTop="0.0dp" />
<TextView
android:text="#string/qte_value"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tv_qte_value_main_activity"
android:layout_weight="0.3"
android:layout_marginRight="2.5dp"
android:layout_marginLeft="13.3dp"
android:layout_gravity="center_vertical"
android:gravity="right"
android:layout_marginTop="0.0dp"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView1"
android:layout_weight="1"
android:layout_marginTop="8.0dp" />
</LinearLayout>
</FrameLayout>
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout17"
android:layout_weight="0.3">
<FrameLayout
android:minWidth="25px"
android:minHeight="25px"
android:id="#+id/frameLayout2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="0.15">
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout5">
<TabHost
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tabHost1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/linearLayout19">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</TabHost>
</LinearLayout>
</FrameLayout>
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:id="#+id/linearLayout16"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="0.85">
<Button
android:text="#string/total"
android:id="#+id/bt_total_activity_main"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/button_selector_rouge" />
<Button
android:text="#string/quantitee"
android:id="#+id/bt_quantitee_activity_main"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/button_selector_enter" />
<Button
android:text="#string/code_barre"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/bt_code_barre_activity_main"
android:layout_weight="1"
android:textSize="16sp"
android:background="#drawable/button_selector_enter" />
<Button
android:text="#string/annuler"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/bt_annuler_activity_main"
android:layout_weight="1"
android:textSize="16sp"
android:background="#drawable/button_selector_enter" />
<Button
android:text="#string/remise"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/bt_remise_activity_main"
android:layout_weight="1"
android:textSize="16sp"
android:background="#drawable/button_selector_enter" />
<Button
android:text="#string/attente"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/bt_attente_activity_main"
android:layout_weight="1"
android:textSize="16sp"
android:background="#drawable/button_selector_enter" />
</LinearLayout>
</LinearLayout>
03-03 13:19:08.004 E/mono ( 7331): Unhandled Exception:
03-03 13:19:08.004 E/mono ( 7331): Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
03-03 13:19:08.004 E/mono ( 7331): at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr,Android.Runtime.JValue[]) [0x00023] in /Users/builder/data/lanes/monodroid-lion-monodroid-4.6.6-branch/6d7480e9/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:368
03-03 13:19:08.004 E/mono ( 7331): at Android.Widget.TabHost.AddTab (Android.Widget.TabHost/TabSpec) [0x0003e] in /Users/builder/data/lanes/monodroid-lion-monodroid-4.6.6-branch/6d7480e9/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.Widget.TabHost.cs:762
03-03 13:19:08.004 E/mono ( 7331): at SymaMobile.Droid.Main.OnCreate (Android.OS.Bundle) [0x000e8] in f:\PFE\VS2012\SymaMobile\SymaMobile\SymaMobile.Droid\Activities\Main.cs:41
03-03 13:19:08.004 E/mono ( 7331): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00010] in /Users/builder/data/lanes/monodroid-lion-monodroid-4.6.6-branch/6d7480e9/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.App.Activity.cs:1859
03-03 13:19:08.004 E/mono ( 7331): at (wrapper dynamic-m

I find it :)
Firstly, you should extends TabHost.ITabContentFactory
public class Main : Activity, TabHost.ITabContentFactory
Secondly, you should add a NewTabSpec in my tabhost.
tabHost.AddTab(tabHost.NewTabSpec("tab_" + categorie.CategorieArticlesConfigID).SetIndicator(createTabView(tabHost.Context, categorie.Nom)).SetContent(this));
And finally, you use the implemented method CreateTabContent like this:
public View CreateTabContent(string tag)
{
LayoutInflater layoutInflater = (LayoutInflater)GetSystemService(Context.LayoutInflaterService);
View view1 = layoutInflater.Inflate(Resource.Layout.gridview_articles_tab, null);
View view2 = layoutInflater.Inflate(Resource.Layout.list_item_article_configuration, null);
View view3 = layoutInflater.Inflate(Resource.Layout.tab_item, null);
GridView gridView = view1.FindViewById<GridView>(Resource.Id.gv_item_articles_gridview_articles_tab_main_activity);
CategoriesArticlesConfig categorie = CategoriesArticlesConfig.ReadFirst("CategorieArticlesConfigID=" + tabCatID[tabHost.CurrentTab]);
gridView.Adapter = new ListArticleTabAdapter(this, categorie.ArticlesConfig.ToList());
gridView.ItemClick += gridView_ItemClick;
return view1;
}
Thanks,

Related

Top Level Element is not Compaleted

I'm really new to this and I've searched for answers but none seem relevant. When I build my android project I have an error. See at the bottom of this xml. Where do I go to find this "top level element" and why might it be incomplete?:
here code in my rowdata.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#drawable/rounded_corner">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical"/>
<TextView
android:id="#+id/listID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="#+id/listJudul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Judul Buku"
android:textSize="15sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/listNama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nama"
android:layout_marginTop="5dp"
android:textSize="14sp"/>
<TextView
android:id="#+id/listTglPinjam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tgl Pinjam"
android:layout_marginTop="5dp"
android:textSize="14sp"/>
<TextView
android:id="#+id/listStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Status"
android:layout_marginTop="5dp"
android:gravity="right"
android:textSize="13sp"/>
</LinearLayout>
</LinearLayout>

handling textview's width with screen rotation xamarin android

I have created a Xamarin.android app with listview inside a horizontalScrollView. I added [Activity(Label = "#string/app_name", Theme = "#style/AppTheme", MainLauncher = true, ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)] to handle screen rotation. but what I realized is that my horizontalScrollView doesn't expand when screen is rotated horizontally so I added this android:fillViewport="true". it worked. but my textviews' widths remained the same. so I guessed it's because I have fixed width values for my textviews. I looked things up and finally came up with determining a weightsum for my linearlayout and weights for my textviews. this is my code:
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
>
<Toolbar
android:minHeight="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
android:minWidth="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar1" >
<SearchView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/searchView1" />
</Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/customselector"
android:padding="8dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="10"
android:orientation="horizontal"
>
<TextView
android:text="Text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="100px"
android:id="#+id/textView1"
android:gravity="center"
android:textColor="#android:color/holo_red_dark"/>
<TextView
android:text="Text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="100px"
android:id="#+id/textView2"
android:layout_toRightOf="#id/textView1"
android:textColor="#android:color/holo_red_dark"
android:gravity="center"
android:layout_marginLeft="2dp"/>
<TextView
android:text="Text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="100px"
android:id="#+id/textView3"
android:layout_toRightOf="#id/textView2"
android:gravity="center"
android:textColor="#android:color/holo_red_dark"
android:layout_marginLeft="2dp"/>
<TextView
android:text="Text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="100px"
android:id="#+id/textView4"
android:layout_toRightOf="#id/textView3"
android:gravity="center"
android:layout_marginLeft="2dp"
android:textColor="#android:color/holo_red_dark"/>
<TextView
android:text="Text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="100px"
android:id="#+id/textView5"
android:layout_toRightOf="#id/textView4"
android:gravity="center"
android:layout_marginLeft="2dp"
android:textColor="#android:color/holo_blue_dark"
/>
</LinearLayout>
</RelativeLayout>
<ListView android:id="#+id/List"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#FFDAFF7F"
android:choiceMode="singleChoice"
/>
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView6"
android:layout_gravity="bottom|end"/>
</LinearLayout>
</HorizontalScrollView>
I chose weightsum=10 because I want my textviews' width to be big enough. so I thought I could view the rest of my textviews by scrolling. but the scrollview stopped working. why is that? what do I do?
thanks in advance.

How to create view in Xamarin.Android

enter image description here
How do I make a view like this in Xamarin.Android?
You can use LinearLayout and GridLayout to achieve it. Use two LinearLayout, set the android:layout_weight=1 both of them, It could divide the entire screen equally, then use GridLayout, add all of Awards, use Textview to show the awards name, use ImageView to show the awards image. You can set the android:background="#drawable/back" as the background(award shelf image) in the outside LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/back"
>
<GridLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:rowCount="2"
android:columnCount="6"
android:orientation="vertical"
>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_row="0"
android:layout_column="0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hamberguer1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myicon"/>
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_row="0"
android:layout_column="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hamberguer1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myicon"/>
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_row="0"
android:layout_column="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hamberguer1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myicon"/>
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_row="0"
android:layout_column="3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hamberguer1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myicon"/>
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_row="0"
android:layout_column="4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hamberguer1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myicon"/>
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_row="0"
android:layout_column="5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hamberguer1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myicon"/>
</LinearLayout>
</GridLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:rowCount="4"
android:columnCount="6"
android:orientation="vertical"
>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_row="0"
android:layout_column="0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hamberguer1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myicon"/>
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_row="0"
android:layout_column="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hamberguer1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myicon"/>
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_row="0"
android:layout_column="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hamberguer1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myicon"/>
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_row="0"
android:layout_column="3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hamberguer1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myicon"/>
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_row="0"
android:layout_column="4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hamberguer1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myicon"/>
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_row="0"
android:layout_column="5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hamberguer1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myicon"/>
</LinearLayout>
</GridLayout>
</LinearLayout>
Here is running gif.
You need to write a new GridView for shelves. Here is code.
public class BookGridView: Android.Widget.GridView
{
Context context;
private Bitmap background;
public BookGridView(Context context, Android.Util.IAttributeSet attributeSet):base(context, attributeSet)
{
this.context = context;
background = null;
}
protected override void DispatchDraw(Canvas canvas)
{
int count = ChildCount;
int top = count > 0 ? GetChildAt(0).Top : 0;
int width = Width;
int height = Height;
int totalCount = this.Count;
//If the total is 0, there is no need to calculate the height and draw the picture
if (totalCount > 0)
{
//get Columns
int numColumns = 3;
// Calculate the row height by calculating the number of rows and height
int rowCount = totalCount / numColumns;
if (totalCount % numColumns != 0)
{
rowCount++;
}
int rowHeight = height / rowCount;
if (background == null)
{
background = BitmapFactory.DecodeResource(Resources,
Resource.Drawable.bookshelf_layer_center);
background = Bitmap.CreateScaledBitmap(background, width, rowHeight, true);
}
int backgroundWidth = background.Width;
int backgroundHeight = background.Height;
for (int y = top; y < height; y += backgroundHeight)
{
for (int x = 0; x < width; x += backgroundWidth)
{
canvas.DrawBitmap(background, x, y, null);
}
}
}
base.DispatchDraw(canvas);
}
}
}
Here is running screenshot. shelves will increase by the number of awards.
I upload my demo, you can set columns of shelves by yourself.
https://github.com/851265601/Xamarin.Android_ListviewSelect/blob/master/App17.zip

My collapsing toolbar is not collapsing

I can drag the toolbar manually and toolbar and navigation drawer are not working
together, collapsingtoolbar is collapsing a little but its is not merging into appbar. And why are there so many requirements for posting a question here , just adding these lines to meet the error of mostly code in your body
XML containing collapsing toolbar
<!-- TODO: Update blank fragment layout -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.NavigationView
android:id="#+id/nav_view1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_view" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="320dp"
android:text="What is PMKVY?"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textSize="30sp"
android:textStyle="bold"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="217dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="400dp"
android:fontFamily="serif"
android:text="#string/pmkvy_string"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="?android:attr/statusBarColor"
android:textSize="18sp"
android:textStyle="bold"
tools:layout_editor_absoluteX="-1dp"
tools:layout_editor_absoluteY="273dp" />
<Button
android:id="#+id/button6"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="140dp"
android:layout_marginTop="720dp"
android:background="#android:color/black"
android:drawableRight="#drawable/ic_play_circle"
android:text="Watch Video"
android:textAlignment="center"
android:textColor="#android:color/background_light"
android:textStyle="bold"
tools:layout_editor_absoluteX="141dp"
tools:layout_editor_absoluteY="519dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="840dp"
android:text="How to Enroll?"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="910dp"
android:fontFamily="serif"
android:text="#string/enroll_string"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="?android:attr/statusBarColor"
android:textSize="18sp"
android:textStyle="bold"
tools:layout_editor_absoluteX="-1dp"
tools:layout_editor_absoluteY="565dp" />
<Button
android:id="#+id/button7"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:background="#android:color/black"
android:text="Call 1800 102 6000"
android:textAlignment="center"
android:textColor="#android:color/background_light"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="1230dp"
android:gravity="center"
android:text="OR"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textSize="30sp"
android:textStyle="bold" />
<Button
android:id="#+id/button8"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="160dp"
android:layout_marginTop="1170dp"
android:background="#android:color/black"
android:text="Enroll"
android:textAlignment="center"
android:textColor="#android:color/background_light"
android:textStyle="bold" />
</RelativeLayout>
</ScrollView>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlways"
android:minHeight="20dp"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="25dp"
app:expandedTitleMarginEnd="30dp"
app:title="PMKVY">
<ImageView
android:id="#+id/toolbarImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
android:src="#drawable/pmkvy_logo"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="250dp"
android:background="#color/colorPrimaryDark"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
.java file of xml
package com.example.shrey.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class ScrollingActivity1 extends AppCompatActivity {
private DrawerLayout drawerlayout1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity_scrolling1);
drawerlayout1 = findViewById(R.id.drawer_layout2);
// set status bar color code
if (Build.VERSION.SDK_INT >= 21) {
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.material_blue_grey_950));
}
Toolbar toolbar = findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_navdrawer);
// <!-- code to change title and its color. -->
actionbar.setTitle(Html.fromHtml("<font color='#ffffff'>PMKVY</font>"));
NavigationView navigationView = findViewById(R.id.nav_view1);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
// set item as selected to persist highlight
menuItem.setChecked(true);
// close drawer when item is tapped
drawerlayout1.closeDrawers();
// Add code here to update the UI based on the item selected
// For example, swap UI fragments here
return true;
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerlayout1.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
}

Android TV move focus between RecyclerViews

I am building an Android TV app with the following layout:
Both lists on the left and on the right are RecyclerViews with vertical LinearLayoutManagers, Header view is static. Navigation with D-PAD works fine within one list, but when switching from one list to another there are issues. Focus moves from, say list1's item 5 to list2' item 5. When list 2 is short has less than 5 items, it just loses focus.
I want the last focused item index saved and when the user navigates list1-list2-list1 the item with that index to gain focus again and also prevent the views from losing focus. Is there any good solution to this?
Required behaviour:
user navigates to the top of the list and presses "UP" - focus stays where it was, nothing happens.
user navigates to the bottom of the list and presses "DOWN" - focus stays where it was, nothing happens.
user navigates from list1 to list2 - the item, that previously had focus in list2, gains focus or item0 gains focus if none was focused previously.
main layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="#+id/iv_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="27dp"
android:layout_marginLeft="48dp"
android:layout_marginRight="48dp"
android:layout_marginTop="27dp"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/iv_poster"
android:layout_width="#dimen/episodes_list_item_height"
android:layout_height="#dimen/episodes_list_image_width"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="#+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/tv_release_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
/>
<TextView
android:id="#+id/tv_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:drawableLeft="#drawable/ic_star_white_24dp"
android:drawablePadding="#dimen/view_padding_small"
/>
</FrameLayout>
<TextView
android:id="#+id/tv_description"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_seasons_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:focusable="false"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_episodes_list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:focusable="false"
/>
</LinearLayout>
</FrameLayout>
item layout, used for both lists:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/seasons_list_item_height"
android:orientation="horizontal"
android:focusable="true"
>
<TextView
android:id="#+id/tv_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_weight="1"
/>
<TextView
android:id="#+id/tv_episodes_count"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:ems="10"
/>
</LinearLayout>
Digging through Google's Leanback library sources answered my payers.
If you face the same trouble, just wrap your RecyclerViews with this and don't forget to set
private boolean mPersistFocusVertical = false;
if you want the focus to be persisted when searching horizontally, like in my layout.
Modify your main layout as below.
For directional navigation we can add properties like nextFocusDown, nextFocusLeft, nextFocusRight, nextFocusUp to focus relevant items as per our requirements.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="#+id/iv_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="27dp"
android:layout_marginLeft="48dp"
android:layout_marginRight="48dp"
android:layout_marginTop="27dp"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/iv_poster"
android:layout_width="#dimen/episodes_list_item_height"
android:layout_height="#dimen/episodes_list_image_width"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="#+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/tv_release_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
/>
<TextView
android:id="#+id/tv_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:drawableLeft="#drawable/ic_star_white_24dp"
android:drawablePadding="#dimen/view_padding_small"
/>
</FrameLayout>
<TextView
android:id="#+id/tv_description"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_seasons_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:focusable="false"
android:nextFocusDown="#+id/rv_episodes_list"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_episodes_list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:focusable="false"
android:nextFocusUp="#+id/rv_seasons_list"
/>
</LinearLayout>
Hope this will solve your problem !!

Resources