I have android layout design in which I have an AppBar
layout which will contain a Toolbar
and one more LinearLayout
design with circle Drawable
as TextView
. which is off same height. So when I try to get the Toolbar
or AppBar
height/width it's returning only 0.
activity_main.xml:
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/colorPrimary"
app:layout_scrollFlags="enterAlways" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
android:orientation="horizontal"
app:layout_scrollFlags="enterAlways">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/marked_questions"
style="@style/textview_summary_omr_toolbar_count"
android:background="@drawable/bg_percentage_default"
android:text="18" />
<TextView
style="@style/textview_heading_summary_omr_toolbar"
android:text="Marked" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/correct"
style="@style/textview_summary_omr_toolbar_count"
android:background="@drawable/bg_percentage_6"
android:text="18"
/>
<TextView
style="@style/textview_heading_summary_omr_toolbar"
android:text="Correct" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/wrong"
style="@style/textview_summary_omr_toolbar_count"
android:background="@drawable/bg_percentage_wrong"
android:text="18" />
<TextView
style="@style/textview_heading_summary_omr_toolbar"
android:text="Wrong" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/conflicts"
style="@style/textview_summary_omr_toolbar_count"
android:background="@drawable/bg_percentage_3"
android:text="0" />
<TextView
style="@style/textview_heading_summary_omr_toolbar"
android:text="Conflicts" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
style="@style/textview_summary_omr_toolbar_count"
android:text="Score:"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/marks_scored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="18/30"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java:
Here I am initializing the Toolbar
and AppBar
and try to print the width and height of both in logs ,which is returning me zero.
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private AppBarLayout app_bar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
app_bar = (AppBarLayout) findViewById(R.id.app_bar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
logToolbarLayoutParams();
}
private void logToolbarLayoutParams() {
Log.d(TAG,"Toolbar width/Height"+this.toolbar.getWidth()+"/"+this.toolbar.getHeight());
Log.d(TAG,"App_bar width/height"+this.app_bar.getWidth()+"/"+this.app_bar.getHeight());
}
I have also referred to this question in the stackoverflow.
final AppBarLayout app_bar = (AppBarLayout) findViewById(R.id.app_bar);
ViewTreeObserver vto = app_bar.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
app_bar.getViewTreeObserver().removeGlobalOnLayoutListener(this);
Log.d(TAG, "Global layout");
logToolbarLayoutParams();
}
});
If I add the following code in my MainActivity
it's getting me the proper height and width of Toolbar
. From the stackoverflow question which I mentioned before, I came to know that, the views were not drawn on the screen, so I have to wait. But my doubt here is, the design which i have specified is not a complex one?
why its taking much time to draw on screen? am I missing something?
I have used Hierarchy Viewer to find out, what's making the child layout to load slowly. I found that I am using mulitple LinearLayout
where single RelativeLayout
can be used to obtain the same structure. I have changed my layout design as the following.
Still I am facing the same issue.
updated: activity_main.xml:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/colorPrimary"
app:layout_scrollFlags="enterAlways" />
<RelativeLayout
android:id="@+id/summary_bottom_sheet"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/marked_questions"
style="@style/textview_summary_omr_toolbar_count"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@drawable/bg_percentage_default"
android:text="18" />
<TextView
android:id="@+id/marked_textview"
style="@style/textview_heading_summary_omr_toolbar"
android:layout_toEndOf="@+id/marked_questions"
android:layout_toRightOf="@+id/marked_questions"
android:text="Marked" />
<TextView
android:id="@+id/correct"
style="@style/textview_summary_omr_toolbar_count"
android:layout_toEndOf="@+id/marked_textview"
android:layout_toRightOf="@+id/marked_textview"
android:background="@drawable/bg_percentage_6"
android:text="18"
/>
<TextView
android:id="@+id/correct_textview"
style="@style/textview_heading_summary_omr_toolbar"
android:layout_toEndOf="@+id/correct"
android:layout_toRightOf="@+id/correct"
android:text="Correct" />
<TextView
android:id="@+id/wrong"
style="@style/textview_summary_omr_toolbar_count"
android:layout_toEndOf="@+id/correct_textview"
android:layout_toRightOf="@+id/correct_textview"
android:background="@drawable/bg_percentage_wrong"
android:text="18" />
<TextView
android:id="@+id/wrong_textview"
style="@style/textview_heading_summary_omr_toolbar"
android:layout_toEndOf="@+id/wrong"
android:layout_toRightOf="@+id/wrong"
android:text="Wrong" />
<TextView
android:id="@+id/conflicts"
style="@style/textview_summary_omr_toolbar_count"
android:layout_toEndOf="@+id/wrong_textview"
android:layout_toRightOf="@+id/wrong_textview"
android:background="@drawable/bg_percentage_3"
android:text="0" />
<TextView
android:id="@+id/conflicts_textview"
style="@style/textview_heading_summary_omr_toolbar"
android:layout_toEndOf="@+id/conflicts"
android:layout_toRightOf="@+id/conflicts"
android:text="Conflicts" />
<TextView
android:id="@+id/score_textview"
style="@style/textview_summary_omr_toolbar_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/conflicts_textview"
android:layout_toRightOf="@+id/conflicts_textview"
android:background="@null"
android:gravity="center"
android:text="Score:"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/marks_scored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_toEndOf="@+id/score_textview"
android:layout_toRightOf="@+id/score_textview"
android:gravity="center"
android:text="18/30"
android:textColor="@android:color/white"
android:textSize="18sp" />
</RelativeLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
You're thinking about this wrong. There is nothing wrong with your layout (as it relates to your question). To prove it, completely change your layout and try again
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
Log.d("SomeTag", "w: " + recyclerView.getWidth() + ", h: " + recyclerView.getHeight());
Log.d("SomeTag", "mw: " + recyclerView.getMeasuredWidth() + ", mh: " + recyclerView.getMeasuredHeight());
}
The output for a view with only 1 child is still
12-14 10:17:04.902 3004-3004/? D/SomeTag: w: 0, h: 0
12-14 10:17:04.902 3004-3004/? D/SomeTag: mw: 0, mh: 0
So there is nothing wrong with your layout (except you do have a misnomer. You call the method logToolbarLayoutParams, but you aren't accessing the layout params at all. You are trying to get properties on the view object before the layout pass has occurred. Maybe this is your confusion). So what you're missing is an understanding of Android's layout system, viewgroups, and views. Romain Guy had a really cool talk at one point on Devoxx about how to build a custom FlowLayout. Either the site is down or the video is pulled but here's the code in GitHub If you look at the method onLayout, you can see that a ViewGroup loops through all of it's children and calls child.layout(). Until this method is called, the view.getWidth and view.getHeight methods will return 0. And this method is called by the OS whenver it schedules it. So basically, you are trying to access the width and height immediately even before the OS scheduled a layout. Make a custom viewgroup yourself, add some break points, and give it a shot.