In my application I want to use service and in this service I should show layout.
I want when click on button show dialog, I write below codes!
But after click on button, show this dialog back of layout and not show any dialog!
My service codes :
public class FloatingLayoutService extends Service implements StepperFormListener {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
//Inflate the layout using LayoutInflater
layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
floatingLayout = (ConstraintLayout) layoutInflater.inflate(R.layout.service_floating_layout, null);
floatingLay_root = floatingLayout.findViewById(R.id.floatingLay_root);
floatingLay_main = floatingLayout.findViewById(R.id.floatingLay_main);
floatingLay_emptyImg = floatingLayout.findViewById(R.id.floatingLay_emptyImg);
...
//Set layout params to display the controls over any screen.
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
LAYOUT_FLAG,
0,
PixelFormat.TRANSLUCENT);
// From API26, TYPE_PHONE deprecated. Use TYPE_APPLICATION_OVERLAY for O
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
params.type = WindowManager.LayoutParams.TYPE_PHONE;
//Initial position of the floating controls
params.gravity = Gravity.BOTTOM | Gravity.START;
params.x = 0;
params.y = 0;
//Add the controls view to windowManager
windowManager.addView(floatingLayout, params);
//Task page btn
floatingLay_infoContentNextPage.setOnClickListener(v -> {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Are you sure?")
.create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
});
return START_STICKY;
}
How can I show dialog, front of layout ?
You can make an activity without a layout just to show the Alert dialog.
public class MyAlertDialog extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Message")
...
...
.show()
}
}
Remember to finish the activity.
You can set the title and message dynamically by passing them through intent.
Service
Intent intent = new Intent(this, MyAlertDialog.class);
intent.putExtra("titleKey", "title");
intent.putExtra("messageKey", "messge");
startActivity(intent);
And inside the activity, read it as:
getIntent().getStringExtra("titleKey")
getIntent().getStringExtra("messageKey")
Related
Right now, I'm using the following permission to show a dialog over another app :
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Dialog :
dialogBuilder = new AlertDialog.Builder(activity);
LayoutInflater inflater2 = (LayoutInflater) AppController.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = inflater2.inflate(R.layout.dialog_view, null);
dialogBuilder.setView(dialogView);
dialog = dialogBuilder.create();
dialog.setCancelable(true);
final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
final Window dialogWindow = dialog.getWindow();
if (dialogWindow != null) {
dialogWindow.setAttributes(layoutParams);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
dialogWindow.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
} else {
dialogWindow.setType(WindowManager.LayoutParams.TYPE_PHONE);
}
}
dialog.show();
Snaptube app, shows a dialog over the YouTube app without asking for any permission :
How I can achieve that?
I am using Google maps API V2 to populate a map with a list of places, whenever the user clicks a marker view it should take him to a new activity with more details. however I am having problems with implementing that
here my
MapsActivity.java
#Override
public View getInfoWindow(Marker marker) {
return null;
//return prepareInfoView(marker);
}
#Override
public View getInfoContents(Marker marker) {
//return null;
return prepareInfoView(marker);
}
private View prepareInfoView(Marker marker){
//prepare InfoView programmatically
Restaurants restaurants = places.get(marker);
Log.d(TAG,"restaurants1"+ restaurants.getName());
LinearLayout infoView = new LinearLayout(MapsActivity.this);
LinearLayout.LayoutParams infoViewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
infoView.setOrientation(LinearLayout.HORIZONTAL);
infoView.setLayoutParams(infoViewParams);
ImageView infoImageView = new ImageView(MapsActivity.this);
//Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
Drawable drawable = getResources().getDrawable(android.R.drawable.ic_dialog_map);
infoImageView.setImageDrawable(drawable);
infoView.addView(infoImageView);
LinearLayout subInfoView = new LinearLayout(MapsActivity.this);
LinearLayout.LayoutParams subInfoViewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
subInfoView.setOrientation(LinearLayout.VERTICAL);
subInfoView.setLayoutParams(subInfoViewParams);
TextView subInfoName = new TextView(MapsActivity.this);
subInfoName.setText("Name: " +restaurants.getName());
TextView subInfoCost = new TextView(MapsActivity.this);
subInfoCost.setText("Cost: " + restaurants.getCost() +"€");
TextView subInfoCuisine = new TextView(MapsActivity.this);
subInfoCuisine.setText("Cuisine: " +restaurants.getMenuType());
TextView subInfoPhone = new TextView(MapsActivity.this);
subInfoPhone.setText("Phone: " +restaurants.getPhone());
subInfoView.addView(subInfoName);
subInfoView.addView(subInfoPhone);
subInfoView.addView(subInfoCost);
subInfoView.addView(subInfoCuisine);
infoView.addView(subInfoView);
Intent intent = new Intent(MapsActivity.this, RestaurantDetailsActivity.class);
Log.d(TAG, "lat" + restaurants.getLat());
Log.d(TAG, "lng" + restaurants.getLng());
intent.putExtra("id", restaurants.getId());
intent.putExtra("name", restaurants.getName());
intent.putExtra("phone", restaurants.getPhone());
intent.putExtra("lat", restaurants.getLat());
intent.putExtra("lng", restaurants.getLng());
intent.putExtra("address1", restaurants.getAddress1());
intent.putExtra("address2", restaurants.getAddress2());
intent.putExtra("offer", restaurants.getOffer());
intent.putExtra("menu_type", restaurants.getMenuType());
intent.putExtra("phone", restaurants.getPhone());
intent.putExtra("rate", restaurants.getRate());
intent.putExtra("cost", restaurants.getCost());
intent.putExtra("delivery", restaurants.getHasDelivery());
intent.putExtra("parking", restaurants.getHasParking());
intent.putExtra("bar", restaurants.getHasBar());
intent.putExtra("card", restaurants.getHasCards());
intent.putExtra("terrace", restaurants.getHasTerrace());
intent.putExtra("reserve", restaurants.getHasReservation());
intent.putExtra("wifi", restaurants.getHasWifi());
infoView.setOnClickListener(this);
return infoView;
}
#Override
public void onClick(View infoView){
//passing restaurants data to an intent.
infoView.getContext().startActivity(intent);
}
}
the error I am getting is "cannot resolve intent" and when I pass "intent" to the onClick method I am getting an error says"cannot override its superclass.
You can use a setOnInfoWindowClickListener() and put your code in onInfoWindowClick() to start a new activity
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
Restaurants restaurants = places.get(marker);
Log.d(TAG,"restaurants1"+ restaurants.getName());
Intent intent = new Intent(MapsActivity.this, RestaurantDetailsActivity.class);
Log.d(TAG, "lat" + restaurants.getLat());
Log.d(TAG, "lng" + restaurants.getLng());
intent.putExtra("id", restaurants.getId());
intent.putExtra("name", restaurants.getName());
startActivity(intent);
}
});
Intent is local object that's why it does not found. Make it global.
Intent intent = new Intent(MapsActivity.this, RestaurantDetailsActivity.class);
#Override
public void onClick(View infoView){
//passing restaurants data to an intent.
infoView.getContext().startActivity(intent);
}
And also remove below line from prepareInfoWindow() method:
Intent intent = new Intent(MapsActivity.this, RestaurantDetailsActivity.class);
I have 3 buttons and a toggle in my activity. When toggled, the buttons should open the camera to capture an image or prompt to select one from gallery and 'save' it. When not-toggled, the buttons should display the image it was used to take in a main ImageView.
Three buttons:
//buttons 1,2,3 are the same
Button button1 = (Button) findViewById(R.id.some_button);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(toggled == true){
takePictureIntent();
}
else {
//display the picture
}
}
});
My camera/gallery code is from a tutorial/forum online:
private void takePictureIntent() {
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, 1);
//}
}
Can somebody please guide me towards how to have the button "remember" the picture taken through it so that, when the toggle is off, it will display it?
I've been trying to figure this out for hours, any help is appreciated.
I'm trying to create a custom notification with ImageButton.
When onclicked, the ImageButton should start an activity named TaskActivity, but i'm unable to achieve this.
The followings are my codes:
MainActivity.java
public void onCreate(Bundle savedInstanceState) {
//button intents
Intent cmdIntent = new Intent(
MainActivity.this, cmdButtonListener.class);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, cmdIntent, 0);
//notification mgr
int notifyID = 001;
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotifyMgr = (NotificationManager)getSystemService(ns);
//new notification
int icon = R.drawable.icon3;
long when = System.currentTimeMillis();
#SuppressWarnings({ "deprecation" })
Notification notify = new Notification(icon,getString(R.string.text),when);
//remote views
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.custom_notification);
contentView.setImageViewResource(R.id.notification_image, R.drawable.icon4);
contentView.setTextViewText(R.id.notification_title, "APP TITLE");
contentView.setTextViewText(R.id.notification_text, "hello");
contentView.setOnClickPendingIntent(R.id.notification_image, pendingSwitchIntent);
notify.contentView = contentView; //set
//notification intent
Intent nIntent = new Intent(MainActivity.this,MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, nIntent, 0);
notify.contentIntent = contentIntent;
mNotifyMgr.notify(notifyID,notify);
}
public class cmdButtonListener extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent){
System.out.println("Here, I am here");
Intent newAct = new Intent(MainActivity.this, TaskActivity.class);
startActivity(newAct);
}
}
Android Manifest
<receiver android:name=".MainActivity$cmdButtonListener" />
I dont know, where i'm wrong as i'm doing exactly as it is shown in most of the tutorials online. :(
Finally I've found a solution...
Declare the below variable
private static Context mContext;
Add below code inside public void onCreate(Bundle savedInstanceState) {
mContext = this;
Finally, change the below piece of code in public class cmdButtonListener extends BroadcastReceiver{
Intent newAct = new Intent(MainActivity.this, TaskActivity.class);
startActivity(newAct);
To
Intent Act = new Intent(mContext,TaskActivity.class);
mContext.startActivity(Act);
Is it possible to get the user-input text from an EditText and display it in an AlertDialog? The Main Activity prompts the user to set an alarm, then enter a message to be displayed later once the alarm goes off. I want to be able to display whatever message the user typed in an AlertDialog.
MainActivity.java
public class MainActivity extends Activity{
TimePicker picker;
DatePicker datepicker;
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings, menu);
return true;
}
public void setAlarm(View view) {
picker = (TimePicker)findViewById(R.id.timePicker1);
datepicker = (DatePicker)findViewById(R.id.datePicker1);
Calendar AlarmCal = Calendar.getInstance();
Intent intent = new Intent(this, AlarmBoadcastReceiver.class);
PendingIntent pendingintent = PendingIntent.getBroadcast(this.getApplicationContext(), 9988, intent, 0);
AlarmCal.setTimeInMillis(System.currentTimeMillis());
AlarmCal.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour());
AlarmCal.set(Calendar.MINUTE, picker.getCurrentMinute());
AlarmCal.set(Calendar.DATE, datepicker.getDayOfMonth());
AlarmCal.set(Calendar.YEAR, datepicker.getYear());
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, AlarmCal.getTimeInMillis(), pendingintent);
}
}
AlarmBroadCastReciever.java
public class AlarmBoadcastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
showNotification(context);
Vibrator vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
}
private void showNotification(Context context) {
PendingIntent contentintent = PendingIntent.getActivity(context, 0, new Intent(context, ShowDialog.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notif)
.setContentTitle("Reminder")
.setContentText("Click Here To View Message");
mBuilder.setContentIntent(contentintent);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
ShowDialog.java
public class ShowDialog extends Activity {
final Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_dialog);
showDialog();
}
void showDialog() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Message");
alertDialogBuilder
.setMessage("message")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ShowDialog.this.finish();
closeApp();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
So when the notification is clicked on, the AlertDialog shows up, but not with the message the user entered. Ive tried getting the values and putting them in a string, but that doesnt work. Any Ideas?
Try to create your own Dialog extending from Dialog class and add this message to constructor.
I hate to answer my own question but I took the advice #StarsSky gave and used Shared Preferences to store and retrieve the message to display in the AlertDialog
save
SharedPreferences.Editor editor = getSharedPreferences(null, MODE_PRIVATE).edit();
editor.putString("text", editText.getText().toString());
editor.commit();
receive
SharedPreferences prefs =getSharedPreferences(null, MODE_PRIVATE);
String text = prefs.getString("text", null);
Add the message as an EXTRA to the alarm pendingIntent:
Intent intent = new Intent(this, AlarmBoadcastReceiver.class);
intent.putExtra("message", yourMessageString);
Get the string in AlarmBoadcastReceiver onReceive method
yourMessageString = intent.getExtras().getString("message","No message");
In showNotification add that string as an EXTRA to pendingintent:
intent = new Intent(context, ShowDialog.class);
intent.putExtra("message", yourMessageString);
PendingIntent contentintent = PendingIntent.getActivity(context, 0, intent, 0);
On ShowDialog onCreate method get the intent extra:
userMessage = getIntent().getExtras().getString("message","No message");
Then set that message to AlertDialog
alertDialogBuilder.setMessage(userMessage)