Reputation: 731
i am new to programming and i tried to to make a actionbar with 2 tabs, what i need to to is, when clicking the 1st tab 2 buttons appear, i tried to do that using SetContentView(button); but that just showed one button fullscreen
Problem: I need it to show 2 buttons instead of 1
Code:
private void Pudisoo_TabSelected1(object sender, ActionBar.TabEventArgs e)
{
Button btnon = new Button(this);
btnon.Text = "ON";
btnon.Click += Btnon_Click1;
SetContentView(btnon);
Button btnoff = new Button(this); //<--- I can only see this button
btnoff.Click += Btnoff_Click;
SetContentView(btnoff); //<--- Because of this
}
Upvotes: 0
Views: 52
Reputation: 49
You haven't specified where you want these buttons to be shown to the user;
The SetContentView
method is used to create a view for an activity by 'inflating' a layout file. You should be creating the majority of your views (e.g. the buttons) through this layout file rather than in code, especially if you are not familiar with C#, Android or programming in general.
Here is some axml to get you started:
<?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">
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnOff" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnOn" />
</LinearLayout>
And the code you should use in your Pudisoo_TabSelected1
method:
private void Pudisoo_TabSelected1(object sender, ActionBar.TabEventArgs e)
{
SetContentView(Resource.Layout.filenamehere);
Button btnOn = FindViewById<Button> (Resource.Id.btnOn);
Button btnOff = FindViewById<Button> (Resource.Id.btnOff);
btnOff.Click += Btnoff_Click;
btnOn.Click += Btnon_Click1;
}
Although this entire approach is actually incorrect (you should be using fragments to achieve this effect) it will at least do what you want it to do and can serve as a starting point while you become more comfortable.
Upvotes: 1