want to add buttons dynamically. I tried like this:
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.home, container, false);
Button newText = new Button(getActivity());
newText.setText("This is a fragment");
newText.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
return view;
}
Inside your View you can add a layout then add your button inside the layout,
like so:
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layoutContainer);
layout.addView(newText);
Related
I have a simple fragment:
public class FR_FragmentPopup extends Fragment {
public FR_FragmentPopup() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
MainActivity.getMain().doFragment(new FR_RecycleViewSettings2(),R.id.over_frame_bottom,false);
return inflater.inflate(R.layout.fragment_popup, container, false);
}
}
I want to modify the xml to wrap height, but programmatically because I'm using the layout in other fragments also.I know that R.layout.fragment_popup is an int and not a view which makes it difficult to use edit it.
return inflater.inflate(
findViewById(R.layout.fragment_popup).setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
, container, false);
How can I do this?
You may do as following:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_help, container, false);
rootView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
return rootView;
}
Theoretically:
View view = inflater.inflate(R.layout.fragment_popup, container, false);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
return view;
Practically:
Never tried.
I'm about to set an ImageView with setImageResource, but it's not working. nothing error too. and the logcat seems fine.
Here is my code :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list_request, container, false);
setHasOptionsMenu(true);
View anotherView = inflater.inflate(R.layout.style_fragment_list_request,container,false);
imgView = (CircleImageView)anotherView.findViewById(R.id.listPhoto);
imgView.setImageResource(R.drawable.pastel_red);
}
Considering that I called and declare the ImageView from another layout. Is at problem if I inflate 2 layouts just for set image for another layout ?
Anything wrong here?
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list_request, container, false);
setHasOptionsMenu(true);
return view;
}
public void onActivityCreated(){
ViewGroup layout = (ViewGroup) getView().findById(R.id.layout_another_view); // need layout for anotherView in fragment_item_list_request.xml
View anotherView = inflater.inflate(R.layout.style_fragment_list_request,container,false);
imgView = (CircleImageView)anotherView.findViewById(R.id.listPhoto);
imgView.setImageResource(R.drawable.pastel_red);
layout.addView(anotherView);
}
What if you do this instead:
View anotherView = inflater.inflate(R.layout.style_fragment_list_request,null,false);
The statement return anotherView; is missing in onCreateView().
Inside a fragment I try to inflate two layouts besides the root layout:
View a, b;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.list_fragment, container, false);
//..
a = inflater.inflate(R.layout.empty_list_view, container);
b = inflater.inflate(R.layout.progress_list_view, container);
//..
return root;
}
public void showB() {
a.setVisibility(GONE);
b.setVisibility(VISIBLE);
}
So I just return a single layout from the onCreateView method. However I inflate two more, a and b.
However when I display b actually a will be displayed. So progress_list_view never shows up. Can someone explain this strange behavior?
I suspect that a and b are both added to the container (ViewGroup). And since a is added first, it will be displayed first.
edit:
The point is, you're doing a huge mess and there is no good reason why you're doing it.
What happens there is that you inflate root, but not attach it to the container with this line View root = inflater.inflate(R.layout.list_fragment, container, false);
Then you inflate two views and add them directly to the container with those lines a = inflater.inflate(R.layout.empty_list_view, container);, which is a wrong thing to do as per Fragment documentation:
The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view
also both a and b are the same object, which is the container object as per documentation for the LayoutInflater
If root was supplied, this is the root
and root was supplied by you and it's the container, so what you have is basically the same asa=container; b=container;
and then you return root, at which point I really don't know what is happening anymore due to this mess. I decribed.
Lucky to fix is easy:
create another XML layout like this (shortened):
<FrameLayout>
<include layout="#layout/empty_list_view"/>
<include layout="#layout/progress_list_view"/>
</FrameLayout>
then you inflate this new XML:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.new_xml, container, false);
a = root.findViewById( .. ID of the root of empty_list_view.. )
b = root.findViewById( .. ID of the root of progress_list_view.. )
return root;
}
then the code will work.
original answer:
There's no strange behavior. You inflated those layouts and you have 2 View objects that are representative of them. But nothing attached those Views to the UI.
Only the view that you return from the onCreateView method that will be attached to the device UI.
For example:
the following code will show a:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//..
View a = inflater.inflate(R.layout.empty_list_view, container);
View b = inflater.inflate(R.layout.progress_list_view, container);
//..
return a;
}
the following code will show b:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//..
View a = inflater.inflate(R.layout.empty_list_view, container);
View b = inflater.inflate(R.layout.progress_list_view, container);
//..
return b;
}
if you want to have them both togeher you should put them together in the XML layout file. Maybe using an include tag if you need it re-usable.
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layoutMain = new LinearLayout(this);
layoutMain.setOrientation(LinearLayout.HORIZONTAL);
setContentView(layoutMain);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 1st Layout
RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate(
R.layout.main, null);
// 2nd Layout
RelativeLayout layoutRight = (RelativeLayout) inflate.inflate(
R.layout.row, null);
RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutMain.addView(layoutLeft, 100, 100);
layoutMain.addView(layoutRight, relParam);
}
You can only return one view from the onCreateView. The view that is returned is the view that is displayed. Looks like you always return view a.
R.layout.list_fragment should look something like this:
<FrameLayout>
<include
android:id="#+id/a"
layout="#layout/empty_list_view"/>
<include
android:id="#+id/b"
layout="#layout/progress_list_view"/>
</FrameLayout>
Your code will look something like this:
View a, b;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.list_fragment, container, false);
a = root.findViewById(R.id.a);
b = root.findViewById(R.id.b);
return root;
}
public void showB() {
a.setVisibility(GONE);
b.setVisibility(VISIBLE);
}
Why don't you inflate new view in the container.
Try this
LayoutInflater mInflater = null;
ViewGroup mContainer = null;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mInflater = inflater;
mContainer = container;
View root = inflater.inflate(R.layout.list_fragment, container, false);
return root;
}
public void showB() {
mInflater.inflate(R.layout.progress_list_view, mContainer, false);
}
public void showA() {
mInflater.inflate(R.layout.empty_list_view, mContainer, false);
}
I am trying to add a dynamic view for my fragment.
I am using this code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Button myButton = new Button(Builtprofile.context);
myButton.setText("Press me");
myButton.setBackgroundColor(Color.YELLOW);
RelativeLayout myLayout = new RelativeLayout(Builtprofile.context);
myLayout.setBackgroundColor(Color.BLUE);
RelativeLayout.LayoutParams buttonParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
myLayout.addView(myButton, buttonParams);
View rootView = inflater.inflate(R.layout.q1, container, false);
// I want to add myLayout in place of R.layout.q1
return rootView;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
Button myButton = new Button(Builtprofile.context);
myButton.setText("Press me");
myButton.setBackgroundColor(Color.YELLOW);
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
View rootView = inflater.inflate(R.layout.q1, container, false);
RelativeLayout myLayout = (RelativeLayout)rootView.findViewById(R.id.mainLayout);
myLayout.setBackgroundColor(Color.BLUE);
myLayout.addView(myButton, buttonParams);
return rootView;
}
Have an empty xml layout with just a RelativeLayout called "mainLayout"(or what ever you want to call it). That way you can append any dynamically generated controls
LayoutInflater vi = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.xml, null);
container.addView(v);
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);
}