Skip to content

Commit

Permalink
Port Phoneword and PopupMenu samples (#340)
Browse files Browse the repository at this point in the history
* Pop Up Menu Sample ported from Xamarin to dotnet 6

* Phoneword Sample ported from Xamarin to dotnet 6

* Move screenshots up 1 directory

We don't want them to show in the project

* Line breaks

* Better/faster TranslateToNumber

* Update to .NET 7

Co-authored-by: Jonathan Peppers <jonathan.peppers@microsoft.com>
  • Loading branch information
LeandraHall and jonathanpeppers authored Dec 19, 2022
1 parent 7587222 commit ba34601
Show file tree
Hide file tree
Showing 60 changed files with 428 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Phoneword/Phoneword.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32519.111
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Phoneword", "Phoneword\Phoneword.csproj", "{9C2983B9-50D8-4BD8-93B7-72027DE935E8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9C2983B9-50D8-4BD8-93B7-72027DE935E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9C2983B9-50D8-4BD8-93B7-72027DE935E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C2983B9-50D8-4BD8-93B7-72027DE935E8}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{9C2983B9-50D8-4BD8-93B7-72027DE935E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C2983B9-50D8-4BD8-93B7-72027DE935E8}.Release|Any CPU.Build.0 = Release|Any CPU
{9C2983B9-50D8-4BD8-93B7-72027DE935E8}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F4BD4825-E0F1-4313-BD5C-D9877C7D0635}
EndGlobalSection
EndGlobal
5 changes: 5 additions & 0 deletions Phoneword/Phoneword/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:label="Phoneword" android:icon="@mipmap/ic_launcher"></application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
37 changes: 37 additions & 0 deletions Phoneword/Phoneword/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Core;

namespace Phoneword;

[Activity(Label = "Phone Word", MainLauncher = true, Icon = "@mipmap/ic_launcher")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);

// Get our UI controls from the loaded layout
EditText phoneNumberText = RequireViewById<EditText>(Resource.Id.PhoneNumberText);
Button translateButton = RequireViewById<Button>(Resource.Id.TranslateButton);
TextView translatedPhoneWord = RequireViewById<TextView>(Resource.Id.TranslatedPhoneWord);

// Add code to translate number
translateButton.Click += (sender, e) =>
{
// Translate user's alphanumeric phone number to numeric
var translatedNumber = PhoneTranslator.ToNumber(phoneNumberText.Text);

if (string.IsNullOrWhiteSpace(translatedNumber))
{
translatedPhoneWord.Text = string.Empty;
Toast.MakeText(this, "Unable to translate number!", ToastLength.Long)!.Show();
}
else
{
translatedPhoneWord.Text = translatedNumber;
}
};
}
}
50 changes: 50 additions & 0 deletions Phoneword/Phoneword/PhoneTranslator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Text;

namespace Core;

public static class PhoneTranslator
{
public static string ToNumber(string? raw)
{
if (string.IsNullOrWhiteSpace(raw))
return "";
else
raw = raw.ToUpperInvariant();

var newNumber = new StringBuilder();
foreach (var c in raw)
{
if (" -0123456789".Contains(c))
newNumber.Append(c);
else
{
var result = TranslateToNumber(c);
if (result != null)
newNumber.Append(result);
}
// otherwise we've skipped a non-numeric char
}
return newNumber.ToString();
}

static int? TranslateToNumber(char c)
{
if ('A' <= c && c <= 'C')
return 2;
if ('D' <= c && c <= 'F')
return 3;
if ('G' <= c && c <= 'I')
return 4;
if ('J' <= c && c <= 'L')
return 5;
if ('M' <= c && c <= 'O')
return 6;
if ('P' <= c && c <= 'S')
return 7;
if ('T' <= c && c <= 'V')
return 8;
if ('W' <= c && c <= 'Z')
return 9;
return null;
}
}
12 changes: 12 additions & 0 deletions Phoneword/Phoneword/Phoneword.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-android</TargetFramework>
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<ApplicationId>com.companyname.Phoneword</ApplicationId>
<ApplicationVersion>1</ApplicationVersion>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
</PropertyGroup>
</Project>
44 changes: 44 additions & 0 deletions Phoneword/Phoneword/Resources/AboutResources.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Images, layout descriptions, binary blobs and string dictionaries can be included
in your application as resource files. Various Android APIs are designed to
operate on the resource IDs instead of dealing with images, strings or binary blobs
directly.

For example, a sample Android app that contains a user interface layout (main.xml),
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png)
would keep its resources in the "Resources" directory of the application:

Resources/
drawable/
icon.png

layout/
main.xml

values/
strings.xml

In order to get the build system to recognize Android resources, set the build action to
"AndroidResource". The native Android APIs do not operate directly with filenames, but
instead operate on resource IDs. When you compile an Android application that uses resources,
the build system will package the resources for distribution and generate a class called "Resource"
(this is an Android convention) that contains the tokens for each one of the resources
included. For example, for the above Resources layout, this is what the Resource class would expose:

