简体   繁体   中英

How to get location of table layout cell in ANDROID

I am adding dynamic row with textview in table layout, Also I want xy coordinates of that textview. For that I used view.getLocationOnScreen(loc) method.

But is showing only last item coordinates, I want to show all item coordinates.

This is my source code please help me for that.

Thanks you.

public class MainActivity extends Activity {
TextView tv1, tv2;
TableRow row;
TextView txt;
TableLayout tl;

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

    tl = (TableLayout) findViewById(R.id.tableLayout1);
    for (int i = 0; i <= 15; i++) {
        row = new TableRow(this);
        txt = new TextView(this);

        tl.addView(row);
        row.addView(txt);
        readLocation();
    }
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
    readLocation();
}

private void readLocation() {
    int[] locationOnScreen = new int[2];
    txt.getLocationOnScreen(locationOnScreen);
    txt.setText(locationOnScreen[0] + " : " + locationOnScreen[1]);
}

}

and my following 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="fill_parent"
android:orientation="vertical" >

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </TableLayout>
</ScrollView>

You shouldn't try to get those coordinates in onCreate method in this way since layout procedures are not yet completed at this point.

Here is a little bit modified demo base on your code:

public class MainActivity extends Activity {

ArrayList<TextView> txt = new ArrayList<TextView>();
TableLayout tl;

Handler _h = new Handler();

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

    tl = (TableLayout) findViewById(R.id.tableLayout1);
    for (Integer i = 0; i <= 15; i++) {
        TableRow therow = new TableRow(this);
        TextView thetxt = new TextView(this);

        therow.addView(thetxt);
        tl.addView(therow);

        txt.add(thetxt);

        thetxt.setText("stub");
    }

    tl.requestLayout();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
protected void onResume() {
    super.onResume();
    _h.postDelayed(new Runnable() {

        @Override
        public void run() {

            for (Integer i = 0; i <= 15; i++) {

                TextView thetxt = txt.get(i);

                int[] locationOnScreen = new int[2];
                thetxt.getLocationOnScreen(locationOnScreen);
                thetxt.setText(locationOnScreen[0] + " : " + locationOnScreen[1]);
            }        


        }
    }, 1000);

}

}

The key point is postDelayed in onResume that lets the system to position all views correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM