0

My java code:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        final TextView textView = (TextView) findViewById(R.id.text1);
        final Button button = (Button) findViewById(R.id.button1);
        button.post(new Runnable() {
            @Override
            public void run() {
                int buttonWidth = button.getWidth();
                int textWidth = textView.getWidth();
                button.setWidth(buttonWidth-textWidth);
            }
        });

My xml views:

 <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Timer"
        android:textSize="16sp"/>

  <TextView
      android:id="@+id/text1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="8dp"
      android:text="+5"/>

I'm using the second way as described in this answer.

What I want to do is have the button fill the entire width with just enough space for the textView. I've been learning Android for the last few months only so it'll be helpful if you could explain in a lucid manner.

harsh99
  • 119
  • 9
  • 2
    You try to get width to early. check https://stackoverflow.com/questions/7733813/how-can-you-tell-when-a-layout-has-been-drawn – colorgreen Aug 09 '18 at 22:46
  • @colorgreen Can you tell me why the solution I've linked to doesn't work for me but apparently does for the other guys that tried it? The method you have referred to doesn't seem to be optimum so I'll try to avoid that. – harsh99 Aug 09 '18 at 23:15
  • i dont know but i thing time is a reason. Layout hasnt been yet inflated and it just a case that ```button.getWidth()``` in Runnable() was slower. Maybe you tried it on slower phone, I dont know. But for future if you want to get some layout params in onCreate you have to use solution from link – colorgreen Aug 09 '18 at 23:23
  • @colorgreen I don't understand how the button.post works at all. Care to clarify, please? – harsh99 Aug 09 '18 at 23:40
  • Oh.. honestly i first time meet this use case, but i found something like this https://stackoverflow.com/questions/8717145/android-when-to-use-button-post – colorgreen Aug 10 '18 at 01:13
  • @colorgreen seems a bit invalid in my situation. Thanks anyway. – harsh99 Aug 10 '18 at 15:50

2 Answers2

0

Use the addOnGlobalLayoutListener instead from the solution you linked, that usually works for me when I see this issue.

myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT > 16) {
                myView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
            else {
                myView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
            myView.getWidth();
        }
    });
CodeSmith
  • 1,621
  • 1
  • 13
  • 31
  • Calling it multiple times would just be a waste of resources, right? – harsh99 Aug 09 '18 at 23:04
  • Yes, if you only need the width to be non-zero once, you can limit the listener to only run once, I updated my response with code to do that. It won't kill you to run repeatedly though unless you're doing a lot of work, getting the width of a view shouldn't be an issue to run multiple times. – CodeSmith Aug 09 '18 at 23:21
  • Can you tell me how I can control the number of times it'll run? – harsh99 Aug 09 '18 at 23:37
0

you can use a linearlayout with your button having a weight of 1 and your textview to wrap content.

try this:

<?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="horizontal">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="some text" />

</LinearLayout>
Jian Liew
  • 16
  • 2