How do I open ContextMenu with a Button - android

I'd like to show a context_menu when a user click on a Button but I'm encountering some problems. For some reason the menu is not appearing, maybe there's something I'm missing. Can you help me please?
That's my code:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnCreateContextMenuListener(this);
registerForContextMenu(btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.showContextMenu();
}
});
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
int i = item.getItemId();
if (i == R.id.share) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Leggi");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.chooser_title)));
return true;
} else {
return onContextItemSelected(item);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
and this is my context_menu in R.menu:
<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=".MainActivity">
<item android:id="#+id/share"
android:title="Condividi"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="#+id/web"
android:title="Mostra articolo"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="#+id/add"
android:title="Aggiungi nei Preferiti"
android:orderInCategory="100"
app:showAsAction="never" />

Change the app:showAsAction="never" to android:showAsAction="never" perhaps ?
(or just remove it).

You can use openContextMenu instead of showContextMenu.
registerForContextMenu(btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openContextMenu(v);
}
});

you can try this code
code:
public class MainActivity extends Activity {
Button btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.Button1);
registerForContextMenu(btn);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
btn.showContextMenu();
}
});
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0,1,0,"copy");
menu.add(0,2,0,"paste");
super.onCreateContextMenu(menu, v, menuInfo);
}
}

Related

Share music on messenger

Hi i made aplication(soundboard)
I have the buton when i click(OnClick) is plays music, when i hold button(longClick) is opens ContextMenu. In ContextMenu i have button. Now i need add the action for this button(button in ContextMenu) "share this music on messneger". Can help me someone?
Java code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button contextMenuButton = (Button) findViewById(R.id.smiech);
registerForContextMenu(contextMenuButton);
final MediaPlayer smiech = MediaPlayer.create(this, R.raw.smiech);
Button playsmiech = (Button) this.findViewById(R.id.smiech);
playsmiech.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
smiech.start();
}
});
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.menu_main,menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.item_option1:
break;
case R.id.item_option2:
break;
case R.id.item_option3:
break;
}
return super.onContextItemSelected(item);
}

Context menu single click Android

I'm new to the Android development. I was trying to add a context menu to my app. I understood that by default it requires long click on the button to open the context menu. But I need to make it to appear on the single click. I tried all the other solutions here in stackoverflow but none of them are really helping me.
I have posted my code below. kindly let me know what are the modifications to be done to make it working.
public class ThirdActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_layout);
confirmButton = (Button) findViewById(R.id.confirmButton);
registerForContextMenu(confirmButton);
}
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select Menu");
menu.add(0, v.getId(), 0, "Action 1");
}
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Action 1") {
//do something
}
}
just :
public class ThirdActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_layout);
confirmButton = (Button) findViewById(R.id.confirmButton);
confirmButton .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
confirmButton .performLongClick();
}
});
registerForContextMenu(confirmButton);
}
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select Menu");
menu.add(0, v.getId(), 0, "Action 1");
}
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Action 1") {
//do something
}
}
You can simply write:
confirmButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view.showContextMenu();
}
});

Android: I can't make collapseActionView

I want to make something like that:
I try to make the popup menu whit the collapseview, y the action bar. but i try all the thing that i found on internet and no solution found. i didnt need suppoart android api less than 15.
this the main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item android:id="#+id/action_search"
android:icon="#android:drawable/ic_dialog_email"
android:title="#string/hello_world"
android:showAsAction="always|collapseActionView"
/>
<item android:id="#+id/action_compose"
android:icon="#android:drawable/btn_star"
android:title="#string/hello_world"
android:showAsAction="never"
/>
<item android:id="#+id/action_compose2"
android:icon="#android:drawable/btn_star"
android:title="#string/hello_world"
android:showAsAction="never"
/>
</menu>
This my activity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar a = getActionBar();
a.setTitle("Mariano");
}
#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);
MenuItem menuItem = menu.findItem(R.id.action_search);
MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem arg0) {
// TODO Auto-generated method stub
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return false;
}
}
If I understood correctly you want to open an Android Popup Menu by click on a Button. Then all you have to do is to add this to your MainActivity:
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
int i = item.getItemId();
if (i == R.id.id1) {
//do something
return true;
}
else if (i == R.id.id2){
//do something
}
else if (i == R.id.id3) {
//do something
return true;
}
else if (i == R.id.id4) {
//do something
return true;
}else {
return onMenuItemClick(item);
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
}

