Pulling a single variable off a webview in android? - javascript

I need to pull a value from a hidden div tag when using a webview. I have turned on javascript in my webview activity but its not liking the "". HOW can I achieve my goal?
public class buttonOne extends Activity{
WebView wb = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buttons);
wb = new WebView(this);
wb.getSettings().setJavaScriptEnabled(true);
wb.setWebViewClient(new HelloWebViewClient());
wb.getSettings().setJavaScriptEnabled(true);
wb.loadUrl("http://ishopstark.com/mobileapp.php?category=1");
setContentView(wb);
}
private class HelloWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
<script type="text/javascript">
{
var varSendText = document.getElementById("sendtextcoupon").value;
}
</script>
return true;
}
}

Here's a short example of how to inject Javascript into a WebView.
If you actually want to get the value and use it in your Java code, you're going to have to register a JavascriptInterface like in this example - extracting HTML from a WebView

Related

How to hide BottomTabNavigator in certain screen in React Navigation (4.x)

I need to hide the bottom tab navigator in my 'Chat' screen in my app built with React Native and React Navigation.
I have the following code:
const UserNavigation= createStackNavigator({
Profile:{screen:Profile},
Search:{screen:Search},
Feedback:{screen:Feedback},
Chat: {screen:Chat}, //I need to hide the bottom tab navigation bar in this screen
},
const TabNavigator = createBottomTabNavigator({
User: UserNavigation,
Settings: SettingsNavigation,
// etc...
});
How can I achieve this ?
You no need to hide the BottomNavBar, instead you can make the Chat Fragment into DialogFragment. So it covers the Full Screen
in Style.xml copy the following code
<style name="MY.DIALOG" parent="AppTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
extend your ChatFragment from Fragment to DialogFragment, then copy the following
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.MY_DIALOG);
}
#Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
dialog.getWindow().setLayout(width, height);
}
}
pass the instance of DialogFrament
loadDiaogFragment(new ChatFragment);
create the following function
private void loadDialogFragment(DialogFragment fragment) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
fragment.show(transaction, "Load Fragment");
}
to close the DialogFragment use following inside onClickLister
dismiss();

What is the Vaadin 8 way of adding code to the html head tag?

Other SO answers suggest overriding ApplicationServlet.writeAjaxPageHtmlHeader, but I cannot find those classes and methods in Vaadin 8.
I cannot find anything similar in com.vaadin.server.VaadinServlet or com.vaadin.ui.UI.
There is the #JavaScript annotation, but if I put that on my UI class, the script would be loaded for every page of my application. I need it only on one specific page.
The initial HTML page is called bootstrap page in Vaadin. There is some documentation that hints you to right direction in Book of Vaadin.
In Vaadin 8, you need to add BootstrapListener to session. You can get created sessions by adding SessionInitListener in VaadinServlet.
Register Session
This example is using Vaadin with Spring Boot but the same principle applies when not using Spring Boot.
#Component("vaadinServlet")
#WebServlet(urlPatterns = "/*", name = "BootstrapVaadinServlet", asyncSupported = true)
#VaadinServletConfiguration(ui = BoostrapUi.class, productionMode = false)
public class BootstrapVaadinServlet extends SpringVaadinServlet {
private static final Logger logger = LoggerFactory.getLogger(BootstrapVaadinServlet.class);
#Override
protected void servletInitialized() throws ServletException {
super.servletInitialized();
getService().addSessionInitListener(this::addBoostrapListenerOnSessionInit);
}
private void addBoostrapListenerOnSessionInit(SessionInitEvent sessionInitEvent) {
sessionInitEvent.getSession().addBootstrapListener(new AppBootstrapListener());
}
}
Implement html head tag modification
public class AppBootstrapListener implements BootstrapListener {
#Override
public void modifyBootstrapFragment(BootstrapFragmentResponse bootstrapFragmentResponse) {
}
#Override
public void modifyBootstrapPage(BootstrapPageResponse res) {
Elements headTags = res.getDocument().getElementsByTag("head");
Element head = headTags.get(0);
head.appendChild(metaExample(res.getDocument()));
}
private Node metaExample(Document document) {
Element meta = document.createElement("meta");
meta.attr("author", "Me");
return meta;
}
}
If using add-on is ok, try HeaderTags
Overview
Using this add-on, you can define tags to add to the host page by
adding annotation to your UI class.
Sample from add ons usage example
#MetaTags({
// Replaces the Vaadin X-UA-Compatible header
#Meta(httpEquiv = "X-UA-Compatible", content = "hello"),
#Meta(name = "test", content = "test") })
// And showing how to create a link tag as well
#Link(rel = "foobar", href = "about:blank")
public class DemoUI extends UI {
...
}

WebView not load when open one url(android)

