I have been trying to add buttons to my fragment dynamically but all the methods I have tried somehow doesnt work.
These are some methods I have tried:
1.
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
LinearLayout linearlayout = new LinearLayout(getActivity());
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearlayout.setLayoutParams(buttonParams);
linearlayout.setOrientation(LinearLayout.HORIZONTAL);
Button button = new Button(getActivity());
button.setLayoutParams(buttonParams);
button.setText("????????????????????");
button.setTextSize(16);
Button button2 = new Button(getActivity());
button2.setLayoutParams(buttonParams);
button2.setText("!!!!!!!!!!!!!!!!!!");
button2.setTextSize(64);
linearlayout.addView(button);
linearlayout.addView(button2);
container.addView(linearlayout);
View myView = inflater.inflate(R.layout.fragment_general_layout, container, false);
return myView;
}
This will give me the following screenshot First method. I really dont like this as this will create a button on the Activity itself which would appear on my other Fragments that are using the same Activity.
- For second method
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_general_layout, container, false);
for (int i = 0; i < ArrayOfNames.length; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(myView.getContext());
btn.setId(i);
final int id_ = btn.getId();
btn.setText(ArrayOfNames[i]);
btn.setBackgroundColor(Color.CYAN);
btn.setLayoutParams(params);
linearlayout.addView(btn, params);
btn = myView.findViewById(id_);
btn.setVisibility(View.VISIBLE);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do smth
}
});
return myView;
}
This method I assume will make the buttons (as the code runs fine and at least the buttons are not having a null pointer references when setting the OnClickListner(). But I cant see actual buttons on my screen. Also, not possible to click on it.
I tried to reference from these urls: Programmatically add buttons to a Fragment
Adding buttons programmatically to a fragment
How to add a button programmatically to a fragment
Any help will be really appreciated.
In the end, I have manged to do it. The second method had problem as there is no
btn = myView.findViewById(id_);
. This will be pointing to null although I have set my button's ID to be the value I stated. Using this method, it is easy to still get the value of the button by setting a separateonClick()
method.The
onCreateView
method expects you to return the view which you are inflating. Your code is good, but you're adding the buttons to the wrong view. Try this instead:This should add your buttons to your view. I'm assuming that your
R.layout.fragment_general_layout
is aLinearLayout
withorientation="vertical"