Eman87
Eman87

Reputation: 2745

Show my Fragment in Dialog android

I have a class that extends a Fragment. I want when press a button from An Activity to opens this Fragment with its layout in dialog in addition to that the fragment opens normally in its place. Is this possible?

This is my Fragment (TipsFragment.java)

public class TipsFragment extends Fragment{

ListView list;
TipsAdapter tipsAdapter;
AlertDialog PageDialog;
DAO db;
Cursor c;

ArrayList<HashMap<String, String>> tipsList;
HashMap<String, String> map;

ArrayList<HashMap<String, String>> pages;
Integer tipType;

ImageButton page;
ImageButton add;
ImageButton search;

EditText inputAdd;
EditText inputSearch;

ImageView addImage;

RelativeLayout layoutAdd;
RelativeLayout layoutSearch;

int itemSelected;


 @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        db = new DAO(activity);
        db.open();
    }

public TipsFragment(){}

public TipsFragment(int tipType, int itemSelected ){

    this.tipType = tipType;
    this.itemSelected = itemSelected;
}



@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
        final Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.tips_fragement, container, false);

    System.out.println("tipType: "+tipType);
    System.out.println("itemSelected: "+itemSelected);

    page = (ImageButton) rootView.findViewById(R.id.pager);
    add = (ImageButton) rootView.findViewById(R.id.add);
    search = (ImageButton) rootView.findViewById(R.id.search);

    inputSearch = (EditText) rootView.findViewById(R.id.inputSearch);
    inputAdd = (EditText) rootView.findViewById(R.id.inputAdd);

    addImage = (ImageView) rootView.findViewById(R.id.imageAdd);

    layoutAdd  = (RelativeLayout) rootView.findViewById(R.id.layoutAdd);
    layoutSearch   = (RelativeLayout) rootView.findViewById(R.id.layoutSearch);

    if (tipType != 0) {
        switch (tipType) {
        case 1:
            System.out.println("All tips");
            if (getActivity().getIntent().getStringExtra("startFrom") == null) {
                c = db.getTips("0");
            } else {
                c = db.getTips(getActivity().getIntent().getStringExtra("startFrom"));
            }

            break;
        case 2:
            System.out.println("favorite tips");
            c = db.getFavoriteTips();
            page.setVisibility(View.GONE);
            System.out.println("in favorite, count_records: "+c.getCount());
            break;

        }
    }

    System.out.println("count_records: "+c.getCount());

    if (c.getCount() != 0) {

        if (getActivity().getIntent().getStringExtra("startLabel") != null) {
            page = (ImageButton) rootView.findViewById(R.id.pager);
        }
        tipsList = new ArrayList<HashMap<String, String>>();

        list = (ListView) rootView.findViewById(R.id.tipList);

        do {
            map = new HashMap<String, String>();

            // adding each child node to HashMap key =&gt; value
            map.put(DAO.TIP_ID, c.getString(c.getColumnIndex(c.getColumnName(0))));
            map.put(DAO.TIP_CONTENT, c.getString(c.getColumnIndex(c.getColumnName(1))));

            // adding HashList to ArrayList
            tipsList.add(map);

        } while (c.moveToNext());

        // Getting adapter by passing xml data ArrayList
        tipsAdapter = new TipsAdapter(getActivity(), tipsList);
        list.setAdapter(tipsAdapter);

        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                map = tipsList.get(position);

                System.out.println("in list select item, tipType: "+tipType);

                if (!MainActivity.tSlidingLayer.isOpened()) {
                    MainActivity.tSlidingLayer.openLayer(true);
                }


            }
        });

        page.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Strings to Show In Dialog with Radio Buttons


            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {                   


            }
        });

        search.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {


            }
        });

        inputSearch.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {

            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
        });

    } 


    addImage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getActivity(), "addImage pressed", Toast.LENGTH_LONG).show();
        }
    });

    return rootView;
}

}

and this is layout tips_fragement.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"

android:orientation="vertical" >

<LinearLayout
    android:id="@+id/titleBar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/add"
            android:layout_width="45dp"
            android:layout_height="35dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:scaleType="fitXY"
            android:src="@drawable/add" />

        <ImageButton
            android:id="@+id/search"
            android:layout_width="45dp"
            android:layout_height="35dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:scaleType="fitXY"
            android:src="@drawable/search" />

        <ImageButton
            android:id="@+id/pager"
            android:layout_width="45dp"
            android:layout_height="35dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:scaleType="fitXY"
            android:src="@drawable/pager" />

</LinearLayout>

<View
    android:layout_width="fill_parent"
    android:layout_height="5dip"
    android:background="@drawable/shadow" >
</View>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layoutAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" >

<EditText
    android:id="@+id/inputAdd"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Add" />

 <ImageView
    android:id="@+id/imageAdd"
    android:padding="5dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/inputAdd"
    android:layout_alignBottom="@+id/inputAdd"
    android:layout_alignRight="@+id/inputAdd"
    android:src="@android:drawable/ic_menu_add" />

</RelativeLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/layoutSearch"
android:visibility="gone" >

<EditText
    android:id="@+id/inputSearch"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Search"/>

 <ImageView
    android:id="@+id/imageSearch"
    android:padding="5dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/inputSearch"
    android:layout_alignBottom="@+id/inputSearch"
    android:layout_alignRight="@+id/inputSearch"
    android:src="@android:drawable/ic_menu_search" />

</RelativeLayout>

<ListView
    android:id="@+id/tipList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/buttons"
    android:divider="#351802"
    android:dividerHeight="2dp"
    android:listSelector="@drawable/list_selector" />

</LinearLayout>

Upvotes: 1

Views: 262

Answers (1)

jankovd
jankovd

Reputation: 1701

Since Android supports nesting Fragments (available in the support library) you could show your TipsFragment as a content of another DialogFragment. Avoid this solution whenever possible.

A better solution would be to make TipsFragment extend DialogFragment and that way you can either show the Fragment inline or in a dialog.

1 - Make TipsFragment extends DialogFragment

public class TipsFragment extends DialogFragment {...}

2 - On button click use FragmentTransaction.add or FragmentTransaction.replace to show the DialogFragment inline or include it in your layout and change its visibility.

3 - Show the TipsFragment as a dialog

TipsFragment fr = new TipsFragment();
fr.show(getSupportFragmentManager(), "dialog_fragment_tag");


Guide: Performing Fragment Transactions. More samples are available in the ApiDemos application from the SDK samples

Upvotes: 1

Related Questions