Example usage for android.app Fragment Fragment

List of usage examples for android.app Fragment Fragment

Introduction

In this page you can find the example usage for android.app Fragment Fragment.

Prototype

public Fragment() 

Source Link

Document

Default constructor.

Usage

From source file:com.nathanson.meterreader.activity.MainActivity.java

private Fragment fragmentFactory(int position) {
    switch (position) {
    case SETTINGS_POSITION:
        return SettingsFragment.newInstance();

    case GRAPH_POSITION:
        return BarChartFragment.newInstance();

    case STATS_POSITION:
        return StatsFragment.newInstance();

    default://from  w  ww .jav a  2s.com
        return new Fragment();
    }
}

From source file:json2notification.MainTest.java

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    supportFragment = new android.support.v4.app.Fragment();
    fragmentActivity = Robolectric.buildActivity(FragmentActivity.class).create().get();
    fragmentActivity.getSupportFragmentManager().beginTransaction().add(supportFragment, null).commit();

    fragment = new Fragment();
    activity = Robolectric.buildActivity(Activity.class).create().get();
    activity.getFragmentManager().beginTransaction().add(fragment, null).commit();

    NotificationManager notificationManager = (NotificationManager) Robolectric.application
            .getSystemService(Context.NOTIFICATION_SERVICE);
}

From source file:com.androidexperiments.sprayscape.androidutils.PermissionRequester.java

public static void requestPermission(final String permission, final String objectName,
        final String methodName) {
    Log.i(TAG, "requestPermission() called for permission: " + permission);

    final Activity currentActivity = UnityPlayer.currentActivity;
    boolean granted = ActivityCompat.checkSelfPermission(currentActivity,
            permission) == PackageManager.PERMISSION_GRANTED;

    if (granted) {
        Log.i(TAG, "Permission already granted: " + permission);
        SendPermissionResult(permission, true, objectName, methodName);
        return;//from  w w w  .j av  a  2s .  c  o  m
    }

    try {
        // use a fragment here because we wouldn't be able to capture onRequestPermissionsResult() on the main activity unless we sub-classed it...
        // final values so the fragment inner-class can access them
        final FragmentManager fragmentManager = currentActivity.getFragmentManager();
        final Fragment request = new Fragment() {

            @Override
            public void onStart() {
                super.onStart();
                Log.i(TAG, "Permission fragment onStart()");
                FragmentCompat.requestPermissions(this, new String[] { permission }, 0);
            }

            @SuppressLint("Override")
            @SuppressWarnings("unused")
            public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
                Log.i(TAG, "onRequestPermissionsResult(" + requestCode + ", " + permissions.toString() + ", "
                        + grantResults.toString() + ")");

                if (grantResults.length > 0) {
                    for (int i = 0; i < grantResults.length; i++) {
                        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                            Log.i(TAG, "Permission granted: " + permissions[i]);
                            SendPermissionResult(permissions[i], true, objectName, methodName);
                        } else {
                            Log.i(TAG, "Permission denied: " + permissions[i]);
                            SendPermissionResult(permissions[i], false, objectName, methodName);
                        }
                    }
                } else {
                    Log.i(TAG, "Permission denied: " + permission);
                    SendPermissionResult(permission, false, objectName, methodName);
                }

                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.remove(this);
                fragmentTransaction.commit();
            }
        };

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(0, request);
        fragmentTransaction.commit();
    } catch (Exception error) {
        Log.e(TAG, "Permission request failed: '" + permission + "'", error);
        SendPermissionResult(permission, false, objectName, methodName);
    }
}

From source file:com.klinker.android.theme_spotlight.activity.SpotlightActivity.java

public void setUpFragment(int position) {
    switch (position) {
    case EVOLVE_FRAGMENT:
        mFragment = ThemeListFragment.newInstance(EVOLVE_SMS);
        break;/*  w  w  w  .j  ava2 s .co  m*/
    case TALON_FRAGMENT:
        mFragment = ThemeListFragment.newInstance(TALON);
        break;
    case FEATURED_FRAGMENT:
    default:
        mFragment = FeaturedThemerFragment.newInstance();
        break;
    }
    if (isTwoPane()) {
        attachFragment(R.id.theme_frame, new Fragment());
    }
    showSearchIcon(mFragment.isSearchable());
}

From source file:com.geecko.QuickLyric.adapter.IntroScreenSlidePagerAdapter.java

@Override
public Fragment getItem(int position) {
    if (rightToLeft)
        position = getCount() - position - 1;
    try {//ww  w.ja  v  a 2 s.  c  om
        return (Fragment) tutorialScreens[position].newInstance();
    } catch (Exception e) {
        e.printStackTrace();
        return new Fragment();
    }
}

From source file:com.klinker.android.theme_spotlight.activity.SpotlightActivity.java

