WebView - Handle JavaScript Pop Up ? - Android Software Development

Hi,
I am new to Android Development. I am making an application with a WebView object. This loads a URL with some elements. However, I cannot get JavaScript PopUp to show up when the button is pressed.
My WebView has JavaScript enabled, but all other properties are disabled.
What is the property to enable WebView popups to show??

I had to do this. Override the onJSAlert() method in the WebChromeClient class:
Code:
public class MyWebChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result)
{
final JsResult finalRes = result;
new AlertDialog.Builder(view.getContext())
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finalRes.confirm();
}
})
.setCancelable(false)
.create()
.show();
return true;
}
}
Then add it to your webview:
Code:
MyWebChromeClient myWebChromeClient = new MyWebChromeClient();
webView.setWebChromeClient(myWebChromeClient);

Thanks. That did work!
One small thing, how do you set a Title for the pop-ups. Currently it shows the url in the Title.

I found a better example that sets the title:
http://lexandera.com/2009/01/adding-alert-support-to-a-webview/

footboydog said:
I found a better example that sets the title:
http://lexandera.com/2009/01/adding-alert-support-to-a-webview/
Click to expand...
Click to collapse
I tried that, but the Title is only displayed when the alert has one button/option. When there are multiple options, the Title shows the URL.

Sorry not sure how to fix that

footboydog said:
Sorry not sure how to fix that
Click to expand...
Click to collapse
Ok, I think I'll have to live with "file:///" showing as the Title for now...

you can convert to toast

Related

[Q] Need some help with my first app

