Question

Need add view component in android parent layout from another class Xamarin. Following way did tried but not able to create view component without context.

/// <summary>
/// Second activity is intended to have a layout which will populated by another class 
/// </summary>
public class SecondActivity : Activity
{

    private LinearLayout homeScreenLinearLayout;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Create your application here
        homeScreenLinearLayout = new LinearLayout(this);
        homeScreenLinearLayout.Orientation = Orientation.Vertical;

        AddCompeonent ObjAddComponent = new AddCompeonent ();

        //The method returns the populated view
        View populatedParentView = ObjAddComponent.AddedView (homeScreenLinearLayout);
        //Show the view generated by add component class
        SetContentView(homeScreenLinearLayout);
    }
}

}

public class AddCompeonent
{
    private TextView homeScreenTextView;
    public View AddedView(View parentView){

        LinearLayout homeLayout =(LinearLayout) parentView;
        //Create textview here
        homeScreenTextView = new TextView (this);
        homeScreenTextView.Text = "here you will load home screen";
        //Add it to the layout
        homeLayout.AddView (homeScreenTextView);
        return homeLayout;
    }
}
Was it helpful?

Solution

If I understand you correctly replace

homeScreenTextView = new TextView (this); 

with

homeScreenTextView = new TextView (parentView.getContext());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top