Adding dynamic views in a fragment - android

I am trying to add a dynamic view for my fragment.
I am using this code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Button myButton = new Button(Builtprofile.context);
myButton.setText("Press me");
myButton.setBackgroundColor(Color.YELLOW);
RelativeLayout myLayout = new RelativeLayout(Builtprofile.context);
myLayout.setBackgroundColor(Color.BLUE);
RelativeLayout.LayoutParams buttonParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
myLayout.addView(myButton, buttonParams);
View rootView = inflater.inflate(R.layout.q1, container, false);
// I want to add myLayout in place of R.layout.q1
return rootView;
}

#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
Button myButton = new Button(Builtprofile.context);
myButton.setText("Press me");
myButton.setBackgroundColor(Color.YELLOW);
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
View rootView = inflater.inflate(R.layout.q1, container, false);
RelativeLayout myLayout = (RelativeLayout)rootView.findViewById(R.id.mainLayout);
myLayout.setBackgroundColor(Color.BLUE);
myLayout.addView(myButton, buttonParams);
return rootView;
}
Have an empty xml layout with just a RelativeLayout called "mainLayout"(or what ever you want to call it). That way you can append any dynamically generated controls

LayoutInflater vi = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.xml, null);
container.addView(v);

Related

Can we modify a layout (programmatically) before inflating it in a fragment?

I have a simple fragment:
public class FR_FragmentPopup extends Fragment {
public FR_FragmentPopup() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
MainActivity.getMain().doFragment(new FR_RecycleViewSettings2(),R.id.over_frame_bottom,false);
return inflater.inflate(R.layout.fragment_popup, container, false);
}
}
I want to modify the xml to wrap height, but programmatically because I'm using the layout in other fragments also.I know that R.layout.fragment_popup is an int and not a view which makes it difficult to use edit it.
return inflater.inflate(
findViewById(R.layout.fragment_popup).setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
, container, false);
How can I do this?
You may do as following:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_help, container, false);
rootView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
return rootView;
}
Theoretically:
View view = inflater.inflate(R.layout.fragment_popup, container, false);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
return view;
Practically:
Never tried.

Add buttons dynamicaly in fragment

want to add buttons dynamically. I tried like this:
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.home, container, false);
Button newText = new Button(getActivity());
newText.setText("This is a fragment");
newText.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
return view;
}
Inside your View you can add a layout then add your button inside the layout,
like so:
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layoutContainer);
layout.addView(newText);

Button array in Fragment

I am new to Android Programming. I want to add an array of buttons fragment layout dynamically.
Activity:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list_dir,
container, false);
LinearLayout linearLayout=new LinearLayout(getActivity());
LayoutParams param=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(param);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams param2=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button dirButton[]=new Button[5];
for(int i=0;i<5;i++)
{
dirButton[i].setLayoutParams(param2);
linearLayout.addView(dirButton[i]);
}
ViewGroup vg=(ViewGroup)rootView;
vg.addView(linearLayout);
return rootView;
}
}
But this is causing Null pointer exception. But if only one button is used then it works like:
Button dirButton=new Button(getActivity());
Is there way to achieve the button array?
Arrays don't work like that. Try something like this:
Button[] buttonArray = new Button[5];
for(int i = 0; i < 5; i++)
{
buttonArray[i] = new Button(getActivity());
buttonArray[i].setLayoutParams(params);
linearLayout.addView(buttonArray[i]);
}
public static class PlaceholderFragment extends Fragment {
private static final String LOG_TAG = PlaceholderFragment.class.getSimpleName();
public PlaceholderFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_my, container, false);
LinearLayout linearLayout = new LinearLayout(getActivity());
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(param);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Button dirButton[] = new Button[5];
for(int i = 0; i < 5; i++) {
dirButton[i] = new Button(getActivity());
dirButton[i].setLayoutParams(param2);
linearLayout.addView(dirButton[i]);
}
ViewGroup vg = (ViewGroup) rootView;
vg.addView(linearLayout);
return rootView;
}
}
When you have an array of objects, the default value is null . You forgot to actually create the Button. Here is the part you needed to change :
dirButton[i] = new Button(getActivity());
dirButton[i].setLayoutParams(param2);

NullPointerException while adding view in a fragment dynamically

I am trying to dynamically add a TableLayout into a LinearLayout inside a fragment.
Code below:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View rootview = inflater.inflate(R.layout.dashboard_fragment, container, false);
dash_board = (LinearLayout) rootview.findViewById(R.layout.dashboard_fragment);
table = new TableLayout(getActivity());
TableRow tr_header = new TableRow(getActivity());
TextView Name = new TextView(getActivity());
Name.setText("Name");
LayoutParams Name_params = new LayoutParams();
Name_params.width= 100;
Name.setLayoutParams(Name_params);
Name.setPadding(2, 0, 2, 2);
tr_header.addView(Name);
TextView Points = new TextView(getActivity());
Points.setText("Points");
LayoutParams point_params = new LayoutParams();
point_params.width= 100;
Points.setLayoutParams(point_params);
Points.setPadding(2, 0, 2, 2);
tr_header.addView(Points);
table.addView(tr_header);
dash_board.addView(table);
return rootview;
}
But while adding table to the dash board layout (dash_board.addView(table);) I am getting a NullPointerException.
What could the issue be?
Actually there is no dashboard_fragment as a ViewGroup in rootView because the rootView is exacly the dashboard_fragment as a ViewGroup.So these two lines of code are the cause of exception:
View rootview =
inflater.inflate(R.layout.dashboard_fragment,container, false);
dash_board =
(LinearLayout)rootview.findViewById(R.layout.dashboard_fragment);
Actually the logic will be correct if you do like this:
View rootview =
inflater.inflate(R.layout.dashboard_fragment,container, false);
dash_board =
(LinearLayout) rootView;
And this is the simplest form for this process:
dash_board
=(LinearLayout)inflater.inflate(R.layout.dashboard_fragment,container, false);
Instead of this
View rootview = inflater.inflate(R.layout.dashboard_fragment, container, false);
dash_board = (LinearLayout) rootview.findViewById(R.layout.dashboard_fragment);
it should be
LinearLayout dash_board = (LinearLayout)inflater.inflate(R.layout.dashboard_fragment, container, false);

How to load layout in code from xml file

I'm using ViewPageIndicator and I have a problem with using XML layout in code. Usually I'm using setContentView(R.layout.activity_main).
Of course I can create layout in code but I prefer to use XML files.
public final class FragmentOne extends Fragment {
private static final String KEY_CONTENT = "FragmentOne";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView text = new TextView(getActivity());
text.setGravity(Gravity.CENTER);
text.setText("some text");
text.setTextSize(20 * getResources().getDisplayMetrics().density);
text.setPadding(20, 20, 20, 20);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.setGravity(Gravity.CENTER);
layout.addView(text);
return layout;
}
}
In onCreateView() you can use the LayoutInflater passed in as the first argument to inflate the layout.
Your code would look something like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.my_fragment_layout, container, false);
}

Categories

Resources