How to set id for the views in RemoteViews (appwidget) programmatically? - java

I generate dates for the month calendar inside appwidget, using the code fragment below:
for (week in 0 until 6) {
val rowRv = RemoteViews(context.packageName, R.layout.w_calendar_row_week_wrapper)
for (day in 0..6) {
...
// Set date into cell
// I need to distinguish textviews later - to choose the date by the click
dateTextCellRv.setTextViewText(
android.R.id.text1,
cal.get(Calendar.DAY_OF_MONTH).toString()
)
// For click on the chosen date
val getDateNumber = cal.get(Calendar.DAY_OF_MONTH).toString().toInt()
val clickOnChosenDate: PendingIntent =
Intent(context, WidgetCalendarProvider::class.java).let { intent ->
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
intent.putExtra("passedDate$appWidgetId", getDateNumber)
intent.action = ACTION_CHOSEN_DATE_CLICK_CALENDAR
PendingIntent.getBroadcast(
context,
appWidgetId,
intent,
PendingIntent.FLAG_IMMUTABLE
)
}
dateTextCellRv.setOnClickPendingIntent(
android.R.id.text1,
clickOnChosenDate
)
rowRv.addView(R.id.row_week_wrapper_w_calendar, cellRv)
cal.add(Calendar.DAY_OF_MONTH, 1)
}
rViews.addView(R.id.calendar_view_w_calendar, rowRv)
}
How to set id for all the textviews with dates programmatically?
Is it possible to distinguish textviews with android.R.id.text1 for getting string values from each of them by click on any TextView?

Related

How can i direct to the PDF file generated by clickable notification

Hello, I am making a project where the record contains ID, Name, Status And I have save it as a PDF the data, and its gets a notification after getting saved,
No I wanted to know how can I make that clickable and direct to file.
Below is the code I tried, but not working
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.addCategory(Intent. CATEGORY_APP_FILES) ;
notificationIntent.setDataAndType(Uri.parse(filePath), "application/pdf");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
PendingIntent resultIntent = PendingIntent. getActivity (SheetActivity. this, 0 , notificationIntent , 0) ;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder
(SheetActivity. this, default_notification_channel_id )
.setSmallIcon(R.drawable. mcclogo )
.setContentTitle("PDF saved")
.setContentText(filePath)
.setContentIntent(resultIntent) ;

How to do nothing if the same option is selected in an Android dialog?

I have the following dialog:
val alertDialog: AlertDialog.Builder = AlertDialog.Builder(context)
val options = arrayOf("Red", "Blue", "Green")
alertDialog.setTitle("Select a color")
alertDialog.setSingleChoiceItems(options, 1) { dialog, position ->
logErrorMessage("position: " + position)
dialog.dismiss()
}
val alert: AlertDialog = alertDialog.create()
alert.show()
The default selected value that is is "Blue", as I pass 1 to setSingleChoiceItems. Every time I click on one of the three options, I get the position printed. Is there any way I can get that log statement only if the option is changed? If the same option remains selected, then I want no log statement? Thanks
Assuming you don't change the default selected value, just do a conditional check that the position isn't equal to 1. If it's more dynamic, then store the current position and compare that instead:
val alertDialog: AlertDialog.Builder = AlertDialog.Builder(context)
val options = arrayOf("Red", "Blue", "Green")
alertDialog.setTitle("Select a color")
alertDialog.setSingleChoiceItems(options, 1) { dialog, position ->
if(position != 1) {
logErrorMessage("position: " + position)
}
dialog.dismiss()
}
val alert: AlertDialog = alertDialog.create()
alert.show()

retrieve data from activity

My app takes data from 4 EditTexts, puts them in an ArrayList and with a press of a button (using Intents) passes the data to second activity which then prints that data in 4 TextViews and has a button that returns me to first activity for next input. Is there any way of keeping all of my input data so I can show everything I've inputted in a TextView or Toast message?
Let's assume your first Activity is A and second activity is B
so in the A activity's button press you can call the 2nd activity using following code.
Intent intent = new Intent(A.this, B.class);
String input1 = txt1.getText().toString();
String input2 = txt2.getText().toString();
intent.putExtra("key1", input1 );
intent.putExtra("key2", input2 );
startActivity(intent);
From the B activity you can access these values using following code
Intent intent = getIntent();
String input1 = intent.getStringExtra("key1");
String input2 = intent.getStringExtra("key2");
yes you can. you can pass your data to second activity like this.
Intent intent1= new Intent(first.this, second.class);
Bundle bundle1=new Bundle();
bundle1.putString("data", et1.getText().toString());
intent1.putExtras(bundle1);
startActivityForResult(intent1, 0);
or you can restore them in a temporary database.

