How to show other layouts via button click? My code won't work

iamaurousiamaurous PHMember ✭✭
edited September 14 in Xamarin.Android

Hi, I have three layouts, the Main.axml and the new layouts I've made named SMS.axml and Menu.axml and two activity, the MainActivity and the SendSMSAct. There's a button in the Main that will redirect to Menu (this works perfectly fine) and also, there's a button in the Menu that when clicked, SMS.axml layout will show on the screen. To do that, I created a new Activity and named it SendSMSAct, here's my code:

namespace MySMS
{
    [Activity(Label = "SendSMSAct")]

    public class SendSMSAct : Activity
    {
        Button btnSMS;

        protected override void OnCreate(Bundle bundle)
        {

            base.OnCreate(bundle);

            btnSMS = FindViewById<Button>(Resource.Id.btnSMSMenu);

            btnSMS.Click += BtnSMS_Click;
        }

        private void BtnSMS_Click(object sender, EventArgs e)
        {
            SetContentView(Resource.Layout.SMS);
        }
    }
}

Tho it doesn't return any errors and debugging correctly, my SMS.axml never shows upon clicking the button, it got stuck in the Menu.axml.

And here's my code in my MainActivity.cs. Use to call the Menu.axml from the Main.axml. It's just the almost the same code as the one I used to call the SMS.axml from a button in the Menu.axml, so it leaves me wondering as to what I need to do

namespace MySMS
{
    [Activity(Label = "MySMS", MainLauncher = true)]

    public class MainActivity : Activity
    {
        Button btnSignIn;

        //private TextView mtxtSignUp;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            btnSignIn = FindViewById<Button>(Resource.Id.bntSignIn);

            btnSignIn.Click += BtnSignIn_Click;
    }

   private void BtnSignIn_Click(object sender, EventArgs e)
   {
           SetContentView(Resource.Layout.Menu);
   }
}

Thanks guys

Answers

  • shash12shash12 INMember ✭✭

    hello @iamaurous you can set visibility property of your layout either true or false.

  • iamaurousiamaurous PHMember ✭✭

    @shash12 said:
    hello @iamaurous you can set visibility property of your layout either true or false.

    Hi @shash12 , I've set it the visibility = visible, but to no avail

  • shash12shash12 INMember ✭✭

    @iamaurous can you share your .axml file?

  • iamaurousiamaurous PHMember ✭✭

    @shash12 , sure, here:

    for the Main.axml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:weightSum="100"
        android:minHeight="25px"
        android:minWidth="25px"
        android:paddingTop="50dp">
        <EditText
            android:id="@+id/EmailMain"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:paddingLeft="5dp"
            android:hint="Email Address"
            android:textColor="#929497"
            android:background="#FFFFFF"
            android:inputType="textEmailAddress"
            android:textColorHint="#929497"
            android:layout_gravity="center"
            android:layout_weight="13"
            android:backgroundTint="#00000000" />
        <EditText
            android:id="@+id/PasswordMain"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:paddingLeft="5dp"
            android:hint="Password"
            android:textColor="#929497"
            android:background="#FFFFFF"
            android:inputType="textPassword"
            android:textColorHint="#929497"
            android:layout_marginBottom="20dp"
            android:password="true"
            android:layout_gravity="center"
            android:layout_weight="13" />
        <Button
            android:text="Sign In"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/bntSignIn"
            android:layout_gravity="center"
            android:layout_weight="15"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:textSize="20dp"
            android:textStyle="normal"
            android:background="#4CBFF9" />
    </LinearLayout>
    

    For the Menu.axml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#D1D3D4"
        android:weightSum="100"
        android:paddingTop="50dp">
        <Button
            android:text="Send SMS"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/btnSMSMenu"
            android:textSize="20dp"
            android:layout_gravity="center"
            android:layout_weight="25"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:layout_marginBottom="25dp"
            android:background="#EAF8FE"
            android:textColor="#919698" />
        <Button
            android:text="send email"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/btnEmailMenu"
            android:textSize="20dp"
            android:layout_gravity="center"
            android:layout_weight="25"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:background="#EAF8FE"
            android:textColor="#919698" />
    </LinearLayout>
    

    And for the SMS.axml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:paddingTop="50dp"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:paddingBottom="30dp"
        android:weightSum="100"
        android:visibility="visible">
        <TextView
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/txtViewMessage"
            android:hint="Enter your message..."
            android:textColorHint="#D8D8D8"
            android:textSize="20dp"
            android:layout_gravity="center"
            android:layout_weight="50"
            android:textColor="#2B2B2B"
            android:layout_marginBottom="30dp" />
        <Button
            android:text="Send SMS"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/btnSendSMS"
            android:layout_gravity="center"
            android:layout_weight="10"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:background="#4CBFF9" />
    </LinearLayout>
    

    Also edited my question and added more details about this problem, hope these might help, thank you

  • shash12shash12 INMember ✭✭

    @iamaurous are you setting layout in your SendSMSAct activity SetContentView(Resource.Layout.SMS)?Because I can't see in SendSMSAct class.

  • iamaurousiamaurous PHMember ✭✭
    edited September 14

    Yes @shash12 , here's my SendSMSAct code:

        namespace MySMS
        {
            [Activity(Label = "SendSMSAct")]
    
            public class SendSMSAct : Activity
            {
                Button btnSMS;
    
                protected override void OnCreate(Bundle bundle)
                {
    
                    base.OnCreate(bundle);
    
                    btnSMS = FindViewById<Button>(Resource.Id.btnSMSMenu);
    
                    btnSMS.Click += BtnSMS_Click;
    
                }
    
                private void BtnSMS_Click(object sender, EventArgs e)
                {
                    ///StartActivity(typeof(CallSMSAct));
                    SetContentView(Resource.Layout.SMS);
                }
            }
        }
    
  • shash12shash12 INMember ✭✭

    @iamaurous you want to navigate from menu activity to SendSMSAct activity on button click.So when you are coming to SendSMSAct activity you are not setting SetContentView(Resource.Layout.SMS) inside OnCreate() Method.

  • iamaurousiamaurous PHMember ✭✭

    Hi @shash12 , isn't it supposed to be like this:

        namespace MySMS
            {
                [Activity(Label = "SendSMSAct")]
    
                public class SendSMSAct : Activity
                {
                    Button btnSMS;
    
                    protected override void OnCreate(Bundle bundle)
                    {
    
                        base.OnCreate(bundle);
    
                        btnSMS = FindViewById<Button>(Resource.Id.btnSMSMenu);
    
                        btnSMS.Click += BtnSMS_Click;
    
                    }
    
                    private void BtnSMS_Click(object sender, EventArgs e)
                    {
                        ///StartActivity(typeof(CallSMSAct));
                        SetContentView(Resource.Layout.SMS);
                    }
                }
            }
    

    Since I want the SMS.axml to show when the button btnSMSMenu was clicked on the Menu.axml?

    If not, can you please provide me an example? I'm totally clueless of what I need to do. Thank you

  • shash12shash12 INMember ✭✭

    @iamaurous Go to your MenuActivity and inside button click event handle method. write this piece of code

    Intent intent =new Intent(this,typeof(SendSMSAct));
    StartActivity(intent);

    then go to your SendSMSAct activity class and write SetContentView(Resource.Layout.SMS) inside onCreate(Bundle bundle) method.

  • shash12shash12 INMember ✭✭

    @iamaurous is it working?

Sign In or Register to comment.