public class Resource {
public class Drawable {
public const int icon = 0x123;
}

public class Layout {
public const int main = 0x456;
}

public class Strings {
public const int first_string = 0xabc;
public const int second_string = 0xbcd;
}
}

You would then use Resource.Drawable.icon to reference the drawable/icon.png file, or
Resource.Layout.main to reference the layout/main.xml file, or Resource.Strings.first_string
to reference the first string in the dictionary file values/strings.xml.
27 changes: 27 additions & 0 deletions Phoneword/Phoneword/Resources/layout/Main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="@string/Phoneword"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/PhoneWordPrompt" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/PhoneNumberText"
android:text="@string/PhoneNumber" />
<Button
android:text="@string/Translate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/TranslateButton" />
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/TranslatedPhoneWord" />
</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ic_launcher_background">#2C3E50</color>
</resources>
7 changes: 7 additions & 0 deletions Phoneword/Phoneword/Resources/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Phone Word</string>
<string name="PhoneNumber">1-855-XAMARIN</string>
<string name="Translate">Translate</string>
<string name="Phoneword">Enter a Phoneword:</string>
</resources>
21 changes: 21 additions & 0 deletions Phoneword/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
name: Xamarin.Android - Phoneword
description: "Sample app for the article, Hello, Android (Quickstart). This version of Phoneword incorporates all of the functionality... (get started)"
page_type: sample
languages:
- csharp
products:
- xamarin
extensions:
tags:
- getstarted
urlFragment: phoneword
---
# Phoneword

This sample app accompanies the article,
[Hello, Android (Quickstart)](https://docs.microsoft.com/xamarin/android/get-started/hello-android/hello-android-quickstart).
This version of **Phoneword** incorporates all of the functionality
explained in this article, and it can be used as the starting point for
the article,
[Hello, Android Multiscreen (Quickstart)](https://docs.microsoft.com/xamarin/android/get-started/hello-android-multiscreen/hello-android-multiscreen-quickstart).
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Phoneword/Screenshots/example-screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions PopupMenuDemo/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true">
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
33 changes: 33 additions & 0 deletions PopupMenuDemo/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace PopupMenuDemo;

[Activity(Label = "PopupMenuDemo", MainLauncher = true)]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle? bundle)
{
base.OnCreate(bundle);

SetContentView(Resource.Layout.Main);

Button showPopupMenu = RequireViewById<Button>(Resource.Id.popupButton);
showPopupMenu.Click += (s, arg) => {

PopupMenu menu = new PopupMenu(this, showPopupMenu);

// Call inflate directly on the menu:
menu.Inflate(Resource.Menu.popup_menu);

// A menu item was clicked:
menu.MenuItemClick += (s1, arg1) => {
Console.WriteLine("{0} selected", arg1.Item!.TitleFormatted);
};

// Menu was dismissed:
menu.DismissEvent += (s2, arg2) => {
Console.WriteLine("menu dismissed");
};

menu.Show();
};
}
}
12 changes: 12 additions & 0 deletions PopupMenuDemo/PopupMenuDemo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-android</TargetFramework>
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<ApplicationId>com.companyname.PopupMenuDemo</ApplicationId>
<ApplicationVersion>1</ApplicationVersion>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
</PropertyGroup>
</Project>
29 changes: 29 additions & 0 deletions PopupMenuDemo/PopupMenuDemo.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32519.111
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PopupMenuDemo", "PopupMenuDemo.csproj", "{66D4DE6D-4B56-4C20-A435-892687B9C26E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{66D4DE6D-4B56-4C20-A435-892687B9C26E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{66D4DE6D-4B56-4C20-A435-892687B9C26E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66D4DE6D-4B56-4C20-A435-892687B9C26E}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{66D4DE6D-4B56-4C20-A435-892687B9C26E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66D4DE6D-4B56-4C20-A435-892687B9C26E}.Release|Any CPU.Build.0 = Release|Any CPU
{66D4DE6D-4B56-4C20-A435-892687B9C26E}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6571484D-6BA8-45C9-9F8D-153C0B060A05}
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = PopupMenuDemo.csproj
EndGlobalSection
EndGlobal
18 changes: 18 additions & 0 deletions PopupMenuDemo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
name: Xamarin.Android - Popup Menu
description: "Demonstrates how to add support for displaying popup menus that are attached to a particular view"
page_type: sample
languages:
- csharp
products:
- xamarin
urlFragment: popupmenudemo
---
# Popup Menu Demo

**PopupMenuDemo** is a sample app that accompanies the article,
[PopUp Menu](https://docs.microsoft.com/xamarin/android/user-interface/controls/popup-menu).
It demonstrates how to add support for displaying popup menus that are attached to
a particular view.

![Popup menu in Android](Screenshots/PopupMenuDemo.png)
Loading

0 comments on commit ba34601

Please sign in to comment.