1

I'm trying to make buttons wich would change part of the context, but the match_parent not working propertly.

got java main file:

class public MainActivity extends ActionBarActivity {

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

        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.drawable.ic_launcher);

        Button bn1 = (Button) findViewById(R.id.navbar_1);
        Button bn2 = (Button) findViewById(R.id.navbar_2);

        bn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentMeters fr = new FragmentMeters();
                FragmentManager fm = getFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.layout_to_replace, fr);
                ft.commit();

            }
        });

        bn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentValues fr = new FragmentValues();
                FragmentManager fm = getFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.layout_to_replace, fr);
                ft.commit();



            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

}

Then another java class FragmentMeters (and similar FragmentValues):

public class FragmentMeters extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_meters, null);

        return view;
    }

}

And main XML, something like this:

<LinearLayout
    android:id="@+id/navbar"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="82dp"
    android:background="@color/highlighted_text_material_dark"
    android:weightSum="4">

    <Button
        android:id="@+id/navbar_1"
        android:text="@string/navbar_1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textSize="13sp"
        android:background="@android:color/transparent"
        android:gravity="bottom|center_horizontal" />
    <Button
        android:id="@+id/navbar_2"
        android:text="@string/navbar_2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textSize="13sp"
        android:background="@android:color/transparent"
        android:gravity="bottom|center_horizontal" />
</LinearLayout>

<LinearLayout
    android:id="@+id/layout_to_replace"
    android:orientation="horizontal"
    android:layout_below="@+id/navbar"
    android:layout_width="match_parent"
    android:background="@color/dim_foreground_disabled_material_dark"
    android:layout_height="82dp"
    android:weightSum="4"/>

And I'm trying to replace the LinearLayout (id="layout_to_replace") with this fragment_meters.xml:

 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_below="@+id/navbar"
        android:layout_width="match_parent"
        android:layout_height="82dp"
        android:background="@color/dim_foreground_disabled_material_dark"
        android:weightSum="4">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="METERS"/>

    </LinearLayout>

But I'm getting this:

enter image description here

Instead of matching it parent's width/height.

codePG
  • 1,754
  • 1
  • 12
  • 19
Tomáš John
  • 314
  • 4
  • 14

4 Answers4

3

i know this issue,in some cases when inflating a layout the arguments layout_width and layout_height change to wrap content.

i suggest to you to set layout params manually after inflating:

View view = inflater.inflate(R.layout.fragment_meters, null);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

(for height put what u want)

Liran Peretz
  • 580
  • 8
  • 11
0

Deleting android:weightSum="4" from the LinearLayout with the id @+id/layout_to_replace should solve your problem.

You should also consider adding the gravity attribute on the corresponding TextView.

0

the best way to it is add resource file in values folder that called dimens.xml, and add there a dimen, like this:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <dimen name="width">100dp</dimen>
    <dimen name="height">100dp</dimen>
</resources>

and in the java code you should get those values from resources by context, like this:

int width = getApplicationContext().getResources().getDimension(R.dimen.width);
int height = getApplicationContext().getResources().getDimension(R.dimen.height);
Liran Peretz
  • 580
  • 8
  • 11
  • Thanks, just had to use getAction(). instead of getApplicationContext(). because of using it inside of Fragment. Wich is returning the float. Thanks again ! – Tomáš John Dec 25 '14 at 19:14
0

@tomáš-john, the issue is exactly like @liran-peretz assumed: your view from fragment_meters does not have "match_parent" in runtime. But the solution should be different.

The reason for this is because you passing null as "view root" when you inflate this view in the FragmentMeters! When no "view root" is passed at inflation - then layout params of "root element" from fragment_meters xml is ignored and thus this is not "match_parent" in LinearLayout.

Instead you have to do: inflater.inflate(R.layout.fragment_meters, parent)

or with attachToRoot set to false (when you don't want your view to be added to the parent). inflater.inflate(R.layout.fragment_meters, parent, false);

Some more links from the following answer could be helpful as well: https://stackoverflow.com/a/25528527/3134602

Community
  • 1
  • 1
Alex
  • 521
  • 7
  • 7