ভূমিকা
এই উদাহরণটি দেখাবে কিভাবে Toolbar-র getHeight() মেথড ব্যবহার করে অ্যাকশনবার/টুলবারের উচ্চতা পিক্সেলে পেতে হয়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project এ গিয়ে একটি নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট ফাইল (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:background="@color/colorAccent">
</android.support.v7.widget.Toolbar>
<Button
android:id="@+id/btnGetHeight"
android:text="Get Action Bar height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/textView"
android:textSize="16sp"
android:textStyle="bold"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="@id/btnGetHeight"/>
</RelativeLayout>
ধাপ ৩: মেইন এক্টিভিটি কোড (src/MainActivity.java)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
Button button;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.actionBarSize);
toolbar.setTitle("MyActionBar");
getSupportActionBar();
textView = findViewById(R.id.textView);
button = findViewById(R.id.btnGetHeight);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int height = toolbar.getHeight();
textView.setText("ActionBar Height: " + height + " pixels");
}
});
}
}
ধাপ ৪: মেনিফেস্ট ফাইল (androidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
অ্যাপ্লিকেশন চালান
অ্যাপটি চালান। "Get Action Bar height" বাটনে ক্লিক করলে টুলবারের উচ্চতা পিক্সেলে TextView-এ দেখাবে।