Intent Intents.Insert.ACTION only works the first time?

My Android service creates a notification with a custom button. The action for the custom button should open the "Create contact" system screen, with the phone number prefilled. This is the code:
// create the base notification
Intent notificationIntent = new Intent(context, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(
context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(text)
.setAutoCancel(false)
.setOngoing(true)
.setContentIntent(pendingIntent);
// add the action to save to contacts
Intent saveIntent = new Intent(ContactsContract.Intents.Insert.ACTION);
saveIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
saveIntent.putExtra(ContactsContract.Intents.Insert.PHONE, text);
saveIntent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
PendingIntent pendingSaveIntent = PendingIntent.getActivity(context, 0, saveIntent, 0);
builder.addAction(0, "Save to Contacts", pendingSaveIntent);
// show the notification
context.getSystemService(Context.NOTIFICATION_SERVICE).notify(0, builder.build());
The code works well the first time (let's say that I pass "01234-1234567" to it). However, subsequent calls keep showing the same dialog shown the first time, with the same value in it. So even if I call this piece code again with a different number in input (the value of "text" for the "Intents.Insert.PHONE" extra value) I'll always get "01234-1234567".
I tried cancelling the Contacts screen, manually create a new contact, completely stopping the Contacts apps...no matter what, next time I tap on the notification's button I'll get the screen with the value I originally passed on the first attempt.
Do you have any explanation or see anything wrong in the code?
I solved it. Basically, the system caches intents, unless they differ for something more than just the extra params. In my code I was only changing the phone number, so it was always using the same (first) cached intent. The solution is to use a flag to cancel the currently cached intent and create a new one
PendingIntent pendingSaveIntent = PendingIntent.getActivity(
context, 0, saveIntent, PendingIntent.FLAG_CANCEL_CURRENT);
The system cashes intents, so in your PendingIntent instead of typing 0 as flag, use (PendingIntent.FLAG_UPDATE_CURRENT), try this:
PendingIntent.getActivity(context, 0, saveIntent, PendingIntent.FLAG_UPDATE_CURRENT);

how to create a new google calendar event and fetch its details immediately afterwards?

My android app creates a new G-calendar event.
The user puts all required fields in the G-calendar Activity and then saves.
I want to save in my mobile DB the details, after the user saves the event.
i.e. the flow is:
click "new event" ButtonView --> Calendar UI opens --> the user fills fields --> the Calendar UI closes --> the client saves all fileds (related to a event_id) in local sqlite DB
I have tried 2 options.
(Option A) doesn't open the G-Calendar Activity for the user to fill in data
(Option B) opens the G-Calendar Activity, but I cannot get the event_id for fetching the users filled data.
private void exportToGCalendar() {
//option A
//Here is an example of inserting an event. This is being performed in the UI thread for simplicity. In practice, inserts and updates should be done in an asynchronous thread to move the action into a background thread. For more information, see AsyncQueryHandler.
long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "Jazzercise");
values.put(Events.DESCRIPTION, "Group workout");
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
Uri uri = cr.insert(Events.CONTENT_URI, values);
// get the event ID that is the last element in the Uri
mEventId = Long.parseLong(uri.getLastPathSegment());
//
// ... do something with event ID
//
//
//=======
//option B
//addCalendarEvent_optionB();
}
private void addCalendarEvent_optionB() {
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra(Events.TITLE, "Learn Android");
intent.putExtra(Events.EVENT_LOCATION, "Home suit home");
intent.putExtra(Events.DESCRIPTION, "Download Examples");
// Setting dates
GregorianCalendar calDate = new GregorianCalendar(2012, 10, 02);
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
calDate.getTimeInMillis());
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
calDate.getTimeInMillis());
// Make it a full day event
intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
// Make it a recurring Event
intent.putExtra(Events.RRULE, "FREQ=WEEKLY;COUNT=11;WKST=SU;BYDAY=TU,TH");
// Making it private and shown as busy
intent.putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE);
intent.putExtra(Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_BUSY);
// intent.setData(CalendarContract.Events.CONTENT_URI);
startActivity(intent);
}
How can I achieve my target?

Categories