Sorry for my english. I have one activity and one fragment(fragment have webView). In activity i have items menu, if i click for item reload link in fragment. Each time when i click for item in menu, i call this code
ItemMenuFragment fragment = new ItemMenuFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, fragment);
fragmentTransaction.commit();
Fragment reload and then he get new link for webView. In fragment load page html like this
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl(Global.URL_MAIN + ((MainActivity) getActivity()).getLink());
Everything work fine. But in html link i have profile page, in this profile page i have button "load image", when i click this button show popup and after this all items menu cant load url. Its show white page and its all.
My ways of solving:
I think, need reload my fragment every times and i add this code when load fragment:
ItemMenuFragment fragment = new ItemMenuFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if(!fragmentTransaction.isEmpty()) fragmentTransaction.remove(fragment);
fragmentTransaction.add(R.id.frame_layout, fragment);
fragmentTransaction.commit();
its not work, then i add this code:
ItemMenuFragment fragment = new ItemMenuFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.frame_layout, fragment);
fragmentTransaction.detach(fragment);
fragmentTransaction.attach(fragment);
fragmentTransaction.commit();
It does not work either. Then I add in fragment code, which the clean webView:
CookieSyncManager.createInstance(getActivity());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
webView.clearCache(true);
webView.clearHistory();
It does not work either. I don't know how to solve my problem. Once again about my problem: after popup window in webView i cant any more load url in webView. It show me white pages.
UPD:
This is simple code my project. My left menu create dynamicly and in left menu i have listView. I have only one fragment, this fragment reload link each time when i click some item in menu
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//menuItems - listview
//item - object in menu(contains link and name)
//adapterMenuItems adapter for lisView
menuItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setLink(item.getUrl());
setFragment();
adapterMenuItems.notifyDataSetChanged();
if(menu.isMenuShowing()) menu.toggle();
}
});
}
public void setFragment(){
ItemMenuFragment fragment = new ItemMenuFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.frame_layout, fragment);
fragmentTransaction.commit();
}
//set link for fragment
public void setLink(String link){
this.link = link;
}
//return link for fragment
public String getLink(){
return link;
}
}
Fragment
public class ItemMenuFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View containerView = inflater.inflate(R.layout.fragment_item_menu, container, false);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl(Global.URL_MAIN + ((MainActivity) getActivity()).getLink());
}}

Wicket Ajax Error Log: ERROR: Cannot bind a listener for event "click" on element "AjaxCheckBox" because the element is not in the DOM

