android layout not displayed - android

I have trouble displaying the layout of my main activity :
I create an ActivityA with an ImageView.
In onCreate(), I launch an AsyncTask, which retrieves content from Internet, and opens an ActivityB.
When I launch my application, it displays ActivityB right away.
public class ActivityA extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyTask mytask = new MyTask();
mytask.execute();
}
}
My MainActivity xml file is the following :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/loadingPanel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/home_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_home_page" />
<ProgressBar
android:id="#+id/marker_progress"
style="?android:attr/progressBarStyle"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical|center_horizontal"
android:indeterminate="true" />
</RelativeLayout>
The ProgressBar is used to show the loading process of the AsyncTask.
thanks for helping

public class ActivityA extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyTask mytask = new MyTask();
mytask.execute();
}
}
private class MyTask extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... params) {
// code for retrieve contents from Internet
}
#Override
protected void onPostExecute(String result) {
// cancel ProgressBar and start activity B here like
Intent i = new Intent(A.this, B.class);
startActivity(i);
}
#Override
protected void onPreExecute() {
// showing ProgressBar
}
}
don't forgot to mention A activity as a launcher.
I hope it will help you to solve this problem.

Related

How get JSON data while in splash view in Android

Here I try to make a app.
First it shows a home page.It will display 2 sec.
Here is the code...
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int secondsDelayed = 3;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(MainActivity.this,SocondActivity.class));
finish();
}
}, secondsDelayed * 1000);
}
}
Then my second activity want to show a splash view until json string download from the rest service.
Here is my code....
public class SocondActivity extends Activity {
private static final String TAG = "SocondActivity";
//please assume my url is OK
private static String url = "my url";
private String jsonStr;
private final int SPLASH_DISPLAY_LENGTH = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
new GetContacts().execute();
Log.d(TAG,jsonStr);
Intent mainIntent = new Intent(SocondActivity.this,
ThirdActivity.class);
SocondActivity.this.startActivity(mainIntent);
SocondActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
protected Void doInBackground(Void... arg0) {
//please assume that my Server Handler class is working fine
ServiceHandler sh = new ServiceHandler();
jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
return null;
}
}
}
Then I got this error......
java.lang.NullPointerException: println needs a message
Because the jsonStr was empty....
Why is that ??
Here is my R.layout.splash_screen
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/loadingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="center"
android:src="#drawable/calendar_50" />
<ProgressBar
android:layout_below="#id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
</RelativeLayout>
</LinearLayout>
Can someone please tell me Is this method is correct and what should I change to make this correct ??
You have a race case. You need to do something like this.
Instead of utilizing a handler in onCreate like you are below (which is only serving to create a layer of complexity that isn't functioning like you expect)
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
new GetContacts().execute();
Log.d(TAG,jsonStr);
Intent mainIntent = new Intent(SocondActivity.this,
ThirdActivity.class);
SocondActivity.this.startActivity(mainIntent);
SocondActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
Just call this in your onCreate()
new GetContacts().execute();
And in your AsyncTask, override the method onPostExecute like so
#Override
public void onPostExecute(Void voidParam){
Log.d(TAG,jsonStr);
Intent mainIntent = new Intent(SocondActivity.this,ThirdActivity.class);
SocondActivity.this.startActivity(mainIntent);
SocondActivity.this.finish();
}
This will tell your app that you want to execute the AsyncTask, and when you are done (and only when you are done) you want to launch your next Activity. This removes the race case as well as the need for a handler/runnable.

How to inflate a layout from a class

So I am trying to inflate a layout from a class but so far no luck at all ... The layout is a loading spinner I want the spinner to be inflated when the class is called because there is an Asynch task withing the class. I want the inflated layout to be removed when the Asynch task finishes. But so far I have nothing.
Here is my XML for the spinner
?xml version="1.0" encoding="utf-8"?>
<ProgressBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LoadingSpinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="?android:attr/progressBarStyleLarge"
android:layout_centerHorizontal="true">
</ProgressBar>
And my class
public class ClusterManager extends ClusterManager {
public ClusterManager(LayoutInflator inflator) {
super();
this.inflater = inflater;
}
private void reload() {
new AsyncTask<Void, Void, Void>()
{
#Override
protected Void doInBackground(Void... params)
{
//Start spinner here
}
#Override
protected void onPostExecute(Void aVoid)
{
//Stop/Remove the spinner
}
}.execute();
}
I have tried to inflate it like this.
View loadingView = inflator.inflate(R.layout.loading_spinner,null);
But no result the layout is not inflated nothing is shown on the screen. What am I doing wrong? Is this the right way to inflate it or there is something that I am missing?

ListView doesn't show progressbar when the data loaded

When I start activity with ListView it start to load some information from the Internet and after apply adapter, but during loading ListVIew doesn't show me a ProgressBar ring. When I trying to use SherlockListActivity instead ListView I have same problem.
<?xml version="1.0" encoding="utf-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/comments"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
public class CommentsActivity extends SherlockActivity{
#InjectView(R.id.comments) ListView mCommentsListView;
private String mPostId;
private CommentsAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.comment_activivty);
ButterKnife.inject(this);
Intent intent = getIntent();
mPostId = intent.getStringExtra("POST_ID");
LC lc = new LC();
lc.execute(mPostId);
}
public void setupAdapter(){
mAdapter = new CommentsAdapter(this);
mCommentsListView.setAdapter(mAdapter);
}
private class LC extends LoadComments{
#Override
protected void onPostExecute(Void result) {
//setup adapter after loading comments
setupAdapter();
}
}
}
Add ProgressBar to your layout file:
<ProgressBar
android:id="#+id/progress_bar"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Inject the ProgressBar in your activity:
#InjectView(R.id.progress_bar) ProgressBar mProgressBar;
Now you have to hide it when the adapter is set:
public void setupAdapter(){
mAdapter = new CommentsAdapter(this);
mCommentsListView.setAdapter(mAdapter);
mProgressBar.setVisibility(View.GONE);
}
Do it like this,
#Override
protected Void OnPreExecute(){
ProgressDialog prgDialog = new ProgressDialog(getContext());
prgDialog.setTitle("Loading");
prgDialog.setMessage("please wait")
prgDialog.setCancelable(false);
prgDialog.show();
}
on post Execute
private class LC extends LoadComments{
#Override
protected void onPostExecute(Void result) {
//setup adapter after loading comments
prgDialog.dismiss();
setupAdapter();
}
}

