Android Developer
Android Developer

Reputation: 9643

Programmatically change title of Custom Action Bar

I am neither able to programmatically change the title of custom action bar nor able to add profile image to action bar. I failed to understand where my implementation is wrong. Below is my code:

actionbar_home

<RelativeLayout
    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:background="@color/tabbackground"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <ImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/profileImageActionBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:visibility="visible"
        app:border_width="1dp"
        app:border_color="#FFFFFF"
        android:layout_alignParentLeft="true"

      />


    <TextView
        android:id="@+id/actionbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:maxLines="1"
        android:singleLine="true"
        android:text="Home"
        android:textColor="@color/white"
        android:textSize="22sp"/>
</RelativeLayout>

Code-

  protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mCamera=(ImageButton)findViewById(R.id.menu_camera);
            mMimic=(ImageButton)findViewById(R.id.menu_mimic);
            mMeme=(ImageButton)findViewById(R.id.menu_makememe);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerList = (ListView) findViewById(R.id.left_drawer);
            mMenuButton = (ImageButton) findViewById(R.id.menu_menu);
            mMenuBackground=(View)findViewById(R.id.menu_white_background);
            header=getLayoutInflater().inflate(R.layout.header, null);
            mProfilePic = (ImageView) findViewById(R.id.circleView);
            mProfileName = (TextView) findViewById(R.id.userName);
            mmenuCamera=(View)findViewById(R.id.menu_camera_box);
            mmenuMimic=(View)findViewById(R.id.menu_mimiclayout);
            mmenuMeme=(View)findViewById(R.id.menu_make_meme);
            transparent=(View)findViewById(R.id.blacktreansparent);

            LayoutInflater mInflater = LayoutInflater.from(this);
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            View mCustomView = mInflater.inflate(R.layout.actionbar_home, null);

            profileImage=(ImageView)mCustomView.findViewById(R.id.profileImageActionBar);
            mFaceBookActionBarPicture = (ProfilePictureView)mCustomView.findViewById(R.id.faceboolUserProfilePicture);

            actionBarTitle=(TextView)mCustomView.findViewById(R.id.actionbar_title);
            getSupportActionBar().setCustomView(R.layout.actionbar_home);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            getSupportActionBar().setDisplayShowCustomEnabled(true);
