Deepshikha Puri, the young Indian Entrepreneur heading the mobile development trade from years to successive extent, has worked with numerous clients and many tremendous brands in this industry of mobile encompassing in India and overseas maintaining promising work relationships with each of them with an impression to manage it's whole thing.

Saturday, April 28, 2018

Change Theme Programmatically Android

change theme programmatically android

Themes and Styles on Android allow us to separate the details of our app design from the UI structure and behavior, Similar to Stylesheets in web design. Download source code from here

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/rl_header"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"></RelativeLayout>

    <Button
        android:id="@+id/btn_red"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:text="Red" />

    <Button
        android:id="@+id/btn_yellow"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:text="Yellow" />


    <Button
        android:id="@+id/btn_green"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:text="Green" />

    <Button
        android:id="@+id/btn_purple"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:text="Purple" />

</LinearLayout>


MainActivity.kt:


package changetheme.deepshikha.com.changetheme

import android.graphics.Color
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        listener()
        init()
    }

    private fun init() {

    }

    private fun listener() {
        btn_red.setOnClickListener(this)
        btn_yellow.setOnClickListener(this)
        btn_green.setOnClickListener(this)
        btn_purple.setOnClickListener(this)
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_red -> {
                fn_changethemecolor(resources.getColor(R.color.theme_red), resources.getColor(R.color.red))
            }

            R.id.btn_yellow -> {
                fn_changethemecolor(resources.getColor(R.color.theme_yellow), resources.getColor(R.color.yellow))
            }

            R.id.btn_green -> {
                fn_changethemecolor(resources.getColor(R.color.theme_green), resources.getColor(R.color.green))
            }

            R.id.btn_purple -> {
                fn_changethemecolor(resources.getColor(R.color.theme_purple), resources.getColor(R.color.purple))
            }
        }
    }


    /**
     * Method for change theme color
     */
    fun fn_changethemecolor(color: Int, header_color: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window = window
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = color

        }
        rl_header.setBackgroundColor(header_color)

    }
}


No comments:

Post a Comment