I have a ViewSwitcher layout which used as a cell inside a TableLayout.
The ViewSwitcher contains two views and I have implemented an OnClickListener on the first child in order to switch views on click.
The problem is that when one cell of a TableRow is clicked, the view is switched, but all the other cells on the same row are resized magically.
- Example 1:
The view displays:
When I click on the left cell it gives:
And when I finally click on the right cell it gives:
- Example 2:
The view displays:
When I click on the right cell it gives:
And when I finally click on the left cell it gives:
My screenshots are not very high quality but one can see that the cell that has not been clicked is truncated as if it was moved down under the parent view from a few pixels.
The proof is that the green border does not show on the bottom.
I have taken a screenshot of the hierarchy viewer for example 1 step 2 here:
- The big rectangle on the background with the thin red border is the table row.
- The rectangle with the white border on the left is the clicked cell (ViewSwitcher).
- The red rectangle with the strong red border is the messed up cell (ViewSwitcher).
So you can see that it's been moved a few pixels below its original position which makes a blanck space on top of it inside the table row, and the bottom part of the cell is not visible.
I really don't know what's going so if you have an idea or if you'd like to try, here is the code:
The activity:
package my.app;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.ViewSwitcher;
public class SampleActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LayoutInflater mInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TableLayout table = (TableLayout) mInflater.inflate(R.layout.content, null);
TableRow row = new TableRow(this);
ViewSwitcher cell1 = (ViewSwitcher) mInflater.inflate(R.layout.cell, null);
cell1.findViewById(R.id.cell_view).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
((ViewSwitcher) v.getParent()).showNext();
}
});
row.addView(cell1);
ViewSwitcher cell2 = (ViewSwitcher) mInflater.inflate(R.layout.cell, null);
cell2.findViewById(R.id.cell_view).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
((ViewSwitcher) v.getParent()).showNext();
}
});
row.addView(cell2);
table.addView(row);
setContentView(table);
}
}
The layout content.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableLayout>
The layout cell.xml:
<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cell"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@drawable/cellborder"
android:padding="1px"
>
<TextView
android:id="@+id/cell_view"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@drawable/select"
android:text="TextView" />
<EditText
android:id="@+id/cell_edit"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:hint="EditText" />
</ViewSwitcher>
And the drawables if you also want the colors:
border.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle"
>
<solid android:color="#FFFFFFFF"/>
<stroke android:width="1dp" android:color="@android:color/holo_green_light"/>
</shape>
select.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" >
<shape>
<gradient
android:startColor="@android:color/white"
android:endColor="@android:color/white"/>
</shape>
</item>
<item android:state_focused="true" android:state_pressed="true">
<shape>
<gradient
android:startColor="@android:color/holo_blue_light"
android:endColor="@android:color/holo_blue_dark"/>
</shape>
</item>
<item android:state_pressed="true">
<shape>
<gradient
android:startColor="@android:color/holo_green_light"
android:endColor="@android:color/holo_green_dark"/>
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="@android:color/holo_orange_light"
android:endColor="@android:color/holo_orange_dark"/>
</shape>
</item>
</selector>
I surrounded each component in you cell.xml with a linear layout and it fixed your issue. Give it a try: