تخصيص خلفية للمكتب

السلام عليكم، في هذه المقالة سوف أوضح لك كيفية تخصيص خلفية للمكتب بواسطة صورة من داخل التطبيق.
أولا قم بإنشاء تطبيق جديد.
بعدها قم بوضع صورة بداخل مجلد (drawable). في هذا المثال وضعت الصورة التالية (allah.jpg).

الخطوة الأولى: وضع الكود التالي مكان الموجود داخل ملف (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="greenfinger.wallpaper.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="تعيين خلفية"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

 

  • الخطوة الثانية: وضع الكود التالي مكان الموجود داخل ملف (MainActivity.java)
package greenfinger.wallpaper;

import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setWallpaper();
            }
        });
    }
    private void setWallpaper() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.allah);
        WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
        try{
            manager.setBitmap(bitmap);
            Toast.makeText(this, "تم تعيين الخلفية!", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(this, "خطأ!", Toast.LENGTH_SHORT).show();
        }
    }
}

 

  • الخطوة التالثة: وضع الكود التالي مكان الموجود داخل ملف (AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="greenfinger.wallpaper">

    <uses-permission android:name="android.permission.SET_WALLPAPER"/>
    <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>

 

لا تنسى تغيير الباكيدج، ضع الخاص بك

package greenfinger.wallpaper

 

الآن قم بتجربة التطبيق

بالتوفيق