onOptionsItemSelected is not called for activity which intent another activity class - java

I want to use a library activity with my test app onOptionsItemSeleceted method() but I figure out that if I use intent. Android use library activity's onOptionsItemSeleceted method() not my test activity method(). I did one test app and my MainActivity like this:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent (this, Test_1.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_setting:
Toast.makeText(getApplicationContext(),
"Button is clicked", Toast.LENGTH_LONG).show();
return true;
case R.id.action_setting1:
Toast.makeText(getApplicationContext(),
"Button is clicked", Toast.LENGTH_LONG).show();
return true;
case R.id.action_settings:
Toast.makeText(getApplicationContext(),
"Button is clicked", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And my test activity class is like:
public class Test_1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_1);
TextView txt = (TextView)findViewById(R.id.textView);
txt.setText("This is new page");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.test_1, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return false;
}
return super.onOptionsItemSelected(item);
}
I want to use onOptionsItemSelected() method in Main Activity class but I couldn't see my Toast messages on the screen.
I really appreciated if you can help me. Thanks :)

Hmm, Maybe it go through ?
default:
return super.onOptionsItemSelected(item);
I'm not sure what is in R.menu.main
But make sure there are R.id.action_setting, R.id.action_setting1, R.id.action_settings.

SUPER:
It is used inside a sub-class method definition to call a method defined in the super class.
In your case Activity is a super class of Test_1 onOptionsItemSelected of activity will be called .. not the MainActivity onOptionsItemSelected
Just add this to see your toast messages. In class Test_1 onOptionsItemSelected :
switch (item.getItemId()) {
case R.id.action_setting:
Toast.makeText(getApplicationContext(),
"Button is clicked", Toast.LENGTH_LONG).show();
return true;
case R.id.action_setting1:
Toast.makeText(getApplicationContext(),
"Button is clicked", Toast.LENGTH_LONG).show();
return true;
case R.id.action_settings:
Toast.makeText(getApplicationContext(),
"Button is clicked", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}

Related

How to go previous activity searchview back button?

I have a very simple question. I have implemented searchview in my menu. So the activity starts with expanded searchview. And now when I press back button, the searchview first collapse and goes back to previous activity.
Here's my code
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchItem.expandActionView();
SearchView mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
mSearchView.setQueryHint(getString(R.string.search_hint));
mSearchView.setIconifiedByDefault(false);
mSearchView.setFocusable(true);
mSearchView.setIconified(false);
mSearchView.requestFocus();
mSearchView.setGravity(Gravity.END);
mSearchView.setTextDirection(View.TEXT_DIRECTION_RTL);
//mSearchView.setLayoutParams(new Toolbar.LayoutParams(Gravity.END));
//String searchQuery = getIntent().getStringExtra("SEARCH");
//SearchActivity.this.myAdapter.filter(searchQuery);
TextView searchText = mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
//searchText.setText(searchQuery);
Typeface customFont = Typeface.createFromAsset(this.getAssets(), "fonts/font.ttf");
searchText.setTypeface(customFont);
try {
ThaanaUtils.thaanafyTextViewSubclass(searchText, "fonts/faruma.ttf");
}catch (IOException e){
e.printStackTrace();
}
searchText.addTextChangedListener(new ThaanaTextWatcher());
View xIcon = ((View) mSearchView.findViewById(R.id.search_plate));
xIcon.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
SearchActivity.this.myAdapter.filter(newText);
return true;
}
});
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
onBackPressed();
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_favorites) {
//Intent newActivity = new Intent(SearchActivity.this,FavoritesActivity.class);
//startActivity(newActivity);
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed()
{
finish();
}
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
But I don't want to show searchview collapsing when pressed back button. I want to go back to previous activity without collapsing searchview.
How can I achieve that?
Any help is appreciated.
Override the onBackPressed() method in your activity and then put your activity redirection code inside the method. This will solve your problem. Happy coding!
#Override
public void onBackPressed() {
// enter your activity redirection code here.
}
If you didn't call finish() on your previous activity, then just call finish() inside the onBackPressed method. This will destroy the current activity(search activity) and will load previous one. Else you need to go through the usual way by declaring an intent.

Why does my BottomNavigationView is not showing up?

I'm trying to add a BottomNavigationView in my project but don't show up when i run the project.
I'd like to put this on several activities, aswell as Toolbar so i created a class that initialize both of them and extends AppCompatActivity so that my activies using the Toolbar or the BottomNavigationView just have to extends this activity and call the method that initialize it (i don't know if this is the right method to use, if no please tell me). So this work with my Toolbar, but my BottomNavigationView isn't showing up.
This is my NavigationActivity i talked about :
public abstract class NavigationActivity extends AppCompatActivity {
private Toolbar mToolbar;
private BottomNavigationView mNavigationView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void initNavigation(int navigationId) {
mNavigationView = (BottomNavigationView) findViewById(navigationId);
mNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navigation_entrainements:
startActivity(new Intent(getBaseContext(),MenuEntrainementsActivity.class));
break;
case R.id.navigation_nutrition:
startActivity(new Intent(getBaseContext(),NutritionActivity.class));
break;
case R.id.navigation_statistiques:
startActivity(new Intent(getBaseContext(),StatistiquesActivity.class));
break;
}
return true;
}
});
}
public void initToolbar(int toolbarId) {
mToolbar = (Toolbar) findViewById(toolbarId);
mToolbar.setNavigationIcon(R.drawable.baseline_arrow_back_black_18dp);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mToolbar.setNavigationOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.btnHome :
startActivity(new Intent(this,MainActivity.class));
return true;
case R.id.btnProfil :
startActivity(new Intent(this,ProfilActivity.class));
return true;
default :
return super.onOptionsItemSelected(item);
}
}
}
and an exemple of how i'm using it in others activities :
initNavigation(R.id.navigation);
initToolbar(R.id.toolbar);
this is how i implement the BottomNavigationView in my XML files :
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation" />
if you need anything else just ask,
thanks for your help :)
(sorry if i made mistakes i'm not great in english)
I Use this from outside of onCreate mothod.
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()) {
case R.id.btnHome :
startActivity(new Intent(this,MainActivity.class));
return true;
case R.id.btnProfil :
startActivity(new Intent(this,ProfilActivity.class));
return true;
default :
startActivity(new Intent(this,MainActivity.class));
return true;
}
}
};

