Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a title in Toolbar from fragment in Android

I have been using the latest Toolbar from AppCompatv7 lib.I have placed a textview in the ToolBar ViewGroup And I want to set a title into this Textview from the fragment in my activity.In case of a custom action bar ((ActionBarActivity)getActivity).setcustomView(..) would have done the job.But due to use of this ToolBar I am not able to use that.Also I have implemented a method in my BaseActivity that is inherited by all Activities.This BaseActivity contains my method to initialize a sliding drawer to the left.I have to initialize the initDrawerLayout() method in activity else the drawer would not be initialized.And if I initialize it in fragment its giving me all empty results,neither the toggle button for drawer and nor is the custom title getting set.

This is my initDrawer code..

public void initDrawerLayout(String toolbar_text) {         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);         mDrawerRelative = (RelativeLayout) findViewById(R.id.drawer_relative);         if (mDrawerLayout != null) {             findViewById(R.id.drawer_btn_a).setOnClickListener(this);             findViewById(R.id.drawer_btn_b).setOnClickListener(this);             findViewById(R.id.drawer_btn_c).setOnClickListener(this);             findViewById(R.id.drawer_btn_d).setOnClickListener(this);             findViewById(R.id.drawer_btn_e).setOnClickListener(this);             findViewById(R.id.drawer_btn_f).setOnClickListener(this);             findViewById(R.id.drawer_btn_g).setOnClickListener(this);             findViewById(R.id.drawer_btn_h).setOnClickListener(this);             findViewById(R.id.drawer_btn_i).setOnClickListener(this);             findViewById(R.id.drawer_btn_j).setOnClickListener(this);             findViewById(R.id.drawer_btn_k).setOnClickListener(this);             findViewById(R.id.drawer_btn_l).setOnClickListener(this);             findViewById(R.id.my_layout).setOnClickListener(this);                  Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);             toolbar.setBackground(getResources().getDrawable(R.drawable.icn_actionbar_background));             TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);             mTitle.setText(toolbar_text);             mTitle.setTypeface(Typeface.DEFAULT_BOLD);             if (toolbar != null) {                 setSupportActionBar(toolbar);                  getSupportActionBar().setDisplayHomeAsUpEnabled(true);             }             toolbar.setNavigationIcon(R.drawable.ic_drawer);               mDrawerToggle = new ActionBarDrawerToggle(                     this,  mDrawerLayout, toolbar,                     R.string.drawer_open, R.string.drawer_close                 );                 mDrawerLayout.setDrawerListener(mDrawerToggle);               toolbar.setNavigationOnClickListener(new OnClickListener() {                  @Override                 public void onClick(View v) {                     if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {                         mDrawerLayout.closeDrawer(Gravity.LEFT);                     } else {                         mDrawerLayout.openDrawer(Gravity.LEFT);                     }                 }             });             mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,                     GravityCompat.START);              mDrawerLayout.setScrimColor(getResources().getColor(                     android.R.color.transparent));             getSupportActionBar().setDisplayHomeAsUpEnabled(true);             getSupportActionBar().setHomeButtonEnabled(true);             getSupportActionBar().setDisplayShowTitleEnabled(false);          }     } 

And this my code in the fragment..

((FirstActivity) getActivity()).initDrawerLayout(mFirst.name); where mFirst is a object of class Person

and the toolbar code..

<android.support.v7.widget.Toolbar             android:id="@+id/my_awesome_toolbar"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:minHeight="?attr/actionBarSize">            <TextView                 android:id="@+id/toolbar_title"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_gravity="center"                 android:gravity="center"                 android:text="Toolbar Title"                 android:textColor="@color/action_text-color"                 android:textSize="18sp"                 android:textStyle="bold" />               </android.support.v7.widget.Toolbar> 

Please help guys..

like image 966
AndroidMech Avatar asked Nov 24 '14 07:11

AndroidMech


1 Answers

I do this like this: from the fragment call

getActivity().setTitle("your title"); 

Also you can call any function of your parent Activity like this:

YourActivity mYourActiviy = (YourActivity) getActivity(); mYourActivity.yourActivityFunction(yourParameters); 
like image 68
Hemant Singh Avatar answered Sep 24 '22 13:09

Hemant Singh