Add to Favourites Button SQLite in Android - java

I am setting up an application and need to have a favourites button in an activity to add the activity to a favourites table. I have a Facebook Login set up with SQLite database storing the email. I was wondering how to save an activity to my favourites and have it stored in the SQLite Database?
I have seen code such as:
btnAddFavourite = (ImageButton) findViewById(R.id.btnAddFavourite);
btnAddFavourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Add code here to save the favourite, e.g. in the db.
}
});
btnAddFavourite.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// Open the favourite Activity, which in turn will fetch the saved favourites, to show them.
Intent intent = new Intent(getApplicationContext(), FavViewActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
return false;
}
});
But I am not sure what code to add in to be able to save the activity to my favourites in the SQLite Database.
Possible Solution - I believe SharedPreferences could be used but how do you save the whole activity?
Therefore when the user goes to their favourites page, they have a button to go to that activity they saved
I would really appreciate your help.
Thank you so much in advance!

your solution as i understand will be that you can get your activity name and save it in db. after that when you want to return to that activity get the name of activity and after that start activity with its name.
to get name of activity use the this.getClass().getSimpleName();
and to start activity from its name as string use : startActivity(this, Class.forName(yourStringClass));
for more information about getting name of activity and start activity from string name refer to links bellow:
Get Activity name dynamically - android
Intent and start activity from string

Related

TextView from SecondActivity become visible after a button from MainActivity is pressed?

I'm very new to Android Studio Development and I was wondering how to do this, when I click a button on MainActivity, it will direct me to secondActivity where the text become visible (Originally TextView will not be visible until the button from MainActivity is pressed)
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
String status = "Success!";
intent2.putExtra("Status",status);
startActivity(intent);
}
});
I want to make an if-else statement for this (on SecondActivity page) where if user straight away go to SecondActivity, it will not display any text there. But if pressed the button on MainAcitivty page, the system will go to SecondActivity with the TextView displayed.
Thanks!
Basically, there are several approaches you can do that.
Use intents pass data
Sure you pass a boolean type or whatever you want into this intent, I think this is the approach you are trying to make here. So I can give you an example:
In your first activity you can do something like this,
button.setOnClickListener {
val intent = Intent(this#MainActivity, SecondActivity::class.java).apply {
val status = true
putExtra("Status", status)
}
startActivity(intent)
}
And in your second activity, in your need to override onCreate to parse your intents to decide your text want to display or not.
val status = intent.extras?.getBoolean("Status")
if(status) {
hideText()
} else {
showText()
}
the other approach you can deal with it is try to create singleton class to keep the status in this class, and based this singleton class status, you may choose to hide/show your text. However this solution isn't the recommended way to do it. Because global state is bad for testing and just pollute the code.

Save settings button and back to previous activity

I have an activity that has edit text and you can type your name in it, and when you click Save button you will be redirected to MainActivity, but I don't want to open MainActivity by Intent, I make that save button save your name with shared prefs and everything works fine but I don't want to open my Main in Intent I want that when I clicked on save button the current activity that saves data close and previous activity open.
sorry for my bad English.
this is my code for save button
submitButt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == submitButt.getId()) {
String name = inputName.getText().toString().trim();
if (isValidInput(name)) {
Intent setint = new Intent(SettingsActivity.this, MainActivity.class);
setint.putExtra("name", name);
saveData();
startActivity(setint);
Toast.makeText(SettingsActivity.this, "Settings Saved", Toast.LENGTH_SHORT).show();
}
}
}
Then you just open the PreviousActivity instead of the MainActivity. Add a checker inside the PreviousActivity if "name" has data or not. You can also use finish(); this will kill the activity.
** Update **
In your MainActivity, let's say you have TextView name. Add:
#Override
public void onResume(){
super.onResume();
// Check your variable if it has value or none
textViewName.setText(variableForSharedPref)
}
Then you can proceed with usual process to go in Settings and Go Back to MainActivity.

Call a Method from another Method Android

I am not sure this workaround is the correct way to achieve my goal of having a prompt text in a spinner. What happens with this application is the spinner navigates to another Activity via an Intent and when the user navigates back to the Main Activity with the spinner they have two ways back. One with a Button and a click event the other by clicking the device BACK button. I am trying to call the code in the click event from the method that manages the device BACK button
I do not know how to call the click event from the device BACK button Method
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Use BACK BUTTON\n\n"+"On the Screen",Toast.LENGTH_LONG).show();
// I want to call goBack(View view) from here
// +++++++++++++++++++++++++++++++++++++++++++
}
public void goBack(View view){
Intent i = new Intent( PageTwo.this, MainActivity.class );
startActivity( i );
}
The reason I use this Intent to navigate BACK to the Main Activity is it reloads the variables in the Spinner
It looks like goBack(View) is most likely from an onClick setup in your layout XML. Since you aren't using the view, just pass null:
#Override public void onBackPressed() {
goBack(null);
}
I don't know if I get you right, if you just want to go back to the activity which started another activity, you can just call finish() method of Activity class:
#Override
public void onBackPressed() {
finish();
}
finish() reference

