Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid ViewSwitcher cell in TableLayout moved and hidden magically
    primarykey
    data
    text
    <p>I have a ViewSwitcher layout which used as a cell inside a TableLayout.</p> <p>The ViewSwitcher contains two views and I have implemented an OnClickListener on the first child in order to switch views on click.</p> <p>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.</p> <ul> <li>Example 1:</li> </ul> <p>The view displays:</p> <p><img src="https://i.stack.imgur.com/JE4C8.png" alt="no action screen"></p> <p>When I click on the left cell it gives:</p> <p><img src="https://i.stack.imgur.com/66SQx.png" alt="select left cell"></p> <p>And when I finally click on the right cell it gives:</p> <p><img src="https://i.stack.imgur.com/XmW2p.png" alt="select right cell"></p> <ul> <li>Example 2:</li> </ul> <p>The view displays:</p> <p><img src="https://i.stack.imgur.com/JE4C8.png" alt="no action screen"></p> <p>When I click on the right cell it gives:</p> <p><img src="https://i.stack.imgur.com/Y1Rfj.png" alt="select right cell"></p> <p>And when I finally click on the left cell it gives:</p> <p><img src="https://i.stack.imgur.com/XmW2p.png" alt="select left cell"></p> <p>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.</p> <p>The proof is that the green border does not show on the bottom.</p> <p>I have taken a screenshot of the hierarchy viewer for example 1 step 2 here:</p> <p><img src="https://i.stack.imgur.com/4YaKR.png" alt="enter image description here"></p> <ul> <li>The big rectangle on the background with the thin red border is the table row.</li> <li>The rectangle with the white border on the left is the clicked cell (ViewSwitcher).</li> <li>The red rectangle with the strong red border is the messed up cell (ViewSwitcher).</li> </ul> <p>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.</p> <p>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:</p> <p>The activity:</p> <pre><code>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); } } </code></pre> <p>The layout content.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" &gt; &lt;/TableLayout&gt; </code></pre> <p>The layout cell.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;TextView android:id="@+id/cell_view" android:layout_width="fill_parent" android:layout_height="50dp" android:background="@drawable/select" android:text="TextView" /&gt; &lt;EditText android:id="@+id/cell_edit" android:layout_width="fill_parent" android:layout_height="50dp" android:hint="EditText" /&gt; &lt;/ViewSwitcher&gt; </code></pre> <p>And the drawables if you also want the colors:</p> <p>border.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape= "rectangle" &gt; &lt;solid android:color="#FFFFFFFF"/&gt; &lt;stroke android:width="1dp" android:color="@android:color/holo_green_light"/&gt; &lt;/shape&gt; </code></pre> <p>select.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_focused="true" &gt; &lt;shape&gt; &lt;gradient android:startColor="@android:color/white" android:endColor="@android:color/white"/&gt; &lt;/shape&gt; &lt;/item&gt; &lt;item android:state_focused="true" android:state_pressed="true"&gt; &lt;shape&gt; &lt;gradient android:startColor="@android:color/holo_blue_light" android:endColor="@android:color/holo_blue_dark"/&gt; &lt;/shape&gt; &lt;/item&gt; &lt;item android:state_pressed="true"&gt; &lt;shape&gt; &lt;gradient android:startColor="@android:color/holo_green_light" android:endColor="@android:color/holo_green_dark"/&gt; &lt;/shape&gt; &lt;/item&gt; &lt;item&gt; &lt;shape&gt; &lt;gradient android:startColor="@android:color/holo_orange_light" android:endColor="@android:color/holo_orange_dark"/&gt; &lt;/shape&gt; &lt;/item&gt; &lt;/selector&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload