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);
Related
I have a ScrollView with a child LinearLayout. I am programmatically adding TextViews and NumberPickers to the LinearLayout through a separate View method. However the dynamic objects do not display when the tab containing the ScrollView is clicked.
Here is my code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.activity_settings_tab, container, false);
super.onCreate(savedInstanceState);
myDb = new DatabaseHelper(getContext());
rootview.findViewById(R.id.scroll_config);
viewFunds();
return rootview;
}
Here is the separate method I have mentioned that dynamically adds objects to the LinearLayout:
public View viewFunds(View rootview) {
ScrollView scrollView = new ScrollView(getActivity());
scrollView.findViewById(R.id.scroll_config);
LinearLayout linearLayout = new LinearLayout(getActivity());
linearLayout.findViewById(R.id.ll_config);
//final View linearLayout = getActivity().findViewById(R.id.ll_config);
linearLayout.removeAllViews(); //clear layout first - LINE WITH ISSUE
linearLayout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//create dynamic objects inside scrollview and dynamic linear layout - horizontal
for (int i = 0; i < res2.getCount(); i++) {
LinearLayout llh = new LinearLayout(getActivity());
llh.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lp_llh = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llh.setLayoutParams(lp_llh);
linearLayout.addView(llh);
NumberPicker numberPicker = new NumberPicker(getActivity());
numberPicker.setMinValue(0);
numberPicker.setMaxValue(100);
LinearLayout.LayoutParams lp_np = new LinearLayout.LayoutParams(70, LinearLayout.LayoutParams.WRAP_CONTENT);
numberPicker.setLayoutParams(lp_np);
numberPicker.setGravity(Gravity.CENTER_HORIZONTAL);
//showMessage("value",res2.getString(3));
numberPicker.setValue(Integer.parseInt(res2.getString(2))); //
TextView textView = new TextView(getActivity());
textView.setText( /*textArray[i] + " " +*/ res2.getString(1));
llh.addView(textView);
linearLayout.addView(numberPicker);
}
return scrollView;
}
What am I doing wrong here?
you are using fragment u have to find view with reference of fragment root view also your below statement doing nothing i will advise u to go through android basic components
rootview.findViewById(R.id.scroll_config);
to solve problem do following
public View viewFunds(View rootview) {
ScrollView scrollView = (ScrollView)rootview.findViewById(R.id.scroll_config);//considering scroll_config is scroll view in xml
LinearLayout linearLayout =(LinearLayout)rootview.findViewById(R.id.ll_config);//considering ll_config is LinearLayout in xml
linearLayout.removeAllViews(); //clear layout first - LINE WITH ISSUE
linearLayout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//create dynamic objects inside scrollview and dynamic linear layout - horizontal
for (int i = 0; i < res2.getCount(); i++) {
LinearLayout llh = new LinearLayout(getActivity());
llh.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lp_llh = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llh.setLayoutParams(lp_llh);
linearLayout.addView(llh);
NumberPicker numberPicker = new NumberPicker(getActivity());
numberPicker.setMinValue(0);
numberPicker.setMaxValue(100);
LinearLayout.LayoutParams lp_np = new LinearLayout.LayoutParams(70, LinearLayout.LayoutParams.WRAP_CONTENT);
numberPicker.setLayoutParams(lp_np);
numberPicker.setGravity(Gravity.CENTER_HORIZONTAL);
//showMessage("value",res2.getString(3));
numberPicker.setValue(Integer.parseInt(res2.getString(2))); //
TextView textView = new TextView(getActivity());
textView.setText( /*textArray[i] + " " +*/ res2.getString(1));
llh.addView(textView);
linearLayout.addView(numberPicker);///chek in which layou you want to add numberPicker
}
}
and in oncreateview
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.activity_settings_tab, container, false);
super.onCreate(savedInstanceState);
myDb = new DatabaseHelper(getContext());
viewFunds(rootview);
return rootview;
}
also go through android training https://developer.android.com/training/index.html
I want to create the table layout that is created dynamically inside the custom dialog. Can anyone tell me how to do this?
I did some thing but nothing is showing within the dialog. I had no table layout within that dialog.
Can anyone tell me where I made the mistake, please?
This is my activity:
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialog_view = inflater.inflate(R.layout.progressdialog, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
dialog.setView(dialog_view);
TableLayout table_layout = (TableLayout)dialog_view.findViewById(R.id.Table_view_in_dialog);
for (int i = 1; i <= 4; i++) {
TableRow row = new TableRow(getActivity());
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// inner for loop
for (int j = 1; j <= 4; j++) {
TextView tv = new TextView(getActivity());
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv.setBackgroundResource(R.drawable.cell_shape);
tv.setPadding(5, 5, 5, 5);
tv.setText("R " + i + ", C" + j);
row.addView(tv);
}
table_layout.addView(row);
}
AlertDialog alert = dialog.create();
alert.show();
Why don't you use dialog fragment , http://developer.android.com/reference/android/app/DialogFragment.html
This is best to use as you can manage this like fragment as this has own UI
as
public class SearchWeeklyAlertDialog extends DialogFragment
{
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.NewDialog);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_serach_days, container);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// your stuff like find ids and perform operations
}
}
and You can call this as
SearchWeeklyAlertDialog dialog = new SearchWeeklyAlertDialog();
dialog.show(getChildFragmentManager(), DIALOG_FRAGMENT);
I have a fragment and I tried to add a dynamical programmatically radio button into a radio group. The radio button is added, but the text is not set.
This is my code:
private View myFragmentView;
private Context context;
private RadioGroup radioGrup;
private ArrayList<Language> allLanguages;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
allLanguages=MyAsyncTask.getAllLanguages();
myFragmentView = inflater
.inflate(R.layout.language_view, container, false);
context = this.getActivity().getApplicationContext();
radioGrup = (RadioGroup)myFragmentView.findViewById(R.id.allLanguages);
addRadioButton();
return myFragmentView;
}
#SuppressLint({ "ResourceAsColor", "NewApi" })
private void addRadioButton(){
for(Language language:allLanguages){
RadioButton newRadioButton = new RadioButton(context);
newRadioButton = new RadioButton(context);
newRadioButton.setText("something");
newRadioButton.setTextColor(android.R.color.black);
newRadioButton.setLayoutParams
(new LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
radioGrup.addView(newRadioButton);
}
}
Can someone to help me?
Modify your code to
private void addRadioButton(){
for(Language language:allLanguages){
RadioButton newRadioButton = new RadioButton(context);
newRadioButton = new RadioButton(context);
newRadioButton.setLayoutParams
(new LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
newRadioButton.setText("something");
newRadioButton.setTextColor(android.R.color.black);
radioGrup.addView(newRadioButton);
}
}
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);
}
What I have are 3 Layouts.
fragment_tag, fragment_stunde and fragment_fach
fach should be in stunde and stunde in tag.
And in fach are some TextViews I want to set the text.
So my code so far is:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout tag = (LinearLayout)inflater.inflate(R.layout.fragment_tag, container, true);
database = new Database(context);
database.open();
for (int j = 1; j < 12; j++) {
LinearLayout stunde = (LinearLayout) inflater.inflate(R.layout.fragment_stunde, tag, true);
String[][] vplanDaten = database.getVplan(Integer.valueOf(DateHelper.get(DateHelper.WEEK_OF_YEAR)), index + 1, j);
for (int k = 0; k < vplanDaten.length; k++) {
LinearLayout fach = (LinearLayout) inflater.inflate(R.layout.fragment_fach, stunde, true);
TextView fach_lehrer = (TextView) fach.findViewById(R.id.textView_lehrer);
TextView fach_fach = (TextView) fach.findViewById(R.id.textView_fach);
TextView fach_raum = (TextView) fach.findViewById(R.id.textView_raum);
fach_lehrer.setText(vplanDaten[k][0]);
fach_fach.setText(vplanDaten[k][1]);
fach_raum.setText(vplanDaten[k][2]);
}
}
database.close();
return tag;
}
But this gives me an Error:
java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to android.widget.LinearLayout
at de.MayerhoferSimon.Vertretungsplan.TagFragment.onCreateView(TagFragment.java:56)
So how do I have to change the code that that what I want to do happens?
Try making the following changes:
LinearLayout tag = (LinearLayout)inflater.inflate(R.layout.fragment_tag, container, true);
to:
LinearLayout tag = (LinearLayout)(inflater.inflate(R.layout.fragment_tag, null)).findViewById(R.id.idOfTag);
Similarly:
LinearLayout stunde = (LinearLayout) inflater.inflate(R.layout.fragment_stunde, tag, true);
to:
LinearLayout stunde = (LinearLayout)(inflater.inflate(R.layout.fragment_stunde, null)).findViewById(R.id.idOfStunde);
And:
LinearLayout fach = (LinearLayout) inflater.inflate(R.layout.fragment_fach, stunde, true);
to:
LinearLayout fach = (LinearLayout)(inflater.inflate(R.layout.fragment_fach, null)).findViewById(R.id.idOfFach);
Change the LinearLayout to a View like such:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View tag = inflater.inflate(R.layout.fragment_tag, container, true);
//Everything else...
return tag;
}
Also I believe you can only inflate one view per onCreateView otherwise the layouts will replace one another. You need to have three separate fragments that each inflate its own view and call these fragments individually from the top parent activity.