Notification icon does not load drawable due locale - android-notifications

I have created a simple Android app (https://github.com/soleimani/android-notification-locale) to notify a text, it has two button one loads default locale text and icon, other one loads "fa" locale. Image, notification title and notification text all respect locale, but notification icon is from default locale ("en")
Button en = (Button) findViewById(R.id.buttonEN);
en.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
conf.locale = new Locale("en");
res.updateConfiguration(conf, dm);
NotificationCompat.Builder overdueNotification = new NotificationCompat.Builder(getApplicationContext())
.setContentText(getText(R.string.notification_service_content_text))
.setSmallIcon(R.drawable.ic_stat);
overdueNotification.setContentTitle(getText(R.string.notification_service_content_title));
notificationManager.notify(1, overdueNotification.build());
image.setImageResource(R.drawable.ic_stat);
image.setImageResource(R.drawable.ic_launcher);
}
});
Button fa = (Button) findViewById(R.id.buttonFA);
fa.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
conf.locale = new Locale("fa");
res.updateConfiguration(conf, dm);
NotificationCompat.Builder overdueNotification = new NotificationCompat.Builder(getApplicationContext())
.setContentText(getText(R.string.notification_service_content_text))
.setSmallIcon(R.drawable.ic_stat);
overdueNotification.setContentTitle(getText(R.string.notification_service_content_title));
notificationManager.notify(2, overdueNotification.build());
image.setImageResource(R.drawable.ic_stat);
image.setImageResource(R.drawable.ic_launcher);
}
});

Related

Can you set icon to a Javafx Alert box? [duplicate]

I might be missing something very obvious, but I can't find out how to set the Icon for a Dialog component (ProgressDialog to be more precise). I know how to do that for a Stage:
this.primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/icon/Logo.png")));
But I don't find anything for the Dialog family. And somehow, setting the Stage Icon does not influence the Dialog Icon.
Thanks
There's an excellent tutorial here by Marco Jakob, where you can find not only how to use dialogs, but also how to solve your problem.
Both for the new dialogs (in JDK8u40 early versions or with openjfx-dialogs with JDK 8u25), or for those in ControlsFX, in order to set the icon of your dialog, you can use this solution:
Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
stage.getIcons().add(
new Image(this.getClass().getResource("<image>.png").toString()));
This code snippet shows how to use a ProgressDialog, from ControlsFX, and set an icon for the dialog:
#Override
public void start(Stage primaryStage) {
Service<Void> service = new Service<Void>() {
#Override protected Task<Void> createTask() {
return new Task<Void>() {
#Override protected Void call() throws InterruptedException {
updateMessage("Message . . .");
updateProgress(0, 10);
for (int i = 0; i < 10; i++) {
Thread.sleep(300);
updateProgress(i + 1, 10);
updateMessage("Progress " + (i + 1) + " of 10");
}
updateMessage("End task");
return null;
}
};
}
};
Button btn = new Button("Start Service");
btn.setOnAction(e -> {
ProgressDialog dialog = new ProgressDialog(service);
dialog.setTitle("Progress Dialog");
dialog.setHeaderText("Header message");
Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image(this.getClass().getResource("<image>.png").toString()));
service.start();
});
Scene scene = new Scene(new StackPane(btn), 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
Just Do like this:
Alert(AlertType.ERROR, "Erreur de connexion! Verifiez vos Identifiants",FINISH); //Cancel..
setTitle("XNotes FX Erreur");
stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image("indiza/XnotesErrorIdz.png")); // To add an icon
showAndWait();
Here is the result
**My friends, is it computer science that we do? : No, we do crafts
**
You can easily use the icon of your application for the alert-icon by setting your application-window as owner of the alert box:
#FXML
Button buShow;
...
Alert alert = new Alert(AlertType.INFORMATION, "Nice Box.", ButtonType.CLOSE);
alert.initOwner(buShow.getScene().getWindow()); // Alert uses the Windows Icon
alert.show();
This is a method that I include in my JavaFX projects, simply calling this method and passing the Alert as a parameter will set both the title bar icon and the header graphic.
public class Msg {
public void showInfo(String title, String header, String message) {
Alert alertShowInfo = new Alert(Alert.AlertType.INFORMATION);
addDialogIconTo(alertShowInfo); //add icon and header graphic
alertShowInfo.setTitle(title);
alertShowInfo.setHeaderText(header);
alertShowInfo.setContentText(message);
alertShowInfo.showAndWait();
}
//this adds images to Alert
public void addDialogIconTo(Alert alert) {
// Add custom Image to Dialog's title bar
final Image APPLICATION_ICON = new Image("icon.png");
Stage dialogStage = (Stage) alert.getDialogPane().getScene().getWindow();
dialogStage.getIcons().add(APPLICATION_ICON);
// Add custom ImageView to Dialog's header pane.
final ImageView DIALOG_HEADER_ICON = new ImageView("icon.png");
DIALOG_HEADER_ICON.setFitHeight(48); // Set size to API recommendation.
DIALOG_HEADER_ICON.setFitWidth(48);
alert.getDialogPane().setGraphic(DIALOG_HEADER_ICON);
}
}
Then, in whatever class I wish to use the Alert, it will already have the customized icon and header graphic.
public static void main(String[] args){
Msg msg = new Msg();
// Alert will now include custom icon and header graphic.
msg.showInfo("Sucess!", "Program succeeded", "Now exiting program");
}
Just similar to any dialog, instead this is inside a button handler.
Alert alert = new Alert(
AlertType.WARNING,
"Alert message here.",
ButtonType.OK
);
alert.initOwner(((Button)event.getSource()).getScene().getWindow());
alert.setTitle("Alert window title");
alert.showAndWait();

