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)

    }
}


Saturday, April 21, 2018

Custom Toast In Android Kotlin

create custom toast class android

A toast is used to display simple feedback about an operation in popup and automatically fade out after timeout. In this tutorial I am create a custom toast by using custom layout. Download source code from here:

activity_main:

<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="10dp">


    <Button
        android:id="@+id/btn_refresh"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Refresh Toast" />

    <Button
        android:id="@+id/btn_error2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:text="Info" />

    <Button
        android:id="@+id/btn_network"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:text="Network Toast" />

    <Button
        android:id="@+id/btn_error"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:text="Error Toast" />

</LinearLayout>

custom_toast.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_container"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="8dp"
    android:background="#DAAA">
    <ImageView android:src="@drawable/ic_launcher_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv_image"
        android:layout_marginRight="8dp"
        />
    <TextView android:id="@+id/tv_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFF"
        />
</LinearLayout>


MainActivity.kt:
package com.deepshikha.customtoast

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.custom_toast.view.*

class MainActivity : AppCompatActivity(), View.OnClickListener {

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

        listener()
    }

    private fun listener() {
        btn_refresh.setOnClickListener(this)
        btn_error2.setOnClickListener(this)
        btn_network.setOnClickListener(this)
        btn_error.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_error2 -> {
                var toast: Toast = Toast(this)
                toast.createToast(this, "Dummy data is benign information", Gravity.BOTTOM, Toast.LENGTH_SHORT, "#eab04c", R.drawable.ic_info_black_24dp)
            }

            R.id.btn_refresh -> {

                var toast: Toast = Toast(this)
                toast.createToast(this, "Data has been Refreshed", Gravity.BOTTOM, Toast.LENGTH_SHORT, "#2ba622", R.drawable.ic_autorenew_black_24dp)

            }

            R.id.btn_network -> {

                var toast: Toast = Toast(this)
                toast.createToast(this, "Check your Internet connection", Gravity.BOTTOM, Toast.LENGTH_SHORT, "#7c5ffa", R.drawable.ic_network_check_black_24dp)

            }

            R.id.btn_error -> {
                var toast: Toast = Toast(this)
                toast.createToast(this, "Error: Server not found", Gravity.BOTTOM, Toast.LENGTH_SHORT, "#ec1e1e", R.drawable.ic_error_outline_black_24dp)
            }
        }
    }

    fun Toast.createToast(context: Context, message: String, gravity: Int, duration: Int, backgroucolor: String, imagebackgroud: Int) {
        val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        /*first parameter is the layout you made
        second parameter is the root view in that xml
         */
        val layout = inflater.inflate(R.layout.custom_toast, (context as Activity).findViewById(R.id.custom_toast_container))

        layout.findViewById(R.id.tv_message).text = message
        layout.setBackgroundColor(Color.parseColor(backgroucolor))
        layout.findViewById(R.id.iv_image).iv_image.setImageResource(imagebackgroud)
        setGravity(gravity, 0, 100)
        setDuration(Toast.LENGTH_LONG);

        setView(layout);
        show()
    }

}