Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Methods to get position of view returns 0

This question has been asked several times, but everyone gives the reason for why this occurs (i.e. calculation occurs before the layout is laid). But I need the solution for this. I tried getBottom();, getTop();, getLocationOnScreen(int[] location);. But all returns the value Zero (0). I even tried giving these in onStart();, to give time for layout to be laid, but no luck. Here is my code:

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

    tv = (TextView) findViewById(R.id.textView1);
    Log.d("getBottom   :", Integer.toString(tv.getBottom()));
    Log.d("gettop      :", Integer.toString(tv.getTop()));

    int[] location = new int[2];
    tv.getLocationOnScreen(location);
    Log.d("getlocationonscreen values :", Integer.toString(location[0])+"  "+Integer.toString(location[1]));
}
@Override
protected void onStart() {
    Log.d("getBottom in onStart :", Integer.toString(tv.getBottom()));
    Log.d("gettop in onStart    :", Integer.toString(tv.getTop()));

    int[] location = new int[2];
    tv.getLocationOnScreen(location);
    Log.d("getlocationonscreen in onStart :", Integer.toString(location[0])+"  "+Integer.toString(location[1]));
    super.onStart();
}

Layout XML:

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="156dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

Again, I apologize for repeating the question. Thanks in advance.

like image 696
Kartik Avatar asked Jan 16 '12 11:01

Kartik


People also ask

What is focusable in Android?

Focusable means that it can gain the focus from an input device like a keyboard. Input devices like keyboards cannot decide which view to send its input events to based on the inputs itself, so they send them to the view that has focus.

What is view in Xml?

A View occupies a rectangular area on the screen and is responsible for drawing and event handling. Views are used for Drawing Shapes like Circles,Rectangles,Ovals etc . Just Use View with background and apply a Shape using Custom Drawable.

How do you find the height of a view?

You can use the window. innerHeight property to get the viewport height, and the window. innerWidth to get its width. let viewportHeight = window.

What is Android view view?

View is a basic building block of UI (User Interface) in android. A view is a small rectangular box that responds to user inputs. Eg: EditText, Button, CheckBox, etc. ViewGroup is an invisible container of other views (child views) and other ViewGroup.


1 Answers

You put the 'measuring' in the onWindowFocusChanged()-method.
As the documentation states:

This is the best indicator of whether this activity is visible to the user.

You could also put it in the onResume() which is the last step before the application is completely on screen and active, however:

Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user (for example, to resume a game).

If the window/view has not yet been displayed there is no guarantee that it has its measurements, thus the previous method would be better.

like image 170
Jave Avatar answered Oct 28 '22 10:10

Jave