Reputation: 166
I can't manage to add programmatically a button to a fragment. (The number of buttons is variable)
I tried to add the button to the rootView with "an" addView method, but there isn't one.
I tried to add the button to the layout before inflating it, but I don't know how to get the layout : (Expected resource of type id error)
RelativeLayout rl = getView().findViewById(R.layout.fragment_main);
But when the inflater inflates it, the resource is good.
I tried to add the button in the "onViewCreated" method, but I got the same problem with findViewById(R.layout.fragment_main) telling me there is an error (Expected resource of type id).
When I create a button like this :
Button bt = new Button(this);
"this" is not a valid context. Don't know what to use instead.
So, how can I achieve it ?
The base code :
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
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_main, container, false);
return rootView;
}
}
}
Activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" />
Fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Upvotes: 3
Views: 9240
Reputation: 294
You can use the view's context to create the button:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.fragment_question_list, container, false);
Button btn = new Button(view.Context);
return view
}
Upvotes: 0
Reputation: 988
I tried to add the button to the layout before inflating it, but I don't know how to get the layout : (Expected resource of type id error)
Your code should be casting it to a relative layout and looking for a resource ID not a layout file.
RelativeLayout rl = (RelativeLayout) getView().findViewById(R.id.fragment_main_layout);
Hope this helps
Upvotes: 0
Reputation: 3045
There are a few thing to correct in your code, on the fragment_main.xml:
If you want to access an element on an xml, you need to give it a unique id. In the case of your relative layout you are missing it and therefore you won't be able to find it on the fragment view.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_main_layout" <!--some id-->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Now in your fragment class you can find it!
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//Find the layout with the id you gave in on the xml
RelativeLayout rl = (RelativeLayout) rootView.findViewById(R.id.fragment_main_layout);
//And now you can add the buttons you need, because it's a fragment, use getActivity() as context
Button bt = new Button(getActivity());
//You can add LayoutParams to put the button where you want it and the just add it
rl.addView(bt);
return rootView;
}
And that's it, you have a new button on your layout
Upvotes: 3