Moataz Mahmoud
Moataz Mahmoud

Reputation: 39

Add linearLayout elements programmatically in activity Extends Fragment

How should I add linearLayout elements programmatically in activity Extends Fragment?

I tried to add views but it only works if activity extends Activity. Is there any other way to make it work in activity extends Fragment?

public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.news_screen, container, false);
LinearLayout myRoot = (LinearLayout) v.findViewById(R.id.my_root);
LinearLayout a = new LinearLayout(this);
a.setOrientation(LinearLayout.HORIZONTAL);
a.addView(view1);
a.addView(view2);
a.addView(view3);
myRoot.addView(a);
return v;
}

There is an error in.

LinearLayout a = new LinearLayout(this);

in (this)

Upvotes: 1

Views: 58

Answers (1)

Anamica Bisht
Anamica Bisht

Reputation: 199

write getActivity() in place of 'this' Try this once maybe it will help you

Thanks..

Upvotes: 2

Related Questions