actionBarTitle.setText("PG");
            options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(android.R.color.transparent)
                    .showImageForEmptyUri(android.R.color.transparent)
                    .showImageOnFail(android.R.color.transparent)
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .considerExifParams(true)
                    .bitmapConfig(Bitmap.Config.RGB_565)
                    .build();
            handler= new Handler();
            ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                    getApplicationContext())
                    .defaultDisplayImageOptions(options)
                    .memoryCache(new WeakMemoryCache())
                    .discCacheSize(100 * 1024 * 1024).build();
            ImageLoader.getInstance().init(config);

          background=(ImageView)findViewById(R.id.backgroundView);
            mFaceBookProfilePictureView = (ProfilePictureView) findViewById(R.id.faceboolUserProfilePicture);
            try {
                setProfile(mProfilePic, mProfileName, mFaceBookProfilePictureView);
            } catch (IOException e) {
                e.printStackTrace();
            }

            mDrawerList.setAdapter(new CustomAdapter(this, mdrawerTitles));
            setupAnimations();
           // getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);


             mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                                   @Override
                                                   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                                    if(position==0)
                                                    {

                                                        Intent viewAboutVideoIntent = new Intent(getApplicationContext(), YouTubeActivity_.class);
                                                        startActivity(viewAboutVideoIntent);
                                                    }
                                                       else if(position==1)
                                                    {
                                                        GApplication.getInstance().triggerShareApp(MainActivity.this);
                                                    }
                                                       else if(position==2)
                                                    {
                                                       GApplication.getInstance().connectUsFaceBookPage(MainActivity.this);
                                                    }
                                                    else if(position==3)
                                                    {
                                                        Session session = Session.getActiveSession();
                                                        if (session != null)
                                                        {
                                                            Session.getActiveSession().closeAndClearTokenInformation();
                                                            Session.getActiveSession().close();
                                                            Session.setActiveSession(null);
                                                        }
                                                        else
                                                        {
                                                            session = new Session.Builder(GApplication.getInstance()).setApplicationId(getResources().getString(R.string.app_id)).build();
                                                            if (session != null)
                                                            {
                                                                Session.setActiveSession(session);
                                                                session.closeAndClearTokenInformation();
                                                                session.close();
                                                                Session.setActiveSession(null);
                                                            }
                                                        }

                                                        Intent i=new Intent(getApplicationContext(),IntroVideoActivity_.class);
                                                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                                        G.FACEBOOK_USER_PROFILE_PIC.write(null);
                                                        G.FACEBOOK_USER_NAME.write(null);
                                                        G.USER_NAME.write(null);
                                                        G.USER_EMAILID.write(null);
                                                        G.GOOGLEPLUS_USER_PROFILE_PIC.write(null);
                                                        G.FIRSTTIME_LAUNCH.write(true);
                                                        startActivity(i);
                                                        finish();
                                                    }
                                                   }
                                               });



            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                    R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {

                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);

                    invalidateOptionsMenu();
                }

                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);

                    invalidateOptionsMenu();
                }
            };

            mDrawerLayout.setDrawerListener(mDrawerToggle);

            final ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
            pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
            final PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
            tabs.setViewPager(pager);

            tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    tabs.setNextFocusRightId(position);
                    switch(position) {
                        case 0:


                            actionBarTitle.setText("Home");

                            break;
                        case 1:


                            actionBarTitle.setText("PG");
                            break;
                        case 2:


                            actionBarTitle.setText("AG");
                            break;
                        case 3:


                            actionBarTitle.setText("VG");
                            break;
                        case 4:

                            actionBarTitle.setText("MG");
                            break;
                        case 5:


                            actionBarTitle.setText("Ch");
                            break;
                        case 6:


                            actionBarTitle.setText("Gi");
                            break;
                        case 7:


                            actionBarTitle.setText("Fl");
                            break;
                    }

                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }

            });
            mIsMenuOpen = false;
            mMenuButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mIsMenuOpen)
                    {
                        transparent.setVisibility(View.GONE);
                        mMenuButton.startAnimation(mCloseAnimation);

                        mmenuCamera.startAnimation(mSlideOutAnimation);
                        mmenuMimic.startAnimation(mSlideOutAnimation);
                        mmenuMeme.startAnimation(mSlideOutAnimation);

                        mMenuBackground.startAnimation(mFadeOutAnimation);


                        mIsMenuOpen = false;
                    }
                    else
                    {
                        transparent.setVisibility(View.VISIBLE);
                        mMenuButton.startAnimation(mOpenAnimation);

                        mmenuCamera.startAnimation(mSlideInAnimation);
                        mmenuMimic.startAnimation(mSlideInAnimation);
                        mmenuMeme.startAnimation(mSlideInAnimation);

                        mMenuBackground.startAnimation(mFadeInAnimation);


                        mIsMenuOpen = true;
                    }
                }
            });

        }

Upvotes: 0

Views: 871

Answers (1)

mmark
mmark

Reputation: 1204

In your case, you are inflating the view twice.

In this line:

 View mCustomView = mInflater.inflate(R.layout.actionbar_home, null);

And this line:

getSupportActionBar().setCustomView(R.layout.actionbar_home);

So, when access the "actionbarTitle" you are referencing the first inflation, not the actual view attached to the ActionBar (as the method setCustomView(int id) inflates the view itself, see docs).

In order to access the actual title you either need to use:

getSupportActionBar().setCustomView(mCustomView);

Or initialise the "mCustomView" view, after you set the resourse to the ActionBar:

getSupportActionBar().setCustomView(R.layout.actionbar_home);
mCustomView = getSupportActionBar().getCustomView();

Upvotes: 2

Related Questions