Rodriquez Rodriquez - 1 year ago 57
Android Question

My fragment not showing on OnClickListener by button

I have a problem with fragment, what i create:

Here is a custom List

class CustomList extends ArrayAdapter<String> {

private final Activity activity;
private final String[] web;
private final Integer[] imageID;

CustomList(Activity activity, String[] web, Integer[] imageID){
super(activity, R.layout.list_single, web);
this.activity = activity;
this.web = web;
this.imageID = imageID;

public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(;
ImageView img = (ImageView) rowView.findViewById(;
return rowView;

Here is a fragment:

public class BackgroundSelectFragment extends Fragment {
ListView list;
String[] web = {
"Old City",
"Clip Board",
} ;
Integer[] imageId = {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listview_fragment, container, false);
CustomList adapter = new
CustomList(getActivity(), web, imageId);
list = (ListView) container.findViewById(;
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
return view;

and the last i try to open a fragment:

backgroundBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
FragmentManager fm = getFragmentManager();
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)

Im not got any error, when i click a button nothing appear, where is my fragment? Of course i initialize it in MainActivity OnCreate like:

backgroundSelectFragment = new BackgroundSelectFragment();

Please help. I dont know what i do wrong.

Answer Source

You have to either add or replace your fragment into your basic layout view

                .replace(containerViewId, fragment, tag) //either use replace
                .add(containerViewId, fragment, tag) //or add

where containerViewId is the view where your fragment should be added You can find the differences between replace() and add here: Difference between add(), replace(), and addToBackStack()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download