I'm trying to draw a line under a TextView (creating a rubber bridge score sheet, where the lines in question mirror the dotted lines here). I can't figure out how to get the position of the bottommost TextView and use those numbers to create a line in the proper position. The line is currently far below the TextView that it should be immediately below, i.e.:
40
——— <---- I want this (close to number)
40
——— <---- I get this (farther from number)
The line does change position depending on the vertical position of the bottommost TextView, so i think i am successfully retrieving its position, but somehow i'm not using it properly to draw the line.
Here's the code getting the position of the TextView and sending it to the line drawing method:
findViewById(R.id.main).getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//remove listener
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN )
findViewById(R.id.main).getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
else
findViewById(R.id.main).getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
TextView bottomTextView = (TextView) weGameLayout
.getChildAt(weGameLayout.getChildCount() - 1);
int[] pos = new int [2];
bottomTextView.getLocationOnScreen(pos);
int gameLinePosition = pos[1];
int height = bottomTextView.getHeight();
((BackgroundLines)findViewById(R.id.background_lines))
.addGameLine(gameLinePosition + height - topdiff);
}
}
);
And here's the line drawing method:
public void addGameLine(int position) {
ShapeDrawable line = new ShapeDrawable(new RectShape());
gameLines.add(line);
gameLinePositions.add(position);
invalidate();
}
@Override
public void onDraw(Canvas canvas) {
int width = this.getWidth();
for( int i=0; i < gameLines.size(); ++i ){
ShapeDrawable gameLine = gameLines.get(i);
int position = gameLinePositions.get(i);
gameLine.setBounds(0, position-2, width, position+2);
gameLine.draw(canvas);
}
}
Let me know if any more info is useful! It seems like a subtle problem and is a little tough to describe, but i think my ignorance of android's layout or drawing management is mostly at fault.
EDIT here's the main layout. It's long, but almost all the relevant stuff is right at the top i think
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/parchment_scroll_background"
tools:context="com.pianomansb.betterbridgescoresheet.MainActivity">
<com.pianomansb.betterbridgescoresheet.BackgroundLines
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/background_lines" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/we_column"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/we_upper"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/we_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/we_text"
style="@style/WeTheyFont"/>
<LinearLayout
android:id="@+id/we_bonus"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/we_game"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/they_column"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/they_upper"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/they_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/they_text"
style="@style/WeTheyFont"/>
<LinearLayout
android:id="@+id/they_bonus"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/they_game"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>