Phoneswapshop
Phoneswapshop

Reputation: 1397

How to open a Xamarin Forms page from a Xamarin Droid Activity from a onclick listener

I am working on adding an assist feature to my app that when the user holds down on the home button an android activity opens with cards that the user can click on that then opens the xamarin forms page in question for example when the math card is clicked the MathPage xamarin forms page opens here's my android layout code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_tools_home_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:id="@+id/whole"
        tools:context="${relativePackage}.${activityClass}"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="0dp">
        <LinearLayout
            android:layout_width="wrap_content"
            android:id="@+id/scroll"
            android:layout_height="wrap_content">
            <android.support.v7.widget.CardView
                android:id="@+id/one"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardBackgroundColor="@color/cardsbg"
                app:cardElevation="3dp"
                android:clickable="false"
                tools:layout_conversion_absoluteX="16dp"
                tools:layout_conversion_absoluteY="8dp"
                tools:layout_conversion_absoluteWidth="379dp"
                tools:layout_conversion_absoluteHeight="540dp">
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <android.support.v7.widget.CardView
                        android:id="@+id/webcard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="4dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="8dp"
                        android:foreground="?android:attr/selectableItemBackground"
                        app:cardBackgroundColor="@color/card_background"
                        app:cardElevation="3dp"
                        android:clickable="true"
                        android:onClick="web">
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
                            <ImageView
                                android:id="@+id/webicon"
                                android:layout_width="68dp"
                                android:layout_height="68dp"
                                android:layout_centerVertical="true"
                                android:padding="5dp"
                                android:src="@drawable/web" />
                            <TextView
                                android:id="@+id/web"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:layout_toRightOf="@id/webicon"
                                android:text="Internet"
                                android:paddingLeft="8dp"
                                android:textColor="@color/cardText"
                                android:textSize="20sp" />
                        </RelativeLayout>
                    </android.support.v7.widget.CardView>
                    <android.support.v7.widget.CardView
                        android:id="@+id/mailcard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:foreground="?android:attr/selectableItemBackground"
                        android:layout_marginBottom="4dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="4dp"
                        android:layout_below="@+id/webcard"
                        app:cardBackgroundColor="@color/card_background"
                        app:cardElevation="3dp"
                        android:clickable="true"
                        android:onClick="email">
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
                            <ImageView
                                android:id="@+id/emailicon"
                                android:layout_width="68dp"
                                android:layout_height="68dp"
                                android:layout_centerVertical="true"
                                android:padding="5dp"
                                android:src="@drawable/email" />
                            <TextView
                                android:id="@+id/email"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:paddingLeft="8dp"
                                android:layout_centerVertical="true"
                                android:layout_toRightOf="@id/emailicon"
                                android:text="Email"
                                android:textColor="@color/cardText"
                                android:textSize="20sp" />
                        </RelativeLayout>
                    </android.support.v7.widget.CardView>
                    <android.support.v7.widget.CardView
                        android:id="@+id/mathcard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:foreground="?android:attr/selectableItemBackground"
                        android:layout_marginBottom="4dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="4dp"
                        android:layout_below="@+id/mailcard"
                        app:cardBackgroundColor="@color/card_background"
                        app:cardElevation="3dp"
                        android:clickable="true"
                        android:onClick="math">
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
                            <ImageView
                                android:id="@+id/musicicon"
                                android:layout_width="68dp"
                                android:layout_height="68dp"
                                android:layout_centerVertical="true"
                                android:padding="5dp"
                                android:src="@drawable/math" />
                            <TextView
                                android:id="@+id/music"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:layout_toRightOf="@id/musicicon"
                                android:text="Math"
                                android:paddingLeft="8dp"
                                android:textColor="@color/cardText"
                                android:textSize="20sp" />
                        </RelativeLayout>
                    </android.support.v7.widget.CardView>
                    <android.support.v7.widget.CardView
                        android:id="@+id/scicard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:foreground="?android:attr/selectableItemBackground"
                        android:layout_marginBottom="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="4dp"
                        android:layout_below="@+id/mathcard"
                        app:cardBackgroundColor="@color/card_background"
                        app:cardElevation="3dp"
                        android:clickable="true"
                        android:onClick="science">
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
                            <ImageView
                                android:id="@+id/playicon"
                                android:layout_width="68dp"
                                android:layout_height="68dp"
                                android:layout_centerVertical="true"
                                android:padding="8dp"
                                android:src="@drawable/sci" />
                            <TextView
                                android:id="@+id/play"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:paddingLeft="8dp"
                                android:layout_toRightOf="@+id/playicon"
                                android:text="Science"
                                android:textColor="@color/cardText"
                                android:textSize="20sp" />
                        </RelativeLayout>
                    </android.support.v7.widget.CardView>
                    <android.support.v7.widget.CardView
                        android:id="@+id/englishcard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:foreground="?android:attr/selectableItemBackground"
                        android:layout_marginBottom="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="4dp"
                        android:layout_below="@+id/scicard"
                        app:cardBackgroundColor="@color/card_background"
                        app:cardElevation="3dp"
                        android:clickable="true"
                        android:onClick="english">
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
                            <ImageView
                                android:id="@+id/playicon"
                                android:layout_width="68dp"
                                android:layout_height="68dp"
                                android:layout_centerVertical="true"
                                android:padding="8dp"
                                android:src="@drawable/handwrittin" />
                            <TextView
                                android:id="@+id/play"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:paddingLeft="8dp"
                                android:layout_toRightOf="@+id/playicon"
                                android:text="Handwriting"
                                android:textColor="@color/cardText"
                                android:textSize="20sp" />
                        </RelativeLayout>
                    </android.support.v7.widget.CardView>
                    <android.support.v7.widget.CardView
                        android:id="@+id/flashcard"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:foreground="?android:attr/selectableItemBackground"
                        android:layout_marginBottom="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="4dp"
                        android:layout_below="@+id/englishcard"
                        app:cardBackgroundColor="@color/card_background"
                        app:cardElevation="3dp"
                        android:clickable="true"
                        android:onClick="flashcard">
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
                            <ImageView
                                android:id="@+id/playicon"
                                android:layout_width="68dp"
                                android:layout_height="68dp"
                                android:layout_centerVertical="true"
                                android:padding="8dp"
                                android:src="@drawable/flashcards" />
                            <TextView
                                android:id="@+id/play"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:paddingLeft="8dp"
                                android:layout_toRightOf="@+id/playicon"
                                android:text="Flashcards"
                                android:textColor="@color/cardText"
                                android:textSize="20sp" />
                        </RelativeLayout>
                    </android.support.v7.widget.CardView>
                    <RelativeLayout
                        android:id="@+id/bttom"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/flashcard"
                        android:paddingTop="8dp"
                        android:layout_marginBottom="8dp">
                        <Button
                            android:id="@+id/button_2"
                            android:onClick="cam"
                            android:foreground="?android:attr/selectableItemBackground"
                            android:layout_alignParentLeft="true"
                            android:layout_marginLeft="50dp"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:background="@drawable/settings" />
                        <Button
                            android:id="@+id/button_1"
                            android:onClick="settings"
                            android:foreground="?android:attr/selectableItemBackground"
                            android:layout_alignParentRight="true"
                            android:layout_marginRight="50dp"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:background="@drawable/settings" />
                    </RelativeLayout>
                </RelativeLayout>
            </android.support.v7.widget.CardView>
        </LinearLayout>
        <Button
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:id="@+id/button_4"
            android:onClick="now"
            android:layout_marginTop="-42dp"
            android:layout_below="@+id/scroll"
            android:foreground="?android:attr/selectableItemBackground"
            android:clipToPadding="false"
            android:layout_centerHorizontal="true"
            android:background="@drawable/books" />
    </RelativeLayout>
