I don't understand why in my activity I don't see the refresh button on the action bar.
Here's my code
public class CommentsActivity extends Activity {
String linkcommenti, cleanedLink;
WebView webview;
private ProgressBar loadingProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
ActionBar actionbar = getActionBar();
actionbar.setBackgroundDrawable(getResources().getDrawable(
R.drawable.bar_back));
actionbar.setDisplayHomeAsUpEnabled(true);
linkcommenti = (String) getIntent().getExtras().get("linkcommenti");
webview = (WebView) findViewById(R.id.webview);
loadingProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
loadingProgressBar.setVisibility(View.VISIBLE);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view,
final String url) {
webview.setVisibility(View.INVISIBLE);
loadingProgressBar.setVisibility(View.VISIBLE);
new Thread(new Runnable() {
public void run() {
final CleanHtml toClean = new CleanHtml(url);
runOnUiThread(new Runnable() {
public void run() {
webview.loadDataWithBaseURL(null,
toClean.url_cleaned, "text/html",
"charset=UTF-8", null);
webview.setVisibility(View.VISIBLE);
loadingProgressBar.setVisibility(View.GONE);
}
});
}
}).start();
return true;
}
});
new Thread(new Runnable() {
public void run() {
final CleanHtml toClean = new CleanHtml(linkcommenti);
runOnUiThread(new Runnable() {
public void run() {
webview.loadDataWithBaseURL(null, toClean.url_cleaned,
"text/html", "charset=UTF-8", null);
loadingProgressBar.setVisibility(View.GONE);
}
});
}
}).start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.comments, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_refresh:
reload();
return true;
}
return super.onOptionsItemSelected(item);
}
And here's the resource R.menu.comments
<menu 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"
tools:context="com.example.intertiamo.CommentsActivity" >
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/action_refresh"
app:showAsAction="ifRoom" />
</menu>
I can't really understand, I call the resource from another activity and I see the refresh button there...the code doesn't return any error.
And the button "android.R.id.home" IS displayed!
Thanks
Try:
app:showAsAction="always"
If that doesn't work (ie as indicated by the comments, if you don't extend ActionBarActivity and extend Activity instead), try this as well:
android:showAsAction="always"
I'm assuming your ActionButton is displaying in the overflow currently.
Related
I've been trying to add a functionality to my android application such that when I click a button, menu listing should be visible:
Here is my code:
menu.xml:
<menu 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"
tools:context="com.example.MainActivity" >
<item android:id="#+id/action_onthego_sentence"
android:title="settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
From the main activity, upon clicking a button, I do:
button.setOnClickListener( new View.OnClickListener()
{
#Override
public void onClick( View view )
{
runOnUiThread( new Runnable()
{
#Override
public void run()
{
openOptionsMenu();
}
} );
}
} );
What I need is:
As shown in the image, I'd like to see the menu is opened. Any suggestions please?
If you are using customized toolbar in your app, then the following way will be useful,
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
toolbar.showOverflowMenu();
}
}, 500);
Use:
MainActivity.this.openOptionsMenu();
Then, check your toolbar if is this okay.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acercade_activity);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// MenuInflater inflater = getMenuInflater();
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
//case R.id.action_settings:
// return true;
case R.id.perfil:
drawerLayout.openDrawer(Gravity.LEFT);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Use this in your menu layout:
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="Setting"
app:showAsAction="ifRoom" />
Hello Dear if you are using toolbar following code
toolbar.showOverflowMenu();
And other wise you can directly call
MainActivity.this.openOptionsMenu();
Try the below solution It will be display click on the button menu listing should be visible:
res/menu/main.xml
<?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/action_changeLang"
android:orderInCategory="100"
android:title="Change Lang" />
<item
android:id="#+id/action_color"
android:orderInCategory="100"
android:title="Select color" />
<item
android:id="#+id/action_applist"
android:orderInCategory="100"
android:title="App List" />
</menu>
MainActivity.java
#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) {
int id = item.getItemId();
switch (id) {
case R.id.action_changeLang:
// Add your code
break;
case R.id.action_color:
// Add your code
break;
case R.id.action_applist:
// Add your code
break;
}
return super.onOptionsItemSelected(item);
}
Try to use above solution. It will work for me
Use Activity.openOptionsMenu() to open menu programmatically, but you may need to post delay to call it. like below
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
openOptionsMenu();
}
}, 1000);
If you need to open sub menu automatically. you can call menu.performIdentifierAction after openOptionsMenu, like below
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
openOptionsMenu();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
mainMenu.performIdentifierAction(R.id.submenu, 0);
}
}, 500);
}
}, 1000);
Android Dev efficiency tools: https://play.google.com/store/apps/details?id=cn.trinea.android.developertools
Android Open Source Projects: https://p.codekk.com/
I am creating a menu to show on top of the screen while longpress in webview.
But I am getting a message "unfortunately, app has stopped", then app closed.
I created mymenu in xml(fn.xml) and placed it in \res\menu\fn.xml
fn.xml file:
<menu 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"
tools:context="com.example.fn.MainActivity" >
<item
android:id="#+id/mark"
android:showAsAction="always"
android:onClick="onContextualMenuItemClicked"
android:title="Mark">
</item>
</menu>
My MainActivity class code is below
public class MainActivity extends ActionBarActivity {
WebView webview;
private ActionMode mActionMode = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.webView1);
webview.loadUrl("http://www.myurl.com");
}
#Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
Toast.makeText(getApplicationContext(), "stateVal",
Toast.LENGTH_SHORT).show();
mActionMode = mode;
Menu menu = mode.getMenu();
mode.getMenuInflater().inflate(R.menu.fn, menu);
}
super.onActionModeStarted(mode);
}
public void onContextualMenuItemClicked(MenuItem item) {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
my Log screen shot
May I know what is missing here?
I have created my dialog when I press one of the menu buttons:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_first__window, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id)
{
case R.id.action_settings:
About_us aboutus = new About_us(this);
aboutus.show();
return true;
case R.id.close: System.exit(0); return true;
}
return super.onOptionsItemSelected(item);
}
My Dialog layout
<Button
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Close"
android:onClick="ext_btn"/>
My Dialog Class
public class mydialog extends Dialog
{
public About_us(Context context) {
super(context);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.about_us);
}
public void ext_btn(View view) {
About_us.this.dismiss();
}
}
I Have tried a lot of codes to make the close button dismiss the dialog.
My app always crashes.
Where is the mistake ?
Try this one.
Button btn_ext = (Button) findViewById(R.id.btn_ext);
btn_ext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
I have a searchview and a button i my actionbar,but the button gets displayed only in the landscape mode and however I try it wont get displayed in the portrait mode.
Landscape Mode
Portrait Mode
this is my menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_item_search"
android:title="Search"
android:showAsAction="ifRoom"
android:icon="#android:drawable/ic_menu_search"
android:actionViewClass="android.widget.SearchView" />
<item
android:id="#+id/submit"
android:title="submit"
android:showAsAction="always" />
</menu>
This is my main activity
public class Myclass extends Activity implements OnQueryTextListener,OnItemClickListener {
GroupAdapter grpAdapter;
public static ArrayList<GroupsModel> arrayOfList;
public static ListView listView;
public static String base_url = "myurl";
private SeekBar volumeControl = null;
private TextView year;
private int progressChanged = 0;
private Menu menu_data;
MenuItem submitBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
ActionBar actionBar = getActionBar();
//actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
volumeControl = (SeekBar) findViewById(R.id.volume_bar);
year = (TextView)findViewById(R.id.year_level);
volumeControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
progressChanged = progress;
year.setText(Integer.toString(progressChanged+7));
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
arrayOfList = new ArrayList<GroupsModel>();
listView = (ListView) findViewById(R.id.group_listview);
listView.setOnItemClickListener(this);
listView.setTextFilterEnabled(true);
new ProgressTask(two.this).execute();
}
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
#SuppressWarnings("unused")
private two activity;
public ProgressTask(two two) {
this.activity = two;
context = two;
dialog = new ProgressDialog(context);
}
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
#Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
protected Boolean doInBackground(final String... args) {
//arrayOfList = new ArrayList<GroupsModel>();
List<NameValuePair> params = new ArrayList<NameValuePair>();
//params.add(new BasicNameValuePair("",""));
JSONParser jp = new JSONParser();
JSONArray groups_obj = jp.makeHttpRequest(base_url + "groups/all", "GET", params);
for (int i = 0; i < groups_obj.length(); i++) {
GroupsModel group = new GroupsModel();
try {
JSONObject grp = groups_obj.getJSONObject(i);
group.setGroupId(grp.getInt("id"));
group.setGroupname(grp.getString("name"));
arrayOfList.add(group);
}
catch (JSONException e) {
e.printStackTrace();
}
}
two.this.runOnUiThread(new Runnable() {
#Override
public void run() {
grpAdapter = new GroupAdapter(two.this, R.layout.two_row,arrayOfList);
listView.setAdapter(grpAdapter);
}
});
return null;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu_data = menu;
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
SearchView searchView = (SearchView) menu.findItem(R.id.menu_item_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(false);
searchView.setOnQueryTextListener(this);
searchView.setQueryHint("My text");
searchView.setIconifiedByDefault(false);
submitBtn = (MenuItem)menu_data.findItem(R.id.submit);
submitBtn.setIcon(R.drawable.button22);
submitBtn.setEnabled(false);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.submit:
if(submitBtn.isVisible())
Toast.makeText(getApplicationContext(), Integer.toString(progressChanged+7),Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtered
if (TextUtils.isEmpty(newText))
{
listView.clearTextFilter();
}
grpAdapter.getFilter().filter(newText.toString());
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// TODO Auto-generated method stub
submitBtn.setIcon(R.drawable.button111);
submitBtn.setEnabled(true);
for (GroupsModel s : arrayOfList)
s.setChecked(false);
final boolean isChecked = GroupAdapter.items.get(position).getChecked();
if(isChecked==false){
GroupAdapter.items.get(position).setChecked(!isChecked);
grpAdapter.notifyDataSetChanged();
}
else{
GroupAdapter.items.get(position).setChecked(isChecked);
grpAdapter.notifyDataSetChanged();
}
} }
Please help me.
If it doesn't concern you that the SearchView is collapsed at the start, try configuring the SearchView like this:
android:showAsAction="collapseActionView|ifRoom"
This should make the actionbar show both, the SearchView and your Button. Plus the SearchView will only take up the remaining space to the left, when it gets expanded.
According to developer.android.com
If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace. Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.) For example:
res/menu/main_activity_actions.xml >>>>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
A SearchView will default to taking up a majority of, if not all of the action bar in portrait mode. Try decreasing the size of the SearchView in menu.xml and see if the button is displayed.
PeterH posted the following code:
//initiate the button
button.performClick();
button.setPressed(true);
button.invalidate();
// delay completion till animation completes
button.postDelayed(new Runnable() { //delay button
public void run() {
button.setPressed(false);
button.invalidate();
//any other associated action
}
}, 800); // .8secs delay time
Can the same type of operation be performed for Action Bar items?
Well, you can save MenuItem as a field and then call onOptionsItemSelected(savedMenuItem). But as ActionBar items are MenuItems and not Buttons (of course, if your action bar isn't customized with view like http://www.vogella.com/articles/AndroidActionBar/article.html#actionbar_dynamic). But if your ActionBar is customized with view and that view has a Button, that Button's behaviour can be customized as considered in your code snippet.
Example:
public class MainActivity extends Activity {
MenuItem item;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setSubtitle("mytest");
actionBar.setTitle("TESTESTEST");
TextView tView = (TextView) findViewById(R.id.textView1);
tView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(item);
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
this.item = item;
Toast.makeText(this, "settings", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
}