I would like the name in the menu to change by the name of the fragment that was clicked. for the code below, I understand that the actual title in each fragment is โHomeโ, and it does not change. but I found that when I click on an item in the menu, the name changes for a second and returns to the "Home" heading. I implemented ondrawer, but still I do not know what can do this.
my code is:
package com.example.matant.gpsportclient; import android.app.Fragment; import android.app.FragmentManager; import android.app.ProgressDialog; import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import com.example.matant.gpsportclient.Controllers.Fragments.CreateEventFragmentController; import com.example.matant.gpsportclient.Controllers.DBcontroller; import com.example.matant.gpsportclient.Controllers.Fragments.GoogleMapFragmentController; import com.example.matant.gpsportclient.Controllers.Fragments.ManageEventFragmentController; import com.example.matant.gpsportclient.Controllers.Fragments.ProfileFragmentController; import com.example.matant.gpsportclient.InterfacesAndConstants.AsyncResponse; import com.example.matant.gpsportclient.InterfacesAndConstants.Constants; import com.example.matant.gpsportclient.Utilities.DrawerItem; import com.example.matant.gpsportclient.Utilities.DrawerItemCustomAdapter; import com.example.matant.gpsportclient.Utilities.SessionManager; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; public class MainScreen extends AppCompatActivity implements AsyncResponse { private String[] mNavigationDrawerItemTitles; private DrawerLayout mDrawerLayout; private ListView mDrawerList; ActionBarDrawerToggle mDrawerToggle; private CharSequence mDrawerTitle; private CharSequence mTitle; private DBcontroller dbController; private ProgressDialog progress = null; private SessionManager sm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_screen); if (getIntent().getBooleanExtra("EXIT", false)) { finish(); return; } mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); sm = SessionManager.getInstance(this); mTitle = mDrawerTitle = "Home"; DrawerItem [] drawerItems = new DrawerItem[Constants.MENU_SIZE]; drawerItems[0] = new DrawerItem(R.drawable.home,"Home"); drawerItems[1] = new DrawerItem(R.drawable.profile,"Profile"); drawerItems[2] = new DrawerItem(R.drawable.search,"Search Events"); drawerItems[3] = new DrawerItem(R.drawable.create,"Create Event"); drawerItems[4] = new DrawerItem(R.drawable.manage,"Manage Event"); drawerItems[5] = new DrawerItem(R.drawable.attending,"Attending List"); drawerItems[6] = new DrawerItem(R.drawable.recent_search_24,"Recent Searches"); drawerItems[7] = new DrawerItem(R.drawable.logout,"Log Out"); DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItems); mDrawerList.setAdapter(adapter); mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_menu, R.string.drawer_open, R.string.drawer_close) { public void onDrawerClosed(View view) { super.onDrawerClosed(view); getSupportActionBar().setTitle(mDrawerTitle); invalidateOptionsMenu(); } public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); getSupportActionBar().setTitle(mDrawerTitle); invalidateOptionsMenu(); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu); getSupportActionBar().setTitle(mNavigationDrawerItemTitles[0]); if (savedInstanceState == null) {
source share