Hi! I'm making my first app, and there are two things I still havet figure out
1: Align the layout?
I want to have a 3-section header like this:
http://bildr.no/view/864588
But i can't get it right, no mather what i do. Aligning it makes the progressbar look skewed..
2: Javascript
I'm working on a app with WebView. I've read alot of tutorials now, but the implementation of javascript does not work.. My app keeps force closeing
Code:
final class JavaScriptInterface {
Context mContext;
/* Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
public void showMessage(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
Webpage i use to test this is: http://jsbin.com/irifo3/2/
TableLayout with a TableRow or LinearLayout with orientation=horizontal
Stacktrace?
would be helpful if you provide some logging output.
-didi

Help writing something with setOnItemClickListener

In my Android app, I have a sound that I want to play when a certain selection has been made from a spinner, but I want it to play the when the user actually makes the proper selection (or just after). My problem is that although the sound does play when they make the correct selection, as long as that selection stays chosen, it also plays every time the app starts up, when it should ONLY play at the time it's chosen. I think I need to change my setOnItemSelectedListener to setOnItemClickListener, but I'm not sure how (still pretty new to java). Can any generous soul out there show me how to change this up (assuming that's how to best solve this problem)?
Here is the code I have now:
Code:
fitnessSpinner = (Spinner) findViewById(R.id.fitness_spinner);
ArrayAdapter adapter4 = ArrayAdapter.createFromResource(
this, R.array.fitness_array, android.R.layout.simple_spinner_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fitnessSpinner.setAdapter(adapter4);
fitnessSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long i) {
Log.d("test", "p: " + position + " " + i);
if(position == 0) {
//First Entry
MediaPlayer mp = MediaPlayer.create(mContext, R.raw.bowchica);
mp.start();
} if(position == 4) {
MediaPlayer mp = MediaPlayer.create(mContext, R.raw.debbie2);
mp.start();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
I haven't try the below code but you can try it on your own and tell us.
In onCreate() declare MediaPlayer mp;
In every if statement that you use for check insert this code:
Code:
if(mp!=null){mp.release();}
int resid = R.raw.yoursound;
mp = MediaPlayer.create(this, resid);
After that override the methods onPause() and onResume() and insert this:
if(mp!=null){mp.release();}
If it is still playing a sound when you start your app, then you should check your code again if you have set as default option any of your selection options.
I would LOVE to try this out...Unfortunately, I'm way too dumb at this point point ot figure out exactly where those code snippets would go inside of what I already have.
Does anyone have a couple of minutes to show me where it would go?
Below is a sample code. Since i don't know your code I give you a snippet that you should adjust it to your code.
Code:
public class SampleSound extends Activity{
private Spinner fitnessSpinner;
private MediaPlayer mp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//here goes your layout
setViews();//here you will set all your views(spinners buttons textviews etc..)
setAdapters();//set your adapters here
setListeners();//
}
private void setListeners() {
fitnessSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long i) {
Log.d("test", "p: " + position + " " + i);
if(position == 0) {
//First Entry
if(mp!=null){mp.release();}
int resid = R.raw.bowchica;
mp = MediaPlayer.create(this, resid);
mp.start();
} if(position == 4) {
if(mp!=null){mp.release();}
int resid = R.raw.debbie2;
mp = MediaPlayer.create(this, resid);
mp.start();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
private void setAdapters() {
ArrayAdapter adapter4 = ArrayAdapter.createFromResource(this, R.array.fitness_array, android.R.layout.simple_spinner_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fitnessSpinner.setAdapter(adapter4);
}
private void setViews() {
fitnessSpinner = (Spinner) findViewById(R.id.fitness_spinner);
}
public void onResume(){
super.onResume();
if(mp!=null){mp.release();}
}
public void onPause(){
super.onPause();
if(mp!=null){mp.release();}
}
}
I really appreciate the help. I put the code in my routine, but it still plays the sound every time the activity is loaded (as long as the selection in the spinner is correct). It should only play the sound when the correct selection is made.
Any other ideas?
I am sure that your Spinner is set to some value (since you have values to display). Because your Spinner points to a selection (doesn't matter if you have selected or it is selected by default) your sound plays (even when you start the app).
A way to stop the sound playing at start is to declare and an other Item like you did with the previous 4 and set it as default selection of your Spinner.
To sum up:
1.You have to append in R.array.fitness_array an Item (like you did with the previous Items) and give it a name.
2.At the end of method setAdapters() insert this:
Code:
fitnessSpiner.setSelection(5);// or whatever is your selection number
Now it should work but you should know that this is not a good practice and you should try make a ListView or something else.
I'd be happy to change this out to a listview, or whatever would work. I just have to give my user a choice of 4 or 5 items, from which they can choose only one. Something like a drop down box, but in Android, I thought my only option was a spinner. But whatever I use, I have to be able to play a sound when certain items are chosen, but ONLY when those items are chosen, NOT whenever the activity is called up.
Any specific ideas of what I might change to?
What if I had another control like a textview or an edittext (with it's visibility property set to false) that I programatically populated with the users selection (when it's the selection that I want) and then have an OnItemClcickListener set to play the sound?
Could that work?
I will answer from the last to the top of your questions.
1.You can do whatever you want with android. You want TextViews and EditTexts with complex and nested Layouts you can do it. Write services that will communicate with your contacts through a content provider? You can do it.
Write, read and test code. Only this way you will actually learn.
2.Read developer.android.com. Read the android tutorials from there and specifically the Notepad example. You will learn a lot.
A good resource with small examples for ListViews is this.
3.Have you tried the changes I told you from the last post? Did it worked?
Since you just started with android and programming you must first be happy if you have the expected result and then read more to make your code better
Your suggested changes (fitnessSpiner.setSelection(5);// or whatever is your selection number) would stop the sound from playing, but defeat the apps purpose. Every time this activity is loaded, the spinners hit preferences to load the previously stored data. So if I force the spinner to a different selection to NOT play sound when the activity loads, then I would be displaying the wrong data for the user.
Yes you are right. So it is better to make a ListActivity. Read developer.android.com and the link i gave you before. You will be ok with this!
You're using "setOnItemSelectedListener", which sounds like when the app starts, its getting "selected" again.
Have you tried using "setOnItemClickListener" instead?
fitnessSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener () {
public void onItemClicked() {}
};
Lakers16 said:
You're using "setOnItemSelectedListener", which sounds like when the app starts, its getting "selected" again.
Have you tried using "setOnItemClickListener" instead?
fitnessSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener () {
public void onItemClicked() {}
};
Click to expand...
Click to collapse
onClickListener doesn't work for the spinner...I wish it did.
I REALLY need the drop down functionality of teh spinner, so I guess I'm going to try and figure out a way to have an invisible edittext that I set to the spinner selection and then use onClickListener or onChange...

Enabling selection in textview

I am using this android:textIsSelectable and still gets the error:
Code:
TextView does not support text selection. Action mode cancelled.
Can any one please tell me what I might be doing wrong?
obscurant1st said:
I am using this android:textIsSelectable and still gets the error:
Code:
TextView does not support text selection. Action mode cancelled.
Can any one please tell me what I might be doing wrong?
Click to expand...
Click to collapse
Please post the code and also the AP version you are testing the app cant say anything without that
I believe testIsSelectable for TextViews was introduced in Honeycomb, if you want to be able to mimic that feature with lower versions of Android, here's a little workaround :
Use an EditText instead of your TextView and set android:textIsSelectable to false
In your activity's java code, override onLongClickListener and set the EditText to selectable in this method, something like this :
Code:
public class yourClass extends Activity implements onLongClickListener {
EditText yourFakeTextView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.whatever);
yourFakeTextView = (EditText) findViewById(R.id.editText1);
yourFakeTextView.setOnLongClickListener(this);
}
@Override
public boolean onLongClick(View v) {
switch (v.getId()) {
case R.id.editText1:
yourFakeTextView.setTextIsSelectable(true);
break;
default:
return;
}
return false;
}
}
This will make the EditText look like a regular TextView, and when the user long-clicks it, it will allow him to select the text (just like if it was a normal, selectable TextView).
Hope this helps.
I want to enable this feature on ICS and upper version of android. I am testing this on an ics device. There is something which is messed up. I will post the code as soon as I reach home!

listView stop responding after back from webview

HI, Guys,
I am a newbie, now i am developing a google rss reader for practicing. I used listview to display all the news titles. and webview to view the contents. Every time when i am back from webview, it shows this error:
Error: WebView.destroy() called while still attached!
I overwritten the onBackPressed() in webview activity like this like suggested on codeoverflow:
 @override
public void onBackPressed() {
super.onBackPressed();
if (mWebView != null) {
WebView w = mWebView;
w.removeAllViews();
w.destroy();
w = null;
mWebView = null;
}
}
but it doesn't work. everytime it still shows the same error.
meanwhile, when i am back from the webview, the pressing the items in the main listview doesn't redirect me to the webview activity anymore.
Thanks a lot if someone can give me guidance on this.
reply
crazybeeliuzhe said:
HI, Guys,
I am a newbie, now i am developing a google rss reader for practicing. I used listview to display all the news titles. and webview to view the contents. Every time when i am back from webview, it shows this error:
Error: WebView.destroy() called while still attached!
I overwritten the onBackPressed() in webview activity like this like suggested on codeoverflow:
@override
public void onBackPressed() {
super.onBackPressed();
if (mWebView != null) {
WebView w = mWebView;
w.removeAllViews();
w.destroy();
w = null;
mWebView = null;
}
}
but it doesn't work. everytime it still shows the same error.
meanwhile, when i am back from the webview, the pressing the items in the main listview doesn't redirect me to the webview activity anymore.
Thanks a lot if someone can give me guidance on this.
Click to expand...
Click to collapse
the error i see after scroll is :
at android.support.v4.view.ViewPager.onPageScrolled(ViewPager.java:1708)
at android.support.v4.view.ViewPager.pageScrolled(ViewPager.java:1646)
anyone has similar experience?
I don't know whether the destroy method is needed. However, I recommend to put that code into the onDestroy() method instead of the onBackPressed() method. Might as well solve the problem.
the same problem
nikwen said:
I don't know whether the destroy method is needed. However, I recommend to put that code into the onDestroy() method instead of the onBackPressed() method. Might as well solve the problem.
Click to expand...
Click to collapse
Thanks Nikwen, but it is still the same after i overwritten onDestroy():
 @override
protected void onDestroy() {
super.onDestroy();
if (mWebView != null) {
WebView w = mWebView;
w.removeAllViews();
w.destroy();
w = null;
mWebView = null;
}
int myPid = android.os.Process.myPid();
Log.d(fTag, "exit.myPid = " + myPid);
android.os.Process.killProcess(myPid);
}
seems the problem doesn't come form there.
crazybeeliuzhe said:
Thanks Nikwen, but it is still the same after i overwritten onDestroy():
@override
protected void onDestroy() {
super.onDestroy();
if (mWebView != null) {
WebView w = mWebView;
w.removeAllViews();
w.destroy();
w = null;
mWebView = null;
}
int myPid = android.os.Process.myPid();
Log.d(fTag, "exit.myPid = " + myPid);
android.os.Process.killProcess(myPid);
}
seems the problem doesn't come form there.
Click to expand...
Click to collapse
My first reaction is the same as nikwen's - do you need to do this at all ?
If you do, then it sounds as though the error is arising because your WebView is still attached to something at the point you are attempting to destroy it. So before you can destroy it you'll need to call removeView() on whatever the parent is (probably a layout, I guess).
thanks i will try
PicomatStudios said:
My first reaction is the same as nikwen's - do you need to do this at all ?
If you do, then it sounds as though the error is arising because your WebView is still attached to something at the point you are attempting to destroy it. So before you can destroy it you'll need to call removeView() on whatever the parent is (probably a layout, I guess).
Click to expand...
Click to collapse
Thanks. Then i think i should also call the removeView() before i try to destroy it. I will keep trying and let you guys know my update.
app uploaded
crazybeeliuzhe said:
Thanks. Then i think i should also call the removeView() before i try to destroy it. I will keep trying and let you guys know my update.
Click to expand...
Click to collapse
i have finished my toy app. this is a Google RSS news reader. A good practice for me. You can now download it from google play by searching my name crazybeeliuzhe. you will see two app from me. one is the ipcam viewer i made the other is the news reader.
I will upload my code later to source forge.
your comments are always welcome.

Dynamically refresh ListView with CursorAdapter

Hi everyone,
First of all, I'd like to apologize if I'm posting this in a wrong place... This is my first post, I'm still getting familiar with this forum.
I would also like to apologize for the lengthy post.
The title, to a certain extent, reflects what my problem is, but to clarify:
I'm learning Android, so I'm making a simple SMS app for practice.
In it, I have a database which has tables for sent messages, received messages and contacts. I have 3 separate activities (which do NOT extend ListActivity) which show lists for sent messages, received messages and contacts, respectively. The lists are populated through CursorAdapter.
Let's consider the activity for contacts...
In it I have list (ListView) which displays contacts (each list element displays name and phone number). Below the list I have a "Add Contact" button. When I click the button a dialog pops up and shows the form for adding new contact. The buttons in the dialog preform all the database operations.
Similarly, when I click some item in the ListView, another dialog pops up. That dialog has buttons for "Send SMS", "Edit" and "Delete" contact. Again, the buttons do all the work with the database.
The trouble:
My trouble is... When I add a new contact, or delete one (after both operations their dialogs dismiss), the ListView is not refreshed.
In order to see the refreshed list I need to close the activity and start it again.
I googled and googled this for 3 days now, and all the answers I found say that I need to call
Code:
adapter.notifyDataSetChanged
and
Code:
adapter.changeCursor
but that doesn't do the trick.
I'll now post the relevant code with the two methods mentioned above. I placed them where i thought they should be, but this doesn't work.
So, I humbly beg someone to guide me through this ordeal.
Many thanks in advanced!
Here comes the code:
The Adapter:
Code:
public class AdapterContactListView extends CursorAdapter {
private MyDatabaseHelper mdbh;
private LayoutInflater myLayoutInflater;
public AdapterContactListView(Context context, Cursor c, int flags) {
super(context, c, flags);
mdbh = MyDatabaseHelper.getMyDatabaseHelper(context);
myLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView fullNameTV = (TextView)view.findViewById(R.id.contactElementNameTV);
TextView phoneNumberTV = (TextView)view.findViewById(R.id.contactElementNumberTV);
String fullName = cursor.getString(cursor.getColumnIndex(mdbh.getContactFirstName()));
fullName = fullName.concat(" ");
fullName = fullName.concat(cursor.getString(cursor.getColumnIndex(mdbh.getContactLastName())));
String phoneNumber = cursor.getString(cursor.getColumnIndex(mdbh.getContactPhoneNumber()));
fullNameTV.setText(fullName);
phoneNumberTV.setText(phoneNumber);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
return myLayoutInflater.inflate(R.layout.contact_element, parent, false);
}
}
And the Activity:
Code:
public class ContactsActivity extends Activity {
private MyUtilities myUtilities;
private MyDatabaseHelper mdbh;
private AdapterContactListView contactsAdapter;
private ListView contactsListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
mdbh = MyDatabaseHelper.getMyDatabaseHelper(this);
myUtilities = new MyUtilities(this);
contactsAdapter = new AdapterContactListView(this,mdbh.getContactsCursor(),CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
contactsListView = (ListView)findViewById(R.id.contactActivityLV);
contactsListView.setAdapter(contactsAdapter);
contactsListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
TextView phoneNumberTV = (TextView)view.findViewById(R.id.contactElementNumberTV);
String phoneNumber = phoneNumberTV.getText().toString();
Contact contact = mdbh.getContactFromPhoneNumber(phoneNumber);
Dialog d = myUtilities.createSelectedContactOptionsDialog(contact);
d.show();
contactsAdapter.changeCursor(mdbh.getContactsCursor());
contactsAdapter.notifyDataSetChanged();
}
});
}
public void addContact(View view) {
Dialog d = myUtilities.createAddContactDialog();
d.show();
contactsAdapter.changeCursor(mdbh.getContactsCursor());
contactsAdapter.notifyDataSetChanged();
}
}
djolec987 said:
Hi everyone,
Click to expand...
Click to collapse
Well notifyDatasetChanged only informs the adapter that the backing interface has new data... but your backing it with a cursor... so in your case it would just cause getView/bindView to be called for all current visible items, thus fire a query at the cursor. I think the cursor will cache the data so it's really a reload on the cursor data and then an adapter notify call you want... If you use loader (depending on target api version) then it should do most of this for you. As it stands if you want to do it manually make sure the cursor is a new cursor of the database that has changed.
(typed in a rush)

Categories

Resources