Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I've tried searching around but I am only limited to a very minimal amount of internet usage till 16th so I was looking around on how I could implement buttons to my action bar.
Right so when you enter the playlist section on my app, there is an about button on the layout. But id like to move that button to my action bar, how can I achieve this?
This is an example how you can add HELP icon to your action bar
Create a menu XML inside your menu folder like the following
<item
android:id="#+id/help_menu_item"
android:icon="#android:drawable/ic_menu_help"
android:title="Help"
android:showAsAction="ifRoom" />
And in your activity do something like that
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.help_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.help_menu_item:
//do your menu press here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayUseLogoEnabled(false);
mActionBar.setDisplayHomeAsUpEnabled(false);
mActionBar.setDisplayShowCustomEnabled(true);
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionBar.setCustomView(R.layout.titlebar);
homeButton = (ImageView) mActionBar.getCustomView().findViewById(R.id.titlebar_iv_home);
menuButton = (ImageView) mActionBar.getCustomView().findViewById(R.id.titlebar_iv_menu);
titlebar_title = (TitleTextView)mActionBar.getCustomView().findViewById(R.id.titlebar_title);
titlebar_title.setText("TITLE");
you can give click events for the buttons also.Here i am taking one custom layout added to action bar. so create custom layout in your res/layout folder. and give appropriate id's to them.and give click events
hope this helps
Related
This question already has answers here:
How to add button in ActionBar(Android)?
(2 answers)
Add button in android action bar
(2 answers)
Closed 5 years ago.
As title says i don't know how to add button to android action bar near title of app with icon.
I need to add it here as shown on picture
enter image description here
First, create a menu resource, name it whatever you want.
Then, add this to your new .xml resource:
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:ignore="AlwaysShowAction">
<item android:id="#+id/your_item_id"
android:icon="#drawable/your_drawable_id"
android:title="#string/your_string_id"
app:showAsAction="always" />
</menu>
The item tag defines the id, the icon and the title. the showAsAction defines if item is located at the action bar or submenu.
It's recommended to use string resources for your title, specially if you want to translate your app.
The menu icon is defined by your drawable resource. In order to use the correct size, I recommend to use the default icon asset wizard.
Add this to your Activity to add the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu_name, menu);
return true;
}
In order to detect menu pushes, use this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.your_item_id) {
//Do your stuff here
return true;
}
return super.onOptionsItemSelected(item);
}
More info: https://developer.android.com/guide/topics/ui/menus.html
How can I add menu items to the menu of the DrawerLayout in Java and set onOptionsItemSelected to it. (I don't know how many items are needed; the user adds them)
I have an arraylist of custom objects that have a title that I want to display in the menu and when clicked on the item I want to change what is being displayed in the main Activity, by passing which one of the objects was clicked on.
I know that it is possible to add a menu item like this, but the problem is that I don't know how many there are going to be, so setting the onOptionsItemSelected to the right amount of items is what I am looking for
private static final int MENU_ITEM_ITEM1 = 1;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, MENU_ITEM_ITEM1, Menu.NONE, "Item name");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ITEM_ITEM1:
return true;
default:
return false;
}
}
Thanks for your help!
EDIT:
I see how how this might seem like a duplicate question, but the other questions and answers did not help. I am looking for a way to set onOptionsItemSelected to an unknown amount of items (the size of an ArrayList) and then display the selected Object in the main Activity
Thanks
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to imitate Instagram on long click feature in my application.
I have a list view and when the user clicks an item, it opens a details activity.
what i'm trying to do is to display the details to the user when long pressing an item without moving to the details activity, i need to display it in the center of the screen in the same activity (like when you long press on a picture in Instagram) and disappears when the user release the touch.
I have tired the following:
view.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Log.d("LongClick", "Long click");
Intent in = new Intent(AppointmentsActivity.this,AppointmentsDetailsActivity.class);
startActivity(in);
return true;
}
});
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
Log.d("TouchTest", "Touch down");
} else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
Log.d("TouchTest", "Touch up");
}
return false;
}
});
For this, just open another activity onLongPress as dialog activity.So Content of detail activity will show in current activity.
To open the activity as dialog activity, you need to set activity theme as dialog theme
< activity android:theme="#android:style/Theme.Dialog" />
For These you don't need to start new Activity. you have only to have datas (preferences or a class with variable). Then with a simple dialogbox you can do that or with a fragment. Because you can't run 2 Activities in the mean time. And if you want to start detail Activity for resons of memorie, your launch activity may be killed and don't still in backstack.
as user2043602 said, you don't need to start a new activity.,
According to me the best alternative is to use ViewAnimator with two layouts,
and dynamically add a fragment to the second layout[which is not visible, until you move to next view in ViewAnimator].
to add fragment at run-time, you can use the following:
FragmentTransaction transaction;
transaction = getFragmentManager().beginTransaction();
page_1 newFragment1 = page_1.newInstance("one", "two");//here page_1 is my fragment..which you can easily do by adding a new default blank fragment in android studio
transaction.add(R.id.holder, newFragment1);//R.id.holder is the layout id under whihc you wish to add the fragment..
you can you ViewAnimator like this:
ViewAnimator viewAnimator;
viewAnimator = (ViewAnimator)findViewById(R.id.group1);//R.id.group1 is the view animator id
viewAnimator.setInAnimation(AnimationUtils.loadAnimation(page2.this, R.anim.first_try)); viewAnimator.setOutAnimation(AnimationUtils.loadAnimation(page2.this, android.R.anim.slide_out_right));
viewAnimator.showNext();
I am wondering how to animate going into and out of the contextual action mode. I am looking for an animation like in the app AwSms when you long press on a conversation (image provided, sorry for no video) The animation is essentially a ripple that comes from the middle and expands outwards on the Action Bar. I have searched long and far with no foreseeable way to animate the contextual action mode. It was definitely done here but I am not sure how. Thank you very much in advance!
Things I have tried:
public ActionMode.Callback iconEditCallback = new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_iconmanager_edit, menu);
mode.getCustomView().startAnimation(ripple);
return true;
}
I also tried:
getSupportActionBar.setShowHideAnimationEnabled(true);
I can't find anything on the google and stackoverflow so I had to ask.
I have menu on Toolbar:
<item
android:icon="#drawable/help_button_selector"
android:title="#string/Menu_Toolbar_Help"
android:id="#+id/MENU_HELP"
app:showAsAction="always"/>
When user long press icon, system shows tooltip (popup/context menu) with title. However on most devices it has black border (like in Toast). Is there any way to style this border/background? I have already created style for toolbar theme and popup, but I can't find a proper item name.
1.- You can follow this post to create your ToolTip link
2.-You can do this to show a Toast as a ToolTip, then you can customize it as you want.
view.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
Toast.makeText(v.getContext(), "I'm a ToolTip", Toast.LENGTH_SHORT).show();
return true;
}
}
Hope it helps :)
Use below link to customize your Action Bar:
http://romannurik.github.io/AndroidAssetStudio/
open url -> GoTo Android Action Bar Style Generator -> Select the options -> click DOWNLOAD.ZIP -> Copy all the componets into their correspondence folders -> In your styles.xml file remove all the code and copy all the code of styles_**.xml file and delete this file.
Menu tooltip
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_skip) {
View view = findViewById(R.id.menu_skip);
Tooltip.make(this, new Tooltip.Builder(101)
.anchor(view, Tooltip.Gravity.BOTTOM)
.closePolicy(mClosePolicy, 5000)
.text("Tooltip on a TabLayout child...Tooltip on a TabLayout child...")
.fadeDuration(200)
.fitToScreen(true)
.activateDelay(2000)
.withCallback(this)
.floatingAnimation(Tooltip.AnimationBuilder.DEFAULT)
.showDelay(400)
.build()
).show();
return true;
}
return super.onOptionsItemSelected(item);
}
Visit more