آموزش ایجاد تم روز و شب در اندروید

یکشنبه 10 اردیبهشت 1396

در این مقاله قصد داریم یک سورس قرار دهیم. این مقاله برای زمانی به درد ما می خورد که شما بخواهید بک گراند گوشی خود را به حالت شب و یا روز در بیاورید که در اکثر اپلیکیشن هایی که مربوط به کتاب هست استفاده می شود.

آموزش ایجاد تم روز و شب در اندروید

ابتدا در Xml قطعه کد زیر را قرار دهید:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

    </style>

</resources>

برای رنگ ها هم از قطعه کد زیر استفاده نمایید:

<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="nightBackground">#555555</color>
    <color name="dayBackground">#fffefe</color>
</resources>

در کلاس main قطعه کد زیر را قرار دهید:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.os.Handler;

public class MainActivity extends AppCompatActivity {

    Button daybutton,nightbutton ;
    ImageView imageView ;
    RelativeLayout relativeLayout ;
    Handler handler;
    Runnable runnable;

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

        daybutton = (Button)findViewById(R.id.button);

        nightbutton = (Button)findViewById(R.id.button2);

        imageView = (ImageView)findViewById(R.id.imageview1);

        relativeLayout = (RelativeLayout)findViewById(R.id.relativelayout1);

        daybutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                DayThemeMode();

            }
        });



        nightbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                NightThemeMode();

            }
        });
    }

    public void DayThemeMode(){

        handler = new Handler();

        imageView.setImageResource(R.drawable.sun);

        imageView.setVisibility(View.VISIBLE);

        relativeLayout.setBackgroundColor(getResources().getColor(R.color.dayBackground));

        runnable = new Runnable() {

            @Override
            public void run() {

                //Hiding image after 4 seconds

                imageView.setVisibility(View.INVISIBLE);

            }
        };
        handler.postDelayed(runnable, 4000);

    }

    public void NightThemeMode(){

        handler = new Handler();

        imageView.setImageResource(R.drawable.moon);

        imageView.setVisibility(View.VISIBLE);

        relativeLayout.setBackgroundColor(getResources().getColor(R.color.nightBackground));

        runnable = new Runnable() {

            @Override
            public void run() {

                //Hiding image after 4 seconds

                imageView.setVisibility(View.INVISIBLE);

            }
        };
        handler.postDelayed(runnable, 4000);

    }
}

برای قسمت design باید قطعه کد زیر را بنویسید:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android_examples.daynightmodetheme_android_examplescom.MainActivity"
    android:id="@+id/relativelayout1">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="ENABLE DAY MODE"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignStart="@+id/button2" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="ENABLE NIGHT MODE"
        android:id="@+id/button2"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="350dp"
        android:layout_height="350dp"
        android:id="@+id/imageview1"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

برنامه نویسان

نویسنده 3355 مقاله در برنامه نویسان

کاربرانی که از نویسنده این مقاله تشکر کرده اند

در صورتی که در رابطه با این مقاله سوالی دارید، در تاپیک های انجمن مطرح کنید