I am developing a new app for android 2.2(Froyo), I need to know how to forcefully display a leyout before loading it with dynamic data. I have a LinearLayout with a empty List, wen the appropriate menu is selected I load it with dynamic data which takes some time to load, but wat happens is the screen is empty till the layout is filled with data. I need to display the empty layout which has ly title and show a ProgreesDialog till the list is filled with data. Here is my Activity class.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deals);
loadDeals();//fills the list with data(URL call)
}
You can user AsyncTask for loading data. During which you will be able to show ProgressDialog as well
http://developer.android.com/reference/android/os/AsyncTask.html
Use AsynchronousTask Class and call loadDeals() from doInBackground Method.Just capture the data on that method.Then work ur UI in postExecute method.It will solve your problem
Related
I have an activity A with a listview and upon selecting some items in the list, and navigating to another activity B with the selected items in the list of Activity A. And made some modifications. After return to A. My screen is not updating with the latest modifications.
Please guide me how to do this. Thank you in advance.
If you have unified data storage for both activities (so that you know that this problem is purely UI issue, because the data itself is updated upon returning from activity B), then you should call this method on your ListView adapter:
yourListView.getAdapter().notifyDataSetChanged()
In order to display what you want, you need to have some TextViews in your other activity and then just add a text to them to display what you need.
When you get the data, just do like this in the onCreate method:
TextView tv = (TextView)inflate.findViewById(R.id.myTextView);
tv.setText(theDataStringYouReceived);
I have a Main Activity with 5-tabs (like Instagram) and because I didn't want to load each of them everytime the user clicks a tab , I used and set the "set Screen Page Limit" command to 5.
However , I understand it is very heavy for memory .
What I've seen a just few seconds ago on the facebook app is that it doesn't auto load the content of all tabs but every tab's content loads when you visit it and by the time a tab is loaded , next time you visit the specific tab , it doesn't load again.
How can we achieve like this ?
Thanks!
First you need to enable cache in api library Like use volley network api. and all operation start onCreate() method like
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_activated_1, android.R.id.text1, DummyContent.ITEMS));
}
because its called once and second time this method will get called and you will get your data as its on second visit.
I'm developing an Android app which looks like Facebook. FeedsActivity.java has a ListView which show user's feeds. When the ListView item is clicked, it starts the activity ViewActivity.java
ViewActivity shows one targeted feed. If user clicks the like button in ViewAcitivity, like count will be updated and then if the user clicks the back button, FeedsActivity will be shown.
I want to update ListView item's like count without refresh.
Here is my logic
FeedsActivity.java
onCreate(){
// http request to get json feeds
// parsing json
// setAdapter
}
setOnItemClickListener(){
// intent.putExtra() // FEED DATA
// startActivity
}
ViewActivity.java
public void goBack(View v){
// intent.putExtra() // FEED DATA INCLUDE UPDATE LIKE COUNT
// startActivityForResult()
}
When I call startActivityForResult() in ViewActivity.java, onCreate() of "FeedsActivity.java" is called so my ListView gets updated. (request json feeds and setAdapter) :(
I want to update only like counts, such as Facebook app.
Is there any other good way?
please help me.
have a look at notifyDataSetChanged() of your adapter. this method basically tells the adapter that the provided data has changed and that it should update the ListView. so this will makes your ListView refresh and display new data
I have defined a HomeActivity with three tabs and each tab is a seperate activity.I used the example in the android developer site. http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
Tab B's (second tab) UI conatins a text and button (named Search).When the user clicks the search button,it should contact a REST webservice and show the results as a list.
In Order to do this,inside TAB B activity,on the click of the button,i call a method which creates an intent and calls a new SearchResultsActivity(referring as SRA henceforth).Inside the SRA(extends List Activity),i have the logic for connecting to the webservice and parsing the JSON result returned which displays the results as a list.I am able to achieve this functionality.But i see some drawbacks here and my questions are:
Is it fine to define a new activty (SRA) to handle the search results? or would it be better to have it handled in TAB B activity itself?The main reason why i went for a seperate activity is,the SRA extends ListActivity which would be needed if i want to display it as List and TabB is just extending Activity and wouldnt allowme to display teh results.So,is there a better way to do this?
Given the above implementation,when i navigate from TabB (click search button) to SRA,the tabs are not seen anymore.As TabB is calling a new activity(
Intent srchIntent = new Intent(TabB.this, SearchResultActivity.class);
TabB.this.startActivity(srchIntent);),the tab goes away.What could be the best solution in this case so that the tabs appear/results shown within Tab B ?
While navigating from TabB to SRA,i am trying to show a Progress dialog/Loading defined in TabB before calling StartActivity and cancel it afterwards.But the loading icon does not appear.I have tried showing the dialog and canceling in SRA as well.But the loading does not appear.
Hey Bala, what i have to say is:
1) It would be best to extend the TAB B as a ListActivity and the search done by a helper class. This way you are making your code more independent.
2) Implement 1) and you will be ok.
3) You should show a progress dialog when you start the request, and stop it when you got results. I would implement a broadcast receiver to achieve that (I can help you out, if you choose to do that).
There are two approaches to achieve this goal..
Whenever you make start your another activity (i.e. search activity), just before that set your search activity to your desired tab. You can achieve this by taking a instance of your TabActivity (i.e. your activity which is extending this class) and calling new Intent().setClass(TABACTIVITY_INSTANCE,ACTIVITY YOU_WANT_TO_SET_TO_THIS_TAB). But make to make the different objects of intent as member class .. Don't do something like this (new Intent().setClass()).
Declare the no of intent objects as no of Tabs you are holding and then use setClass method.
(This will solve your problem of the tab disappearing)
Now for taking data from server, I suggest you to implement AsyncTask (wonderful api available on Android):
private class DownloadImageTask extends AsyncTask<String, Void, String>
{
AbousUsHandler aboutHandeler;
#Override
protected void onPreExecute()
{
mProgress.setMessage("Please wait");
mProgress.show();
super.onPreExecute();
}
protected String doInBackground(String... urls)
{
/* Do you your background task i.e. getting data from server
but don't do ui related things here as this method is called in Thread
pool not on Android Ui thread.*/
return null;
}
protected void onPostExecute(String result)
{
try
{
mProgress.dismiss();
/* set Your List View Adapter here */
}
}
}
Execute this from your UI thread only by calling new DownloadImageTask().execute().
First preExecute will be called and then doInBackground and when you get your data from the server, onPostExecute will be called.
I hope this solves your problem.
I have a simple application, it starts, loads xml feed from the net, you can browse a list of news and then read details for a chosen news item. What I would like to do is have a splash screen, meaning as soon as you click application, it should display an image (app name in my case) and then display news list only after they've loaded.
I read about similar (I think) problems, and usually people say to use FrameLayout, but I can't really sort it out. I'm not sure if this can be done in the first activity that is launched, maybe I should just display this splash image in one activity and only then call activity displaying my news list?
I know that on iPhone you can set splash screen in app settings while developing, would be nice to have this functionality in android's app's manifest...
Android suggests you take advantage of using a splash screen when performing lengthy calculations on start up. Here's an excerpt from the Android Developer Website - Designing for Responsiveness:
"If your application has a time-consuming initial setup phase, consider showing a splash screen or rendering the main view as quickly as possible and filling in the information asynchronously. In either case, you should indicate somehow that progress is being made, lest the user perceive that the application is frozen." -- Android Developer Site
You can create an activity that shows a Progress Dialog while using an AsyncTask to download the xml feed from the net, parse it, store it to a db (if needed) and then start the Activity that displays the News Feeds. Close the splash Activity by calling finish()
Here's a skeleton code:
public class SplashScreen extends Activity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// set the content view for your splash screen you defined in an xml file
setContentView(R.layout.splashscreen);
// perform other stuff you need to do
// execute your xml news feed loader
new AsyncLoadXMLFeed().execute();
}
private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute(){
// show your progress dialog
}
#Override
protected Void doInBackground(Void... voids){
// load your xml feed asynchronously
}
#Override
protected void onPostExecute(Void params){
// dismiss your dialog
// launch your News activity
Intent intent = new Intent(SplashScreen.this, News.class);
startActivity(intent);
// close this activity
finish();
}
}
}
hope that helps!
I know this is old but for those of you who are still facing this problem, you can use this simple android-splash library to show your splash screen.
SplashBuilder
.with(this, savedInstanceState)
.show();
You can set SplashTask that will execute while the splash screen is displayed.