I need to create four different layouts, each one to a page of ViewPagerIndicator.
How can I do this? The ViewPagerIndicator is already working, but I'm using the sample from http://viewpagerindicator.com and there is created a simple TextView for all pages.
See the sample (TestFragment.java):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView text = new TextView(getActivity());
text.setGravity(Gravity.CENTER);
text.setText(mContent);
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;
}
I need to identify the current page (position) and refer to a related resource layout (XML). Is it possible?
I also need to ensure that all Views of all pages to be loaded at once only one time when I create the activity, allowing values to be updated later.
I appreciate any help!!
Thanks
I found a simple solution which works for me. Here is what I did.
TestFragment.class
public static TestFragment newInstance(String content) {
TestFragment fragment = new TestFragment();
//
// StringBuilder builder = new StringBuilder();
// for (int i = 0; i < 20; i++) {
// builder.append(content).append(" ");
// }
// builder.deleteCharAt(builder.length() - 1);
fragment.mContent = content;
return fragment;
}
Comment out the for loop here. Just get the mContent which we are going to use as a Flag.
Now in your onCreateView() change it as follows,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TextView text = new TextView(getActivity());
// text.setGravity(Gravity.CENTER);
// text.setText(mContent);
// 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);
Log.i("mContent",mContent);
View view=null;
if(mContent.equalsIgnoreCase("title1"))
{
view = inflater.inflate(R.layout.one, container, false);
}
else if(mContent.equalsIgnoreCase("title2"))
{
view = inflater.inflate(R.layout.two, container, false);
}
else if(mContent.equalsIgnoreCase("title3"))
{
view = inflater.inflate(R.layout.three, container, false);
}
return view;
}
That is all it takes. Now you will be able to inflate your views based on the Title name which we have used as the flag.
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 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 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);
}
This is the code:
public class HelpDetailsFragment extends Fragment
{
private static final String TAG = "MeMoGame";
public static HelpDetailsFragment newInstance(int index)
{
HelpDetailsFragment detailFragment = new HelpDetailsFragment();
Bundle bundleArgs = new Bundle();
bundleArgs.putInt("index", index);
detailFragment.setArguments(bundleArgs);
return detailFragment;
} // newInstance()
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
if(container == null)
{
Log.i(TAG, "Different layouts and in one this fragment's containing frame does not exist.");
return null;
} else {
// I checked that container is NOT null
Log.i(TAG, "This is the parent view that the fragment's UI should be attached to.");
}
View mView = new View(getActivity());
container.addView(mView);
return container;
}
This error message appears:
AndroidRuntime(785): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Can / will someone please explain what I'm doing wrong?
When I do:
View mView = new View(getActivity());
TextView text = (TextView) new TextView()
mView.addView(text);
return mView;
I get the same error message.
My salvation lies inhere:
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
Context context = getActivity();
FrameLayout frameLayout = new FrameLayout(context);
int height = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, getActivity()
.getResources().getDisplayMetrics());
int padding = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 10, context
.getResources().getDisplayMetrics());
// set the header
TextView headText = new TextView(context);
headText.setHeight(height);
headText.setPadding(padding, 0, 0, 0);
headText.setTextAppearance(context, R.style.details_Header);
headText.setText(HelpScreenData.HELP_HEADERS[getCurrentIndex()]);
frameLayout.addView(headText);
ScrollView scroller = new ScrollView(context);
// set detail text
TextView detailText = new TextView(context);
detailText.setPadding(padding, padding + height, padding, padding);
detailText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
detailText.setText(HelpScreenData.HELP_DETAILS[getCurrentIndex()]);
scroller.addView(detailText);
frameLayout.addView(scroller);
return frameLayout;
} // onCreateView()
An explanation would still be very welcome!
The issue here is you are returning the container-- return container; you should be returning mView in your case, the container is the actual container for the fragment in your activity layout
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);
}