dynamically added views inside fragment disappears on changing orientation

I am new to android application development. I have a fragment (layout of which contains one vertical linear layout). I am adding some views dynamically in the linear layout. When i change orientation to landscape from portrait the layout of fragment is recreated and all dynamically added views disappear. How can i prevent the fragment's layout from recreating?
EDIT: the code below is inside fragment. clicking on a button(say add button) dynamically adds a view in vertical linear layout but changing orientation these views disappear.
buttonAdd.setOnClickListener(new OnClickListener() {
#SuppressWarnings("unchecked")
#Override
public void onClick(View arg0) {
methodForCheckEmail(textIn);
if(textIn.getText().toString().length() == 0) {
Toast.makeText(getActivity(), "please enter valid email", Toast.LENGTH_LONG).show();
} else if (textIn.getText().toString().length() > 0 && checkEmail == false) {
Toast.makeText(getActivity(), "please enter valid email", Toast.LENGTH_LONG).show();
} else {
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row2, null);
final TextView textOut = (TextView) addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
Button buttonRemove = (Button) addView.findViewById(R.id.remove);
arrayList.add(textIn.getText().toString());
buttonRemove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((LinearLayout) addView.getParent()).removeView(addView);
// textIn.setText("");
arrayList.remove(textOut.getText().toString());
}
});
textIn.setText("");
container.addView(addView);
}
//textIn.setText("");
}
});
If you change oriantation the activity will be recreated so you have to add the fragment again.
Please add code if you ask a question. You will get more help.
Use the activity lifecycle methods:
http://developer.android.com/training/basics/activity-lifecycle/index.html
Read this post:
Activity restart on rotation Android

WebEngine not loading URL on button click

