In our app we are trying to dynamically add fragments to a GridLayout. The empty grid layout is defined in XML as is the layout for the fragment. At run time we examine some data and from that determine the number of fragments to add to the layout as well as which layout to use for each fragment. When we have the fragment assign a size to its generated view it all works, however if we specify the size in the layout file for the fragment nothing shows up in the grid layout. Obviously we could simply specify the size when we create the view but we would prefer to do it in the xml layouts for the fragments because that would allow us to take advantage of Android's built in system for selecting the correct layouts for each device.
I am using support library fragments. I am NOT using support library GridLayout if that makes a difference
The relevant code and xml follows:
The GridLayout XML:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:id="#+id/grid_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_fragment"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="8dp"
android:layout_marginTop="?android:attr/actionBarSize"
android:overScrollMode="ifContentScrolls" >
<GridLayout
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alignmentMode="alignMargins"
android:animateLayoutChanges="true"
android:columnCount="3"
android:columnOrderPreserved="true"
android:orientation="horizontal"
android:overScrollMode="ifContentScrolls"
android:rowOrderPreserved="true" >
</GridLayout>
</ScrollView>
</merge>
An Example of the Fragment XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="100dp"
android:alpha="1" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="1.0" />
</RelativeLayout>
The Fragment onCreateView() Method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view;
GridLayout.Spec rowSpec = GridLayout.spec(mRowStart, mRowSpan);
GridLayout.Spec columnSpec;
GridLayout.LayoutParams childParams;
if (large) {;
view = inflater.inflate(R.layout.my_place_large, container, false);
columnSpec = GridLayout.spec(mColumnStart, 2);
childParams = new GridLayout.LayoutParams(rowSpec, columnSpec);
//childParams.width = 200; //If I do this everything works regardless of the layout size
} else {
view = inflater.inflate(R.layout.my_place_small, container, false);
columnSpec = GridLayout.spec(mColumnStart, 1);
childParams = new GridLayout.LayoutParams(rowSpec, columnSpec);
//childParams.width = 100; //If I do this everything works regardless of the layout size
}
childParams.setMargins(0, 0, 0, 0);
//childParams.height = 100; //If I do this everything works regardless of the layout size
view.setLayoutParams(childParams);
view.setId(ID);
return view;
}
To Add Fragments to the Layout
private void populateGrid() {
RelativeLayout gridParent = (RelativeLayout) mParentActivity.findViewById(R.id.locations);
mLocationsGrid = (GridLayout) gridParent.findViewById(R.id.grid);
nColumns = mLocationsGrid.getColumnCount();
mAdapter = new MyAdapter(mContext, this, mResolver); //This is how I keep track of the various fragments depending on my app's state
int nCards = mAdapter.getNumberOfCards();
FragmentManager fragmentManager = mParentActivity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for (int i = 0; i < nCards; ++i) {
fragmentTransaction.add(mLocationsGrid.getId(), mAdapter.getFragmentAtIndex(i), String.valueOf(i));
}
fragmentTransaction.commit();
mPopulated = true;
}
I think that should cover it. Just to reiterate, if I uncomment the lines which explicitly set the dimension in onCreateView(), they show up properly in GridLayout so I know everything that keeps track of the fragments and such works, as does the fragment transaction. The issue comes when I try and specify the size in the fragment's xml in which case I get a blank screen.
You thoughts, suggestions and musings are appreciated.
Thanks,
Jared
This is coming very late, but in the off chance this may still be of use. The problem is that you are overriding the XML layout parameters when you dynamically set the new LayoutParams. IE, when you do:
view.setLayoutParams(childParams);
This will erase the XMLs original height and width setting. Just because you are leaving the childParams width/height blank in code doesn't mean a value is not set. In a GridLayout's case, they are set to undefined.
The fix would be to first save the View's existing LayoutParam's height/width and use that when creating the new LayoutParam dynamically. Example:
if (large) {;
view = inflater.inflate(R.layout.my_place_large, container, false);
ViewGroup.LayoutParams oldParams = view.getLayoutParams();
columnSpec = GridLayout.spec(mColumnStart, 2);
childParams = new GridLayout.LayoutParams(rowSpec, columnSpec);
childParams.width = oldParams.width;
}
That will allow you to keep the width/height in XML while applying the row/col specs in code.
Related
Earlier on I asked a question about how to reach the last item of a scrollview and someone pointed out that I should be using NestedScrollView, at first it worked but now it's not what I want.
I want to fit my list of item in the ScrollView so only that part of the screen can be scrolled and the other parts stay at their place (the 3 TextView)
So basically my xml file is like this :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/cl_framgnent_detail_apero"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
tools:context=".ui.home.AperoDetailFragment">
<TextView
android:id="#+id/name_apero"
/>
<TextView
android:id="#+id/date_apero"
/>
<TextView
android:id="#+id/ingredient_title_apero"
/>
<ScrollView
android:id="#+id/rv_apero_ingredient"
android:layout_width="408dp"
android:layout_height="603dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/ingredient_title_apero"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ingredient_title_apero"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:id="#+id/vertical_layout_ingredient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
And in my Java code I try to populate my list with the following code :
public class AperoDetailFragment extends Fragment {
private View root;
private Apero detailApero;
public AperoDetailFragment(Apero apero) {
this.detailApero = apero;
}
#Override
public View onCreateView(#NonNull final LayoutInflater inflater,
final ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_detail_apero, container, false);
TextView name = (TextView) root.findViewById(R.id.name_apero);
name.setText(detailApero.getName());
TextView date = (TextView) root.findViewById(R.id.date_apero);
date.setText(detailApero.getDate());
LinearLayout ll = (LinearLayout) root.findViewById(R.id.vertical_layout_ingredient);
LinearLayout a = new LinearLayout(root.getContext());
ConstraintLayout.LayoutParams lparams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT);
a.setLayoutParams(lparams);
a.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < 20; i++) {
Button b = new Button(root.getContext());
b.setText("Button " + i);
a.addView(b);
}
ll.addView(a);
return root;
}
}
The problem is that the item are covering the whole screen instead of staying in the parent container (ScrollView) :
How can I fit my list of item to stay in the parent ?
If this is your actual code, then it might help to set constraints to the text views too.
Also, if your ScrollView has a fixed height, then you should remove either the top or bottom constraint. So if you want it to stick to the bottom, remove the top constraint.
I solved this with removing the ScrollView and using a ListView
I have an activity that has a ScrollView with a vertical LinearLayout that has two fragments that are PreferenceFragment instances as shown in the following layout file:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.foo.app.SettingsActivity">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.foo.app.SettingsFragment"
android:id="#+id/fragment_settings"/>
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.foo.app.NotificationSettingsFragment"
android:id="#+id/fragment_notification_settings"/>
</LinearLayout>
The problem is that the two fragments show up with just their PreferenceCategory title and the actual fragment UI is zero height and not visible. Oddly, it is possible to scroll each fragment individually and see the missing fragment UI. It is as if each Fragment is inside a ScrollView.
What I expected was for the two fragments to be sized to wrap their content and there be a single vertical slider to scroll the LinearLayout containing both fragments.
In case it is relevant, the two fragments extends android.preference.PreferenceFragment and do not define their layout in a layout file. Instead they load their Preference UI as follows:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.pref_notification);
}
TIA for your help.
Remove the outer ScrollView. I had the same problem and that solved it for me.
I haven't found it in the documentation yet, but apparently the PreferenceFragment supplies its own ScrollView around the PreferenceScreen. Somehow that leads to a wrap_content height just large enough to show the first element (e.g. a category header).
The question is quite old but in case someone else stumbles upon this, I've managed to find a solution. Just like #Ewoks mentioned, I had to settle for a fixed height as well (which didn't always play well with varying dpi of the devices).
But with the help of this code I managed to make the height dynamic, which wraps the content nicely.
In your PreferenceFragment class do as follows:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (getView() != null) {
ListView listView = (ListView) getView().findViewById(android.R.id.list);
Adapter adapter = listView.getAdapter();
if (adapter != null) {
int height = 0;
//int height = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < adapter.getCount(); i++) {
View item = adapter.getView(i, null, listView);
item.measure(0, 0);
height += item.getMeasuredHeight();
}
FrameLayout frame = (FrameLayout) getActivity().findViewById(R.id.content_frame); //Modify this for your fragment
ViewGroup.LayoutParams param = frame.getLayoutParams();
param.height = height + (listView.getDividerHeight() * adapter.getCount());
frame.setLayoutParams(param);
}
}
}
Hope this helps someone!
Given that you can't remove the outer ScrollView, what worked for me is to change it to be a android.support.v4.widget.NestedScrollView and then in Activity's onCreate to run:
findViewById(R.id.nested_scroll_view).setNestedScrollingEnabled(false);
Try setting the the android:layout_weight in each fragment to roughly the size of each settings list. E.g.
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:name="com.foo.app.SettingsFragment"
android:id="#+id/fragment_settings"/>
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:name="com.foo.app.NotificationSettingsFragment"
android:id="#+id/fragment_notification_settings"/>
My layout was working fine before I switched it to use fragments inside the DrawerLayout. After that the ListView in the main view ghosts a copy of the list when scrolling.
The ListView contents scroll, but a copy of the first page remains on screen. I've been logging debug messages and there is no second copy of listview created, and no second copy of data sent from the adapter.
The layout for the activity uses DrawerLayout with fragments before switching to fragments it worked fine.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="400dp"
android:layout_height="800dp">
<!-- The main content view -->
<fragment android:name="com.nosweatbudgets.receipts.ReceiptsFragment"
android:id="#+id/receipts_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!-- The navigation drawer -->
<fragment android:name="com.nosweatbudgets.navigate.NavigateFragment"
android:id="#+id/navigate_fragment"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
/>
</android.support.v4.widget.DrawerLayout>
The fragment classes are each very simple.
ReceiptsFragment's onCreateView looks like this.
View view = inflater.inflate(R.layout.receipts, container, false);
_adapter = new ReceiptsAdapter(inflater);
ListView list = (ListView) view.findViewById(R.id.receipt_list);
list.setAdapter(_adapter);
_adapter.Refresh();
return view;
NavigateFragment's onCreateView is basically the same with a different adapter. It's the ReceiptsFragment that is ghosting while the NavigateFragment does not ghost. Yet they can basically the same approach.
View view = inflater.inflate(R.layout.navigate, container, false);
_navigate = new NavigateAdapter(inflater);
// setup the list view for the side
ListView list = (ListView) view.findViewById(R.id.left_drawer);
list.setAdapter(_navigate);
_navigate.Refresh();
return view;
For the above fragment classes I'm using the following layouts.
receipts.xml looks like this.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/receipt_list"/>
</RelativeLayout>
navigate.xml looks like this.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:background="#EEEEEE"/>
</RelativeLayout>
The pull out navigate listview works just fine, but the main receipts listview has the ghosting problem.
I'm completely stumped as to what is causing this.
EDIT:
Here is how views are created in the ReceiptsAdapter.
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
view = _inflater.inflate(R.layout.receipt, null);
}
TextView txt_vendor = (TextView) view.findViewById(R.id.receipt_vendor);
TextView txt_category = (TextView) view.findViewById(R.id.receipt_category);
TextView category_btn = (TextView) view.findViewById(R.id.category_btn);
Model receipt = _receipts.get(position);
int vendor_id = receipt.getAsInteger("smart_vendor_id");
int category_id = receipt.getAsInteger("smart_category_id");
if (_vendors.indexOfKey(vendor_id) >= 0)
{
txt_vendor.setText(_vendors.get(vendor_id));
}
else
{
txt_vendor.setText("Select Vendor");
}
if (_categories.indexOfKey(category_id) >= 0)
{
String cat = _categories.get(category_id);
txt_category.setText(cat);
category_btn.setText(Category.ShortTitle(cat));
category_btn.setBackgroundColor(Category.Color(cat));
//txt_category.setBackgroundColor(c);
}
else
{
txt_category.setText("Select Category");
category_btn.setText("N/A");
}
((TextView) view.findViewById(R.id.receipt_amount)).setText("$" + receipt.getAsString("amount"));
((TextView) view.findViewById(R.id.receipt_payment)).setText(receipt.getAsString("payment"));
((TextView) view.findViewById(R.id.receipt_date)).setText(receipt.getAsString("receipt_date"));
return view;
}
EDIT:
This is how the Activity for the layout attaches the fragments via the onCreate method.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// the layout uses fixed width/size, because the layout editor won't render match_parent.
// so I replace the layout parameters here with match_parent.
DrawerLayout layout = (DrawerLayout) getLayoutInflater().inflate(R.layout.main, null);
DrawerLayout.LayoutParams param = new DrawerLayout.LayoutParams(ViewGroup.LayoutParams
.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(param);
setContentView(layout);
// configure this activity
StyleActionBar();
// Add the fragments
getSupportFragmentManager().beginTransaction()
.add(R.id.navigate_fragment, new NavigateFragment())
.add(R.id.receipts_fragment, new ReceiptsFragment())
.commit();
}
I mistakenly followed the fragments tutorial incorrectly, and created both a static fragment in the layout XML and also created a dynamic fragment for the same class at run-time.
The main layout used the <fragment> tag to create a fragment which is automatically attached at runtime via the android:name property.
This is what I have in my layout.xml
<fragment android:name="com.nosweatbudgets.receipts.ReceiptsFragment"
android:id="#+id/receipts_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
That was enough to create the fragment, but I mistakenly added code from the fragments tutorial in my activity as follows.
getSupportFragmentManager().beginTransaction()
.add(R.id.navigate_fragment, new NavigateFragment())
.add(R.id.receipts_fragment, new ReceiptsFragment())
.commit();
That added two additional fragment views to the activity at runtime. This resulted in two listviews for the same position.
What appeared to be a ghosting artifact was infact two fragments for the same class on top of each other. Removing the dynamic creation in onCreate resolved my problem.
For large screens I have two fragments, they shall be either top/bottom or left/right. The problem is that one fragment is taking (almost) all space is taking up by one fragment. For my activity I have two main.xml (one for portrait one for landscape). But they are basically similar (apart from orientation).
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:id="#+id/frag_cont" >
</LinearLayout>
In onCreate I add my fragments to that layout:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
OverviewFragment of = new OverviewFragment();
FragmentTransaction tof = getFragmentManager().beginTransaction();
tof.add(R.id.frag_cont, of);
DetailFragmentInitial df = new DetailFragmentInitial();
tof.add(R.id.frag_cont, df);
tof.commit();
}
The two fragments look kind of similar in terms of onCreateView()
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.overview, container, false);
}
Finally the two xml files for these fragments look like this, first the smaller fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1">
<ImageView
android:id="#+id/imageLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/logo"
android:layout_centerInParent="true" />
<TextView
android:id="#+id/textB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"
android:layout_above="#id/imageLogo"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
And now the greedy one taking the space:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="10000" >
<ListView
android:id="#+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
I assumed that when I set both layout_weight to 1 then both will take the same amount of space, but even with the setting 1000:1 the bigger one takes about 2/3 of the space. Leaving the weight away will give the bigger fragment all the screen and the other one is not visiable at all. Any ideas?
Edit:
I followed the approach given below. Much better now in terms of space. On screen rotate the overview fragment (basically list view) is empty, the other fragment is still okay.
In OverviewFragment.java I repopulate the fragment in onActivityCreated():
public void onSaveInstanceState(Bundle bundle)
{
bundle.putSerializable("list", mList);
super.onSaveInstanceState(bundle);
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null)
{
mList = (ArrayList<myObject>) savedInstanceState.getSerializable("list");
}
else
{
mAppList = new ArrayList<myObject>();
new getApps().execute();
}
ListView lv = (ListView) getActivity().findViewById(R.id.ListView01);
lv.setOnItemClickListener(new TableItemSelected());
mItemAdapter = new ItemAdapterOverview(getActivity().getApplicationContext(), mList);
lv.setAdapter(mItemAdapter);
}
when rotating the screen the onSaveInstanceState() is called first and then onActivityCreated() twice. The first call the saveInstanceState is not null, the second time it is. Anyway in both cases I should see my list, either build up from scratch or the saved list. Why isn't it and why is that method called twice?
Not entirely sure whether this will be causing your issue (I suspect it does), but using FragmentTransaction.add(..) doesn't add the Fragment into the container, it essentially replaces the container with that Fragment. The issue here is that you're adding both Fragments to the same container so they will take up the same space and cause issues.
A better way to do this would be to add two container views into your frag_cont LinearLayout (e.g. FrameLayouts), and give them two separate ids (e.g. frag_one and frag_two). Then in your onCreate(..), change it to read:
FragmentTransaction tof = getFragmentManager().beginTransaction();
tof.add(R.id.frag_one, of);
DetailFragmentInitial df = new DetailFragmentInitial();
tof.add(R.id.frag_two, df);
tof.commit();
This will make it so that the layout with id frag_one is filled by the OverviewFragment and the one with id frag_two replaced by the DetailFragmentInitial. You can then use the views in main.xml to tweak the sizes of the fragments as you see fit.
I want to show two views in one activity. If I clicked on button in the first view I want to see the second and other way round.
The views should not have the same size as the screen so I want e.g. to center it, like you see in first.xml.
But if I add the views with
addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
the views are not centered. They are shown at top left.
How can I use the xml settings to e.g. center it?
first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#drawable/background"
android:layout_gravity="center"
android:minWidth="100dp"
android:minHeight="100dp"
android:paddingBottom="5dp"
>
<LinearLayout android:id="#+id/head"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton android:id="#+id/first_button"
android:src="#drawable/show_second"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null" />
</LinearLayout>
second.xml same as first.xml but with
<ImageButton android:id="#+id/second_button"
android:src="#drawable/show_first"
... />
ShowMe.java
public class ShowMe extends Activity {
View mFirstView = null;
View mSecondView = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initFirstLayout();
initSecondLayout();
showFirst();
}
private void initFirstLayout() {
LayoutInflater inflater = getLayoutInflater();
mFirstView = inflater.inflate(R.layout.first, null);
getWindow().addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton firstButton = (ImageButton)mMaxiView.findViewById(R.id.first_button);
firstButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ShowMe.this.showSecond();
}
});
}
private void initSecondLayout() {
// like initMaxiLayout()
}
private void showFirst() {
mSecondView.setVisibility(View.INVISIBLE);
mFirstView.setVisibility(View.VISIBLE);
}
private void showSecond() {
mFirstView.setVisibility(View.INVISIBLE);
mSecondView.setVisibility(View.VISIBLE);
}}
Hope someone can help.
Thanks
Why don't you use setContentView(R.layout.yourlayout)? I believe the new LayoutParams you're passing in addContentView() are overriding those you defined in xml.
Moreover, ViewGroup.LayoutParams lacks the layout gravity setting, so you would have to use the right one for the layout you're going to add the view to (I suspect it's a FrameLayout, you can check with Hierarchy Viewer). This is also a general rule to follow. When using methods that take layout resources as arguments this is automatic (they might ask for the intended parent).
With this consideration in mind, you could set your layout params with:
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(/* wrap wrap */);
lp.setGravity(Gravity.CENTER);
addContentView(mYourView, lp);
But I would recommend setContentView() if you have no particular needs.
EDIT
I mean that you create a layout like:
~~~/res/layout/main.xml~~~
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="....."
android:id="#+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
then in your onCreate() or init...Layout():
setContentView(R.layout.main);
FrameLayout mainLayout = (FrameLayout)findViewById(R.id.mainLayout);
// this version of inflate() will automatically attach the view to the
// specified viewgroup.
mFirstView = inflater.inflate(R.layout.first, mainLayout, true);
this will keep the layout params from xml, because it knows what kind it needs. See reference.