Hey I’m quite new to creating android apps. I want to create three Tabs in the Actionbar. I’ve already done this by creating them using New->Android Activity-> Navigation with Swipe and Tabs.
So that’s my code of the MainActivity (used one of android.com’s tutorials):
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create a new TextView and set its text to the fragment's section
// number argument value.
int current_section = getArguments().getInt(ARG_SECTION_NUMBER);
switch(current_section){
case 1:
{
EditText element1 = new EditText(getActivity());
element1.setHint(R.string.edit_text_hint);
element1.setGravity(Gravity.TOP);
element1.setPadding(40,40,40,0);
element1.setSingleLine();
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setText("Search");
return element1;
}
case 2:
{
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setText("User Account");
return textView;
}
case 3:
{
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setText("Last Updated");
return textView;
}
}
TextView textView1 = new TextView(getActivity());
textView1.setGravity(Gravity.CENTER);
// textView.setText(Integer.toString(getArguments().getInt(
// ARG_SECTION_NUMBER)));
return textView1;
}
}
So in each of the three Tabs there is a different Item display, unfortunatelly it is only possible to display one – and not more. I think it’s quite an easy question but I’m just not figuring out how this could work.
Hope you can help me with this.
In the tutorial, you are returning a textview. Try to add the textview to a Layout, and return the layout.
I think what you want is use different fragments for different tabs. You can then specify your layout and other code in the fragment itself, instead of building one BIG fragment with a switch case.
For every Fragment you create a new class. This Fragment can be added to a TabBarListener.
In the onCreateView of Fragment, you can create your layout programmatically by adding objects to the view parameter. You can also use the inflator to inflate your XML layout.
I created a sample project, were 1 Fragment view is inflated, the other is created programmatically:
here
If you read the android documentation it will be much more clear. I think they even have a sample code project.
Actiobar, the tab sections
More info about fragments