0

I'm trying to set the same action bar (with a custom font) to all my activities, but my "method" works only for the MainActivity called LoginActivity.

public class LoginActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        this.getActionBar().setDisplayShowCustomEnabled(true);
        this.getActionBar().setDisplayShowTitleEnabled(false);

        LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.actionbar, null);
        Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Billabong.ttf");

        TextView title = ((TextView)v.findViewById(R.id.custom_bar));
        title.setText(this.getTitle());
        title.setTypeface(font);

        this.getActionBar().setCustomView(v);

If I call the same piece of code to another activity, for example MessagesActivity:

public class MessagesActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messages);

        this.getActionBar().setDisplayShowCustomEnabled(true);
        this.getActionBar().setDisplayShowTitleEnabled(false);

        LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.actionbar, null);
        Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Billabong.ttf");

        TextView title = ((TextView)v.findViewById(R.id.custom_bar));
        title.setText(this.getTitle());
        title.setTypeface(font);

        this.getActionBar().setCustomView(v);

When I click on the button to open this activity I get the error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rram.wehere/com.rram.wehere.MessagesActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
        at android.app.ActivityThread.startActivityNow(ActivityThread.java:2045)
        at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
        at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
        at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
        at android.widget.TabHost.setCurrentTab(TabHost.java:413)
        at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
        at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:548)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18439)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5149)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.rram.wehere.BaseActivity.onCreate(BaseActivity.java:23)
        at com.rram.wehere.MessagesActivity.onCreate(MessagesActivity.java:20)

This tells me that this.getActionBar() returns null, even if I already declared that the theme has an ActionBar, as defined in my Manifest and later in my styles.xml like this

<resources>
    <style name="myTheme"  parent="@style/Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/myTheme.ActionBar</item>
        <item name="android:actionMenuTextColor">@color/actionBarText</item>
    </style>

    <style name="myTheme.ActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@color/bluefb</item>
        <item name="android:titleTextStyle">@style/myTheme.ActionBar.Text</item>
        <item name="android:gravity">center</item>
    </style>

    <style name="myTheme.ActionBar.Text" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/black</item>
        <item name="android:textStyle">bold</item>
    </style>

I don't know how to fix this problem, can someone help me to fix this issue?

3 Answers3

2

Make one Activity class with Actionbar setup as you like and extend that activity on other activities throughout your app.

Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
  • 1
    I tried it, but the error persists on the this.getActionBar().... line of code, it seems that it can't find the ActionBar in the other activities – Andrea Montanari May 07 '14 at 11:21
0

See this..

    public class ActionBarActivity extends Activity
    {


       setcontrol();

    }


    public static void setcontrol() {
        // TODO Auto-generated method stub
        ActionBar actionBar = getActionBar();
        LayoutInflater inflater = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.actionbar_custom_view_home, null);
        actionBar.setCustomView(view, new ActionBar.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayUseLogoEnabled(false);

        customActionbar_back_btn = (Button) view.findViewById(R.id.customActionbar_back_btn);
        customActionbar_back_btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.addnew_button));
        customActionbar_back_btn.setCompoundDrawablesWithIntrinsicBounds( R.drawable.abc_ic_ab_back_holo_dark, 0, 0, 0);
    }

Now extend this ActionBarActivity in your Activity and use this method to set the actionbar.

Born To Win
  • 3,319
  • 3
  • 19
  • 27
0

Hello everyone I achieved same custom action bar/ app bar in the all activities.

Here I am hiding defaults app bar from app theme and creating my own custom app bar and adding in fragment then loading fragment in all activities.

<androidx.cardview.widget.CardView 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="@dimen/dimen_60"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   app:cardElevation="@dimen/dimen_5"
   android:background="#ECF3F8">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginStart="@dimen/dimen_30">

        <ImageView
            android:layout_width="@dimen/dimen_25"
            android:layout_height="@dimen/dimen_25"
            android:id="@+id/iv_voice"
            android:src="@drawable/ic_voice"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:layout_width="@dimen/dimen_25"
            android:layout_height="@dimen/dimen_25"
            android:id="@+id/iv_battery"
            android:src="@drawable/ic_battery"
            app:layout_constraintStart_toEndOf="@id/iv_voice"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginStart="@dimen/dimen_20"/>

        <ImageView
            android:layout_width="@dimen/dimen_25"
            android:layout_height="@dimen/dimen_25"
            android:id="@+id/iv_wifi"
            android:src="@drawable/ic_wifi"
            app:layout_constraintStart_toEndOf="@id/iv_battery"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginStart="@dimen/dimen_20"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tc_time"
        android:format12Hour="hh:mm a"
        android:textSize="@dimen/text_20"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginEnd="@dimen/dimen_30">

        <ImageView
            android:layout_width="@dimen/dimen_30"
            android:layout_height="@dimen/dimen_30"
            android:id="@+id/iv_more"
            android:src="@drawable/ic_more"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:layout_width="@dimen/dimen_25"
            android:layout_height="@dimen/dimen_25"
            android:id="@+id/iv_user"
            android:src="@drawable/ic_user"
            app:layout_constraintStart_toEndOf="@id/iv_more"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginStart="@dimen/dimen_20"/>

        <ImageView
            android:layout_width="@dimen/dimen_25"
            android:layout_height="@dimen/dimen_25"
            android:id="@+id/iv_map"
            android:src="@drawable/ic_map"
            app:layout_constraintStart_toEndOf="@id/iv_user"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginStart="@dimen/dimen_20"/>

        <ImageView
            android:layout_width="@dimen/dimen_25"
            android:layout_height="@dimen/dimen_25"
            android:id="@+id/iv_home"
            android:src="@drawable/ic_home"
            app:layout_constraintStart_toEndOf="@id/iv_map"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginStart="@dimen/dimen_20"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

This my app bar UI

Then created fragment and include above UI in xml file

 <include
    layout="@layout/app_bar_layout"
    android:id="@+id/app_bar_layout"/>

Now we can access app bar icon in fragment and we can add event listener

public class AppBarFragment extends Fragment {

FragmentAppBarBinding binding;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    binding = FragmentAppBarBinding.inflate(getLayoutInflater());

    binding.appBarLayout.ivVoice.setOnClickListener(v ->
            startActivity(new Intent(requireActivity(), 
    ActivityContact.class)));


    return binding.getRoot();
}
}

Now we need to replace this fragment in all activities where we want to show app bar.

This is java code for all activities

 FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.app_bar_layout, new AppBarFragment());
    transaction.commit();

This is xml code for all activities

<FrameLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:id="@+id/app_bar_layout"
   app:layout_constraintTop_toTopOf="parent"/>

May this will help you

-Thanks.