0

I have an activity that has two fragments. One is a list and another for the detail. The weight of each are 1 and 3 respectively. Now what I want to do is that when the user taps one of the list objects another fragment will pop out from the right side of the screen that will occupy about 1/5 of the screen so basically the weights should be 1, 3 and 1. But what's happening is that the third fragment is occupying almost 95% of the screen.

Here's my code:

public class BracketActivity extends TorneyoBaseActivity implements Fragment2FragmentCommunicator {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bracket);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_bracket, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void loadItemToDetailFragment(Object item) {
        if(item instanceof Bracket){
            Bracket data = (Bracket) item;
            FragmentManager manager = getSupportFragmentManager();
            BracketDetailFragment bracketDetailFragment = (BracketDetailFragment) manager.findFragmentById(R.id.activity_fragment_bracket_detail);
            bracketDetailFragment.loadBracket(data);
            showMatchFragment();
        }
    }

    MatchListFragment matchFragment;
    public void showMatchFragment(){

        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_bracket_container);

        FragmentManager fm = getSupportFragmentManager();
        matchFragment = (MatchListFragment) fm.findFragmentByTag("MatchListFragment");
        if (matchFragment == null) {
            FragmentTransaction ft = fm.beginTransaction();
            matchFragment = new MatchListFragment();
            ft.add(linearLayout.getId(), matchFragment, "MatchListFragment");
            ft.commit();
        }
    }
}

This is the fragment getting called

public class MatchListFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_match_list, container, false);


        LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) view.getLayoutParams();
        param.weight = 1.0f;
        view.setLayoutParams(param);
        return view;
    }
}

It doesn't seem like the weight adjustment is working. Here's what it looks like atm.

Before click

After click

EDIT: XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/activity_bracket_container"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <fragment
        android:id="@+id/activity_fragment_bracket_list"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="1"
        class="BracketListFragment" />

    <fragment
        android:background="?android:attr/detailsElementBackground"
        android:id="@+id/activity_fragment_bracket_detail"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:layout_width="0dp"
        class="BracketDetailFragment" />


</LinearLayout>
gdubs
  • 2,724
  • 9
  • 55
  • 102

1 Answers1

0

following step step 1- add the linear layout width 0dp step 2- add the linear layout weight whatever your design like weight=1 step 3- add the linear layout root element weight sum like weight=4 and now check.

I hope this step is helping you.

Omey
  • 11
  • 3