-1

[UPDATE BELOW]

I'm trying to change the title in the default title bar after clicking an item in the RecyclerView which is inside onLoadFinished (AsyncTaskLoader) but title doesn't change. The idea is when I tab an item in the RecyclerView and data is loaded, title bar text should be changed too but it doesn't.

catsRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, catsRecyclerView,new RecyclerTouchListener.ClickListener() {
   @Override
   public void onClick(View view, int position) {
          Categories category = categories.get(position);
          int catId = category.getId();
          setTitle(category.getTitle());
          drawerLayout.closeDrawer(GravityCompat.START);
          commonActions(); //restarting loader and executing other methods
   }

   @Override
          public void onLongClick(View view, int position) {

           }
}));

When I log category.getTitle() I get the correct text but title doesn't change how can I achieve that?

[Update]

Replaced the default title bar with a custom action bar here's the layout of the activity:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include layout="@layout/actionbar"/>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
     >
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />

  </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

The actiobar layout

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:id="@+id/primaryToolbar">

</android.support.v7.widget.Toolbar>

The activity

toolbar = findViewById(R.id.primaryToolbar);
    setSupportActionBar(toolbar);
    setTitle("My new title");
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);

But it's size is so large taking the whole screen

enter image description here

PHP User
  • 2,350
  • 6
  • 46
  • 87

3 Answers3

0

I think the problem is in the listener. When I attach a listener to recycler view I do it throughout Using interface in adapter, this blog shows the way, also check out this StackOverflow answer in which and example is provided.

Younes Charfaoui
  • 1,059
  • 3
  • 10
  • 20
0

You have to enable the action bar to setTitle() by setting the following flag.

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);

The above code works for AppCompatActivity.

CodeDaily
  • 756
  • 4
  • 17
  • and how to change the title after adding this code? I have the default title bar should I remove it and add the action bar instead? – PHP User May 11 '19 at 09:23
  • replaced the default title bar with the custom action bar but there's no title at all even after adding your code!!!! I can see the menu icon but no title – PHP User May 11 '19 at 09:54
0

I had 2 issues. The first one is about not changing the title of the titlebar after tabbing an item in the RecyclerView to reload the AsyncTaskLoader. The problem is there's another setTitle in the Loader function in addition to onLoadFinished so I removed it and kept the one inside onLoadFinished and now it's working fine so there's no need to replace the default title bar with the action bar.

The second issue when I tried to add the action bar, the title was taking the whole screen as you see in the image because there are 2 elements in the root view which are the actionBar and the NavigationView which made the actionBar the actual content that's why it was resized to take the whole screen. So i placed it inside a LinearLayout and the actual content goes below it that fixed the second problem

PHP User
  • 2,350
  • 6
  • 46
  • 87