I Have a Alert like this
new AlertDialog.Builder(context)
.setTitle("Exit Confirmation")
.setMessage("Are you sure you want to Exit?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.show();
I want to change Button text of this alert.
Suppose android.R.string.yes is "I Do" and android.R.string.no is "I Don't"
Now I want to set comic.tft to it.
Please help...
For the font, after alert.show(), you can use:
Button btnPositive = alert.getButton(Dialog.BUTTON_POSITIVE);
btnPositive.setTypeFace(myTypeFace);
You can use something like this:
getResources().getString(R.string.about)
in string.xml
<string name="about">I do</string>
<string name="aboutno">I don't</string>
Code:
new AlertDialog.Builder(context)
.setTitle("Exit Confirmation")
.setMessage("Are you sure you want to Exit?")
.setPositiveButton(getResources().getString(R.string.about), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(getResources().getString(R.string.aboutno), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.show();
Related
In Android Studio I have made a TextView showing "dailyQuote1" when i press/tapping the TextView.
I also added a button with id "buttonquote2", when I press that button once I want that the "action" of the TextView to start showing "displayQuote2" instead of "displayQuote1" when press/tapping TextView.
How should the code for the button look like to do this?
TextView "action":
TextView mTextView = (TextView)findViewById(R.id.textViewNextQuote);
mTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
displayQuote1(); //I want this to change
}
});
for this What you can do is implement
View.OnClickListener with the class or activity you are working with and then check for the id of the view which is being clicked/tapped and choose the method according to the id. Like this
class MainActivity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView=findViewById....
Button button=findViewById...
textView.setOnClickListener(this)
button.setOnclickListener(this)
}
override onClick(View view) {
switch(view.getId())
{
case textView.getId():
displayQuote1();
case button.getId():
displayQuote2();
}
}
}
Try this:
public void buttonClick(View v)
{
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(displayQuote2());
}
I have created an android application with simple webview function. In my html page which webview loads, I am using following script.
var message="!!YOU CANNOT COPY ANY TEXT OR IMAGE FROM THIS APP!";
function clickIE4()
{
if (event.button==2)
{
alert(message);
return false;
}
}
function clickNS4(e)
{
if (document.layers||document.getElementById&&!document.all)
{
if (e.which==2||e.which==3)
{
alert(message);
return false;
}
}
}
if (document.layers)
{
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById)
{
document.onmousedown=clickIE4;
}
document.oncontextmenu=new Function("alert(message);return false")
//for disable select option
//document.onselectstart = new Function("alert(message);return false");
When I open this in android webview application, it does not show the alert message. It works fine when I open it in normal browser.
You need to create WebChromeClient and set to WebiView. Also override onJsAlert method.
WebChromeClient webChromeClient = new WebChromeClient() {
#Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
new AlertDialog.Builder(context)
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
})
.create()
.show();
return true;
}
}
webView.setWebChromeClient(webChromeClient);
i want to make toggle button that can play background sound service. here is my code.
public class MainActivity extends Activity {
private Switch mySwitch;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void playRain(View view) {
// Is the toggle on?
boolean on = ((Switch) view).isChecked();
if (on) {
Intent objIntent = new Intent(this, PlayRain.class);
startService(objIntent);
} else {
Intent objIntent = new Intent(this, PlayRain.class);
stopService(objIntent);
}
}
}
when i switch the toggle button to on, the sound play. but when i hit the home button and re open the app the toggle button go back to off. how to save changes to toggle button? so that the toggle button does not go back to off state by itself. sorry for my english.
You might need to save the state of the toggle button in SharedPreferences.
This might help.
i have written a sample code. I hope it helps
public class MainActivity1 extends Activity {
private Switch mySwitch;
SharedPreferences pref;
Editor edit;
final String SOUND_PREF_KEY = "background_sound";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySwitch = (Switch) findViewById(R.id.switch1);
pref = getPreferences(MODE_PRIVATE);
edit = pref.edit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mySwitch.setChecked(pref.getBoolean(SOUND_PREF_KEY, false));
}
#Override
protected void onStop() {
super.onStop();
if (mySwitch.isChecked()) {
edit.putBoolean(SOUND_PREF_KEY, true);
} else {
edit.putBoolean(SOUND_PREF_KEY, false);
}
edit.commit();
}
public void playRain(View view) {
// Is the toggle on?
boolean on = ((Switch) view).isChecked();
if (on) {
Intent objIntent = new Intent(this, PlayRain.class);
startService(objIntent);
} else {
Intent objIntent = new Intent(this, PlayRain.class);
stopService(objIntent);
}
}
}
I have to call a javascript method on OK clicked from an alert created in java plugin. Actually the alert is displayed on click of the notification from the status bar.
// Method to collect info from pending intent on notification click
public void launchNotification()
{
Intent intent = getIntent();
if(intent!=null)
{
if(intent.getExtras().getBoolean("fromNotification"))
{
final String notificationType = intent.getStringExtra("notificationType");
final String twoWeeksNotificationType = intent.getStringExtra("2WeeksNotificationType");
final String tasksNotificationType = intent.getStringExtra("tasksNotificationType");
if(notificationType != null || twoWeeksNotificationType != null || tasksNotificationType != null)
{
if(notificationType != null && notificationType.equals("dailyReviewAvailable"))
{
NotificationManager nMgr = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancel("DailyReview", 0);
Handler handler = new Handler();
handler.postDelayed(
new Runnable() {
public void run() {
showAlertForNotifications(notificationType);
}
}, 1000L);
}
intent.putExtra("fromNotification",false);
}
else
finish();
}
}
// Method for launching alert after notification is clickedpublic void
showAlertForNotifications(String notificationIdStr) {
if(notificationIdStr.equals("dailyReviewAvailable")) {
Log.d("Notification", "Daily Review Start");
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Actions Hero");
adb.setMessage("Would you like to send your daily review?");
adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
showDailyReview();
}
});
adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
// Action for 'Cancel' Button
dialog.cancel();
finish();
}
});
//adb.setIcon(R.drawable.icon);
adb.show();
}
else {
//do later
}
}
// Method to call javascript function on OK click of alert button in java
public void showDailyReview() {
Log.v("showDailyReview", "Before");
// This javascript function cannot be invoked super.sendJavascript("javascript:app.runningDailyReview('DailyReview')");
Log.v("showDailyReview", "After");
finish();
}
How should I invoke the javascript method on clicking OK on the alert shown ??
Thanks..
I have a webView in an Activity. Inside, the webView I have a button that opens a new HTML page. When this button is pressed and the new html page opens I would like the screen orientation to change to horizontal.
var searchButton = $('<button/>',
{
text:'Search!',
"class":"buttons",
id: "searchButtonID",
click: function(){
var select = document.getElementById("subCategory");
var option = select.options[select.selectedIndex];
var subCategoryId = option.id;
window.location.href = "deals.html?subCategoryId=" + subCategoryId;
Android.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
});
I recently learnt about WebAppInterfaces that allows Javascript to interact with Android. I tried adding the line:
Android.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
However, I get an error message saying:
Uncaught ReferenceError: ActivityInfo is not defined
I imported import android.content.pm.ActivityInfo; in the relevant class.
Not sure if what I'm doing is even possible...
Any help would be greatly appreciated!
private void rotate(float degree) {
final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(0);
rotateAnim.setFillAfter(true);
oWebView.startAnimation(rotateAnim);
}
btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
rotate(90);
}
});
try this
Do something like this
boolean rotationRequired = false;
private void openWebviewDialog(String days)
{
dialog = new Dialog(MainActivity.GLOBAL_CONTEXT);
dialog.setContentView(R.layout.simple_webview);
WebView wb = (WebView) dialog.findViewById(R.id.simple_webview);
WebSettings webSettings = wb.getSettings();
wb.setWebViewClient(new MyWebViewClient());
webSettings.setJavaScriptEnabled(true);
wb.addJavascriptInterface(this, "javaScriptInterface");
String url = "http://www.yourdomain.com" // your url here
String title = "Hello";
wb.loadUrl(url);
dialog.setCancelable(true);
dialog.setTitle(title);
dialog.show();
}
class MyWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.contains("openExternal"))
{
return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
}
else
{
view.loadUrl(url); // Stay within this webview and load url
return true;
}
}
#Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
if(rotationRequired)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
}
}
#JavascriptInterface
public void passResult(String status)
{
rotationRequired = status.equalsIgnoreCase("true") ? true : false;
}
and don't forget to add this to your manifest:
android:configChanges = "orientation"
control the rotation as
<p>rotate...</p><script type='text/javascript' language='javascript'>javaScriptInterface.passResult('true');</script>
Please define and import packages
WebView myWebView;
WebAppInterface mWebAppInterface;
add this code to onCreate() of your activity
mWebAppInterface = new WebAppInterface(this);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.addJavascriptInterface(mWebAppInterface, "Android1");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//load your URL by myWebView.loadUrl("www.example.com");
Add this public function also
#JavascriptInterface
public void setorientation(){
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
Now you can call this public function for setting orientation from javascript button click function
Android1.setorientation();
visit also
http://developer.android.com/guide/webapps/webview.html
using javascript in android webview