0

i created an a custom toolbar to replace the default action bar of appcompat in android. but when i run the application i get two title, like in the picture

https://i.stack.imgur.com/wOPVE.png

i want only the title in the middle here the code: Main.java

public class MainMenu extends AppCompatActivity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);
    Toolbar toolbar = (Toolbar)findViewById(R.id.mytoolbar);
    setSupportActionBar(toolbar);

    Typeface mistral = Typeface.createFromAsset(getAssets(), "MISTRAL.TTF");
    TextView title = (TextView)findViewById(R.id.toolbar_title);
    title.setText("Fyllo");
    title.setTypeface(mistral);


}
@Override
public boolean onCreateOptionsMenu(Menu menu){
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.my_menu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id =item.getItemId();
    if(id == R.id.action_settings){
        Toast.makeText(getApplicationContext(),"this is a menu option",Toast.LENGTH_SHORT).show();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

MainMenu.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.adams.fyllo.MainMenu">

<android.support.v7.widget.Toolbar
    android:id="@+id/mytoolbar"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="#00000000"
    android:elevation="4dp"
    android:title="Services">

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_gravity="center"
        android:text="Toolbar Title" />
</android.support.v7.widget.Toolbar>
</RelativeLayout>

And for style, i am using AppTheme

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#000000</item>
    <item name="colorPrimaryDark">#000000</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:titleTextStyle">@style/NoTitleText</item>
</style>

<style name="NoTitleText">
    <item name="android:textSize">0sp</item>
    <item name="android:textColor">#00000000</item>
</style>


</resources>

What can i do to remove this error

adams
  • 101
  • 1
  • 7

2 Answers2

1

You have given titles two times either you want to remove upper one remove TextView which is inside Toolbar and setTitle of toolbar.

otherwise remove title from the Toolbar

Keyur Lakhani
  • 4,321
  • 1
  • 24
  • 35
0

You can remove title with help of

getSupportActionBar().setDisplayShowTitleEnabled(false);
Kathi
  • 1,061
  • 1
  • 16
  • 31
  • when i do it in the emulator, it crash. i tried it on my phone, it is working. Do you know why? – adams Apr 02 '16 at 08:46
  • Can i see crash report ? – Kathi Apr 02 '16 at 09:03
  • Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference at com.example.adams.fyllo.MainMenu.onCreate(MainMenu.java:22) – adams Apr 02 '16 at 09:11