Creating a Stylish UI with Bootstrap Theme in Android

bootstrap-android

Creating a Bootstrap-inspired theme in Android involves defining custom styles and colors that mimic Bootstrap’s design principles. While Bootstrap itself is primarily used for web development, we can adapt its styling for Android applications. In this example, we’ll create a simple UI with buttons and text views that resemble Bootstrap’s appearance.

Setting Up the Bootstrap-Inspired Theme

First, define custom colors and styles that emulate Bootstrap’s look and feel.

Step 1: Define Colors

Define the primary Bootstrap colors in your res/values/colors.xml file:
<resources>
    <color name="primary">#007bff</color>
    <color name="secondary">#6c757d</color>
    <color name="success">#28a745</color>
    <color name="danger">#dc3545</color>
    <color name="warning">#ffc107</color>
    <color name="info">#17a2b8</color>
    <color name="light">#f8f9fa</color>
    <color name="dark">#343a40</color>
</resources>

Step 2: Define Styles

Define custom styles in your res/values/styles.xml file:
<resources>
    <!-- Base application theme -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here -->
        <item name="colorPrimary">@color/primary</item>
        <item name="colorSecondary">@color/secondary</item>
        <item name="colorSuccess">@color/success</item>
        <item name="colorDanger">@color/danger</item>
        <item name="colorWarning">@color/warning</item>
        <item name="colorInfo">@color/info</item>
        <item name="colorLight">@color/light</item>
        <item name="colorDark">@color/dark</item>
    </style>

    <!-- Bootstrap button style -->
    <style name="BootstrapButton" parent="Widget.AppCompat.Button.Colored">
        <item name="android:backgroundTint">@color/primary</item>
        <item name="android:textColor">@color/light</item>
    </style>

    <!-- Bootstrap text style -->
    <style name="BootstrapText">
        <item name="android:textColor">@color/dark</item>
        <item name="android:textSize">16sp</item>
    </style>
</resources>

Example: Creating a Bootstrap-Inspired UI

Now, let’s create a simple UI with buttons and text views that utilize the Bootstrap-inspired styles.

Step 3: Define the Layout

Create an XML layout file (activity_main.xml) with Bootstrap-inspired components:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bootstrap-Inspired UI"
        style="@style/BootstrapText"
        android:layout_gravity="center"
        android:layout_marginBottom="32dp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Primary Button"
        style="@style/BootstrapButton"
        android:layout_marginBottom="16dp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Secondary Button"
        style="@style/BootstrapButton"
        android:backgroundTint="@color/secondary"
        android:layout_marginBottom="16dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bootstrap-Inspired Text"
        style="@style/BootstrapText"
        android:layout_gravity="center"
        android:layout_marginTop="32dp"/>

</LinearLayout>

Step 4: Apply the Theme

Apply the Bootstrap-inspired theme to your MainActivity in the Android manifest (AndroidManifest.xml):
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bootstraptheme">

    <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">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Need Debugging? – Try RobotQA and Start Debugging on Real Devices. Download Plugin

Conclusion

By defining custom styles and colors inspired by Bootstrap, you can create a modern and visually appealing UI in your Android application. This example demonstrated how to set up the Bootstrap-inspired theme, define styles, and apply them to UI components. You can further customize and expand upon these styles to create a cohesive design language for your app. Happy coding!
Tagged:

Related Posts