Trouble making menuitem visible after asynctask - android

I have a Refresh button that I would like to make visible depending on the situation.
When the Refresh button is clicked I can make it invisible without a problem, however, I cannot make it visible again once the AsyncTask process finishes. I have trouble passing the MenuItem value back to the AsyncTask.
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getSupportMenuInflater().inflate(R.menu.refresh_action_provider, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_refresh:
item.setVisible(false); //hide refresh button
setSupportProgressBarIndeterminateVisibility(true);
Toast.makeText(getApplicationContext(), "REFRESH CLiCKED", Toast.LENGTH_SHORT).show();
new DownloadNewsTask().execute();
return true;
}
return false;
}

You could pass your item in your task's constructor, store it and make it visible in onPostExecute method:
public class DownloadNewsTask extends AsyncTask<...> {
private final MenuItem item;
public DownloadNewsTask(MenuItem item) {
this.item = item;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
item.setVisible(true);
}
...
}
And then:
new DownloadNewsTask(item).execute();
You can also have the item be a member of your activity class and access it from your task if it is defined as an inner class of your activity:
public class TestActivity extends Activity {
protected MenuItem refreshItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getSupportMenuInflater().inflate(R.menu.refresh_action_provider, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_refresh:
this.refreshItem = item;
item.setVisible(false); // hide refresh button
setSupportProgressBarIndeterminateVisibility(true);
Toast.makeText(getApplicationContext(), "REFRESH CLiCKED", Toast.LENGTH_SHORT).show();
new DownloadNewsTask().execute();
return true;
}
return false;
}
public class DownloadNewsTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
// your stuff...
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
refreshItem.setVisible(true);
}
}
}

Related

Textview not resolved inside onOptionsItemSelected(MenuItem item) method

Here is MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView myTxt = findViewById(R.id.view);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.toast:
myTxt.setText("what a yummy toast!");
return true;
case R.id.nutella:
return true;
case R.id.milk:
default:
return super.onOptionsItemSelected(item);// ken jét this .onOption infinite loop
}
}
}
Here is the menu XML file
<?xml version="1.0" encoding="utf-8"?>
<item android:title="#string/toast"
android:id="#+id/toast"/>
<item android:title="#string/nutella"
android:id="#+id/nutella"/>
<item android:title="#string/milk"
android:id="#+id/milk"/>
I want to handle myTxt which is a textview whenever I choose an item from the menu option the problem is that myTxt is not recognized. How Should I handle Menus correctly and what are
Here is an image for more explanation
You need to declare your TextView myTxt as global outside onCreate() method
SAMPLE CODE
public class MainActivity extends AppCompatActivity {
TextView myTxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTxt = findViewById(R.id.view);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.toast:
myTxt.setText("what a yummy toast!");
return true;
case R.id.nutella:
return true;
case R.id.milk:
default:
return super.onOptionsItemSelected(item);// ken jét this .onOption infinite loop
}
}
}
myTxt is declared inside onCreate() so you can not access it from outside this function. Try declare it outside onCreate()
public class MainActivity extends AppCompatActivity {
private TextView myTxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTxt = findViewById(R.id.view);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.toast:
myTxt.setText("what a yummy toast!");
return true;
case R.id.nutella:
return true;
case R.id.milk:
default:
return super.onOptionsItemSelected(item);// ken jét this .onOption infinite loop
}
}
}

BaseActivity with common toolbar and menuItems activities can not catch onMenuItemClick

I've the base activity where all toolbar initializations and options menu are done, the activities extending the base can not fire onitemclick
In the base i have
public class BaseActivity extends AppCompatActivity {
private MenuItem refresh;
public Toolbar getToolbar() {
return toolbar;
}
public MenuItem getRefresh() {
return refresh;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
refresh = menu.findItem(R.id.action_refresh);
refresh.setActionView(R.layout.menu_item_view);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return false;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
switch (mState) {
case Const.STATE_IDLE:
refresh.setVisible(true);
break;
case STATE_WORKING:
refresh.setVisible(false);
break;
default:
refresh.setVisible(true);
break;
}
return super.onPrepareOptionsMenu(menu);
}
}
In one of the activites I handle it like
public class CommentsActivity extends BaseToolbarActivity
{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId() == R.id.action_refresh){
setState(Const.STATE_WORKING);
showMsg(contentRoot,"oops");
return true;
}
return super.onOptionsItemSelected(item);
}
}
but the click items won't fire
After a while I came to realize that since I was setting a custom layout for my items (useful for animations) an option menu with a custom view that is either set in the xml or dynamically using
item.setActionView(R.layout.menu_lay);
Just like my problem above the menu item can never be invoked by the normal onOptionsItemSelected listener so the way to make it work is to implement onClickListener on the item's custom View so in my case the way to make it invoke is by
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getRefresh.getActionView().setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//refresh some data
}
});
return true;
}

