BCA / B.Tech 13 min read

Screen Orientation and the Action Bar in Hindi

Screen Orientation और Action Bar

1: Screen Orientation को Adapt और Manage करना

Screen Orientation क्या है?

Screen Orientation device की physical orientation को दर्शाता है। Android में दो मुख्य orientations हैं:

  • Portrait (पोर्ट्रेट): Device सीधा पकड़ा जाता है (vertical)।
  • Landscape (लैंडस्केप): Device sideways पकड़ा जाता है (horizontal)।

Orientation Change के साथ समस्या

जब user device को rotate करता है, तो Android default रूप से current activity को destroy करता है और उसे फिर से recreate करता है। ऐसा इसलिए होता है ताकि app alternative resources (जैसे landscape के लिए special layout) को load कर सके।

समस्या: इस प्रक्रिया में, activity की current state (जैसे `EditText` में लिखा text या counter की value) lost हो जाती है।

Orientation Changes को Manage कैसे करें

1. State को Save और Restore करना (Recommended)

Activity state को handle करने का यह सबसे अच्छा तरीका है।

  • onSaveInstanceState(): जब activity destroy होने वाली होती है, तो यह method call होता है। आप यहाँ temporary data को `Bundle` object में save कर सकते हैं।
  • onCreate() या onRestoreInstanceState(): जब activity recreate होती है, तो save किया गया `Bundle` आपको `onCreate()` method में मिलता है, जहाँ से आप data को restore कर सकते हैं।

Java में उदाहरण:


public class MainActivity extends AppCompatActivity {
    private int counter = 0;
    private TextView counterTextView;
    static final String STATE_COUNTER = "counterState";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        counterTextView = findViewById(R.id.counter_tv);

        // Check करें कि क्या कोई saved state है
        if (savedInstanceState != null) {
            counter = savedInstanceState.getInt(STATE_COUNTER);
        }
        counterTextView.setText(String.valueOf(counter));
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // Counter की value को save करें
        outState.putInt(STATE_COUNTER, counter);
        
        // हमेशा superclass method को call करें
        super.onSaveInstanceState(outState);
    }
    
    // मान लीजिए, button click पर counter बढ़ता है
    public void incrementCounter(View view) {
        counter++;
        counterTextView.setText(String.valueOf(counter));
    }
}

2. Orientation को Lock करना

यदि आप चाहते हैं कि आपकी app सिर्फ एक ही orientation में चले, तो आप इसे `AndroidManifest.xml` file में lock कर सकते हैं।


<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait"> 
    <!-- या "landscape" -->
</activity>

2: Action Bar का उपयोग करना

Action Bar (या Toolbar) क्या है?

Action Bar (जिसे modern apps में `Toolbar` या `AppBar` कहा जाता है) screen के top पर एक UI component है। यह आपकी app की branding (logo/title) और user के लिए important actions (जैसे search, settings, share) को display करता है।

Action Bar के मुख्य हिस्से:

  • App Icon: App का icon या logo।
  • Title: Activity का title।
  • Action Items: Clickable icons जो app में common actions perform करते हैं।
  • Overflow Menu: तीन डॉट्स वाला menu जो कम उपयोग होने वाले actions को दिखाता है।

Action Bar में Items कैसे Add करें

Action Bar के items को menu resource file (XML) में define किया जाता है।

Step 1: Menu XML File बनाएं

1. `res` folder पर right-click करें > New > Android Resource Directory. Directory name में `menu` चुनें।
2. `res/menu` folder पर right-click करें > New > Menu Resource File. नाम दें (जैसे `main_menu.xml`)।

`main_menu.xml` का उदाहरण:


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="Search"
        app:showAsAction="ifRoom" /> 
        <!-- ifRoom का मतलब है, जगह हो तो दिखाओ, नहीं तो overflow menu में डालो -->

    <item
        android:id="@+id/action_settings"
        android:title="Settings"
        app:showAsAction="never" />
        <!-- never का मतलब है, हमेशा overflow menu में दिखाओ -->

</menu>

Step 2: Activity में Menu को Inflate करें

अपनी activity की Java file में `onCreateOptionsMenu()` method को override करें।


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
}

Step 3: Menu Item Clicks को Handle करें

जब user किसी action item पर click करता है, तो `onOptionsItemSelected()` method call होता है।


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    int id = item.getItemId();

    if (id == R.id.action_search) {
        Toast.makeText(this, "Search clicked", Toast.LENGTH_SHORT).show();
        return true;
    } else if (id == R.id.action_settings) {
        Toast.makeText(this, "Settings clicked", Toast.LENGTH_SHORT).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}