findViewById<Button>(R.id.supabutton).setOnClickListener{Log.d("BUTTONS","User tapped the Supabutton")}
Java
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.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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-27 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],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)."]]