First of all I would like to say, that I want to say \"Hello in here\".
Requirements:
I should make it possible to create a client applicati
I got the same Problem and to my amzement I found a solution! Create a blank XML layout file like this...
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</FrameLayout>
In the fragment where your layout is dynamically created, inflate this blank layout XML file.
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.blank_layout, container, false);
return view;
Afterwords, in onViewCreated() method you can create your layout dynamically.
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// This will create the LinearLayout
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);
// Configuring the width and height of the linear layout.
LinearLayout.LayoutParams llLP = new LinearLayout.LayoutParams(
//android:layout_width="match_parent" an in xml
LinearLayout.LayoutParams.MATCH_PARENT,
//android:layout_height="wrap_content"
LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(llLP);
TextView tv = new TextView(mContext);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
//android:text="@string/c4r"
tv.setText("Hello android !");
//android:padding="@dimen/padding_medium"
tv.setPadding(8, 8, 8, 8);
ll.addView(tv);
ViewGroup viewGroup = (ViewGroup) view;
viewGroup.addView(ll);
}
}
package com.example.test;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class FragmentExample extends android.app.Fragment
{
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
//Set a linearLayout to add buttons
LinearLayout linearLayout = new LinearLayout(getActivity());
// Set the layout full width, full height
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(params);
linearLayout.setOrientation(LinearLayout.HORIZONTAL); //or VERTICAL
Button button = new Button(getActivity());
//For buttons visibility, you must set the layout params in order to give some width and height:
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
Button button2 = new Button(getActivity());
button2.setLayoutParams(params);
//... and other views
ViewGroup viewGroup = (ViewGroup) view;
linearLayout.addView(button);
linearLayout.addView(button2);
viewGroup.addView(linearLayout);
}
}