NullpointerException in onPreExcute of AsyncTask for ProgressBar

I had a NullPointerException when I try to set ProgessBar to visible in onPreExecute AsyncTask (mProgressBar.setVisibility(View.VISIBLE);). I don't know what went wrong! Thanks you all!
<ProgressBar
android:id="#+id/pb_featured_game_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
public class MainActivity extends Activity {
...
private ProgressBar mProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
new FetchGamesTask().execute();
private class FetchGamesTask extends
AsyncTask<Integer, Integer, List<GameInfo>> {
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mProgressBar = (ProgressBar)findViewById(R.id.pb_featured_game_progress);
mProgressBar.setVisibility(View.VISIBLE);
}
Before you call the execute on your FetchGamesTask, you should associate the main view with the xml file.
Like this
protected void onCreate(Bundle savedInstanceState) {
// the OS will inflate the main_activity.xml
// file and use it for this activity
setContentView(R.layout.main_activity);
new FetchGamesTask().execute();

Show ProgressBar in another thread

For a few days a have some problem.
I need to show simple ProgressBar (not dialog) while doing some stuff in main thread...
I thought its a very simple question, but i cant do this, help me please.
First i tried simple setVisibility(View.VISIBLE) before and setVisibility(View.GONE) after.
But this is doing in the same thread, and ProgressBar freezed while my function working.
Now i have this code, but i have some error, and i dont know whats wrong..
my simple layout :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ProgressBar
android:id="#+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
i have a base activity :
public class BaseActivity extends Activity {
public ProgressBar loading;
public class ProgressBarShow extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... unused) {
return(null);
}
protected void onProgressUpdate() {
}
protected void onPreExecute() {
loading.setVisibility(View.VISIBLE);
}
protected void onPostExecute() {
}
}
}
and finally my working activity , which extends BaseActivity
public class SearchActivity extends BaseActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loading = (ProgressBar) findViewById(R.id.loading);
new ProgressBarShow().execute();
//doing long stuff
//new ProgressBarHide().execute(); there isnt, but sense the same
}
}
I have many activities, which need progress bar, thats why i have created BaseActivity,
to not to dublicate code.
I need to do long work (stuff function) in main thread, because i want to freeze main window and not to allow user do anything (click button etc..), just show working ProgressBar.
Whats wrong in my example? Or give me some advice how can i do this better
class ProgressTask extends AsyncTask<Integer, Integer, Void>{
ProgressBar progress;
Context context;
public ProgressTask(ProgressBar progress, Context context) {
this.progress = progress;
this.context = context;
}
#Override
protected void onPreExecute() {
// initialize the progress bar
// set maximum progress to 100.
progress.setMax(100);
}
#Override
protected Void doInBackground(Integer... params) {
// get the initial starting value
int start=params[0];
// increment the progress
for(int i=start;i<=100;i+=5){
try {
boolean cancelled=isCancelled();
//if async task is not cancelled, update the progress
if(!cancelled){
publishProgress(i);
SystemClock.sleep(1000);
}
} catch (Exception e) {
Log.e("Error", e.toString());
}
}
return null;
}
//Has direct connection to UI Main thread
//Called everytime publishProgress(int) is called in doInBackground
#Override
protected void onProgressUpdate(Integer... values) {
progress.setProgress(values[0]);
Toast.makeText(context, "test"+values[0], Toast.LENGTH_SHORT).show();
}
#Override
protected void onPostExecute(Void result) {
// async task finished
Log.v("Progress", "Finished");
}
#Override
protected void onCancelled() {
progress.setMax(0);
}
}
Use AsyncTask http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html Put your ProgressBar in it, while all work is doing in main thread

Categories

Resources