how to search text in webview

I am working in android... I am a newbie in this field... I developed a webview...i need to search for a particular text inside the webview... I searched about some questions like this but i didn't get a proper answer
I did this much code but when executed the webview is showing but the buttons i gave in the code are missing
*
private static final int SEARCH_MENU_ID = Menu.FIRST;
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0, SEARCH_MENU_ID, 0, "Search");
return true;
}
public boolean onPrepareOptionsMenu(Menu menu){
super.onPrepareOptionsMenu(menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case SEARCH_MENU_ID:
search();
return true;
}
return true;
}
public void search(){
container = (LinearLayout)findViewById(R.id.lLayout1);
nextButton = new Button(this);
nextButton.setText("Next");
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mWebView.findNext(true);
// TODO Auto-generated method stub
}
});
container.addView(nextButton);
closeButton = new Button(this);
closeButton.setText("Close");
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
container.removeAllViews();
// TODO Auto-generated method stub
}
});
container.addView(closeButton);
findBox = new EditText(this);
findBox.setMinEms(30);
findBox.setSingleLine(true);
findBox.setHint("Search");
findBox.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event){
if((event.getAction() == KeyEvent.ACTION_DOWN) && ((keyCode == KeyEvent.KEYCODE_ENTER))){
mWebView.findAll(findBox.getText().toString());
try{
Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
m.invoke(mWebView, true);
}catch(Exception ignored){}
}
return false;
}
});
}
}
*
While this is an older question, based on AndroidEnthusiastic's version I managed to put together one that was actually working fairly decently. There is a minor error with the type of button on the Samsung Note I tested it on, but no matter how I was changing the IME types, it just got messed up. So I'm just hiding the soft-key keyboard. But the layout is better constructed.
public class SearchDemoActivity extends ActionBarActivity implements View.OnClickListener
{
WebView mWebView;
private RelativeLayout container;
private Button nextButton, closeButton;
private EditText findBox;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
getActionBar().setTitle(R.string.information_display);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://devemat-androidprogramming.blogspot.com/");
nextButton = (Button) findViewById(R.id.nextButton);
closeButton = (Button) findViewById(R.id.closeButton);
findBox = (EditText) findViewById(R.id.findBox);
findBox.setSingleLine(true);
findBox.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if ((event.getAction() == KeyEvent.ACTION_DOWN) && ((keyCode == KeyEvent.KEYCODE_ENTER)))
{
mWebView.findAll(findBox.getText().toString());
try
{
// Can't use getMethod() as it's a private method
for (Method m : WebView.class.getDeclaredMethods())
{
if (m.getName().equals("setFindIsUp"))
{
m.setAccessible(true);
m.invoke(mWebView, true);
break;
}
}
}
catch (Exception ignored)
{
}
finally
{
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// check if no view has focus:
View vv = getCurrentFocus();
if (vv != null)
{
inputManager.hideSoftInputFromWindow(v.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
return false;
}
});
nextButton.setOnClickListener(this);
closeButton.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.searchview_in_menu, menu);
return true;
}
public boolean onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_search:
search();
return true;
}
return true;
}
public void search()
{
container = (RelativeLayout) findViewById(R.id.layoutId);
if (container.getVisibility() == RelativeLayout.GONE)
{
container.setVisibility(RelativeLayout.VISIBLE);
}
else if (container.getVisibility() == RelativeLayout.VISIBLE)
{
container.setVisibility(RelativeLayout.GONE);
}
}
#Override
public void onClick(View v)
{
if (v == nextButton)
{
mWebView.findNext(true);
}
else if (v == closeButton)
{
container.setVisibility(RelativeLayout.GONE);
}
}
}
The layout XML (activity_search.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/layoutId" />
<RelativeLayout
android:id="#+id/layoutId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone" >
<Button
android:id="#+id/closeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Close" />
<Button
android:id="#+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/closeButton"
android:text="Next" />
<EditText
android:id="#+id/findBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/nextButton"
android:hint="Enter search keyword here."
android:singleLine="true" />
</RelativeLayout>
</RelativeLayout>
The menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
custom:showAsAction="always"
android:title="#string/action_search"/>
</menu>
Please try this snippet
package com.search.demo;
import java.lang.reflect.Method;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class SearchDemoActivity extends Activity {
WebView mWebView;
private LinearLayout container;
private Button nextButton, closeButton;
private EditText findBox;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView)findViewById(R.id.webview);
mWebView.loadUrl("http://devemat-androidprogramming.blogspot.com/");
}
private static final int SEARCH_MENU_ID = Menu.FIRST;
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0, SEARCH_MENU_ID, 0, "Search");
return true;
}
public boolean onPrepareOptionsMenu(Menu menu){
super.onPrepareOptionsMenu(menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case SEARCH_MENU_ID:
search();
return true;
}
return true;
}
public void search() {
container = (LinearLayout) findViewById(R.id.layoutId);
nextButton = new Button(this);
nextButton.setText("Next");
nextButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mWebView.findNext(true);
}
});
container.addView(nextButton);
closeButton = new Button(this);
closeButton.setText("Close");
closeButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
container.removeAllViews();
}
});
container.addView(closeButton);
findBox = new EditText(this);
findBox.setMinEms(30);
findBox.setSingleLine(true);
findBox.setHint("Search");
findBox.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && ((keyCode == KeyEvent.KEYCODE_ENTER))) {
mWebView.findAll(findBox.getText().toString());
try {
Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
m.invoke(mWebView, true);
} catch (Exception ignored) {
}
}
return false;
}
});
container.addView(findBox);
}
}
I simplified it for my usage
findBox.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
webView.findAll(findBox.getText().toString());
}
#Override
public void afterTextChanged(Editable s) {
}
});
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView.findNext(true);
}
});
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layoutId.setVisibility(View.GONE);
webView.clearMatches();
closeKeyboard(MainActivity.this);
}
});
To check continuously text from WebView use this code:
webView.setFindListener(new WebView.FindListener() {
#Override
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
webView.findAllAsync("Search Text"); //find text for each foundresult
}
});

