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.
Related
I'm trying to add editText and textView to a fragment. The code to generate the editText and TExtView is correct because I've used it before, but in this case nothing appears. Any ideas?
Fragment code:
public class MarcadoresFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.fragment_marcadores, container, false);
return myFragmentView;
}
}
Add the editText:
public void createEditText(int size){
Log.d("det", String.valueOf(size));
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.fragment_marcadores, null);
LinearLayout layout = (LinearLayout) dialogView.findViewById(R.id.marcadoresFrag); // this is whatever view you want to add them to
EditText editText;
TextView textView;
if(layout!=null)
for(int i=0;i<size;i++){
textView = new TextView(dialogView.getContext());
textView.setText("Descrição do marcador " + String.valueOf(i + 1));
textView.layout(0, 5, 0, 5);
// add to the view
layout.addView(textView);
editText = new EditText(dialogView.getContext());
editText.setId(i);
editText.setHint("Ex: Bebedouro ndsfhjdsjvdsbjvjhvjdfjbvfjfvjklbjklfv" + i + 1);
Log.d("det", "jsdgbiusdbgksjd");
allEds.add(editText);
// add to the view
layout.addView(editText);
}
}
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);
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);
I need some help in setting the background colour using code for my current fragment. The fragment is created on selecting a particular tab.
Below is my main activity code
public class TabActionBarActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the text and background colour
setTheme(R.style.MyTheme);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
String label6 = getResources().getString(R.string.label6);
tab = actionBar.newTab();
tab.setText(label6);
TabListener<Tab6Fragment> tl6 = new TabListener<Tab6Fragment>(this,
label6, Tab6Fragment.class);
tab.setTabListener(tl6);
actionBar.addTab(tab);
String label7 = getResources().getString(R.string.label7);
tab = actionBar.newTab();
tab.setText(label7);
TabListener<Tab7Fragment> tl7 = new TabListener<Tab7Fragment>(this,
label7, Tab7Fragment.class);
tab.setTabListener(tl7);
actionBar.addTab(tab);
}
Now the code for the Tab7Fragment class looks like this
public class Tab7Fragment extends Fragment {
TextView tv1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ScrollView sv = new ScrollView(this.getActivity());
LinearLayout ll = new LinearLayout(this.getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this.getActivity());
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
EditText et = new EditText(this.getActivity());
et.setText("weeeeeeeeeee~!");
ll.addView(et);
Button b = new Button(this.getActivity());
b.setText("I don't do anything, but I was added dynamically. :)");
ll.addView(b);
for(int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(this.getActivity());
cb.setText("I'm dynamic!");
ll.addView(cb);
}
return this.getView();
}
}
Now how can i set the view for this fragment?
this.getActivity().setContentView(sv);
I know the above method is incorrect. But i need to set the content view with the scrollview layout. and another question is how do i set the background colour for this view?(using setBackgroundColor()?
Try this code in your onCreateView():
View view = inflater.inflate(R.layout.your_fragment_layout, container, false);
Then dont forget to return the view that you just inflated to the android runtime.
Remove return this.getView(); and add return view;
To set background color :
sv.setBackgroundColor(getRessources().getColors(R.color.yourcolor));
To inflate layout into a fragment, you need to return a View from onCreateView :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.your layout, container, false);
}
Hy, I have this code in my Fragment:
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fr_menu,container,false);
final ViewGroup dismissableContainer = (ViewGroup)view.findViewById(R.id.dismissable_container);
for (int i = 0; i < 3; i++) {
final Button dismissableButton = new Button(getActivity());
dismissableButton.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
dismissableButton.setText("Button " + (i + 1));
dismissableContainer.addView(dismissableButton);
}
return view;
}
in the same fragment I have a method that receives an object from the outside
public void sendObject(MyObject t){
Button b=new Button(getActivity());
b.setText(t.getNum_tav());
// Now I want to put **b** under button1 button2 and button3
}
in the method sendobject ... How can I change the fragment?
I have to put the button with the other 3 button
Just add it to the same ViewGroup, assuming it's a vertical LinearLayout:
getView().findViewById(R.id.dismissable_container).addView(b);
If it's not, then you'll have to work out the layout to prepare a suitable position for it, but it should be conceptually similar.