@Override
public boolean onQueryTextChange(String newText) {
    if (isTwoPane() && !currentSearch.equals(newText)) {
        attachFragment(R.id.theme_frame, new Fragment());
        showTextLabel(true);/*from  w  ww.j  a  va2 s .c o  m*/
    }

    currentSearch = newText;

    if (mFragment != null) {
        return mFragment.onQueryTextChange(newText);
    } else {
        return false;
    }
}

From source file:com.example.android.navigationdrawerexample.Controller.PilihanController.java

private void selectItem(int position) {
    // update the main content by replacing fragments
    /*Fragment fragment = new PlanetFragment();*/
    fragment = null;//from  w ww.j a  va2s  . com
    Bundle args = new Bundle();

    switch (position) {
    case 0:
        fragment = new Fragment() {
            @Override
            public void onResume() {
                super.onResume();
                new GetAllJadwalTask(PilihanController.this).execute(username);
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);

                View view = inflater.inflate(R.layout.list_jadwal, container, false);
                GetAllJadwalListView = (ExpandableListView) view.findViewById(R.id.GetAllJadwalListView);
                ImageView buat = (ImageView) view.findViewById(R.id.button);
                //Button pilihan = (Button) view.findViewById(R.id.button9);
                if (role == 0)
                    buat.setVisibility(View.GONE);
                else
                    buat.setVisibility(View.VISIBLE);

                new GetAllJadwalTask(PilihanController.this).execute(username);

                GetAllJadwalListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

                    @Override
                    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                        GetAllJadwalListView.expandGroup(groupPosition);
                        return true;
                    }
                });

                GetAllJadwalListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

                    @Override
                    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                            int childPosition, long id) {
                        try {
                            //Toast.makeText(getApplicationContext(), listDataChild2.get(listDataHeader2.get(groupPosition)).get(childPosition).toString(), Toast.LENGTH_LONG).show();
                            int jadwalp = listDataChild2.get(listDataHeader2.get(groupPosition))
                                    .get(childPosition).getInt("Id");
                            int kelasp = listDataChild2.get(listDataHeader2.get(groupPosition))
                                    .get(childPosition).getInt("Id_kelas");
                            ////                                    JSONObject mahasiswaClicked = jsonArray.getJSONObject(groupPosition+childPosition);
                            ////                                    int jadwalp = mahasiswaClicked.getInt("Id");
                            ////                                    int kelasp = mahasiswaClicked.getInt("Id_kelas");
                            Intent showDetails = new Intent(getActivity(), JadwalController.class);
                            showDetails.putExtra("JadwalID", jadwalp);
                            showDetails.putExtra("KelasID", kelasp);
                            showDetails.putExtra("Username", username);
                            showDetails.putExtra("View", "detailJadwal");
                            startActivity(showDetails);

                        } catch (JSONException e) {
                            e.printStackTrace();
                            //Toast.makeText(getApplicationContext(), "lala masuk ex", Toast.LENGTH_LONG).show();
                        }
                        return false;
                    }
                });

                buat.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        Intent showDetails = new Intent(getActivity(), JadwalController.class);
                        //asumsi username gak null
                        showDetails.putExtra("Username", username);
                        showDetails.putExtra("View", "createJadwal");

                        startActivity(showDetails);
                    }
                });
                return view;
            }
        };
        args.putString(FragmentOne.ITEM_NAME, dataList.get(position).getItemName());
        args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(position).getImgResID());
        args.putInt("role", 2);
        break;
    case 1:
        fragment = new RoleFragment();
        args.putInt("role", 1);
        break;
    case 2:
        fragment = new Fragment() {
            @Override
            public void onResume() {
                super.onResume();
                new GetAllEnrollTask(PilihanController.this).execute(username);
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);

                view = inflater.inflate(R.layout.list_enroll, container, false);

                //GetAllEnrollListView = (ListView) view.findViewById(R.id.GetAllJadwalListView);
                ImageView enroll = (ImageView) view.findViewById(R.id.button);
                expListView = (ExpandableListView) view.findViewById(R.id.lvExp);

                new GetAllEnrollTask(PilihanController.this).execute(username);

                expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

                    @Override
                    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                            int childPosition, long id) {
                        //asumsikan kalo dipencet bisa ngeluarin infromasi asistennya
                        //viewnya di view_profile_personal
                        //kelasnya inten, nah loh pake controller mana nih??
                        //asumsikan gue punya kelas ProfileAsdos.java
                        String userAsdos = listDataChild.get(listDataHeader.get(groupPosition))
                                .get(childPosition);
                        Intent showDetails = new Intent(getActivity(), MenjabatController.class);
                        showDetails.putExtra("Username", userAsdos);
                        startActivity(showDetails);
                        return false;
                    }
                });

                enroll.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        Intent showDetails = new Intent(getActivity(), EnrollController.class);
                        showDetails.putExtra("Username", username);
                        startActivity(showDetails);
                    }
                });
                return view;
            }

        };
        args.putInt("role", 0);
        break;
    case 3:
        fragment = new Fragment() {
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);

                View view = inflater.inflate(R.layout.choose_forum, container, false);

                Button buttonForumReply = (Button) view.findViewById(R.id.choose_forum_1);
                Button buttonForumForum = (Button) view.findViewById(R.id.choose_forum_2);
                Button buttonForumPolling = (Button) view.findViewById(R.id.choose_forum_3);

                buttonForumReply.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(), ForumQA.class);
                        intent.putExtra("Username", username);
                        startActivity(intent);
                    }
                });

                buttonForumForum.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(), ForumRequest.class);
                        intent.putExtra("Username", username);
                        startActivity(intent);
                    }
                });

                buttonForumPolling.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(), ForumPolling.class);
                        intent.putExtra("Username", username);
                        startActivity(intent);
                    }
                });

                return view;
            }

        };
        args.putInt("role", 3);
        break;
    case 4:
        //ProfileController profileController2 = new ProfileController(username);
        if (role == 2) {
            fragment = new Fragment() {

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                    StrictMode.setThreadPolicy(policy);

                    view = inflater.inflate(R.layout.admin, container, false);

                    //GetAllEnrollListView = (ListView) view.findViewById(R.id.GetAllJadwalListView);
                    Button kelas = (Button) view.findViewById(R.id.button9);
                    Button role = (Button) view.findViewById(R.id.button10);
                    Button database = (Button) view.findViewById(R.id.button11);
                    Button forum = (Button) view.findViewById(R.id.button12);

                    kelas.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            Intent showDetails = new Intent(getActivity(), KelasController.class);
                            //asumsi username gak null
                            showDetails.putExtra("Username", username);
                            showDetails.putExtra("View", "listKelas");
                            startActivity(showDetails);
                        }
                    });
                    role.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            Intent showDetails = new Intent(getActivity(), RoleController.class);
                            //asumsi username gak null
                            showDetails.putExtra("Username", username);
                            startActivity(showDetails);
                        }
                    });
                    database.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            Intent showDetails = new Intent(getActivity(), Database.class);
                            startActivity(showDetails);
                        }
                    });

                    forum.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            Intent showDetails = new Intent(getActivity(), AdminThread.class);
                            startActivity(showDetails);
                        }
                    });
                    return view;
                }
            };
        } else {
            fragment = new FragmentOne();
            Toast.makeText(getApplicationContext(), "Hei Anda Bukan Admin", Toast.LENGTH_LONG).show();
        }
        args.putInt("role", 4);
        break;
    case 5:
        session.logoutUser();
        finish();
        return;
    }
    /*args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);*/
    fragment.setArguments(args);

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

