1

I have an activity that can show two different layouts. Both of the layouts are pre-defined (XML). Basically, if a condition is met, then layout A should be displayed. If the condition fails, then layout B should be displayed.

Layout A is a simplistic Linear Layout - it's my main "form", so to speak. Layout B is a simplistic Relative Layout - it's a placeholder until some data can be downloaded. Once the data is downloaded (and a notification is sent), then I want to remove Layout B and display Layout A.

I've tried calling the invalidate() method on Layout B in the onResume() method of my Activity but that doesn't work.

I'm not sure what approach I should take, in (1) where to "correctly" switch the layouts, and (2) how I should go about displaying it. I'm assuming I need to inflate Layout A when my condition is met, but I'm not 100% sure about that.

Edit: Snipped of my onCreate() method:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layoutA); // will happen 99% of the time
    ...
    if (!dbHelper.tableIsPopulated()) {
        setContentView(R.layout.layoutB); // show placeholder bc no data exists
        getData();
    }
}
acedanger
  • 1,197
  • 2
  • 15
  • 34
  • How long does the data take to be downloaded and why aren't you just using an `AyncTask` with a `ProgressDialog`? – codeMagic Mar 07 '13 at 00:27
  • It's a smallish amount of data, typically downloaded in less than 20 seconds (even on Sprint 3G). I am using an `AsyncTask` to get the data. I honestly didn't consider creating a `ProgressDialog` from the `AsyncTask` since it's in an external Java class (i.e. not a public inner class). – acedanger Mar 07 '13 at 00:35
  • It was, originally, but then I had a need to call it from another part of my application, so I made it an external class. – acedanger Mar 07 '13 at 00:36
  • I still think you are better off with that. I'm not sure what it being an external class matters – codeMagic Mar 07 '13 at 00:38
  • It may not matter. I did it before I knew as much about Java as I do now. I will try your answer. Thanks for being a sounding board and helping me. – acedanger Mar 07 '13 at 00:40
  • No, problem. Glad I could help. Like I said in my answer, I believe it will be less expensive than inflating another, essentially useless, `View`. – codeMagic Mar 07 '13 at 00:42
  • Added a note to my answer just in case. Hope it helps – codeMagic Mar 07 '13 at 00:48
  • Thanks again for your help. I don't have another Android developer to bounce ideas off of at home. My wife can only handle me talking to her about this kinda stuff for so long. It's always nice to be able to bounce ideas off of people who know understand what I'm trying to do. – acedanger Mar 07 '13 at 02:05
  • You are quite welcome and I understand that ;) That's what the community is here for – codeMagic Mar 07 '13 at 02:20

1 Answers1

2

Unless you have a reason to not use a background Thread, I suggest using an AsyncTask and using a progress bar. It will be less costly than using a dummy Layout just as a placeholder until you get the data. And you said it won't be used but 1% of the time. Seems like a waste in my opinion

 public class TalkToServer extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);

        }

        @Override
        protected String doInBackground(String... params) {
        //do your work here
            return something;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
               // do something with data here-display it or send to mainactivity
    }

You apparently know about AsyncTask but Here are the Docs for future visitors and it has an example of using the ProgressDialog.

Note

Since it isn't an inner class you will just need to pass your Context to the constructor of your AsyncTask if you are going to show your ProgressDialog from there.

codeMagic
  • 44,549
  • 13
  • 77
  • 93