How to use AndroidBootstrap without extends Application

513 Views Asked by At

I am trying to use Android Bootstrap library. I followed Quick Start. In Quick Start, it says I should override my class like this:

public class SampleApplication extends Application {
    @Override public void onCreate() {
        super.onCreate();
        TypefaceProvider.registerDefaultIconSets();
    }
}

How can I use this library without extending Application class? I want to use this library in my Activity classes.

LoginActivity:

public class Login extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TypefaceProvider.registerDefaultIconSets();
        setContentView(R.layout.activity_login);
    }
}

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
    tools:context="com.example.merve.tev.Login">



    <com.beardedhen.androidbootstrap.BootstrapDropDown
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:bootstrapText="Medium {fa_thumbs_o_up}"
        app:bootstrapBrand="regular"
        app:roundedCorners="true"
        app:bootstrapSize="md"
        app:dropdownResource="@array/bootstrap_dropdown_example_data"
        app:bootstrapExpandDirection="down"
        tools:layout_editor_absoluteY="202dp"
        tools:layout_editor_absoluteX="115dp" />
</LinearLayout>

In my MainActivity class, I placed the button. When I click it, I should go LoginActivity class. However, I get an error:

java.lang.RuntimeException: Unable to start activity ComponentInfo: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.beardedhen.androidbootstrap.BootstrapDropDown
2

There are 2 best solutions below

3
On BEST ANSWER

In your activity class:

In onCreate() Method, write this line before setContentView();

protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         TypefaceProvider.registerDefaultIconSets();
         
}

I hope it will work.

0
On

It's recommended to invoke TypefaceProvider.registerDefaultIconSets(); in your application class because that will load the FontAwesome typeface, before any views are displayed on screen.

If you're not loading FontAwesome icons then you can skip this step. If you're worried about startup time then you could try performing it asynchronously.

Finally, if you know for a fact that your app will always launch from a certain activity, then you can call TypefaceProvider.registerDefaultIconSets(); before setContentView is called, and should still be able to use FontAwesome icons.

The only tradeoff here is that most apps have multiple Activities which act as entry points, meaning you might have to add this setup logic to multiple places. That's why the current advice is to set it up in your Application class - you'll only ever need to initialise it once.