I am coding a Tabbed web browser in JAVAFx. The problem i am facing is :-
When I click on Home Button (HomeB) it is not loading the DEFAULT_URL in the current tab. Here is some useful part of my code. Some body please fix it. Thanks
class Browser extends Region{
final BorderPane borderPane;
final TabPane tabPane;
private final HBox toolbarMain;
WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
private String DEFAULT_URL= "http://www.google.com";
final TextField urlField = new TextField(DEFAULT_URL);
//Custom function for creation of New Tabs.
private Tab createAndSelectNewTab(final TabPane tabPane, final String title) {
Tab tab = new Tab(title);
webEngine.locationProperty().addListener(new ChangeListener<String>() {
#Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
urlField.setText(newValue);
}
});
final ObservableList<Tab> tabs = tabPane.getTabs();
tab.closableProperty().bind(Bindings.size(tabs).greaterThan(2));
tabs.add(tabs.size() - 1, tab);
tabPane.getSelectionModel().select(tab);
return tab;
}
//Initialization function of the program.
public Browser() {
borderPane = new BorderPane();
tabPane = new TabPane();
toolbarMain = new HBox();
Button HomeB = new Button();
HomeB.setText("HOME");
tabPane.setSide(Side.TOP);
final Tab newtab = new Tab();
newtab.setText("+");
newtab.setClosable(false); // this will not let the New Tab button(TAB) close
tabPane.getTabs().addAll(newtab); //Addition of New Tab to the tabpane.
createAndSelectNewTab(tabPane, " ");
//Function to add and display new tabs with default URL display.
tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {
#Override
public void changed(ObservableValue<? extends Tab> observable,
Tab oldSelectedTab, Tab newSelectedTab) {
if (newSelectedTab == newtab) {
Tab tab = new Tab();
//WebView - to display, browse web pages.
WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
webEngine.load(DEFAULT_URL);
EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent event) {
webEngine.load(urlField.getText().startsWith("http://")
? urlField.getText()
: "http://" + urlField.getText());
}
};
urlField.setOnAction(goAction);
final VBox vBox = new VBox(5);
vBox.getChildren().setAll(browser);
VBox.setVgrow(browser, Priority.ALWAYS);
tab.setContent(vBox);
final ObservableList<Tab> tabs = tabPane.getTabs();
tab.closableProperty().bind(Bindings.size(tabs).greaterThan(2));
tabs.add(tabs.size() - 1, tab);
tabPane.getSelectionModel().select(tab);
}
}
});
//OnClick handling HomeB
HomeB.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event){
webEngine.load(DEFAULT_URL);
}
});
toolbarMain.getChildren().addAll(HomeB,urlField);
//Placement of elements in borderpane
borderPane.setCenter(tabPane);
borderPane.setTop(toolbarMain);
getChildren().add(borderPane);
}
}
When you click on the HomeB the default URL is loaded into the browser, a global WebView. That works, but you don't see the URL loaded, because you haven't added this browser to any of your tabs.
Assuming you create the first tab for the home button:
tabPane.getTabs().addAll(newtab); // tab 0, then moves to 1
// Here you create a new tab, but put it on the 0 index:
createAndSelectNewTab(tabPane, " ");
// You can add now your global browser to the first tab:
final VBox vBoxIni = new VBox(5);
vBoxIni.getChildren().setAll(browser);
VBox.setVgrow(browser, Priority.ALWAYS);
tabPane.getTabs().get(0).setContent(vBoxIni);
Other option will be using the local webView you have created for each tab, and load the default URL on the active tab:
HomeB.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event){
VBox vBox=(VBox)tabPane.getSelectionModel().getSelectedItem().getContent();
if(vBox!=null){
WebView webView=(WebView)vBox.getChildren().get(0);
webView.getEngine().load(DEFAULT_URL);
}
}
});
Note this won't work on the first tab, since you haven't set any content there.

How to use Simple dialog in fragment?

In my application i have created a simple custom dialog. I have to call that in a fragment class.Dont know how to proceed can anyone help me with this? Is there any way that we can use simple dialog in fragment without going for Dialogfragment?
using dialog fragment would be the better practice.
Dialog Fragment They have given different dialog fragment concepts will match all the needs related to dialog. other than this
Simple alert dialog in fragment
AlertDialog ad = new AlertDialog.Builder(getActivity())
.create();
ad.setCancelable(false);
ad.setTitle(title);
ad.setMessage(message);
ad.setButton(context.getString(R.string.ok_text), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
custom dialog in fragment
final Dialog dialog = new Dialog(getActivity);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
UPDATE 1 :
Here i give u one example that i used to show date picker using dialog fragment
public static class SelectDateFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
#Override
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
dobString = myYear + "-" + myMonth + "-" + myDay;
populateSetDate(yy, mm + 1, dd);
}
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
}
}
And to show the dialog
public void selectDate(View view) {
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getSupportFragmentManager(), "DatePicker");
}
which will be called from a button click..

