Android UI How to - Post from TextView








The following code shows how to post Runnable from TextView.

Example

Layout xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:id="@+id/my_text_view"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Hello World, MainActivity!"/>

</LinearLayout>

Java code

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
  private TextView mTextView;

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

    mTextView = (TextView) findViewById(R.id.my_text_view);

    mTextView.post(new Runnable() {

      @Override
      public void run() {
        String size = String.format("TextView's width: %d, height: %d",
            mTextView.getWidth(), mTextView.getHeight());
        Toast.makeText(MainActivity.this, size, Toast.LENGTH_SHORT).show();

      }
    });
  }
}
null