Step 1: Change Main Activity From Constraint Layout to Relative Layout
Step 2: Create a New Layout Resource file
Step 3: Write This code on toolbar_layout.xml
<?xml version="1.0" encoding="utf-8"?><androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>
N:B: You can do this by dragging and dropping the toolbar container
Step 4: Create a New Resource Directory - name menu
Step 5: Create a Menu Resource file - name toolbar_menu.xml
Step 6: Drag and Drop as many Menu Item you need
Step 7: Set toolbar in activity_main.xml - Write this code:
layout="@layout/toolbar_layout"
android:id="@+id/toolbar"/>
Step 8: Set toolbar in MainActivity.java - Write this code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return true;
}
@SuppressLint("NonConstantResourceId")
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.firstId:
Toast.makeText(this, "This is first id", Toast.LENGTH_SHORT).show();
break;
case R.id.secondId:
Toast.makeText(this, "This is second id", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
}
Step 9: Replace DarkActionBar to NoActionBar
একটি মন্তব্য পোস্ট করুন
0 মন্তব্যসমূহ