Hey when I run my program, I want to dynamically add Panels to the mainpage and therfore work with "setVisible(boolean)".
But not I get the error:
ERROR: Cannot bind a listener for event "click" on element "institutCheck7" because the element is not in the DOM
In the Ajax Debugger.
I saw that it has something to do with the visibility in this
post, but I didn't get what I have to do, it says, that this is being resolved in Wicket 7, but I'm still running 6.20.
adminUIForm.add(myPanel= new MyPanel("myPanel", allThings));
myPanel.setOutputMarkupId(true);
myPanel.setVisible(false);
//Note: Setting the OutputMarkupId allows actions such as setting components in/visible
List<IColumn<ContactInfo, String>> columns = new ArrayList<IColumn<ContactInfo, String>>();
columns = initializeColumns(columns);
DataTable<ContactInfo, String> datatable = new DataTable<ContactInfo, String>("datatable", columns, provider, 10){
private static final long serialVersionUID = 1L;
#Override
protected Item<ContactInfo> newRowItem(final String id, final int index,
final IModel<ContactInfo> model) {
Item<ContactInfo> rowItem = new Item<ContactInfo>(id, index, model);
rowItem.add(new AjaxEventBehavior("onclick") {
private static final long serialVersionUID = 1L;
#Override
protected void onEvent(AjaxRequestTarget target) {
System.out.println("click");
myPanel.setSelectedKontakt(model);
myPanel.setVisible(true);
target.add(myPanel);
}
});
return rowItem;
}
};
setTableProperties(datatable);
adminUIForm.add(datatable);
}
I override the newRowItem method to add an onClick event. I get "click" in the console each time I hit a column, but I get this error log:
ERROR: Wicket.Ajax.Call.processComponent: Component with id [[institutTablePanel6]] was not found while trying to perform markup update. Make sure you called component.setOutputMarkupId(true) on the component whose markup you are trying to update.
ERROR: Cannot bind a listener for event "click" on element "institutCheck7" because the element is not in the DOM
ERROR: Cannot bind a listener for event "click" on element "cont8" because the element is not in the DOM
ERROR: Cannot bind a listener for event "click" on element "institutCheck9" because the element is not in the DOM
ERROR: Cannot bind a listener for event "click" on element "conta" because the element is not in the DOM
ERROR: Cannot bind a listener for event "click" on element "institutCheckb" because the element is not in the DOM
ERROR: Cannot bind a listener for event "click" on element "contc" because the element is not in the DOM
ERROR: Cannot bind a listener for event "click" on element "institutCheckd" because the element is not in the DOM
ERROR: Cannot bind a listener for event "click" on element "conte" because the element is not in the DOM
Those error all bind with components in "myPanel", I don't know how to resolve this. If I set it to visible earlier I get the same problem, including all elements, which will be hidden later in the workflow.
Edit:
public class MyPanel extends Panel {
private static final long serialVersionUID = 1L;
private Form<Void> institutForm = new Form<Void>("institutForm");
private ListView<Institut> listView;
private IModel<ContactInfo> selectedKontakt;
private Set<Institut> allInstitut;
#Override
protected void onModelChanged() {
//TODO maybe here
super.onModelChanged();
}
public InstitutTablePanel(String id, final Set<Institut> allInstitut) {
super(id);
this.allInstitut = allInstitut;
this.add(institutForm);
List<Institut> allInstitutList = new ArrayList<Institut>(allInstitut);
institutForm.add(listView = new ListView<Institut>("listView", allInstitutList) {
private static final long serialVersionUID = 1L;
protected void populateItem(final ListItem<Institut> item) {
final IModel<Boolean> checked = Model.of(Boolean.FALSE);
final IModel<String> expandState = Model.of("+");
#SuppressWarnings("unused")
final Label institutLabel;
final Institut institut = item.getModelObject();
// Define variables which concern only one item in populateItem
final WebMarkupContainer div = createMarkUpContainer("cont");
final WebMarkupContainer container = createMarkUpContainer("container");
container.setEnabled(false);
compareInstitute(checked, institut, container);
item.add(container);
div.add(new AjaxEventBehavior("onclick") {
private static final long serialVersionUID = 1L;
#Override
protected void onEvent(AjaxRequestTarget target) {
expandOrCollapse(expandState, container);
target.add(institutForm);
}
});
item.add(div);
div.add(new AjaxCheckBox("institutCheck", checked) {
private static final long serialVersionUID = 1L;
#Override
protected void onUpdate(AjaxRequestTarget target) {
//Not Important for this
target.add(institutForm);
}
});
container.add(new ErhebungMatrixPanel("erhebungMatrix", institut, selectedKontakt));
container.setVisible(false);
div.add(institutLabel = new Label("institutLabel", new PropertyModel<Institut>(institut, "langName")));
}
});
listView.setOutputMarkupId(true);
listView.setReuseItems(true);
institutForm.setOutputMarkupId(true);
}}
I edited my setSelectedKontakt method like this:
public void setSelectedKontakt(IModel<ContactInfo> model) {
this.selectedKontakt = model;
modelChanged();
}
to trigger the change if necessary.
The problem is target.add(myPanel); won't trigger populateItem again.
The error message says it pretty clearly and a look into the DOM tells that the markup for the panel is not there. Thats because setVisible(false) doens't even render the component with CSS display: none; as expected. It's just not there so it can't be modified afterwards. But you can tell Wicket to render a placeholder instead with setOutputMarkupPlaceholderTag(true). Wicket then creates an invisible markup e.g. <span style="display: none;" id="mypanel1a"></span>, which then is swaped with the real component when setting the visiblity to true later.
Just a tip to those that do get this error that a common mistake is to use:
<wicket:container wicket:id="panel"/>
...instead of...
<div wicket:id="panel"/>
...for where you put the panel. The method call setOutputMarkupId will not inform you that it is unable to add an id to a wicket:container, and hence your target.add(panel) will also fail.

How to change one of the ribbontext value in other acitvity?

Description: In my mainActivity class I have added one header and ribbon with text and send it to other secondActivity class to add some more rows (by setting onclick listeners) by using addRow(rootLayout, length,buttonHeight). My requirement is, If we click on any of the row which we added in secondActivity class, I have to change the ribbon text which we added in the mainActivity class.
Could any one please help out of this?
code:
MainActivity.java
-----------------
/*Main Activity Class which consists ribbon text and called method for creating the second row*/
public class MainActivity extends Acitvity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mainView); **//Main view**
final FrameLayout rootLayout = (FrameLayout)findViewById (R.id.mainViewRootLayout);
FrameLayout ribbonLayout = (FrameLayout)findViewById(R.id.mainribbonLayout);
TextView ribbonText = (TextView)findViewById(R.id.mainribbonLayoutText);
ribbonText.setText("MainRibbonViewBeforeClick");
LayoutInflater inflater = (LayoutInflater)getSystemService (context.Layout_Inflater_service);
addRow(rootLayout);
}
}
/*Here is the calling method in same class which adds one more text view, if we click on text view the ribbon text of oncreate method has to be changed*/
public void addRow(FrameLayout rootLayout)
{
Rect rect = new Rect();
rootLayout.getDrawingRect(rect);
FrameLayout ribbonLayout = (FrameLayout)findViewById(R.id.subRibbonLayout);
TextView subText = (TextView)findViewById(R.id.subribbonLayoutText);
subText.setText("ClicktoChangeTheRibbon");
subText.setOnclickListener(new onClickListener() **//Setting up the listener
{
public vid onclick(View v){
/*Here I need to implement a code that changes the ribbon text which I have added in onCreate method*/
}
});
}
}
Please have a look on the above code and suggest me the solution.
Create a static variable assign the text value of the ribbon to it.On the clickevent of any rows in the secondActivity class change the value of the variable to the value you want to display for the ribbon text.

Categories