findViewById<Button>(R.id.supabutton).setOnClickListener{Log.d("BUTTONS","User tapped the Supabutton")}
جاوا
Buttonbutton=(Button)findViewById(R.id.supabutton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){Log.d("BUTTONS","User tapped the Supabutton");}});
دکمه خود را سبک کنید
ظاهر دکمه شما - تصویر پسزمینه و فونت - بین دستگاهها متفاوت است، زیرا دستگاههای سازندههای مختلف اغلب سبکهای پیشفرض متفاوتی برای کنترلهای ورودی دارند.
برای سفارشی کردن دکمهها با پسزمینه متفاوت، ویژگی android:background با یک منبع قابل ترسیم یا رنگ مشخص کنید. از طرف دیگر، میتوانید یک استایل برای دکمه اعمال کنید، که مشابه سبکهای HTML برای تعریف ویژگیهای چند سبک مانند پسزمینه، فونت و اندازه کار میکند. برای اطلاعات بیشتر در مورد اعمال سبکها، به سبکها و تمها مراجعه کنید.
دکمه بدون حاشیه
یکی از طراحی هایی که می تواند مفید باشد دکمه "بدون حاشیه" است. دکمههای بدون حاشیه شبیه دکمههای اصلی هستند با این تفاوت که هیچ حاشیه یا پسزمینهای ندارند، اما همچنان در حالتهای مختلف، مانند هنگام ضربه زدن، ظاهر تغییر میکنند.
برای ایجاد یک دکمه بدون حاشیه، مانند مثال زیر، سبک borderlessButtonStyle را روی دکمه اعمال کنید:
اگر می خواهید واقعاً ظاهر دکمه خود را دوباره تعریف کنید، می توانید یک پس زمینه سفارشی را مشخص کنید. با این حال، به جای ارائه یک بیت مپ یا رنگ ساده، پسزمینه شما باید یک منبع فهرست وضعیت باشد که بسته به وضعیت فعلی دکمه ظاهر آن را تغییر میدهد.
شما می توانید لیست حالت را در یک فایل XML تعریف کنید که سه تصویر یا رنگ را برای استفاده برای حالت های مختلف دکمه تعریف می کند.
برای ایجاد یک لیست وضعیت قابل ترسیم برای پس زمینه دکمه خود، موارد زیر را انجام دهید:
سه بیت مپ برای پسزمینه دکمه ایجاد کنید که نشاندهنده وضعیت دکمههای پیشفرض، ضربهخورده و متمرکز است. برای اطمینان از اینکه تصاویر شما با دکمههایی در اندازههای مختلف مطابقت دارند، نقشههای بیتی را به صورت بیت مپهای نه وصله ایجاد کنید.
بیت مپ ها را در دایرکتوری res/drawable/ پروژه خود قرار دهید. هر بیت مپ را برای نشان دادن حالت دکمه ای که نشان می دهد، نام ببرید، مانند button_default.9.png ، button_pressed.9.png ، و button_focused.9.png .
یک فایل XML جدید در دایرکتوری res/drawable/ ایجاد کنید. نام آن را چیزی شبیه button_custom.xml بگذارید. XML را مانند زیر وارد کنید:
برای اطلاعات بیشتر در مورد این نحو XML، از جمله نحوه تعریف دکمه غیرفعال، شناور یا در حالت دیگری، درباره StateListDrawable بخوانید.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# Add buttons to your app\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add components in Compose. \n[Button →](/develop/ui/compose/components/button) \n\nA button consists of text or an icon, or both, that communicates what action occurs when the user\ntaps it.\n| **Note:** For a better UI and user experience, see the [Material Design button](https://m3.material.io/components/all-buttons) documentation.\n\nYou can create a button in your layout in one of three ways, depending on\nwhether you want a button with text, an icon, or both: \n\n```xml\n \n \u003cLinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n android:paddingLeft=\"16dp\"\n android:paddingRight=\"16dp\"\n android:orientation=\"vertical\" \u003e\n \n \u003cButton\n android:id=\"@+id/supabutton\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:text=\"I'm a button\" /\u003e\n \n \u003cImageButton\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:contentDescription=\"A tiny Android icon\"\n android:src=\"@drawable/baseline_android_24\"\n app:tint=\"#ff0000\" /\u003e\n \n \u003cButton\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:drawableStart=\"@drawable/baseline_android_24\"\n android:drawablePadding=\"4dp\"\n android:drawableTint=\"#ff0000\"\n android:text=\"I'm a button with an icon\" /\u003e\n \u003c/LinearLayout\u003e\n```\n\nThe previous code generates something like this:\n**Figure 1.** Three styles of buttons.\n\nRespond to click events\n-----------------------\n\nWhen the user taps a button, the\n[Button](/reference/android/widget/Button) object receives an\non-click event.\n\nTo declare the event handler programmatically, create an\n[View.OnClickListener](/reference/android/view/View.OnClickListener)\nobject and assign it to the button by calling\n[setOnClickListener(View.OnClickListener)](/reference/android/view/View#setOnClickListener(android.view.View.OnClickListener)),\nas in the following example: \n\n### Kotlin\n\n```kotlin\nfindViewById\u003cButton\u003e(R.id.supabutton)\n .setOnClickListener {\n Log.d(\"BUTTONS\", \"User tapped the Supabutton\")\n }\n```\n\n### Java\n\n```java\nButton button = (Button) findViewById(R.id.supabutton);\nbutton.setOnClickListener(new View.OnClickListener() {\n public void onClick(View v) {\n Log.d(\"BUTTONS\", \"User tapped the Supabutton\");\n }\n});\n```\n\nStyle your button\n-----------------\n\nThe appearance of your button---the background image and font---varies between devices,\nbecause devices by different manufacturers often have different default styles for input\ncontrols.\n\nTo customize individual buttons with a different background, specify the\n[android:background](/reference/android/R.attr#background) attribute\nwith a drawable or color resource. Alternatively, you can apply a *style* for the button,\nwhich works in similarly to HTML styles to define multiple style properties such as the background,\nfont, and size. For more information about applying styles, see\n[Styles and themes](/guide/topics/ui/themes).\n\n### Borderless button\n\nOne design that can be useful is a \"borderless\" button. Borderless buttons resemble basic buttons\nexcept that they have no borders or background but still change appearance during different states,\nsuch as when tapped.\n\nTo create a borderless button, apply the\n[borderlessButtonStyle](/reference/android/R.attr#borderlessButtonStyle)\nstyle to the button, as in the following example: \n\n```xml\n\u003cButton\n android:id=\"@+id/supabutton\"\n style=\"?android:attr/borderlessButtonStyle\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:text=\"I'm a button\" /\u003e\n```\n\n### Custom background\n\nIf you want to truly redefine the appearance of your button, you can specify a custom background.\nInstead of supplying a simple bitmap or color, however, your background must be a state list\nresource that changes appearance depending on the button's current state.\n\nYou can define the state list in an XML file that defines three images or colors to use for the\ndifferent button states.\n\nTo create a state list drawable for your button background, do the following:\n\n1. Create three bitmaps for the button background that represent the default, tapped, and focused button states. To ensure that your images fit buttons of various sizes, create the bitmaps as [nine-patch](/guide/topics/graphics/2d-graphics#nine-patch) bitmaps.\n2. Place the bitmaps into your project's `res/drawable/` directory. Name each bitmap to reflect the button state it represents, such as `button_default.9.png`, `button_pressed.9.png`, and `button_focused.9.png`.\n3. Create a new XML file in the `res/drawable/` directory. Name it something like `button_custom.xml`. Insert XML like the following: \n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cselector xmlns:android=\"http://schemas.android.com/apk/res/android\"\u003e\n \u003citem android:drawable=\"@drawable/button_pressed\"\n android:state_pressed=\"true\" /\u003e\n \u003citem android:drawable=\"@drawable/button_focused\"\n android:state_focused=\"true\" /\u003e\n \u003citem android:drawable=\"@drawable/button_default\" /\u003e\n \u003c/selector\u003e\n ```\n\n This defines a single drawable resource that changes its image based on the current state of\n the button.\n - The first `\u003citem\u003e` defines the bitmap to use when the button is tapped (activated).\n - The second `\u003citem\u003e` defines the bitmap to use when the button is focused, such as when the button is highlighted using the trackball or directional pad.\n - The third `\u003citem\u003e` defines the bitmap to use when the button is in the default state, neither tapped nor focused.\n\n | **Note:** The order of the `\u003citem\u003e` elements is important. When this drawable is referenced, the `\u003citem\u003e` elements are traversed in order to determine which one is appropriate for the current button state. Because the default bitmap is last, it is only applied when the conditions `android:state_pressed` and `android:state_focused` are evaluated as false.\n\n This XML file represents a single drawable resource. When referenced by a `Button` for\n its background, the image displayed changes based on the button's state.\n4. Apply the drawable XML file as the button background: \n\n ```xml\n \u003cButton\n android:id=\"@+id/button_send\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:text=\"@string/button_send\"\n android:onClick=\"sendMessage\"\n android:background=\"@drawable/button_custom\" /\u003e\n ```\n\nFor more information about this XML syntax, including how to define a button that is disabled,\nhovered, or in another state, read about\n[StateListDrawable](/guide/topics/resources/drawable-resource#StateList)."]]