I have a list. I shoot data using firebase database. I have a favorite button image on my list. When I click the button, favorites are added to the favorite page. When I click the button, the favorite button image changes. But when I scroll up or down on the page or switch to another page, this changed image returns to its original state. What can I do.
The state of the button is not preserved when moving through activities, because the activity object is re-instantiated each time, so in order to fix it, you just make it a static variable.
private static boolean isCandyPressed = false;
Then in your onclick() of button, set it to true. And thus, handle your logic throughout by cheking this variable.
Hope this solves your problem
Related
I have a button in an activity, on clicking that button, I direct the user to a search activity with a search view in the toolbar..
I'm using android.support.v7.widget.SearchView, I managed to expand it automatically using searchView.setIconifiedByDefault(false) in onCreateOptionsMenu..
I also open the keypad automatically in activity onCreate, yet I still have to click in the searchView to start typing there, I want the cursor to be set there automatically..
any help?
call setIconifiedByDefault(false) when the searchview is intialized.
Sets the default or resting state of the search field. If true, a single search icon is shown by default and expands to show the text field and other buttons when pressed. Also, if the default state is iconified, then it collapses to that state when the close button is pressed. Changes to this property will take effect immediately.
Try the below, it worked for me once.
searchView.setIconifiedByDefault(true);
searchView.setFocusable(true);
searchView.setIconified(false);
searchView.requestFocusFromTouch();
Update: If you are Using android.support.v7.widget.SearchView the behaviour is very different. clearFocus is needed if you don’t want the keyboard pop-up all the time.
Also in the searchview layout, use <requestFocus /> tag.
So at the moment the user will select a colour, shape and time. When the app first starts up there is a default for each. What I want to happen is to keep the users selection for all 3 of these even after they close the app while it runs in the background.
If you need me to post anything to help ask away. Thanks.
After writing out what the problem was, it's not that they're not being saved it's the fact that the selections ARE being saved but not being displayed properly. I.E by a border or the dropdown menu results back to it's default value instead of the users selection.
So the values are being saved(my bad) but the app overwrites these saves and doesn't use the border or beings the dropdown menu back to it's default value.
circularImageView = (CircularImageView)findViewById(R.id.activity_main_silver_color_button);
circularImageView.setBorderColor(getResources().getColor(R.color.unselected_border));
circularImageView = (CircularImageView)findViewById(v.getId());
circularImageView.setBorderColor(getResources().getColor(R.color.selected_border));
These work independent of each other but when the border is selected, if a another color was selected then it becomes an unselected border. But what happens if "selected_border" state is never saved.
public static void saveLockScreenDuration(Context context, int duration){
getStoredPreferences(context).edit().putInt(SELECTED_DURATION, duration).apply();
public static int getLockScreenDuration(Context context){
return getStoredPreferences(context)
.getInt(SELECTED_DURATION, DEFAULT_DURATION);
}
This is where all the prefereces are stored. DEFAULT_DURATION and SELECTED_DURATION are in a class called constants.
Upon selection you can store selected values in Shared Preferences. And upon launching it again you can check if those values are null or not. And display accordingly. Read how to use Shared Preferences here.
if you talking about for activity restore data.
then see here.
https://developer.android.com/guide/components/activities/activity-lifecycle.html#saras
Correct me if i am wrong. I think you can use PreferenceManager.getDefaultSharedPreferences(Context context)
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
which is shared across all your Activity and Service classes. If you set an accessibility service to get the event when app is in background and used defaultsharedprefrences to store i think it will work for your case.
I am trying to make a like button with two states in my app but i am confused on what type of button that is used in android app's like instagram and facebook is it a normal button,image button or a toggle button, i've searched around for this but i haven't gotten any answers, please help
A sleek implementation would be to create a Custom ImageView, which has an object (boolean, int, etc) that maintains it's state. You maintain this state by it's onClickListener associated with the imageview, and set the drawable to be a new image corresponding to it's respective state when triggered.
For example, say you have a "like" button similar to Facebook's. Initially, it has a background drawable of a gray thumb and say our state is a boolean value which is false when gray. When a user clicks on the thumb, state would change to true and the image drawable becomes blue indicating it has been activated. We know to make the drawable the blue version because that state is now "true".
So each click event would swap states, and set the drawable to be a new image based on state. This implies have multiple types of images.
EDIT:
Additionally, this could actually be a button with Text in it and a drawable using drawableLeft , drawableRight , etc... which you change the text color and corresponding drawable on state change via method in your Custom Button implementation
You could make a custom checkbox. When the user presses the checkbox, it changes to the like state. If it's already checked, uncheck the box.
Here is the question I found: Defining custom-checkbox in android
My overriding question is this: In Android PreferenceActivity, how can I write an OnClickListener which will have the same functionality as pressing the Android back button as I navigate through PreferenceScreen defined menus? That is to say, I would like users of my App to explicity see a menu choice "Back" which will bring them to the previous menu, or bring them out of the menu activity to their previous activity if they are at the root of this particular PreferenceActivity session.
The android developer documents tell us
Note that this XML resource contains a preference screen holding another fragment, the Prefs1FragmentInner implemented here. This allows the user to traverse down a hierarchy of preferences; pressing back will pop each fragment off the stack to return to the previous preferences.
And they are correct about that. I navigate happily through my menus by clicking on PreferenceScreen items to get to that screen, and using the Android back button to go Back up a level. But I'm not sure a casual user really understands the "Back" button, I know I didn't until I read about it in Developer docs. SO I would would like them to have an explicit Preference defined menu choice whos OnClickListener duplicates the function of the Android back button.
So I tried to put in a Preference in my menu that would go back. Having determined that a not Overriden onBackPressed in a my subclass of PreferenceActivity just referred back to Activity.onBackPressed() which merely calls finish(), I tried this OnClickListener:
private OnPreferenceClickListener clickFinishesSuccessfully = new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
finish();
return true;
}
};
As it turns out, this did NOT do the same thing as pressing the back button! Pressing this button always took me out of the PreferenceActivity entirely, back to the Activity from which I had called my PreferenceActivity. Specifically, it did NOT navigate back through my menus no matter how deep I was when I clicked it.
I am guessing here: When I have gotten to a submenu by clicking an onscreen preference which is really a PreferenceScreen, I am no longer in my own PreferenceActivity. I must be in some other Activity?
So my functional question: what can I put in my OnClickListener of my "Back" Preference to get the same function as the Android Back button navigating through my menus?
I think the casual user should know about the back button. The button is used everywhere so it might be a problem getting used in the first day but after that it's natural. Without being used to the "back" button I can hardly imagine doing the everyday tasks.
The preference you want to add just duplicates functionality and doesn't provide a consistent way with the rest of the system. If Google was considering back being an uncommon thing for casual users would have added that option in phone's Settings which is also a PreferenceActivity.
When the user opens the application, there is a screen with a button on it, which says "login."
The user clicks on the button, and a webview pops up to allow him to log in to the website.
After logging in (the app would need to know somehow), the webview would disappear, and then a list of usernames will pop up. (ListView?)
When the user clicks on one of the usernames, a webview of the username's profile will pop up. Of course, when the user pushes "back", it goes back to the list of usernames.
Can someone explain this to me in terms of Activity and Views? Am I using two activities to do this? Do I hide webview or listview when the user clicks between them?
I did the tutorial (notepad tutorial), but I'm still confused as to what is the best way to develop this.
Thanks
When the user opens the application, there is a screen with a button on it, which says "login." The user clicks on the button, and a webview pops up to allow him to log in to the website. After logging in (the app would need to know somehow),
Yo can do this with two separate Activity classes. I would put the WebView in its own Activity. This is easier than managing lots of different View objects yourself. Also, you'll get transitions between different things if you put each part in its own Activity.
You'll can launch the login Activity with the startActivityForResult() method, allowing it to return if the login was successful or not.
If you want to detect the login, you can monitor events in a WebView using a WebViewClient. You set the WebViewClient of your WebView using the setWebViewClient() method.
the webview would disappear,
Simply start the next Activity using an Intent and call the finish() method on your first Activity. If you do this then the use won't come back to login button Activity if they click back as it won't be on the stack any more.
What I'm not clear on is how long the login at the website will be valid for. You may need to set the flags on the Activities in your Manifest, to ensure the user has to log in again if they leave and then return to your application.
and then a list of usernames will pop up. (ListView?)
Use a ListActivity. This is an Activity which comes with the API designed for displaying a single ListView.
When the user clicks on one of the usernames, a webview of the username's profile will pop up. Of course, when the user pushes "back", it goes back to the list of usernames.
So use the onListItemClick() method in ListActivity to detect the touch and launch a new Activity containing the WebView to show the profile. As this is in a new Activity the back handling is all automatic.
Essentially it could all be done within one activity. I can try to easily demonstrate how it could be done.
When you run your app, Activity (AndroidManifest.xml) is called.
View (could be called, main.xml) is shown displaying a login button
Upon clicking the button, you launch your WebView
Successful login, switches the View to a custom view displaying usernames on a ListView (might be a predefined XML file you create named userlist.xml)
Using onClick on a specific username will launch a WebView regarding that specific username.
To return back to your application when the user hits the Back button, you might need to utilize onPause and onResume but I am not sure.
This may not be the best approach, or even a good one, but it might help clear up any confusion.
EDIT:
An Activity is what gets bound to the AndroidManifest.xml as the main entry point into an application. A View contains user interactive components within your Activity, such as a login button, displaying username's, viewing contents on the web (WebView), etc.
First of all I'd consider a different way of approaching this problem.
You could create your own login layout in Andoid and send your login data to the website. After doing this you should create a ListView of all user names. If the user selects one of these user names you should open a WebView.
The problem would be, that you would have to check whether the login was successful or not! Basically you need to do a HttpRequest, parse the output and check it.
If it's your website you want to login you could write a small wrapper for your login which returns true/false for your login. (php wrapper which returns xml or plaintext)
Anyway, if you really want to do the login via WebView you can check it via:
private void initWebView(final WebView mWebView) {
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Page = new WebView(YourClass.this);
Page.getSettings().setJavaScriptEnabled(true);
Page.loadUrl(url);
if(url.contains("OkDoLogIn.php")) {
/* Check if login was successful if true start new Activity */
}
return true;
}
});
}
With url.contains("OkDoLogIn.php") you basically check which url is about to open. In your case it would be the url or a part of the url of your login button.
Nevertheless you would have to check if your login was successful or not!
Edit: See 1st comment!