Question

I want to show an image when i'm connected to the server(i'm developping a SIP application). I've made this(created an XML file[connected.xml] with a textview and an image), but have a FC:

public void onRegistrationDone(String localProfileUri, long expiryTime) {
                        updateStatus("Registered to server.");
                        Log.d("SUCCEED","Registration DONE");
                        setContentView(R.layout.connected);
                      }

I want then add an image when connecting and disconnected... How can i solve this problem ? Thank you very much.

EDIT: XML:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

<TextView
  android:id="@+id/sipLabel"
  android:textSize="20sp"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/>

<ViewFlipper
    android:id="@+id/flipper" 
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <RelativeLayout android:layout_width="fill_parent" 
        android:layout_height="fill_parent">


  <ImageView android:id="@+id/disconnected" android:src="@drawable/disconnected" android:layout_below="@id/sipLabel" 
  android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_weight="0.35" android:gravity="center"
    />
    </RelativeLayout>

    <RelativeLayout android:layout_width="fill_parent" 
        android:layout_height="fill_parent">


  <ImageView android:id="@+id/connected" android:src="@drawable/connected" android:layout_below="@id/sipLabel" 
  android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_weight="0.35" android:gravity="center"
    />
    </RelativeLayout>

</ViewFlipper>
</RelativeLayout>
Was it helpful?

Solution

You shouldn't call setContentView multiple times in one activity, unless you are 100% sure that you've cleaned up everything in it.
Even then, your users might find strange, that pressing the back button they won't see what they were waiting, though starting the same activity.

So you should rather think about

  • changing your views' visibility to show/hide different parts of it, or
  • a more elegant and easier solution: use a ViewFlipper.

Update 1
Here is a sample usage of the ViewFlipper:
put these lines in your layout xml:

<ViewFlipper android:id="@+id/flipper" 
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <RelativeLayout android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
        <!-- your first view comes here -->
    </RelativeLayout>
    <RelativeLayout android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
        <!-- your second view comes here -->
    </RelativeLayout>
</ViewFlipper>

and in your java code, when you need to change the view, you write:

flipper = (ViewFlipper)findViewById(R.id.flipper);
flipper.showNext();

Extra: You can animate your flipping, all you have to do is set the in and out animation of your flipper before calling showNext or showPrevious:

Animation inAnimation = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT, +1.0f, 
        Animation.RELATIVE_TO_PARENT, 0.0f,
        Animation.RELATIVE_TO_PARENT, 0.0f, 
        Animation.RELATIVE_TO_PARENT, 0.0f);
inAnimation.setDuration(250);
inAnimation.setInterpolator(new AccelerateInterpolator());

Animation outAnimation = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT, 0.0f, 
        Animation.RELATIVE_TO_PARENT, -1.0f,
        Animation.RELATIVE_TO_PARENT, 0.0f, 
        Animation.RELATIVE_TO_PARENT, 0.0f);
outAnimation.setDuration(250);
outAnimation.setInterpolator(new AccelerateInterpolator());

flipper = (ViewFlipper)findViewById(R.id.flipper);
flipper.setInAnimation(inAnimation);
flipper.setOutAnimation(outAnimation);
flipper.showNext();

Update 2:

A sample for ViewFlipper with global header:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:id="@+id/status_text" android:text="Status: "
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <ViewFlipper android:id="@+id/flipper" android:layout_below="@id/status_text"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <RelativeLayout android:layout_width="fill_parent" 
            android:layout_height="fill_parent">
            <!-- your first view comes here without the status TextView -->
        </RelativeLayout>
        <RelativeLayout android:layout_width="fill_parent" 
            android:layout_height="fill_parent">
            <!-- your second view comes here -->
        </RelativeLayout>
    </ViewFlipper>
</RelativeLayout>

OTHER TIPS

Probably a thread problem: UI changes must happen on the Activity's main loop thread. Look at http://developer.android.com/reference/android/os/Handler.html:

  1. Create a Handler for the activity.
  2. Send a Message (http://developer.android.com/reference/android/os/Message.html) when the registration is done.
  3. In the Handler, receive that message and make your UI change.

Let me know how that works.

EDIT: fixed the message link

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top