Using two Buttons on one Activity

I'm still new to android. I have a kids book that l have done with two buttons "READ TO ME" and "READ TO MYSELF".
Read to me onclick will play a recording as it reads the book but it goes to SoundOne Activity.
Read to myself will display text and it leads to PageOne Activity.
How do l create some sort of an "if statement" such that when l click read to me it will play the recording but lead to PageOne Activity. And when l click read to myself it will display the text but still leads to PageOne Activity
This might help reduce the number of classes l have created so far and avoid ANR l assume. Some source code or a tutorial will help Thanks in advance my code is below:
package com.inerds.donkiejoukie;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.content.Context;
public class Fbone extends MainActivity {
MediaPlayer one;
MediaPlayer mb;
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fbone);
one = MediaPlayer.create(this, R.raw.pageone);
mb = MediaPlayer.create(this, R.raw.menubar);
mp = MediaPlayer.create(this, R.raw.pageflip);
ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
imageButton1.setOnClickListener (new OnClickListener() {
public void onClick(View vone) {
mb.pause();
mb.stop();
mp.start();
startActivity(new Intent(getApplicationContext(), PageOne.class));
finish();
}
});
ImageButton readtome = (ImageButton) findViewById(R.id.readtome);
readtome.setOnClickListener (new OnClickListener() {
public void onClick(View v) {
mb.pause();
mb.stop();
one.start();
startActivity(new Intent(getApplicationContext(), SoundOne.class));
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0);
finish();
}
});
}
}
The Intent mechanism is used to communicate from one Activity to another. In the Intent, as well as a class to start, you can specify the name of an action to perform, as well as other options. In this case, you might want to use an extra to tell the following class whether to play sound or not. When you start the intent:
Intent nextActivity = new Intent(getApplicationContext(), SoundOne.class);
// Put true or false in the next line according to which click handler you're in.
nextActivity.putBooleanExtra(SoundOne.extra_enableSound, true);
startActivity(nextActivity);
and then in your SoundOne activity, you need a constant field for the extra name:
static final String extra_enableSound = "enableSound";
and you can find the value of this extra from your onCreate, or wherever you want to start the sound:
if (getIntent().getBooleanExtra(extra_enableSound, false)) {
// start the sound
}
Now your PageOne activity is unused and can be deleted. The documentation for Intent tells you what other information you can stash in there.
I'm not a professional programmer so this might not be the best way to do this, but it works well for me with minimal code.
Here's what I'm doing.
Set an "onClick" for the buttons in my XML
<Button
android:id="#+id/button2"
android:layout_width="144dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/button1"
android:onClick="sendMessage1"
android:text="#string/cancel"
/>
the in the activity i set up my buttons
Button button1;
Button button2;
then use a sendmessage instead of onButtonclick
public void sendMessage(View view)
{
Intent intent = new Intent(this, ThirdActivity.class);
startActivity(intent);
}
public void sendMessage1(View view)
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
package com.inerds.donkiejoukie;
public class Fbone extends MainActivity {
MediaPlayer one;
MediaPlayer mb;
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fbone);
one = MediaPlayer.create(this, R.raw.pageone);
mb = MediaPlayer.create(this, R.raw.menubar);
mp = MediaPlayer.create(this, R.raw.pageflip);
ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
ImageButton readtome = (ImageButton) findViewById(R.id.readtome);
imageButton1.setOnClickListener (handleOnClickListener);
readtome.setOnClickListener (handleOnClickListener);
}
private OnClickListener handleOnClickListener = new OnClickListener() {
public void onClick(View v) {
switch(v.getId()){
case R.id.imageButton1:
mb.pause();
mb.stop();
mp.start();
startActivity(new Intent(getApplicationContext(), PageOne.class));
finish();
break;
case R.id.readtome:
mb.pause();
mb.stop();
one.start();
startActivity(new Intent(getApplicationContext(), SoundOne.class));
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0);
finish();
break;
}
}
};
}

Resources