How do get rid of this resource id error on Xamarin Android? - xamarin.android

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>

Related

Android - EditText hint not updating in soft keyboard screen

The hint for my EditText when the soft keyboard is full screen mode is not reflecting the correct value after assigning a new value for hint to the EditText. Please see gif above.
Does anyone know how to resolve this? Below is isolated code to recreate this issue:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding mBinding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(mBinding.getRoot());
}
public void onButtonClick(View v) {
mBinding.barcodeEdittext.setHint("Some other Hint");
}
}
activity_main.xml
<?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="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/barcode_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Initial Hint" />
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:text="Change Hint!"
android:onClick="onButtonClick" />
</LinearLayout>

Issue with Forward and backward options in Video player in Xamarin Android

i need to display a video in mobile app which was developed in Xamarin(Andriod) in which the for forward it moving 15 sec ahead and for backward it is moving 5 sec back
Can i get some help on this
In your case you seems to use the MediaController . However , if you want to custom the length of forward and back , you need to create a custom MediaController .
in XML
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main"
android:orientation="vertical">
<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/videoView1" />
<LinearLayout
android:background="#android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:text="back"
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<Button
android:text="play"
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<Button
android:text="forward"
android:id="#+id/forward"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
in Activity
VideoView videoView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
//...
videoView = FindViewById<VideoView>(Resource.Id.videoView1);
videoView.SetVideoURI(Android.Net.Uri.Parse("xxx.mp4")); // Path of your saved video file.
videoView.Start();
Button back = FindViewById<Button>(Resource.Id.back);
back.Click += Back_Click;
Button play = FindViewById<Button>(Resource.Id.play);
play.Click += Play_Click;
Button forward = FindViewById<Button>(Resource.Id.forward);
forward.Click += Forward_Click; ;
}
private void Forward_Click(object sender, EventArgs e)
{
videoView.SeekTo(videoView.CurrentPosition + 5 * 1000);
}
private void Play_Click(object sender, EventArgs e)
{
var button = sender as Button;
if (videoView.IsPlaying)
{
button.Text = "Play";
videoView.Pause();
}
else
{
button.Text = "Pause";
videoView.Start();
}
}
private void Back_Click(object sender, EventArgs e)
{
videoView.SeekTo(videoView.CurrentPosition-15*1000);
}
And in this way you just need to set the icon of the Buttons as you want .

Click event fires when scrolling on EditText to see all its content(Xamarin.Android)

When I scroll in an EditText to see its content when it's too long, its click event fires. How can I prevent that? The EditText is not editable.
Here is my activity:
public class MainActivity : AppCompatActivity, View.IOnTouchListener
{
EditText et;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
et = FindViewById<EditText>(Resource.Id.editText1);
et.SetOnTouchListener(this);
et.Click += delegate { this.OnClicking(); };
}
private void OnClicking()
{
//Show alert dialog
}
public bool OnTouch(View v, MotionEvent e)
{
v.OnTouchEvent(e);
var imm = (Android.Views.InputMethods.InputMethodManager)v.Context.GetSystemService("input_method");
imm?.HideSoftInputFromWindow(v.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
return true;
}
}
Here is my axml file:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:text="Very long text Very long text Very long text Very long text Very long text "
android:layout_width="300dp"
android:focusable="false"
android:clickable="true"
android:singleLine="true"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:id="#+id/editText1" />
</LinearLayout>

How to make an EditText to be focused but not clickable?(Xamarin.Android)

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;
}
}

Handler for Button Declared in axml Layout

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.

Resources