Search code examples
androidandroid-pageradapter

Passing calling activity to pager adapter


Trying to send calling activity to pager adapter in order for these two lines to work in the pager adapter:

((AppCompatActivity) callingActivity).setSupportActionBar(toolbar);

((AppCompatActivity)callingActivity).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

(note using pager adapter not fragment pager adapter).


Solution

  • You can do this by using this code:

    <android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
    <TextView
        android:id="@+id/toolbar_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/app_name"
        android:textColor="@color/white"
        android:textSize="@dimen/text_size_medium" />
    
    </android.support.v7.widget.Toolbar>
    

    And put this code in your activity

    Typeface typeface = Typeface.createFromAsset(getAssets(), SCTypeface.TTF_REGULAR);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            if (getSupportActionBar() != null) {
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setDisplayShowTitleEnabled(false);
            }
    
            TextView toolbarTitle = (TextView) findViewById(R.id.toolbar_text);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            toolbarTitle.setText("YOUR TITLE");
            toolbarTitle.setTypeface(typeface);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onBackPressed();
    
                }
            });