BCA / B.Tech 10 min read

User Interface (UI) के Fundamentals in Hindi

User Interface (UI) के Fundamentals

1: एक Screen के Components को समझना

Android application का User Interface (UI) उन सभी visual elements से बनता है जिन्हें user screen पर देखता है और interact करता है। Android UI के दो fundamental building blocks हैं:

1. View

  • क्या है: एक `View` screen पर एक rectangular area होता है जो user input का जवाब दे सकता है। इसे "widget" भी कहा जाता है। यह UI का सबसे basic component है।
  • उदाहरण:
    • `TextView`: Text दिखाने के लिए।
    • `Button`: Clickable button बनाने के लिए।
    • `ImageView`: Image दिखाने के लिए।
    • `EditText`: User से text input लेने के लिए।
    • `ProgressBar`: Progress दिखाने के लिए।

2. ViewGroup

  • क्या है: एक `ViewGroup` एक special type का View है जो दूसरे Views (और ViewGroups) के लिए एक container (कंटेनर) के रूप में काम करता है। इसका मुख्य काम child views को organize करना और उनकी position तय करना है। इन्हें "layouts" भी कहा जाता है।
  • उदाहरण:
    • `LinearLayout`: अपने child views को एक single direction में arrange करता है (horizontally या vertically)।
    • `RelativeLayout`: Child views को एक दूसरे के relative या parent के relative position करता है।
    • `ConstraintLayout`: एक flexible layout जो आपको complex UIs बनाने की अनुमति देता है। यह modern Android development में recommended है।
    • `FrameLayout`: Child views को एक stack में रखता है (एक के ऊपर एक)।

2: Programmatically User Interface बनाना

Android में UI बनाने के दो तरीके हैं:

  1. Declarative (XML Layouts): यह सबसे आम और recommended तरीका है, जहाँ आप XML files में UI design करते हैं।
  2. Programmatic (Java/Kotlin Code): आप Java code का उपयोग करके runtime पर UI elements बना और manage कर सकते हैं। यह dynamic UI बनाने के लिए उपयोगी है।

Java में Programmatically UI बनाने का उदाहरण:


// MainActivity.java के onCreate() method के अंदर

// 1. एक नया ViewGroup (LinearLayout) बनाएं
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL); // Orientation को vertical set करें

// 2. एक नया View (TextView) बनाएं
TextView myTextView = new TextView(this);
myTextView.setText("Hello from Java Code!");
myTextView.setTextSize(24);

// 3. एक और View (Button) बनाएं
Button myButton = new Button(this);
myButton.setText("Click Me");

// 4. Views को ViewGroup में add करें
mainLayout.addView(myTextView);
mainLayout.addView(myButton);

// 5. इस layout को activity का content view set करें
setContentView(mainLayout);

3: UI Notifications (Events) के लिए Listen करना

UI को interactive बनाने के लिए, हमें user actions (जैसे clicks, swipes) का जवाब देना होता है। यह event listeners का उपयोग करके किया जाता है।

Event Listener क्या है?

एक event listener एक interface है जो एक specific event होने पर call होने वाले callback method को define करता है।

सबसे आम Event: Button Click

Button click को handle करने के लिए, हम `View.OnClickListener` interface का उपयोग करते हैं।

Java में OnClickListener का उदाहरण:

यह उदाहरण मानता है कि आपके पास XML layout में एक Button (id: `my_button`) और एक TextView (id: `my_text_view`) है।


// MainActivity.java के onCreate() method के अंदर

// 1. XML layout से views को find करें
Button button = findViewById(R.id.my_button);
TextView textView = findViewById(R.id.my_text_view);

// 2. Button के लिए OnClickListener set करें
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // यह code तब चलेगा जब button पर click होगा
        textView.setText("Button was clicked!");
    }
});