Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. pipiscrew renamed this gist Apr 12, 2016. 1 changed file with 0 additions and 0 deletions.
  2. pipiscrew revised this gist Apr 12, 2016. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion call method exists in fragment from actiobar settings.java
    Original file line number Diff line number Diff line change
    @@ -66,7 +66,13 @@ public boolean onOptionsItemSelected(MenuItem item) {

    private void test()
    {
    //is a test!
    //is a test!
    Fragment currentFragment = getFragmentManager().findFragmentById(R.id.frame_container);

    if (currentFragment instanceof my_fragment) {
    my_fragment x = (my_fragment) currentFragment;
    x.pipiscrew_method();
    }
    }

    //my_fragment.java
  3. pipiscrew revised this gist Mar 4, 2016. No changes.
  4. pipiscrew renamed this gist Mar 4, 2016. 1 changed file with 0 additions and 0 deletions.
  5. pipiscrew created this gist Mar 4, 2016.
    97 changes: 97 additions & 0 deletions call method exists in fragment from actiobar settings
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    //source - http://stackoverflow.com/a/22182746/1320686
    //the activity_main.xml
    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Framelayout to display Fragments -->
    <FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

    <!-- Listview to display slider menu -->
    <ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="#272727"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector"
    android:background="#303030"/>
    </android.support.v4.widget.DrawerLayout>


    //menu > main.xml
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
    android:id="@+id/action_test"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_test"/>
    <item
    android:id="@+id/action_pipiscrew"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/pipiscrew"/>
    </menu>

    //activity_main.xml
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
    return true;
    }

    // Handle action bar actions click
    switch (item.getItemId()) {
    case R.id.action_test:
    test();
    return true;
    default:
    return super.onOptionsItemSelected(item);
    }
    }

    private void test()
    {
    //is a test!
    }

    //my_fragment.java
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    //Report that this fragment would like to participate in populating the options menu by receiving a call - http://developer.android.com/reference/android/app/Fragment.html
    setHasOptionsMenu(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

    // Handle action bar actions click
    switch (item.getItemId()) {
    case R.id.action_pipiscrew:
    pipiscrew_method();
    return true;
    default:
    return super.onOptionsItemSelected(item);
    }
    }

    private void pipiscrew_method()
    {
    //is a pipiscrew_method!
    }