I'm guessing this is easy, but can't seem to nail it.
I want to define a button in an axml file, and link the click up to a handler. I know that I can manually set the handler using FindViewById<Button>(Resource.Id.Button2).Click += buttonClick;, however I'm wondering if that's necessary.
How do I have the handler bound by merit of android:onClick="buttonClick" in the axml file? The error thrown is
Could not find a method buttonClick(View) in the activity class md....Activity1 for onClick handler on view class android.widget.Button with id 'Button1'
[Activity (Label = "SlidingMenuExample", MainLauncher = true)]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
SetContentView (Resource.Layout.menu);
}
public void buttonClick(object sender, EventArgs e) {
}
// This seems similar to the Java approaches. Why doesn't it work?
//public void buttonClick(View view) {
//}
}
<?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">
<Button
android:id="#+id/Button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button 2"
android:onClick="buttonClick" />
</LinearLayout>
Thanks.
Sure you can do that. You need to add a reference to Mono.Android.Export.
Then you need to annotate your method like:
[Export("buttonClick")]
private void ButtonClick(View view)
{
}
If you don't annotate the method, the Java world won't know of your method in the Activity.
Related
I got a simple layout with TabLayout and PageViewer but can't get them to work together
<com.google.android.material.tabs.TabLayout
android:id="#+id/sliding_tabs_emoji"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabMode="scrollable"
app:tabGravity="fill"
app:tabIndicatorColor="#color/gray"
app:tabIndicatorHeight="1dp"
android:paddingBottom="2dp"/>
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewpager_emoji"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/red"/>
In my Activity OnCreate initializing the following
//Fragment array
var fragments = new AndroidX.Fragment.App.Fragment[]
{
Library.Fun.Emoji.Fragments.Recent.NewInstance(),
Library.Fun.Emoji.Fragments.People.NewInstance(),
};
//Tab title array
var titles = Android.Runtime.CharSequence.ArrayFromStringArray(new[] {
"Recent" ,
"People"
});
var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager_emoji);
//viewpager holding fragment array and tab title text
viewPager.Adapter = new EmojiTabsPagerAdapter(SupportFragmentManager, fragments, titles);
// Give the TabLayout the ViewPager
TAB_Layout.SetupWithViewPager(viewPager);
Toast.MakeText(this, "SET", ToastLength.Short);
TAB_Layout.GetTabAt(0).SetIcon(Resource.Drawable.ic_camera);
TAB_Layout.GetTabAt(1).SetIcon(Resource.Drawable.ic_camera);
Where the Adapter is simple as it could be as following
public class EmojiTabsPagerAdapter : FragmentPagerAdapter
{
private readonly AndroidX.Fragment.App.Fragment[] fragments;
private readonly ICharSequence[] titles;
public EmojiTabsPagerAdapter(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment[] fragments, ICharSequence[] titles) : base(fm)
{
this.fragments = fragments;
this.titles = titles;
}
public override int Count
{
get
{
return fragments.Length;
}
}
public override AndroidX.Fragment.App.Fragment GetItem(int position)
{
return fragments[position];
}
public override ICharSequence GetPageTitleFormatted(int position)
{
//return titles[position];
return null;
}
}
Tabs are appearing but Fragments are not loading, The results are the following
Where it should load RECENT and PEOPLE fragments on each tab, my Fragment XML is the following
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="300dp"
android:background="#color/green">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="RECENTS"/>
</RelativeLayout>
And fragment's code is the following (for RECENT)
public class Recent : AndroidX.Fragment.App.Fragment
{
public static Recent NewInstance()
{
var frag = new Recent { Arguments = new Bundle() };
return frag;
}
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.fragment_emoji_recent, container, false);
return view;
}
}
Any Idea what am I doing wrong?
I use two Fragments: TabFragment1, TabFragment2 to test. In your code, you did provide the code about SetupWithViewPager.
The code below works well for me.
void setupViewPager(Android.Support.V4.View.ViewPager viewPager)
{
var adapter = new Adapter(SupportFragmentManager);
adapter.AddFragment(new TabFragment1(), "First Fragment");
adapter.AddFragment(new TabFragment2(), "Second Fragment");
viewPager.Adapter = adapter;
viewpager.Adapter.NotifyDataSetChanged();
//viewpager.OffscreenPageLimit(4);
}
In MainActivity, you could set like below.
var tabLayout = FindViewById<TabLayout>(Resource.Id.tabs);
tabLayout.SetupWithViewPager(viewpager);
You could download the source file from the link: https://www.c-sharpcorner.com/article/xamarin-android-create-viewpager-tablayout-floatingactionbutton-supportacti/
Did this work before moving to AndroidX?
I think you may need to pass ChildFragmentManager instead of FragmentManager to your PagerAdapter.
I am new to Xamarin android. I keep getting this error and have no idea how to get rid of it.
Error CS0117 'Resource.Id' does not contain a definition for 'button_1'
This is the code I'm using. The code works when I add a breakpoint to the Button button21 line but I keep getting an error in my visual studio editor. I am trying to use button click to navigate to another page.
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
Button button21 = FindViewById<Button>(Resource.Id.button_1);
button21.Click += Button_Click;
}
private void Button_Click(object sender, EventArgs e)
{
SetContentView(Resource.Layout.layout1);
}
<?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:minWidth="25px"
android:minHeight="25px">
<Button
android:text="Next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button_1" />
</LinearLayout>
I have 3 EditText:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusedByDefault="false"
android:minWidth="25px"
android:minHeight="25px"
android:id="#+id/editText1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusedByDefault="true"
android:clickable="false"
android:id="#+id/editText2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusedByDefault="false"
android:id="#+id/editText3" />
I want when the activity is opened the second EditText to be focused(with the cursor blinking in it) but when I tap on it I don't want the keyboard to show up.
Here's an example I put together that works for me. And with this solution, you can remove the "focusedByDefault" and "clickable" attributes from all of your EditText views in your layout file.
public class MainActivity : AppCompatActivity, View.IOnTouchListener {
private EditText editText2;
protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.main);
editText2 = FindViewById<EditText>(Resource.Id.editText2);
editText2.RequestFocus();
editText2.SetOnTouchListener(this); // Requires addition of View.IOnTouchListener interface to class
}
public bool OnTouch(View v, MotionEvent e) {
v.OnTouchEvent(e);
var imm = (Android.Views.InputMethods.InputMethodManager)v.Context.GetSystemService(InputMethodService);
imm?.HideSoftInputFromWindow(v.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
return true;
}
}
My problem is that I require the first pages of my app to be Native. I need a way to Navigate from a Xamarin Forms Content Page to the first native pages of the app when a user signs out. Is there anyway of from a Forms page starting a native ViewController (iOS) or starting an Activity (Android). I use custom renderers regularly.
It would be brilliant if I could somehow restart the app or call AppDelegate again or something.
Any help is appreciated.
If you use custom renderers regularly, then you can create a custom view renderer like this. For example:
In PCL:
public class NewView : View
{
}
In Android platform, create a layout under /Resources/layout first like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/tv"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="this is new view." />
<Button android:id="#+id/btn"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="change text" />
</LinearLayout>
Then create the renderer for NewView like this:
public class NewViewRenderer : ViewRenderer
{
private TextView tv;
private Android.Widget.Button btn;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
{
base.OnElementChanged(e);
if (Control == null)
{
var context = Xamarin.Forms.Forms.Context;
LayoutInflater minflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
var view = minflater.Inflate(Resource.Layout.newview, this, false);
tv = view.FindViewById<TextView>(Resource.Id.tv);
btn = view.FindViewById<Android.Widget.Button>(Resource.Id.btn);
SetNativeControl(view);
}
if (e.OldElement != null)
{
btn.Click -= Btn_Click;
}
if (e.NewElement != null)
{
btn.Click += Btn_Click;
}
}
private void Btn_Click(object sender, EventArgs e)
{
tv.Text = "Text changed!";
}
}
Finally use this view in ContentPage to make it like a page:
<ContentPage.Content>
<local:NewView />
</ContentPage.Content>
For iOS platform, there should be a method to set the ViewController as native control for a view renderer. But I'm not familiar with iOS, you may give a try.
I have two fragments in my main Activity.
Each fragment should play different youtube video.
But In my code,both the fragments are playing same video although I passes different url to the two instances of YouTubePlayerSupportFragment class.
MainActivity-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoFragment f1=VideoFragment.newInstance("kXYiU_JCYtU");
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container1, f1).commit();
VideoFragment f2 = new VideoFragment();
f2=VideoFragment.newInstance("k4V3Mo61fJM");
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container2, f2).commit();
}
activity_main.xml-
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity"
>
<FrameLayout
android:id="#+id/fragment_container1"
android:layout_width="fill_parent"
android:layout_height="150dp"
>
</FrameLayout>
<FrameLayout
android:id="#+id/fragment_container2"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/fragment_container1"
>
</FrameLayout>
</RelativeLayout>
VideoFragment class-
public VideoFragment() { }
public static VideoFragment newInstance(String url) {
VideoFragment f = new VideoFragment();
Bundle b = new Bundle();
b.putString("url", url);
f.setArguments(b);
f.init();
return f;
}
private void init() {
initialize("AIzaSyDM2vaVDpwGGFEcQCcD_hA38ZQo2Tqa06o", new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.cueVideo(getArguments().getString("url"));
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});
}
I would be grateful if anyone could help me.
Its impossible. Youtube player based on singletone patern, so every time you will get same player