{"id":611,"date":"2024-06-11T13:20:26","date_gmt":"2024-06-11T13:20:26","guid":{"rendered":"https:\/\/robotqa.com\/blog\/?p=611"},"modified":"2024-06-11T13:20:26","modified_gmt":"2024-06-11T13:20:26","slug":"creating-a-modern-ui-with-material-dark-theme-in-android","status":"publish","type":"post","link":"https:\/\/robotqa.com\/blog\/creating-a-modern-ui-with-material-dark-theme-in-android\/","title":{"rendered":"Creating a Modern UI with Material Dark Theme in Android"},"content":{"rendered":"<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-561\" src=\"http:\/\/blog.robotqa.com\/wp-content\/uploads\/2024\/06\/2024061109571832.png\" alt=\"material-dark-android\" width=\"1064\" height=\"1064\" srcset=\"https:\/\/blog.robotqa.com\/wp-content\/uploads\/2024\/06\/2024061109571832.png 1064w, https:\/\/blog.robotqa.com\/wp-content\/uploads\/2024\/06\/2024061109571832-300x300.png 300w, https:\/\/blog.robotqa.com\/wp-content\/uploads\/2024\/06\/2024061109571832-1024x1024.png 1024w, https:\/\/blog.robotqa.com\/wp-content\/uploads\/2024\/06\/2024061109571832-150x150.png 150w, https:\/\/blog.robotqa.com\/wp-content\/uploads\/2024\/06\/2024061109571832-768x768.png 768w\" sizes=\"auto, (max-width: 1064px) 100vw, 1064px\" \/>\n\nThe Material Dark theme offers a visually appealing dark interface that enhances readability in low-light environments and provides a sleek, modern look for your Android applications. In this blog, we&#8217;ll walk through the steps to implement a simple Android UI using the Material Dark theme.\n<p><\/p>\n<h3><strong>Setting Up the Material Dark Theme<\/strong><\/h3>\nFirst, ensure you have the necessary dependencies in your <code>build.gradle<\/code> file.\n<p><\/p>\n<h4><strong>Step 1: Add Dependencies<\/strong><\/h4>\nOpen your <code>build.gradle<\/code> file and add the Material Components dependency:\n<pre class=\"lang:xhtml decode:true \">dependencies {\n    implementation 'com.google.android.material:material:1.8.0'\n    implementation 'androidx.appcompat:appcompat:1.3.1'\n    implementation 'androidx.core:core-ktx:1.6.0'\n}\n<\/pre>\n<h4><strong>Step 2: Define the Theme in styles.xml<\/strong><\/h4>\nOpen your <code>res\/values\/styles.xml<\/code> file and define the Material Dark theme:\n<pre class=\"lang:xhtml decode:true \">&lt;resources&gt;\n    &lt;!-- Base application theme --&gt;\n    &lt;style name=\"AppTheme\" parent=\"Theme.MaterialComponents.DayNight.DarkActionBar\"&gt;\n        &lt;!-- Customize your theme here --&gt;\n        &lt;item name=\"colorPrimary\"&gt;@color\/purple_500&lt;\/item&gt;\n        &lt;item name=\"colorPrimaryDark\"&gt;@color\/purple_700&lt;\/item&gt;\n        &lt;item name=\"colorAccent\"&gt;@color\/teal_200&lt;\/item&gt;\n    &lt;\/style&gt;\n&lt;\/resources&gt;\n<\/pre>\n<h3><strong>Example: Creating a Simple UI with Material Dark Theme<\/strong><\/h3>\nLet\u2019s create a simple UI that includes a welcome message and a button, all styled with the Material Dark theme.\n<p><\/p>\n<h4><strong>Step 3: Define the Layout<\/strong><\/h4>\nCreate an XML layout file (<code>activity_main.xml<\/code>) with the following content:\n<pre class=\"lang:xhtml decode:true \">&lt;com.google.android.material.textfield.TextInputLayout\n    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:padding=\"16dp\"&gt;\n\n    &lt;com.google.android.material.textfield.TextInputEditText\n        android:id=\"@+id\/inputEditText\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:hint=\"Enter text\"\n        android:inputType=\"text\" \/&gt;\n\n    &lt;com.google.android.material.button.MaterialButton\n        android:id=\"@+id\/submitButton\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Submit\"\n        android:layout_marginTop=\"16dp\"\n        app:cornerRadius=\"8dp\"\n        android:backgroundTint=\"@color\/teal_200\"\n        android:textColor=\"@android:color\/black\" \/&gt;\n&lt;\/com.google.android.material.textfield.TextInputLayout&gt;\n<\/pre>\n<p><\/p>\n<h4><strong>Step 4: Initialize in Activity<\/strong><\/h4>\nIn your <code>MainActivity.java<\/code> or <code>MainActivity.kt<\/code> file, initialize the views and add any necessary functionality.\n<h5><strong>Java<\/strong><\/h5>\n<pre class=\"lang:java decode:true \">package com.example.materialdarktheme;\n\nimport android.os.Bundle;\nimport androidx.appcompat.app.AppCompatActivity;\nimport com.google.android.material.button.MaterialButton;\nimport com.google.android.material.textfield.TextInputEditText;\nimport com.google.android.material.textfield.TextInputLayout;\nimport android.widget.Toast;\n\npublic class MainActivity extends AppCompatActivity {\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n\n        TextInputLayout textInputLayout = findViewById(R.id.inputEditText);\n        TextInputEditText textInputEditText = findViewById(R.id.inputEditText);\n        MaterialButton submitButton = findViewById(R.id.submitButton);\n\n        submitButton.setOnClickListener(v -&gt; {\n            String inputText = textInputEditText.getText().toString();\n            if (inputText.isEmpty()) {\n                Toast.makeText(MainActivity.this, \"Please enter some text\", Toast.LENGTH_SHORT).show();\n            } else {\n                \/\/ Handle the input text\n                Toast.makeText(MainActivity.this, \"You entered: \" + inputText, Toast.LENGTH_SHORT).show();\n            }\n        });\n    }\n}\n<\/pre>\n<h5><strong>Kotlin<\/strong><\/h5>\n<pre class=\"lang:kotlin decode:true \">package com.example.materialdarktheme\n\nimport android.os.Bundle\nimport android.widget.Toast\nimport androidx.appcompat.app.AppCompatActivity\nimport com.google.android.material.button.MaterialButton\nimport com.google.android.material.textfield.TextInputEditText\nimport com.google.android.material.textfield.TextInputLayout\n\nclass MainActivity : AppCompatActivity() {\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n\n        val textInputLayout: TextInputLayout = findViewById(R.id.inputEditText)\n        val textInputEditText: TextInputEditText = findViewById(R.id.inputEditText)\n        val submitButton: MaterialButton = findViewById(R.id.submitButton)\n\n        submitButton.setOnClickListener {\n            val inputText = textInputEditText.text.toString()\n            if (inputText.isEmpty()) {\n                Toast.makeText(this, \"Please enter some text\", Toast.LENGTH_SHORT).show()\n            } else {\n                \/\/ Handle the input text\n                Toast.makeText(this, \"You entered: $inputText\", Toast.LENGTH_SHORT).show()\n            }\n        }\n    }\n}\n<\/pre>\n<!-- CTA Section -->\n<p><\/p>\n<div class=\"bg-primary text-white text-center\">\n<div class=\"container space-1\"><span class=\"h6 d-block d-lg-inline-block font-weight-light mb-lg-0\"> <span class=\"font-weight-semi-bold\">Need Debugging?<\/span> \u2013 Try RobotQA and Start Debugging on Real Devices. <\/span> <a class=\"btn btn-sm btn-white transition-3d-hover font-weight-normal ml-3\" href=\"https:\/\/plugins.jetbrains.com\/plugin\/24460-robotqa-real-device-debugging-on-cloud\">Download Plugin<\/a><\/div>\n<\/div>\n<p><\/p>\n<!-- End CTA Section -->\n<h3><strong>Conclusion<\/strong><\/h3>\nUsing the Material Dark theme allows your app to provide a modern and user-friendly interface that is easy on the eyes, especially in low-light environments. By following this guide, you can easily implement the Material Dark theme in your Android application. This example demonstrated how to set up the theme, create a corresponding layout, and initialize the views in your activity. Happy coding!","protected":false},"excerpt":{"rendered":"<p>The Material Dark theme offers a visually appealing dark interface that enhances readability in low-light environments and provides a sleek, modern look for your Android applications. In this blog, we&#8217;ll walk through the steps to implement a simple Android UI&#8230;<\/p>\n","protected":false},"author":1,"featured_media":561,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[39,40],"class_list":["post-611","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-app-debugging","tag-android-development","tag-android-themes"],"_links":{"self":[{"href":"https:\/\/robotqa.com\/blog\/wp-json\/wp\/v2\/posts\/611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/robotqa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robotqa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robotqa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robotqa.com\/blog\/wp-json\/wp\/v2\/comments?post=611"}],"version-history":[{"count":0,"href":"https:\/\/robotqa.com\/blog\/wp-json\/wp\/v2\/posts\/611\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/robotqa.com\/blog\/wp-json\/wp\/v2\/media\/561"}],"wp:attachment":[{"href":"https:\/\/robotqa.com\/blog\/wp-json\/wp\/v2\/media?parent=611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robotqa.com\/blog\/wp-json\/wp\/v2\/categories?post=611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robotqa.com\/blog\/wp-json\/wp\/v2\/tags?post=611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}