0

//Here is my fragment

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white">

            <RelativeLayout
                android:id="@+id/background"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_420sdp"
                android:background="#EAEAEA"
                >

                <RelativeLayout
                    android:id="@+id/home_bg_layout"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/_400sdp"
                    android:background="@drawable/sama">

                    <Button
                        android:id="@+id/bookAppointmentBtn"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/_55sdp"
                        android:layout_marginLeft="@dimen/_30sdp"
                        android:layout_marginRight="@dimen/_30sdp"
                        android:fontFamily="@string/roboto_light"
                        android:textSize="@dimen/_20sdp"
                        android:layout_alignParentBottom="true"
                        android:layout_marginBottom="@dimen/_10sdp"
                        android:background="@drawable/book_appointmentbg"
                        android:textColor="@color/white"
                        android:text="@string/bookappointment" />

                </RelativeLayout>


            </RelativeLayout>

            <Button
                android:id="@+id/ContactNoBtn"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_46sdp"
                android:text="@string/shop_number"
                android:textColor="#ffffff"
                android:textSize="@dimen/_25sdp"
                android:layout_below="@+id/discountsTv"
                android:textAllCaps="false"
                android:layout_marginRight="@dimen/_30sdp"
                android:layout_marginTop="@dimen/_5sdp"
                android:layout_marginLeft="@dimen/_30sdp"
                android:background="@drawable/black_background"/>


            <LinearLayout
                android:id="@+id/servicesLinearLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:baselineAligned="false"
                android:layout_marginLeft="@dimen/_20sdp"
                android:layout_marginRight="@dimen/_20sdp"
                android:orientation="horizontal"
                android:layout_below="@+id/servicesupperview"
                >

                <LinearLayout
                    android:id="@+id/menLayout"
                    android:layout_width="@dimen/_108sdp"
                    android:layout_height="@dimen/_27sdp"
                    android:layout_margin="@dimen/_10sdp"
                    android:layout_weight="1"
                    >
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@string/roboto_medium"
                        android:textColor="#535353"
                        android:layout_marginStart="@dimen/_17sdp"
                        android:layout_marginTop="@dimen/_5sdp"
                        android:textSize="@dimen/_12sdp"
                        android:text="@string/mens"
                        />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/womenLayout"
                    android:layout_width="@dimen/_108sdp"
                    android:layout_height="@dimen/_27sdp"
                    android:layout_margin="@dimen/_10sdp"
                    android:layout_weight="1"
                    >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@string/roboto_medium"
                        android:textSize="@dimen/_12sdp"
                        android:layout_marginStart="@dimen/_10sdp"
                        android:layout_marginTop="@dimen/_5sdp"
                        android:textColor="#535353"
                        android:text="@string/womens"
                        />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/childrenLayout"
                    android:layout_width="@dimen/_108sdp"
                    android:layout_height="@dimen/_27sdp"
                    android:layout_margin="@dimen/_10sdp"
                    android:layout_weight="1">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="@dimen/_12sdp"
                        android:fontFamily="@string/roboto_medium"
                        android:textColor="#535353"
                        android:layout_marginStart="@dimen/_20sdp"
                        android:layout_marginTop="@dimen/_5sdp"
                        android:text="@string/children_caps"
                        />
                </LinearLayout>
            </LinearLayout>
    </ScrollView>
</RelativeLayout>

I am having toolbar in MainActivity. This is my fragment i want to set title, image and back button inside the toolbar. Here i am using bottom navigation to change the fragments. Each and every fragment i want to set different title and image. I have to set title as center of the toolbar and back button as right corner of the toolbar.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Deepa Dinesh
  • 171
  • 2
  • 2
  • 11

3 Answers3

1

In your MainActivity add these lines in OnCreate() method

    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle("My Activity Title");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

To use Back arrow you can write this code outside onCreate()

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        Toast.makeText(this, "Back button pressed", Toast.LENGTH_SHORT).show();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

In you fragment you can change title of Toolbar

getActivity().setTitle("My fragment Title");
Ali Ahmed
  • 2,130
  • 1
  • 13
  • 19
0

You can Create custom Toolbar with ImageView and TextView with your arrow . include_tool_bar.xml

<com.mastercard.wallet.ui.widget.CustomTextView
    android:id="@+id/toolbar_title"
    style="@style/Toolbar.TitleText"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical|left"
    android:textSize="@dimen/px_48"
    app:fontName="InterStateLight"
    customviews:fontName="InterStateLight" />

<ImageView
    android:id="@+id/toolbar_logo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:contentDescription="@string/adaCitiPayLogoText"
    android:src="@drawable/ab_logo"
    android:visibility="gone" />

And Add Toolbar

Toolbar toolbar = (Toolbar) appCompatActivity.findViewById(R.id.toolbar);

  ImageView logo = (ImageView) toolbar.findViewById(R.id.toolbar_logo);
  CustomTextView textView = (CustomTextView) toolbar.findViewById(R.id.toolbar_title);

  logo.setVisibility(View.VISIBLE);
  logo.setImageResource(logoResourceId);
  textView.setVisibility(View.GONE);
  appCompatActivity.setSupportActionBar(toolbar);

 appCompatActivity.getSupportActionBar().setDisplayShowTitleEnabled(false);
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
0

title of the toolbar can be changed from fragment calling this in onActivityCreated

     @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            ((MainActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.menu_about_us));
            ((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            ((MainActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

in your activity make sure to add a toolbar like this.

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

and set it in your activity

setSupportActionBar(findViewById(R.id.toolbar));

and for the back press just add a listener and do the needful.

toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       // pop up fragment or take another action

   }
});
vikas kumar
  • 10,447
  • 2
  • 46
  • 52