User Interface

 User Interface


User Interface (UI) is one of the most crucial aspects of Android development. The Android SDK provides several UI components such as TextView, EditText, Button, ListView, and RecyclerView, which can be used to create an attractive and interactive user interface.


Here's an example of how to create a simple UI using XML layout:


php


<LinearLayout

   xmlns:android="http://schemas.android.com/apk/res/android"

   android:orientation="vertical"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   android:gravity="center">


   <TextView

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="Hello World!"

       android:textSize="24sp"

       android:textColor="#000000"/>


   <Button

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="Click me!"

       android:textSize="20sp"

       android:onClick="buttonClicked"/>


</LinearLayout>

In the above example, we have created a LinearLayout with vertical orientation and added a TextView and a Button to it. The TextView displays the text "Hello World!" and the Button displays the text "Click me!". We have also set the onClick attribute of the Button to "buttonClicked".


Here's an example of how to handle the button click event in the Activity class:



public class MainActivity extends AppCompatActivity {


   @Override

   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

   }


   public void buttonClicked(View view) {

       Toast.makeText(this, "Button clicked!", Toast.LENGTH_SHORT).show();

   }

}

In the above example, we have defined a method called "buttonClicked" with a parameter of type View. This method is called when the button is clicked, as we have set the onClick attribute of the button to "buttonClicked". The method displays a short toast message with the text "Button clicked!".

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...