I’m working in Android Studio java project.Just wanted to create a menu item on the toolbar.But after running the project , I don’t see the TOAST view is working.Please help.enter image description here
This is the activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
app:title="@string/app_name"
app:menu="@menu/main_menu"
android:theme="@style/Theme.AppCompat.DayNight"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/firstRowLinearLayout"
>
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:lines="2"
android:layout_margin="5dp"
>
</Button>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:lines="2"
android:layout_margin="5dp"
>
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/secondRowLinearLayout"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:lines="2"
android:layout_margin="5dp"
>
</Button>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:lines="2"
android:layout_margin="5dp"
>
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/thirdRowLinearLayout"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:lines="2"
android:layout_margin="5dp"
>
</Button>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:lines="2"
android:layout_margin="5dp"
>
</Button>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/question_text"
android:id="@+id/txtQuestionNumber"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="@dimen/margin_value"
android:textColor="#fff"
>
</TextView>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:id="@+id/imgAnimal"
android:layout_marginBottom="@dimen/margin_value"
android:layout_marginLeft="@dimen/margin_value_left"
android:layout_marginRight="@dimen/margin_value_right"
android:scaleType="fitCenter"
>
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/txtAnswer"
android:id="@+id/txtAnswer"
android:layout_gravity="center_horizontal|bottom"
android:gravity="center_horizontal"
android:layout_marginBottom="@dimen/margin_value"
android:textStyle="italic"
android:textSize="@dimen/answer_font_size"
android:textColor="#fff"
>
</TextView>
</LinearLayout>
**
This is the menu xml that I have created and named as ‘main_menu.xml’ and created an item inside this called ‘new_setings’: ** **
<?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/new_settings"
android:icon="@drawable/baseline_build_24"
android:title="settings"
app:showAsAction="ifRoom"
/>
</menu>
The java code in mainactivity:
package com.example.dontknow;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// MenuInflater inflater = getMenuInflater();
// inflater.inflate(R.menu.main_menu, menu);
// return true;
// }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id==R.id.new_settings){
Toast.makeText(this, "clicked dfsssssssssssssssss ewrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "Nothing has happend", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toolbar toolbar = findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
// ActionBar actionBar = getSupportActionBar();
if(getSupportActionBar()!=null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
}
2
Answers
Problem is solved. This code was actually missing in activity_main.xml inside Toolbar :
Needed to create a custom item style in the main_menu.xml for changing menu icons I have inserted these codes for the icons otherwise only changing the drawables icons inside 'item' won't help.
and added the new item of this MyTheme inside the parent application theme
You need to call setContentView before setting up the toolbar.