Thursday, August 29, 2013

How to move or put the android button in the middle of the screen

Ques: How to centre buttons on screen horizontally and vertically ?

Default Screen:




Ans: You need to use a Relative Layout for android GUI in the activity_fullscreen.xml  or whatever you name it. You just need to add the android:layout_centerInParent="true" on your relative layout scope.

In default android generate the layout as linear layout for your GUI activity. In that activity you can't use android:layout_centerInParent="true".


It will generate warning as ---

Invalid layout param in a LinearLayout: layout_centerInParent    activity_fullscreen.xml    /YourProject/res/layout    line 52    Android Lint Problem
How?:  

Step 1. First open the layout xml from /YourProject/res/layout .

Step 2. Change all of the <FrameLayout> ... </FrameLayout> to <RelativeLayout> ... </RelativeLayout>

Step 3.  Delete default button code portion from you xml and add the following

    <RelativeLayout
            android:id="@+id/fullscreen_content_controls"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:orientation="vertical"
            tools:ignore="UselessParent" >

            <Button
                android:id="@+id/dummy_button"
                android:layout_width="180dip"
                android:layout_height="80dip"
                android:layout_centerInParent="true"
                android:layout_marginTop="100dip"
                android:text="@string/dummy_button" />
        </RelativeLayout>
Step 4. Use the bold attribute to arrange your button on screen.


Now the output looks like ---





No comments:

Post a Comment

How to Generate and use the ssh key on Gerrit, github.io, gitlab, and bitbucket.

 Details can be found here -