</RelativeLayout>

heres my code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms.Platform.Android;

namespace AppName.Droid
{
[Activity(LaunchMode = LaunchMode.SingleInstance, Theme = "@style/Theme.Transparent")]
    [IntentFilter(new[] { Intent.ActionAssist }, Categories = new[] { Intent.CategoryDefault })]    
    public class ToolBelt : FormsApplicationActivity
    {

        Button mathbutton;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

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

        }


        //Code That Opens The Math  Part!

        [Java.Interop.Export("math")] // The value found in android:onClick attribute.
        public void btnOneClick4(View v) // Does not need to match value in above attribute.
        {

            //code to open the MathPage Forms Page Goes here
        }

        //Code That Opens The Internet  App!

        [Java.Interop.Export("web")] // The value found in android:onClick attribute.
        public void btnOneClick8(View v) // Does not need to match value in above attribute.
        {
            var uri = Android.Net.Uri.Parse("http://www.google.com");
            var intent = new Intent(Intent.ActionView, uri);
            StartActivity(intent);
        }



        //Code That Opens The Gmail App!

        [Java.Interop.Export("email")] // The value found in android:onClick attribute.
        public void btnOneClick3(View v) // Does not need to match value in above attribute.
        {
            var intent = PackageManager.GetLaunchIntentForPackage("com.google.android.gm");
            StartActivity(intent);
        }
    }
}

Upvotes: 0

Views: 1096

Answers (1)

SushiHangover
SushiHangover

Reputation: 74144

Here is a simple example...

A NON-Form's based Page Selection Activity:

User taps a button and we create an Intent to open the main Forms' based Activity and add a string Extra with the "page" that that user selected.

[Activity(Label = "PageSelectionActivity", Theme = "@style/MyTheme",  Name = "com.sushhangover.openpage.PageSelectionActivity")]
public class PageSelectionActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.PageSelection);
        Button button = FindViewById<Button>(Resource.Id.myButton);
        button.Click += delegate 
        {
            var intent = new Intent(ApplicationContext, typeof(MainActivity));
            intent.PutExtra("page", "StackOverflowPage");
            StartActivity(intent);          
        };
    }
}

Modify your MainActivity to obtain the Extra (if any) and pass it to the App .actor:

[Activity(Label = "OpenPage.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

        var page = Intent.GetStringExtra("page");
        LoadApplication(new App(page));
    }
}

Modify the App default .actor to accept parameter and act upon it:

public class App : Application
{
    public App(string pageName = "SomeDefaultMainApplicationEntryPage")
    {
        switch (pageName)
        {
            case "StackOverflowPage":
                MainPage = new NavigationPage(new StackOverflowPage);
                break;
            default:
                MainPage = new NavigationPage(new SomeDefaultMainApplicationEntryPage);
                break;
        }
    }
}

Testing

You can open the page selection Activity using adb to launch it:

adb shell am start -n com.sushhangover.openpage/.PageSelectionActivity

Note: You will need to handle the Xamarin.Form Page life-cycles as it can get messy depending upon if your ToolBelt Activity is opened while the user already has your app open and it is in the middle of some activity and you destroy their work by re-starting the app....

Upvotes: 1

Related Questions