4

I'm trying to change the default ActionBar of my app, and i try this in my MainActivity:

    package com.example.shaytsabar.footballtables.activities;

import android.app.ActionBar;
import android.net.Uri;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.LoginFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.example.shaytsabar.footballtables.R;
import com.example.shaytsabar.footballtables.fragments.TableStandingsFragment;
import com.example.shaytsabar.footballtables.fragments.TopBarLeaguesFragment;

public class MainActivity extends AppCompatActivity{

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

This is the layout file that i want to be instead of the regular ActionBar- trytopbarleagues.xml:

<?xml version="1.0" encoding="utf-8"?
 <android.support.constraint.ConstraintLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">

  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@color/colorAccent"
    android:minHeight="?attr/actionBarSize"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

  </android.support.v7.widget.Toolbar>
  </android.support.constraint.ConstraintLayout>

Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shaytsabar.footballtables">

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme.NoActionBar">
    <!--@style/Theme.AppCompat.Light.NoActionBar AppTheme -->
    <activity android:name=".activities.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
            <action android:name="android.intent.action.VIEW"/>

            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>

            <!-- Accepts URIs that begin with "java-lang-
            programming://android-app-google-plus-demo" -->
            <data
                android:host="java-lang-programming"
                android:scheme="android-app-google-plus-demo"/>
        </intent-filter>
    </activity>
</application>
</manifest>

The problem is that the ActionBar doesn't change. it remains the same as the defualt one.

Thanks for helping :)

Shayts
  • 107
  • 9

2 Answers2

2

You need to set the theme to that activity to NoactionBar in the AndroidManifest.xml file using the following

android:theme="@style/AppTheme.NoActionBar"

Then in the MainActivity.java, instead of the following two lines

android.support.v7.app.ActionBar mActionBar = getSupportActionBar();
mActionBar.setCustomView(R.layout.trytopbarleagues);

add these lines of code

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Saila Lopa
  • 81
  • 1
  • 7
  • I tried to use android:theme="@style/AppTheme.NoActionBar", but i got an error: "cannt resolve symbol '@style/AppTheme.NoActionBar'. Then i tried to write @style/Theme.AppCompat.Light.NoActionBar, and i didn't got any errors, but then everything became darker, and i couldn't see any action bar on top. – Shayts Aug 15 '17 at 19:36
  • In your style.xml file add the following – Saila Lopa Aug 15 '17 at 19:45
  • What do you mean no action bar at all? Can you post the manifest file and activity file content ? – Saila Lopa Aug 15 '17 at 20:04
  • i've uploded the manifest and edited the mainActivity. It's in the question on top – Shayts Aug 15 '17 at 20:18
1
actionBar.setDisplayShowCustomEnabled(true);

add this line before setting custom view.

Levon Petrosyan
  • 8,815
  • 8
  • 54
  • 65
  • Try that, but it shows me my pink toolbar from the xml file, on the default action bar, with paddings. So what i actually see is pink action bar on the regular blue one. – Shayts Aug 15 '17 at 19:32
  • Did you add these lines? Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); – Levon Petrosyan Aug 15 '17 at 19:46
  • Do you mean in like that? Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); android.support.v7.app.ActionBar mActionBar = getSupportActionBar(); mActionBar.setDisplayShowCustomEnabled(true); setSupportActionBar(toolbar); – Shayts Aug 15 '17 at 20:06
  • Try to move this line to the end mActionBar.setDisplayShowCustomEnabled(true); And then set custom view. – Levon Petrosyan Aug 15 '17 at 20:12
  • Then i have this exception: "This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead." – Shayts Aug 15 '17 at 20:27
  • https://stackoverflow.com/questions/30669926/do-not-request-window-feature-action-bar-and-set-windowactionbar-to-false-in-you – Levon Petrosyan Aug 15 '17 at 20:30