Android Addicted Apps Creator

September 11, 2012

Make Activity using Theme.Dialog fill the entire screen

Filed under: Tips — alinberce @ 07:55
Tags: , , ,

   Admitted, you hate using AlertDialog for complex layouts that contain many controls. I hate it, it makes me feel so limited. Of course it has its own advantages, let’s not be mean, but sometimes a new Activity unleashes the power within.

So, how do we make an Activity look like a dialog ? Simple, just add in manifest:

 <activity
            android:name=".MyDialogActivity"
             android:theme="@android:style/Theme.Dialog" />

    But the problem is, even if we set our layout properties, to fill_screen, the activity will wrap around the content and its size will vary of the info. I thought, if the xml setting is ignored, maybe setting it in code might work… and surprise, it does work. All you have to do is:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.my_dialog_layout);

    //after setContentView, set the Layout parameters to fill parent, this makes our activity to go full screen
    getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
Advertisements

August 2, 2012

Tip #1 Make TableRow items of same width

Filed under: Android,Tips — alinberce @ 15:56

I usually don’t use TableLayout, but when I do I get annoyed by it. To be honest I find it way more flexible to use nested LinearLayouts as it will be easier in the future to update the design. I had this page where I needed a grid of 3×3 buttons. Of course, using LinearLayout over and over again wouldn’t have been too pleasant, so I went for the TableLayout. The curious thing was that even if I added a layout_weight=”1” for each button, in the end their width varied based on their text. After saying that TableLayout sucks… well… ergh… found the solution. Set layout_width=”0dip”. Simple yet effective.

So, to recap: if you want views from a TableRow to have the same width you need to set two properties, for each view:

 android:layout_width="0dip"
 android:layout_weight="1"

So in the end it would look something like:

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

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:text="I am equal with my brother button" />

             <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:text="I am equal with my brother button" />
        </TableRow>
...