ClickListener on custom ActionBar Item (ActionBarSherlock)

I'm putting some custom items in my ActionBarSherlock AB like this in my SherlockFragmentActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu_builtin, menu);
MenuItem selectAll = menu.findItem(R.id.selectall);
selectAll.setActionView(R.layout.selectalllayout);
return super.onCreateOptionsMenu(menu);
}
Neither onMenuItemSelected nor onOptionsItemSelected are called when a custom item is clicked, they are when I add a 'standard item' with menu.add(String).
I also tried:
selectAll.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
return false;
}
});
and
selectAll.getActionView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
they aren't called either. My layout components are made clickable and everthing that has to do with the menu is imported from ABS, not android.
Any ideas on what's wrong here?
You can do it two ways
First:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
View view = LayoutInflater.from(this).inflate(R.layout.your_layout, null);
actionBar.setCustomView(view);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//Do your click stuff
}
});
}
Second:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/view_id"
android:title="#string/name"
android:actionLayout="#layout/your_layout"
android:showAsAction="always" />
</menu>
In your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getSupportMenuInflater().inflate(R.menu.main_menu, menu);
View view = (View) menu.findItem(R.id.view_id).getActionView();
// to get child view - example:
//ImageView image = (ImageView)view.findViewById(R.id.my_item);
//image.setOnClickListener....
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do stuff here
}
});
return true;
}
Dont forget to import these.
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

Categories

Resources