How to replace Android Toolbar icons programmatically?

I have a ListView and a Toolbar above of it in my Activity. I want to replace the Toolbar default icons (search and settings) with delete and edit icons when clicking an item of the ListView to perform some action.
So what I've understood from your question, you're looking for ActionMode which might serve your purpose.
So here's an implementation guideline.
Declare an ActionMode in your Activity and let your Activity to implement ActionMode.Callback.
public class YourActivity extends AppCompatActivity implements ActionMode.Callback {
// Declare ActionMode here
private ActionMode actionMode;
// Now implement the callback functions for ActionMode
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
#Override
public boolean onCreateActionMode(final ActionMode actionMode, Menu menu) {
MenuInflater inflater = actionMode.getMenuInflater();
// Inflate your menu here
inflater.inflate(R.menu.list_item_click_menu, menu);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_delete:
// Do something
actionMode.finish();
return true;
case R.id.action_edit:
// Do something
actionMode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode actionMode) {
try {
this.actionMode = null;
// Do something. Reset the views maybe?
} catch (Exception e) {
e.printStackTrace();
}
}
}
Now to initiate your ActionMode you need to have this in your onClick function of the list item.
listItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (actionMode != null) {
return true;
}
// Show ActionMode
actionMode = startSupportActionMode(this);
actionMode.invalidate();
}
});
You may need to reset the ActionMode sometimes.
#Override
public void onResume() {
super.onResume();
if (actionMode != null) {
actionMode.finish();
actionMode = null;
}
}

saving the edittext value to another activity on click of save actionbutton in android

how to save the text given to edit text, after clicking the save action button in action bar that should save to another activity.
my first activity Add.java
public class Add extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
getActionBar().setDisplayShowHomeEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflate=getMenuInflater();
getMenuInflater().inflate(R.menu.activity_add_action, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:save();
return true;
case R.id.cancel:cancelnavi();
return true;
}
return super.onOptionsItemSelected(item);
}
private void save() {
EditText titlename;
titlename=(EditText)findViewById(R.id.edittitle);
String title=titlename.getText().toString();
if (title.equalsIgnoreCase("")){
Toast.makeText(Add.this, "Script title should not be empty", Toast.LENGTH_LONG).show();
} else {
Intent i;
i=new Intent(getApplicationContext(), Scripts.class);
i.putExtra("n", titlename.getText().toString());
startActivityForResult(i, 0);
titlename.setText("");
}
}
}
It should be saved to scripts.java
public class Scripts extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scripts);
getActionBar().setDisplayShowHomeEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflate=getMenuInflater();
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit:editscript();
break;
}
return true;
}
private void editscript() {
Intent editinIntent;
editinIntent=new Intent(Scripts.this, Edit.class);
startActivity(editinIntent);
}
}
Maybe this can help you.
public class Scripts extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scripts);
getActionBar().setDisplayShowHomeEnabled(false);
//Extract the data
String yourTitle = getIntent().getStringExtra("n");
}
}

Android webview fire custom CAB during longClick and Selection mode

I use this code to override webview Context Action Bar but during longclick the CAB appears but no text selection how can i only fire CAB when text selection mode?
public class MainActivity extends Activity {
ActionMode.Callback mCallback;
ActionMode mMode;
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview=webview = (WebView) findViewById(R.id.webView1);
webview.loadUrl("http://www.google.com");
webview.setOnLongClickListener(new OnLongClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressLint("NewApi")
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
if (mMode != null)
return false;
else
mMode = startActionMode(mCallback);
return true;
}
});
mCallback = new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
mMode = null;
}
#SuppressLint("NewApi")
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("1 selected");
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#SuppressLint("NewApi")
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(getBaseContext(), "Selected Action1 ",
Toast.LENGTH_LONG).show();
mode.finish();
break;
}
return false;
}
};
}
}
during long press my custom CAB appear without text selection
how to make it appear only in the case of text selection?
many thanks
don't do this,it is not necessary to overwrite longClick, you should make your custom webview and do some thing that below link display:
Look At This Answer

Categories

Resources