How to go another Activity using function instead of Button widget

I hope if someone help me in this ,
in my projet I am trying to open another avtivity used voice command Ex, say "one " it should compare if the string is equal to what I? have it should switch to another activity or external activity such as Phone call, Camera. What I did here, I store the recognized ward in Edittext and store it in string and compare it . my 2 projects counted on how to do this
Inside onCreate:
final Button btntx = (Button)findViewById(R.id.btn1);
final EditText edittxt1 = (EditText)findViewById(R.id.edttxt);
btntx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent (Intent.ACTION_DIAL); // sow the Dial window
intent.setData(Uri.parse("tel:"));
startActivity(intent);
);
}
////////////////////////// outside the onCreate //////// Function after I get the result from the Speech recognizer and call this function to see if the what it said is equal to my keyword . SO i need to call the button to open that activity
String gwdata= edtxt.getText().toString(); // data from EditText
if (gwdata.equals("One"))
{
Toast.makeText(getBaseContext(),"They are equal", Toast.LENGTH_SHORT).show();
// Here Do someting to go to another activity
}
Thnks guys in advance .

keeping data updated between two activities in android

How can I keep data updated between two android activities. I am working on a android album project where the mainactivity is album activity(where I can add remove and rename albums). But when the user clicks on one of the albums just created I start a intent with a photo activity where he can add remove and move photos. All the operations are done for the album in the photoactivity and I use serialization to write the data back to a text file in both activities. The problem is when I back out to the main activity from the photo activity making some changes on a particular album. The mainactivity doesn't know about the updates.
Code in main activity to start the photo intent on a particular album selected by user
albumGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
Intent photoIntent = new Intent(MainActivity.this, Photo.class);
photoIntent.putExtra("userNode", user);
photoIntent.putExtra("position", position);
startActivity(photoIntent);
// Toast.makeText(MainActivity.this, position, Toast.LENGTH_SHORT).show();
}
});
You see I pass in the user object which is linkedlists of all album and photo nodes which added removed or moved. It is basically the entire user data of the that user. So whenever I start the old reference of the user node is passed on to the photo intent. I have implemented readerUser() and writeUser methods using serialization. I need to keep the reference of the user object in the main activity updated with all the changes in the photo activity..
Use contentProvider to provide unique access to your photo data and implement an Observer design pattern. That is , in on side, inside the the ContentProvider, when dataset changed due to insert ,update,or delete, notify the the contentResolver;on the other side, user has to register the notification by calling getContentResolover().registerContentObserver
Check out those links:
http://developer.android.com/reference/android/content/ContentProvider.html
http://developer.android.com/reference/android/content/ContentResolver.html
http://mylifewithandroid.blogspot.com/2008/03/observing-content.html
Look into implementing an Android Application. The Application class essentially gives you a GUI-less activity that remains constant for the duration of your session. Instead of serializing objects (slow, excess overhead), you can just call a method on your Application to save images to a data structure in the Application.
Using an Application means that any of your normal Activities can obtain a reference to the Application singleton and access any field or method which you expose.
Read the offical doc and do a Google search for some implementation examples, of which there are many.
I think what you need is database. Use sqlite http://developer.android.com/reference/android/database/sqlite/package-summary.html .
That is guessing you also want the data persisted and restored when the application is run a second time.
Implement broadcast receivers.
http://developer.android.com/reference/android/content/BroadcastReceiver.html
I used as #Vegito1044 proposed a local BroadcastReceiver in the main Activity that can be triggered from other activities. The relevant code for this looks like this:
public class AlbumActivity extends Activity {
private BroadcastReceiver myReceiver = null;
class _AlbumUpdateReceiver extends BroadcastReceiver {
#Override
public void onReceive (Context context, Intent intent) {
Log.i(Global.LOG_CONTEXT, "AlbumActivity.onReceive(intent='" + intent + "')");
reloadGui();
}
}
#Override
public void onResume() {
super.onResume();
if (myReceiver == null)
{
myReceiver = new _AlbumUpdateReceiver();
IntentFilter filter = new IntentFilter(Global.REFRESH_GUI);
registerReceiver(myReceiver, filter);
}
reloadGui();
}
#Override
public void onPause() {
if (myReceiver != null)
{
unregisterReceiver(myReceiver);
myReceiver = null;
}
super.onPause();
}
void reloadGui()
{
Log.d(Global.LOG_CONTEXT, "AlbumActivity.refreshGui()");
... do what is neccessary to update gui
}
}

Categories

Resources