How I can attach icon of progress bar to action bar that display loading process of some Internet data?
For example:
And icon while load data
First of all you have to create a custom menu to use inside your activity.
Secondly, you have to request the feature of progress bar inside your "onCreate".
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
// rest of your code
[....]
}//end of onCreate
Then your my_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:icon="# drawable/ic_menu_refresh "
android:orderInCategory="100"
android:showAsAction="always"
android:title="Refresh"
android:visible="true"/>
</menu>
Back to your activity, you have to inflate this menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_refresh:
// refresh icon clicked
//show the indeterminate progress bar
setSupportProgressBarIndeterminateVisibility(true);
// Rest of your code to do re refresh here
[...]
default:
return super.onOptionsItemSelected(item);
}
}
PS: All the above code is working using the ActionBar Compat Library.
EDIT:
Ok, to hide the action bar icon do this:
-Create a menuitem outside your functions:
MenuItem refreshIcon;
Edit your onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
//Initializing menu item
refreshIcon = menu.findItem(R.id.action_refresh);
return super.onCreateOptionsMenu(menu);
}
Then when you press the button change the visibility:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_refresh:
// refresh icon clicked
//show the indeterminate progress bar
setSupportProgressBarIndeterminateVisibility(true);
//hiding action icon
refreshIcon.setVisible(false);
// Rest of your code to do re refresh here
[...]
default:
return super.onOptionsItemSelected(item);
}
}
Finally, when your data is finished updating set the visibility to "true" and hide progressbar:
[...] //progress inside your AsyncTask or wherever you have your update
//show Refresh Icon again
refreshIcon.setVisible(true);
//hide the indeterminate progress bar
setSupportProgressBarIndeterminateVisibility(true);
Related
By default android have an option to enable BACK ARROW on the left of the toolbar using
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
same like i need TICK MARK on the right of the toolbar, is there any method to enable TICK MARK in android by default.
First download the "done" icon as png from the materials icon library
Copy the icons to your projects drawable folder.
Then create a file in res/menu called menu_example.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_done_white_24dp"
android:title="#string/action_done"
app:showAsAction="always" />
</menu>
Then add the following code to your Activity.
#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_example, menu);
return true;
}
To have a onClick listener for the tick you can implement this:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_example, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
toast.show();
return true;
}
I have created an action bar Search in the action bar menu.
Well, I am trying to make it go to its normal state if I click a go button or a click anywhere on the screen. Here is my code :
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
final EditText editText = (EditText) menu.findItem(
R.id.menuSearch).getActionView();
editText.addTextChangedListener(textWatcher);
menuItem = menu.findItem(R.id.menuSearch);
menuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
menuItem.collapseActionView();
// Do something when collapsed
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
//editText.clearFocus();
return true; // Return true to expand action view
}
});
return true;
}
It works well. But, It doesn't go to its normal state. I used this function
menuItem.collapseActionView();
But, no use. Thanks in advance
try this code
menuItem.collapseActionView();
inside
public boolean onOptionsItemSelected(MenuItem item) {
The framework calls onMenuItemCollapsed() to notify your code that the search view has been collapsed. A call to "menuItem.collapseActionView()" does no good in that method body. Your calls to collapseActionView() should be in the code where you want to force the view to be collapsed, such as a keystroke handler for the Go button.
Trying to add actionar icons to actionbar and i'm using support library here is link i'm following developerspage
and here is my code to implement it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == R.id.toggle) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
return true;
}
and menu.xml is
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/toggle"
android:icon="#drawable/menu"
android:orderInCategory="100"
android:title="menutoggle"/>
In your xml, you can include this in each item you want to be clickable:
android:onClick="aRandomMethod"
Then in your activity, you must implement a method that supports the click, in this case, a method called aRandomMethod, like this:
public void aRandomMethod(MenuItem item){
/// stuff to do when you click in the button
}
If you want your icon to be always displayed in the action bar, add:
android:showAsAction="always"
The onOptionsItemSelected method that you have in your code is when you have the settings icon (three small dots) and then a submenu in the icon; is to handle the submenu.
I want to call an activity after clicking on share icon in action bar. For this I
create a menu named 'flip' which contains an item named 'menu_share' for Android
2.2+. Flip menu is inflated in 1st activity named 'ShareActivity'. After this I want to call other activity after clicking on shareicon in action bar but icon not responding.
Code for menu xml file.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto=" >
<item android:id="#+id/menu_share"
android:title="share"
android:icon="#drawable/shar"
yourapp:showAsAction="ifRoom"
yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
Code for 1st activity.
public class ShareActivity extends ActionBarActivity {
SocialAuthAdapter adapter;
EditText edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
ActionBar ab=getSupportActionBar();
ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
ab.setDisplayHomeAsUpEnabled(true);
ab.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();
inflater.inflate(R.menu.flip, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
case R.id.menu_share:
startActivity(new Intent(this,ShActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
You have set an ActionProvider with yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider". This is likely intercepting the event, thus preventing your Activity from getting the call to onOptionsItemSelected().
Since you want to start your own Activity instead of using Android's default share behavior, simply remove this line.
i use action bar sherlock and i set custom layout in the menu:
<item
android:id="#+id/action_menu"
android:actionLayout="#layout/menu_item"
android:icon="#drawable/b_menu"
android:orderInCategory="1"
android:showAsAction="always"/>
<item
android:id="#+id/action_audio"
android:actionLayout="#layout/audio_item"
android:icon="#drawable/b_audio"
android:orderInCategory="2"
android:showAsAction="always"/>
<item
android:id="#+id/action_mySong_details"
android:actionLayout="#layout/my_song_properties"
android:icon="#drawable/b_mysong"
android:orderInCategory="2"
android:showAsAction="always"/>
to control those items i use:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Create an option menu from res/menu/items.xml */
getSherlock().getMenuInflater().inflate(R.menu.main_tab, menu);
/** Get the action view of the menu item whose id is search */
View v = (View) menu.findItem(R.id.action_menu).getActionView();
Button b = (Button) v.findViewById(R.id.btnMenu);
return super.onCreateOptionsMenu(menu);
}
for some reason the action bar add unnecessary item, does anybody knows how this case happene?
You are using Action bar sherlock. So use SupportMenuInflater.
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_menu:
// Intent i=new Intent(A.this,B.class);
//startActivity(i);
return true;
}
return false;
}
I found the mistake: i remove the android:orderInCategory from all the items and it's ok now, i this case i don't need this option.