Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: using fragments programmatically

For some reasons I can't use xml layout files.

But I need to create a tablet android app.

I decided to use fragments.

I want to create the same layout that this xml generates:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <fragment class="com.example.ItemsList"
            android:id="@+id/items_list" android:layout_weight="1"
            android:layout_width="0dp" android:layout_height="fill_parent" />

    <FrameLayout android:id="@+id/item_details" android:layout_weight="1"
            android:layout_width="0dp" android:layout_height="fill_parent"
            android:background="?android:attr/detailsElementBackground" />

</LinearLayout>

But I have problems with adding fragments to my linearLayout:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(createUI());
    }

    private View createUI() {
        LinearLayout layout = new LinearLayout(this);

        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setLayoutParams(new LinearLayout.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.FILL_PARENT));
        layout.setId(0x101);
        {
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.add(0x101, new ItemsList());
            fragmentTransaction.add(0x101, new ItemDetails());
            fragmentTransaction.commit();
        }
        return layout;
    }

Actually I can't even create LinearLayout with two identical fragments:

            ...
            fragmentTransaction.add(0x101, new ItemsList());
            fragmentTransaction.add(0x101, new ItemsList());
            ...

Please help

Btw I can't figure out why do we need to declare "FrameLayout" for itemDetails Fragment but "fragment" is enough for itemsList ListFragment?

UPD:

To do so one should just add third param:

            ...
            fragmentTransaction.add(0x101, new ItemsList(), "uniqueTag1");
            fragmentTransaction.add(0x101, new ItemsList(), "uniqueTag2");
            ...

Default value for the tag parameter is null, so I was trying to create two different elements with identical ids. Thanks to p-lo for his comment.

like image 204
leshka Avatar asked Jul 29 '11 10:07

leshka


2 Answers

I've found the solution.

(the better way was added to the question. Thanks, p-lo)

If you want to place more than one fragment on activity you should create layouts for each fragment:

    private View createUI() {
        LinearLayout layout = new LinearLayout(this);

        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setLayoutParams(new LinearLayout.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.FILL_PARENT));


        LinearLayout innerLayout1 = new LinearLayout(this);
        innerLayout1.setLayoutParams(new LinearLayout.LayoutParams(300, ViewGroup.LayoutParams.FILL_PARENT));
        innerLayout1.setId(ITEMS_LIST_ID);
        {
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.add(ITEMS_LIST_ID, new ItemsList());
            fragmentTransaction.commit();
        }
        layout.addView(innerLayout1);

        LinearLayout innerLayout2 = new LinearLayout(this);
        innerLayout2.setLayoutParams(new LinearLayout.LayoutParams(300, ViewGroup.LayoutParams.FILL_PARENT));
        innerLayout2.setId(ITEM_DETAILS_ID);
        {
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.add(ITEM_DETAILS_ID, new ItemDetails());
            fragmentTransaction.commit();
        }
        layout.addView(innerLayout2);

        return layout;


    }
like image 92
leshka Avatar answered Sep 22 '22 00:09

leshka


What you're missing is the LayoutInflator. See this example from an activity:

private void createPolicyTable() {

    if (customerInfo.getPolicies() == null || customerInfo.getPolicies().isEmpty()) {
        Log.v(TAG, "no policies were found.");
        return;
    }

    LinearLayout policyTable = (LinearLayout)findViewById(R.id.manage_my_policy_policy_table);      
    LayoutInflater inflater = getLayoutInflater();
    for(int i = 0; i < customerInfo.getPolicies().size(); i++) {
        Policy policy = customerInfo.getPolicies().get(i);

        LinearLayout row = (LinearLayout)inflater.inflate(R.layout.policy_table_row, null);

        ImageView type = (ImageView)row.findViewById(R.id.policy_table_row_type);

        if (policy instanceof PersonalVehiclePolicy) {
            type.setImageResource(R.drawable.auto_policy);
        }
        else if (policy instanceof HomeownersPolicy) {
            type.setImageResource(R.drawable.home_policy);
        }

        ((TextView)row.findViewById(R.id.policy_table_row_policy_number)).setText(policy.getPolicyNumber());
        ((TextView)row.findViewById(R.id.policy_table_row_amount_due)).setText(policy.getAmountDue());
        ((TextView)row.findViewById(R.id.policy_table_row_due_date)).setText(policy.getDueDate());

        row.setOnClickListener(createPolicyDetailsOnClickListener(policy));

        policyTable.addView(row);

    }

}

and the layout that's being inflated:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android" style="@style/ListItem">

    <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
      android:stretchColumns="*">
        <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" 
          android:paddingBottom="5dp">
            <TextView android:text="@string/type_label" style="@style/RowLabel" />

            <ImageView android:id="@+id/policy_table_row_type" android:layout_width="wrap_content"
              android:layout_height="wrap_content" />         
            <TextView style="@style/RowLabel" android:text="@string/policy_num_label" 
              android:paddingRight="5dp" />
            <TextView android:id="@+id/policy_table_row_policy_number" style="@style/DefaultText" />
        </TableRow>

        <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" 
          android:paddingBottom="5dp">
            <TextView style="@style/RowLabel" android:text="@string/due_label" android:paddingRight="5dp" />
            <TextView android:id="@+id/policy_table_row_amount_due" style="@style/DefaultText" android:paddingRight="5dp" />
            <TextView style="@style/RowLabel" android:text="@string/on_lc" android:paddingRight="5dp" />
            <TextView android:id="@+id/policy_table_row_due_date" style="@style/DefaultText" />
        </TableRow>
    </TableLayout>


</LinearLayout>
like image 45
Zack Marrapese Avatar answered Sep 20 '22 00:09

Zack Marrapese