Search code examples
c#androidandroid-layoutandroid-activityxamarin.android

Controls showing in designer view but some controls not showing in android device when debugging?


I am new to xamarin.android, I created a blank android app and added two buttons and an image view in activity_main.xml, when debugging on my android device ( Redmi 5A) only imageview is showing and buttons are not showing.

Main.xml

    <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="match_parent">
    <ImageView
    android:src="@android:drawable/ic_menu_gallery"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="100dp"
    
    android:id="@+id/imageView1" />
<Button
    android:text="Capture Image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    android:layout_toLeftOf="@id/imageView1"
    android:layout_below="@+id/imageView1"
    
    android:id="@+id/button1" />
<Button
    android:text="Upload Image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/imageView1"
    android:layout_below="@+id/button1"
    
    android:id="@+id/button2" />

MainActivity.cs

         using Android.App;
         using Android.OS;
         using Android.Support.V7.App;
         using Android.Runtime;
         using Android.Widget;

          namespace Route_Problem_Update
          {
             [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
             public class MainActivity : AppCompatActivity
             {
                 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.Main);
                 }

                 public override void OnRequestPermissionsResult(int requestCode, string[] 
                 permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
                 {
                   Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, 
                   grantResults);

                   base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
                 }
                }
               }

I have tried: cleaning and rebuilding the solution. Checked that SetContentView (Resource.Layout.Main); was uncommented.

But none of these seems to work for me;

What can be the problem?


Solution

  • Your layout has some issue with two Buttons, you can try it like blow:

    <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="match_parent">
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:id="@+id/imageView1" />
    <Button
        android:text="Capture Image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:layout_below="@+id/imageView1"
        android:id="@+id/button1" />
    <Button
        android:text="Upload Image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:id="@+id/button2" />