How to add App Store link to menu item?

I can give link via Button.But I want to add a App store link to "rateus" in menu item.(please see attached image)here is the button code in MainActivity.java.This is not working for menu item.please help me.
//rateus button
android.widget.Button ratebutton = (android.widget.Button) findViewById(R.id.id_rateus);
ratebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
startActivity(new Intent(Intent.ACTION_VIEW,
android.net.Uri.parse("market://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
}catch (android.content.ActivityNotFoundException e){
startActivity(new Intent(Intent.ACTION_VIEW,
android.net.Uri.parse("https://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
}
}
});
//end rateus button code
here is my menu item image...
rate us ite menu
here is the code for rate us item
<item
android:id="#+id/id_rateus"
android:orderInCategory="100"
android:title="#string/action_rateus"
android:textAllCaps="false"
app:showAsAction="ifRoom" />
You need to override the menu functions, something like the code below.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds appModels to the action bar if it is present.
this.menu = menu;
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.share:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBodyText = "Check it out. Your message goes here";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBodyText);
startActivity(Intent.createChooser(sharingIntent, "Shearing Option"));
return true;
case R.id.id_rateus:
try {
startActivity(new Intent(Intent.ACTION_VIEW,
android.net.Uri.parse("market://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
}catch (android.content.ActivityNotFoundException e){
startActivity(new Intent(Intent.ACTION_VIEW,
android.net.Uri.parse("https://play.google.com/store/apps/details?id=com.slsindupotha&hl=en")));
}
return true;
}
return super.onOptionsItemSelected(item);
}
This is how you can handle a menu item click
Inflate the menu file
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_file, menu);
return true;
}
Then handle onCLick here
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.id_rateus:
//handle onclick event for intent to playstore
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Android onOptionsItemSelected method of Fragment doesn't get called

there are many questions about the topic but i can't figure out my problem. I have a menu declared in my MainActivity(ActionBarActivity). Now i want to work with MenuItem in onOptionsItemSelected of a Fragment class. Here is my MainActivity methods
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.add_note:
createNewNote();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void createNewNote() {
Intent addIntent = new Intent(MainActivity.this, AddNote.class);
startActivity(addIntent);
}
And Fragment methods
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_note:
Toast.makeText(getActivity(), "Entered into fragment", Toast.LENGTH_LONG).show();
createNewNote();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
private void createNewNote() {
Intent addIntent = new Intent(getActivity(), AddNote.class);
startActivity(addIntent);
}
Here in MainActivity onOptionsItemSelected get called even in Fragment but doesn't in Fragment as i don't see Toast in Fragment. I think something is missed in my code. Thanks in advance.
inside onOptionsItemSelected() in activity inside your switch after calling createNewNote() instead of returning true return super.onOptionsItemSelected(item)
you must call setHasOptionsMenu() inside onCreate of fragment for menu related method to work.
Your onCreateOptionsMenu() method does not inflate the menu file:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}

Where to declare onOptionsItemSelected for global use?

I'm very new to Android development.
In my MainActivity.java file, I've declared an onOptionsItemSelected(MenuItem menu) method that allows the user to jump between the current MainActivity.java page and another page I created called Settings.java.
When the user goes to the Settings.java page, and they click on the Home option from the menu, nothing happens. I know this is because the onOptionsItemSelected(MenuItem menu) is only defined in the MainActivity.java class and not in the Settings.java class.
I'm overcoming this right now by copying the onOptionsItemSelected(MenuItem menu) from MainActivity.java into Settings.java. But this is very redundant.
Where should I be declaring methods that could be reused in different classes?
MainActivity.java
public class MainActivity extends Activity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
showMsg("Settings Clicked");
this.startActivity(new Intent(this, Settings.class));
return true;
case R.id.action_home:
showMsg("Home clicked");
this.startActivity(new Intent(this, MainActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void showMsg(String msg) {
Toast toast = Toast.makeText(this.getBaseContext(), msg, Toast.LENGTH_LONG);
toast.show();
}
}
Settings.java
public class Settings extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
this.startActivity(new Intent(this, Settings.class));
return true;
case R.id.action_home:
this.startActivity(new Intent(this, MainActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
If you create an Activity called BaseActivity that has your common onOptionsItemSelected() code then extend this class to create MainActivity and Settings both of these classes will use the super class' (i.e. BaseActivity's) onOptionsItemSelected().
Another approach is to switch to Fragments, since Fragments use the host Activity's onOptionsItemSelected() as well as their own. Both of these tactics allow you to use "centralized" code and not have to maintain multiple "cut and paste" copies.

Categories

Resources