From source file:at.software2014.trackme.MainActivity.java

public void selectItem(int position, String argument) {

    Fragment fragment = new Fragment();

    mCurrentPosition = position;/*from w w  w.j  ava2 s  .  c om*/

    switch (position) {
    case 0:
        if (mIsGooglePlayServicesAvailable == true) {
            if (argument == "") {
                fragment = GMapFragment.newInstance(position, mName);
            } else {
                fragment = GMapFragment.newInstance(position, mName, argument);
            }
        }
        break;
    case 1:
        fragment = FriendsListFragment.newInstance(position);
        break;
    case 2:
        fragment = ContactsFragment.newInstance(position);
        break;
    case 3:
        fragment = AboutFragment.newInstance(position);
        break;
    case 4:
        exitActivity();
    }

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mMenuTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerLinear);
}

From source file:android.support.v17.leanback.app.BrowseFragment.java

private void swapToMainFragment() {
    final VerticalGridView gridView = mHeadersFragment.getVerticalGridView();
    if (isShowingHeaders() && gridView != null && gridView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
        // if user is scrolling HeadersFragment,  swap to empty fragment and wait scrolling
        // finishes.
        getChildFragmentManager().beginTransaction().replace(R.id.scale_frame, new Fragment()).commit();
        gridView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override/*from   www.ja  va2  s.c  o  m*/
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    gridView.removeOnScrollListener(this);
                    FragmentManager fm = getChildFragmentManager();
                    Fragment currentFragment = fm.findFragmentById(R.id.scale_frame);
                    if (currentFragment != mMainFragment) {
                        fm.beginTransaction().replace(R.id.scale_frame, mMainFragment).commit();
                    }
                }
            }
        });
    } else {
        // Otherwise swap immediately
        getChildFragmentManager().beginTransaction().replace(R.id.scale_frame, mMainFragment).commit();
    }
}