I am dynamically generating "row" TableLayout's at runtime. These are
being nested inside of a "master" TableLayout who is nested inside a
ScrollView who is nested inside a LinearLayout. I am setting a
onTouch against the individual "rows" to respond to events. One of
the things I would like to do is change the background when the "row"
is touched. Problem I am running into is when the background changes
it messes up the layout for the entire set of "rows" inside the
"master". For instance my right aligned column is no longer right
aligned.
Offending Code:
----------------------------------------------------------------
tableLayoutRow1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent
event) {
Log.d("OnTouch", "OnTouch Event for " +
Integer.toString(v.getId()) + " event:" +
Integer.toString(event.getAction()));
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.d("OnTouch",
"Changing to GREEN");
v.setBackgroundColor(Color.GRAY);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_MOVE:
Log.d("OnTouch",
"Changing to BLACK");
v.setBackgroundColor(Color.BLACK);
break;
}
return true;
}
});
COMPLETE CODE:
-----------------------------------------------------------------------
package com.developersinfo.testing;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class TableActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
linearLayout.setWeightSum(1);
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TableLayout tableLayoutMaster = new TableLayout(this);
tableLayoutMaster.setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// <table>
// <tr><td><table><tr><td></td><td></td><td></td></tr></
table></td></tr>
// <tr><td></td></tr>
// </table>
for(int i=0; i<=30; i++)
{
TableLayout tableLayoutRow1 = new TableLayout(this);
tableLayoutRow1.setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
tableLayoutRow1.setColumnStretchable(2, true);
//tableLayoutRow1.setBackgroundColor(Color.GREEN);
TableRow tableRow = new TableRow(this);
ImageView left_column = new ImageView(this);
left_column.setImageResource(17301550);
tableRow.addView(left_column);
TextView middle_column = new TextView(this);
middle_column.setText("Left Column:" + Integer.toString(i));
middle_column.setTypeface(null, Typeface.BOLD);
middle_column.setGravity(Gravity.LEFT);
middle_column.setPadding(5, 0, 0, 0);
tableRow.addView(middle_column);
TextView right_column = new TextView(this);
right_column.setText("Right Column:" + Integer.toString(i));
right_column.setTypeface(null, Typeface.BOLD);
right_column.setGravity(Gravity.RIGHT);
right_column.setPadding(0, 0, 5, 0);
tableRow.addView(right_column);
tableLayoutRow1.addView(tableRow);
tableRow = null;
// now for the bottom row
TextView bottom_column = new TextView(this);
bottom_column.setText("Starting activity
com.developersinfo.testing.TableActivity on device emulator-5554");
bottom_column.setTextSize(12f);
bottom_column.setGravity(Gravity.LEFT);
bottom_column.setPadding(30, 0, 0, 0);
tableLayoutRow1.addView(bottom_column);
tableLayoutRow1.setId(i);
tableLayoutRow1.setOnFocusChangeListener(new
OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean
hasFocus) {
// TODO Auto-generated method stub
Log.d("OnFocusChange", "OnFocusChange
Event for " +
Integer.toString(v.getId()));
}
});
tableLayoutRow1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("OnClick", "OnClick Event for " +
Integer.toString(v.getId()));
}
});
tableLayoutRow1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent
event) {
Log.d("OnTouch", "OnTouch Event for " +
Integer.toString(v.getId()) + " event:" +
Integer.toString(event.getAction()));
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.d("OnTouch",
"Changing to GREEN");
v.setBackgroundColor(Color.GRAY);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_MOVE:
Log.d("OnTouch",
"Changing to BLACK");
v.setBackgroundColor(Color.BLACK);
break;
}
return true;
}
});
// now add to master table
tableLayoutMaster.addView(tableLayoutRow1);
}
scrollView.addView(tableLayoutMaster);
linearLayout.addView(scrollView);
setContentView(linearLayout);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en