I am able to select text when long pressed but when longClick on image its not showing save as option.? Tried all solutions but not getting results. I have tried solution on stackoverflow.com but not working for me.
I have tried below stuff
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// Confirm the view is a webview
if (v instanceof WebView) {
WebView.HitTestResult result = ((WebView) v).getHitTestResult();
if (result != null) {
int type = result.getType();
// Confirm type is an image
if (type == WebView.HitTestResult.IMAGE_TYPE || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
String imageUrl = result.getExtra();
Toast.makeText(this, imageUrl, Toast.LENGTH_LONG).show();
}
}
}
}
are u use registerForContextMenu()?
try this
activity.registerForContextMenu(webView);
Related
I have a web view that I want to get the URL when the user long clicks on links. I know I can do this:
// Register the context menu for web view
registerForContextMenu(webView);
And:
#Override
public void onCreateContextMenu(#NonNull ContextMenu menu, #NonNull View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
final WebView.HitTestResult result = webView.getHitTestResult();
Log.i("url is ===" , "result.getExtra()")
}
But this way, In some search engines like Google, Not be return a URL. This is my log when I long clicked on google search results:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAA+klEQVR4AWMgBnwr4pRloAR8KuA89KmAK4V8A/LZ1YGGvPiQy65MtCaQjR8LuDxh/M8FXNlAQ078D2VgJs6APE6rjwWcj4CaOkGaIIZybPucz1FHvCuyeYVBmkBhAArIL6XcEiCvfCzkNMOpCeRPoPMXgFyACAPOcnAYFLK7fSrkDAR6rQmnAe/KBfmBNhQCDbkGtP0yyO8gsU+FHLYgL4EClGgvgDR9zudcCrT9PchVX4q49YmKc5AmkLNBsQDyOygsQK4iygCQoo8FHLGg0AcFIDQm3gPpvXg1zlu88j++2ACFPB51CAmgbf9xYcIGEAAEDSAWM1ATAAC4CbdSzzGZLwAAAABJRU5ErkJggg==
I download a web browser open source from GitHub. In this project, the Programmer does this and this is worked:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
// Get link-URL.
String url = (String) msg.getData().get("url");
Log.i("get url" , url);
}
};
So I write this code in my app, But handleMessage not be run. Do I need to do anything else to use handleMessage method?
Do you know another way to get a URL in google search results?
Finally, after days, I found a solution. Please try this way:
#Override
public void onCreateContextMenu(#NonNull ContextMenu menu, #NonNull View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
String url = null, imageUrl = null;
WebView.HitTestResult result = ((WebView) v).getHitTestResult();
switch (result.getType()) {
case WebView.HitTestResult.SRC_ANCHOR_TYPE:
url = result.getExtra();
break;
case WebView.HitTestResult.IMAGE_TYPE:
imageUrl = result.getExtra();
break;
case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
case WebView.HitTestResult.EMAIL_TYPE:
case WebView.HitTestResult.UNKNOWN_TYPE:
Handler handler = new Handler();
Message message = handler.obtainMessage();
((WebView) v).requestFocusNodeHref(message);
url = message.getData().getString("url");
if ("".equals(url)) {
url = null;
}
imageUrl = message.getData().getString("src");
if ("".equals(imageUrl)) {
imageUrl = null;
}
break;
}
showLongPressMenu(url, imageUrl);
}
I want to Create a Context Menu for web view in android. My problem is that I cant get the URL from the clicked link. I write this code for log the clicked link:
webView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(final View v) {
WebView webView1 = (WebView) v;
WebView.HitTestResult hitTestResult = webView1.getHitTestResult();
Log.i("LinkClicked", hitTestResult.getExtra());
return false;
}
});
With this code, when long click on links in websites like stackoverflow, log like this:
https://stackoverflow.com/questions
everything is ok, but when long click on google search results, log like this:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAJFBMVEVHcEz/AAD/AAD/AAD/AAD/AAD/AAD/AAD/g4P/////KCj/7e0I8rGvAAAAB3RSTlMAT0g5Ggp+EilkVQAAAENJREFUGJVjYCAGsDAzggEzC5jLxo4E2BgYWNlRACsDM5jmggkwMzCBaQ5uqBATTICTk4OAAEILhqEY1mI4DNPphAAASIsES4gsgWgAAAAASUVORK5CYII=
it does not return a link. How I can fix this?
public class MainActivity extends AppCompatActivity {
WebView webView;
String URL1 = "https://stackoverflow.com/questions/60577403/how-to-get-url-from-long-click-links-in-a-webview/60577736?noredirect=1#comment107173847_60577736";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
public void onPageFinished(WebView view, String url) {
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
URL1 = url;
return super.shouldOverrideUrlLoading(view, url);
}
});
webView.loadUrl(URL1);
// Register the context menu for web view
registerForContextMenu(webView);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// Get the web view hit test result
final WebView.HitTestResult result = webView.getHitTestResult();
// If user long press on url
if (result.getType() == WebView.HitTestResult.ANCHOR_TYPE ||
result.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
// Set the title for context menu
menu.setHeaderTitle("\t\t\t\t\t\t\t\t\t\t ◦ ◉ ⦿ Select ⦿ ◉ ◦ \t");
// Add an item to the menu
menu.add(0, 1, 0, " \t \t➤\t Show URL")
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
String Pressed_url = result.getExtra();
Toast.makeText(MainActivity.this, "URL is:-" + Pressed_url,
Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
}
Download Full Code from github.
Output
I am new to programming and wanted to know that how is the onListItemClick() method invoked:
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id)
};
My question is that when we click on the list item who calls this method :
is it some kind of System callback,
is DVM responsible for invoking it,
or what is it?
My question is that when we click on the list item who calls this
method : is it some kind of System callback, is DVM responsible for
invoking it, or what is it?
ListActivity does what you would have done if you were extending Activity instead of ListActivity. It registers an instance of AdapterView.OnItemClickListener, and when the callback's method is invoked, it calls the method you are overriding.
Android is an open source platform so you can check all classes from SDK to figure how they work.
Checking AbsListView, which is parent of ListView, you will see this method:
#Override
public boolean performItemClick(View view, int position, long id) {
boolean handled = false;
boolean dispatchItemClick = true;
if (mChoiceMode != CHOICE_MODE_NONE) {
handled = true;
boolean checkedStateChanged = false;
if (mChoiceMode == CHOICE_MODE_MULTIPLE ||
(mChoiceMode == CHOICE_MODE_MULTIPLE_MODAL && mChoiceActionMode != null)) {
boolean checked = !mCheckStates.get(position, false);
mCheckStates.put(position, checked);
if (mCheckedIdStates != null && mAdapter.hasStableIds()) {
if (checked) {
mCheckedIdStates.put(mAdapter.getItemId(position), position);
} else {
mCheckedIdStates.delete(mAdapter.getItemId(position));
}
}
if (checked) {
mCheckedItemCount++;
} else {
mCheckedItemCount--;
}
if (mChoiceActionMode != null) {
mMultiChoiceModeCallback.onItemCheckedStateChanged(mChoiceActionMode,
position, id, checked);
dispatchItemClick = false;
}
checkedStateChanged = true;
} else if (mChoiceMode == CHOICE_MODE_SINGLE) {
boolean checked = !mCheckStates.get(position, false);
if (checked) {
mCheckStates.clear();
mCheckStates.put(position, true);
if (mCheckedIdStates != null && mAdapter.hasStableIds()) {
mCheckedIdStates.clear();
mCheckedIdStates.put(mAdapter.getItemId(position), position);
}
mCheckedItemCount = 1;
} else if (mCheckStates.size() == 0 || !mCheckStates.valueAt(0)) {
mCheckedItemCount = 0;
}
checkedStateChanged = true;
}
if (checkedStateChanged) {
updateOnScreenCheckedViews();
}
}
if (dispatchItemClick) {
handled |= super.performItemClick(view, position, id);
}
return handled;
}
My question is that when we click on the list item who calls this
method : is it some kind of System callback, is DVM responsible for
invoking it, or what is it?
Yes. It is some kind of callback.
I have a app which is based on RFID reader and providing data is based on that tags. my layout view contains the edittext which has the Listener OnEditorActionListener to read rfid tags and Gridview to show the calendar (customised calendar).Listener and editText is everything working fine upto Jellybean 4.3.
but am facing problem only in kitkat (4.4) .In KitKat i can read only single card after that the edittext focus is disabled automatically. I can't scan/read the second card .
Listener Code:
editText_readRfid
.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (!event.isShiftPressed()) {
rfidData = editText_readRfid.getText()
.toString();
editText_readRfid.getText().clear();
if (mLinkedHashMap.containsKey(rfidData))
{
String timeStamp = (String) mLinkedHashMap
.get(rfidData);
if (Util.checkTimeDifference(
timeStamp,
Util.getCurrentTimeStamp())) {
mLinkedHashMap.put(rfidData,
Util.getCurrentTimeStamp());
displayDetails(rfidData);
}
else {
Util.showmessage(context, "Alert",
"You have already swiped the card");
}
}
else {
mLinkedHashMap.put(rfidData,
Util.getCurrentTimeStamp());
displayStudentDetails(rfidData);
}
return true; // consume.
}
}
return false; // pass on to other listeners
}
});
after the focus disabled ,i tried this, inside edittext cursor blinking but no response
editText_readRfid.setOnFocusChangeListener(new OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
Toast.makeText(context, "Focus :" + hasFocus,
Toast.LENGTH_LONG).show();
v.requestFocus();
v.setSelected(true);
v.setFocusable(true);
}
}
});
After swiping the card am showing records and am updating gridview also so that focus went to Gridview.for that i disabled the Gridview cell(Button) focus and now its working fine.
gridcell.setFocusable(false);
I am developing an online magazine application. It downloads 50 to 100 html5 files from server and store it on sd card and shows this html5 files as pages of magazine. I have used gallery widget to load html pages, It works as my requirement.
But Some issues like loading html on gallery seems slow and page moves so slowly when swiping the pages still exist. My code is given bellow
public View getView(int position, View convertview, ViewGroup parent) {
try{
vi = convertview;
final int loctn = position;
if (convertview == null)
vi = inflater.inflate(R.layout.contentsadapter, null);
WebView webview=(WebView)vi.findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(pages.get(loctn));
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
&& view.canGoBack()) {
view.goBack();
return true;
}
return super.shouldOverrideKeyEvent(view, event);
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
/*Intent intent = new Intent(Femi9Activity.this,
SplashScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Femi9Activity.this.startActivity(intent);*/
}
});
webview.getSettings().setDomStorageEnabled(true);
// Set cache size to 8 mb by default. should be more than enough
webview.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
webview.getSettings().setAllowFileAccess(true);
webview.getSettings().setAppCacheEnabled(true);
webview.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
gallery.onTouchEvent(event);
return false;
}
});
}catch(Exception e){
}finally{
}
return vi;
}
}
so the question is
How to improve speed of HTML loading.
can I load previous and next page html on current page
Is there a better way to do this without using gallery
You can use ViewPager instead of Gallery.