1

I am in need of a custom toolbar for a new app that I am writing. When I try to add the view to my main layout it doesn't align properly. Can someone please explain why?

public class LjCustomToolbar extends Toolbar {

private Context context;
private LayoutInflater layoutInflater;
private TextView toolbar_title;

public LjCustomToolbar(Context context) {
    super(context);
    initialize(context);
}

public LjCustomToolbar(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize(context);
}


public LjCustomToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initialize(context);
}

private void initialize(Context context) {
    this.context = context;
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layoutInflater.inflate(R.layout.custom_toolbar, this);
}

}

custom_toolbar.xml

<Toolbar
style="@style/LjToolBarDefault"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Profile"
    style="@style/LjBodyNormalText"
    android:textColor="@color/white"
    android:layout_gravity="center"
    android:id="@+id/toolbar_title_text" />

 </Toolbar>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:background="@color/lj_color_primary">

<us.lj.CustomViews.LjCustomToolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</RelativeLayout>

Click here to see my current layout

1 Answers1

0

Can you show a screenshot, to see what is not working.
Why do you want to customize the Toolbar? If you want to add another views inside the toolbar you can do it in xml as all other UI elements

Pedro Varela
  • 2,296
  • 1
  • 26
  • 32
  • I have different requirements for toolbar for different activities. Like custom colors and I need to be able to control the back button visibility too. Please click on the link at the end of the question to see the image. – REVANTH SAI Feb 14 '17 at 17:55
  • First, surround your Toolbar with AppBarLayout. Second, to customize colors use themes instead. – Pedro Varela Feb 14 '17 at 18:18