تغییر رنگ پس زمینه toast در اندروید

دوشنبه 13 شهریور 1396

در این مقاله قصد داریم رنگ پس زمینه ی مورد نظر را در toast تغییر دهیم شاید شما در بعضی از اپ های خود از toast استفاده کرده باشید و شاید بخواهید رنگ پس زمینه ی آن را تغییر دهید با این سورس می توانید رنگ پس زمینه ی مورد نظر را تغییر دهید.

تغییر رنگ پس زمینه toast در اندروید

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

 package com.android_examples.com.toastbackgroundcolorchange;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {

 Button BT;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 BT = (Button)findViewById(R.id.button1);
 BT.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 Toast ToastMessage = Toast.makeText(getApplicationContext(),"Change Toast Background color",Toast.LENGTH_SHORT);
 View toastView = ToastMessage.getView();
 toastView.setBackgroundResource(R.layout.toast_background_color);
 ToastMessage.show();
 
 }
 });
 }
}

حالا xml مورد نظر را قرار دهید:

 <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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.toastbackgroundcolorchange.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="CLICK HERE TO SHOW TOAST MESSAGE WITH DIFFERENT BACKGROUND COLOR INCLUDING BORDER" />

</RelativeLayout>

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

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

 <solid android:color="#00dcfd" />
 <stroke
 android:width="3dp"
 android:color="#fd0261" ></stroke>

</shape>

خروجی به صورت زیر خواهد بود:

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

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

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

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