LoginSignup
13
14

More than 5 years have passed since last update.

動的に追加したTextViewのサイズ(幅)を取得する方法

Last updated at Posted at 2015-02-23

固定配置してるTextViewなら、onCreateでTextViewを宣言してたらgetWidth()で幅を取得出来るが、例えばリストをクリックする度に何文字になるか分からないTextViewを動的に追加するといった実装の場合、追加直後にはgetWidth()では取得結果が0になった。

そのため、やや無理矢理だがTextViewにsetされた文字数などから幅を割り出すことが出来ないかと調べたら、下記の書き方で取得出来た。

TextView newTextView = (TextView) layoutInflater.inflate(R.layout.hoge, null);
newTextView.setText(dynamic_text);    // 追加したいテキスト

int thisSize = (int)Layout.getDesiredWidth(newTextView.getText(), newTextView.getPaint())
13
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14