Change the size of a fragment with a button



I'm trying to change the weight of "MapView" and "InfoContent" when i click on a button inside the mapview fragment. The mapView Fragment is defined in the PlaceholderFragment2 Java Class. The first click should change the weight to a new value, while the second click should restore the old value.


XMl:



<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>

<FrameLayout
android:id="@+id/TitleBar"
android:layout_width="fill_parent"
android:layout_height="50dp"
>
</FrameLayout>

<FrameLayout
android:id="@+id/MapView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>

<FrameLayout
android:id="@+id/InfoTitleBar"
android:layout_width="fill_parent"
android:layout_height="170dp"
>
</FrameLayout>

<FrameLayout
android:id="@+id/InfoContent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0"
>
</FrameLayout></LinearLayout>


MainActivity.Java



public class MainActivity extends Activity {

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

PlaceholderFragment frg=new PlaceholderFragment();
PlaceholderFragment2 frg2=new PlaceholderFragment2();
PlaceholderFragment3 frg3=new PlaceholderFragment3();
PlaceholderFragment4 frg4=new PlaceholderFragment4();

FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();

transaction.add(R.id.TitleBar, frg, "Frag_Top_tag");
transaction.add(R.id.MapView, frg2, "Frag_Middle_tag");
transaction.add(R.id.InfoTitleBar, frg3, "Frag_Bottom_tag");
transaction.add(R.id.InfoContent, frg4, "Frag_Content_tag");

transaction.commit();
}


And PlaceHolderFragment2.java



public static class PlaceholderFragment2 extends Fragment {

public PlaceholderFragment2() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.mapview, container,
false);

final Button button =
(Button) rootView.findViewById(R.id.zoomMap);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonClicked(v);
}
});

return rootView;
}

No comments:

Post a Comment