I have a customized action sheet on my wear watch. This is because the normal action sheet is white and at the Android Store they want it to be black. The code for my CustomActionSheet.axml sheet is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/background_dark">
<TextView
android:id="@+id/message"
android:editable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:singleLine="false"
android:textSize="16sp"
android:gravity="center"
android:textColor="@android:color/primary_text_dark" />
<!-- divider -->
<TextView
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/darker_gray" />
<ListView
android:id="@+id/actionList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="true"
android:clickable="true" />
</LinearLayout>
This customized sheet is called by an interface IActionSheet.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using KorfballTimerWatch.Droid.Interfaces;
using KorfballTimerWatch.Interfaces;
using System.Threading.Tasks;
using KorfballTimerWatch.Droid;
using static Android.Icu.Text.CaseMap;
[assembly: Dependency(typeof(ActionSheet))]
namespace KorfballTimerWatch.Droid.Interfaces
{
class ActionSheet : IActionSheet
{
private TaskCompletionSource<String> tcs = null;
private String strCancel;
private AlertDialog dlg = null;
public Task<String> UseActionSheet(Page p, String title, String message, params String[] buttons)
{
if (tcs != null)
{
tcs.Task.Dispose();
}
if (dlg != null)
{
dlg = null;
}
tcs = new TaskCompletionSource<string>();
try
{
Activity act = MainActivity.PrimaryActivity;
if (act != null)
{
CreateDialog(title, message, buttons);
if (dlg != null)
{
dlg.Show();
}
}
}
catch (Exception e)
{
p.DisplayAlert("Error", e.Message, "OK");
}
return tcs.Task;
}
private void CreateDialog(String title, String message, params String[] buttons)
{
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.PrimaryActivity);
LayoutInflater inflater = Android.Views.LayoutInflater.FromContext(MainActivity.PrimaryActivity);
Android.Views.View v = inflater.Inflate(Resource.Layout.CustomActionSheet, null);
v.SetBackgroundColor( Android.Graphics.Color.Black);
// set the text widget
TextView tv = (TextView)v.FindViewById(Resource.Id.message);
if (tv != null)
{
tv.Text = title;
}
TextView tv1 = (TextView)v.FindViewById(Resource.Id.textView1);
if (tv1 != null)
{
tv1.Text = message;
}
// set up the list
Android.Widget.ListView lv = (Android.Widget.ListView)v.FindViewById(Resource.Id.actionList);
if (lv != null)
{
ArrayAdapter strAdapter = new ArrayAdapter(Android.App.Application.Context, Resource.Layout.TextViewItem, buttons);
lv.Adapter = strAdapter;
lv.ItemClick += Lv_ItemClick;
}
adb.SetView(v);
//adb.SetNegativeButton(cancel, OnItemSelect);
adb.SetCancelable(false);
dlg = adb.Create();
}
void Lv_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
if (sender is Android.Widget.ListView lv)
{
String s = lv.GetItemAtPosition(e.Position).ToString();
if (String.IsNullOrEmpty(s) == false)
{
tcs.TrySetResult(s);
dlg.Dismiss();
}
}
}
private void OnItemSelect(Object sender, DialogClickEventArgs e)
{
tcs.TrySetResult(strCancel);
dlg.Dismiss();
}
}
}
In my view I get a screen with a whitespace on top. How can I remove the whitespace?
