androidandroid-layoutlayout-inflaterviewstub

How to use LayoutInflater / ViewStub for an overlay


As I am actually not very confident with programatically changing Views, I have following problem:

At the first start of my app, I want to have an overlay for the main screen, that tells the user to have a look at the settings, as there are two critical options the user has to configure.

I don't want to use an AlertDialog and rather not use a wizard. So, I decided to take an approach similar to Go SMS and create an overlay at the first start. The mockup I created looks like this:

Normal menu: Normal

First start: enter image description here

So these are the problems I have:

  1. Like I said, I don't want to use a screenshot overlaying on first start, as this would take too much space and would not be language and screen independent.
  2. I would have the circle as an png, but I don't know how exactly put it over the image
  3. The same problem with the text
  4. And finally I want to put a semi-transparent white over the app. It does not necessarily need the hole for the icon, though it would be nice.

In case you need the Layout Source, you can get it at pastebin

So, I just need to get a start here, if it is better to use LayoutInflater or ViewStub and how to realize it, as I have absolutely no experience with it...

Thanks!

/edit: I uploaded a new, more well-arranged layout.


Solution

  • I have faced a similar problem, I client wanted a walkthrough of the application, where the entire screen had to become whiter (as they said: "transparent"), except for the button being explained by an overlay speech-bubble.
    Fortunately for you, your layout is not nearly as complicated as the one I had to work with :)

    Now, you can get the transparency-effect in two ways, either have a white background and call all the views setAlpha() methods, or you can create a half-transparent white overlay.
    If you go with the overlay, you'll have to find a way to display the opaque buttons through the overlay. This can get a bit complicated. If you go with the first option, you can just setAlpha(1) on the opaque view to get it to show up.

    The setAlpha() method is only available from api version 11+, so if you target an earlier version, you might have to do it in a slightly more complicated way.
    Example of setting alpha on views pre-honeycomb:

    Layout for your buttons (make them however you want, just make them similar so you can loop through them):

    <LinearLayout 
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView 
            android:tag="image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/tile"/>
        <TextView 
            android:tag="text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FF000000"
            android:text="button1"/>
    </LinearLayout>
    

    In your program, when you are want to make the buttons transparent:

    LinearLayout l = (LinearLayout) findViewById(R.id.button1);
    ((ImageView)l.findViewWithTag("image")).setAlpha(0x7F);
    ((TextView)l.findViewWithTag("text")).setTextColor(0x7F000000);
    



    When you have decided on how you want to create the transparency effect, you will have to decide on how to display the overlay-text/bubble. You'll most likely want to put this in a separate layer on top of your entire layout, to make sure that it is not affected by your new view.
    One way to achieve this is by changing your root layout element to a FrameLayout, and then creating/displaying in this. e.g:

    <FrameLayout background="#FFFF"> <!-- white background, just in case -->
        <LinearLayout>
            <!-- the rest of your layout -->
        </LinearLayout>
        <LinearLayout visibility="gone"> <!-- this will be your overlay view -->
            <ImageView /> <!-- the arrow/ring -->
            <TextView /> <!-- the description -->
        </LinearLayout>
    </FrameLayout>
    

    When the introduction is displayed, you set the position of the hidden overlay-view to the position of the table item to be explained, change the text to an appropriate string/resource and display the view.

    When the introduction is over, you reset the alpha values of all buttons, and set the visibility of the overlay to gone again.