I add a view to my project like so:
View inflater = getLayoutInflater().inflate(R.layout.fragment_miles, null, true);
//Add views
LinearLayout contentDraw = (LinearLayout) findViewById(R.id.linear_main);
contentDraw.addView(inflater);
However when I display the view I get a blank page.
Here is my view layout file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linear_miles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<FrameLayout
android:id="#+id/rootMilesFrag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:visibility="gone"
android:layout_weight="9"
app:layout_constraintTop_toTopOf="#+id/nav_miles">
<TextView
android:id="#+id/text_miles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:textColor="#000000"
android:text="Hello Miles!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_miles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:layout_weight="1"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
</FrameLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I cannot seem to figure out what is happening.
Here is my code for loading the view:
FrameLayout mLayoutMiles = (FrameLayout) findViewById(R.id.rootMilesFrag);
for (int i = 0; i < mLayoutMiles.getChildCount(); i++) {
View v = mLayoutMiles.getChildAt(i);
v.setVisibility(View.VISIBLE);
v.postInvalidate();
}
mLayoutMiles.setVisibility(View.VISIBLE);
mLayoutMiles.postInvalidate();
The page does show the bottom navigation panel at the top of the screen, however it does not show the edit text or the green background.
Related
I'm having a problem with my toolbar.
I want the device app and toolbar to be the same color, but don't use findviewbyid. (I'm migrating the entire project just to view binding
)
OK behavior (using findviewbyid).
NOK behavior (using view binding), is giving a little misalignment of the textView, it seems to receive a margin that was not placed.
But if I declare different in onWindowsInsetsChanged method or don't use it, I get another problem, the color is not correct.
Using findviewbyid:
override fun onWindowsInsetsChanged(view: View, insets: WindowInsets, padding: InitialPadding) {
with(binding) {
val toolbar = fragmentEditObjectiveNameRoot.findViewById<View>(R.id.toolbar)
toolbar.updatePadding(top = topInset)
editObjectiveNameRoot.updatePadding(
bottom = insets.systemWindowInsetBottom
)
}
}
Using view binding:
override fun onWindowsInsetsChanged(view: View, insets: WindowInsets, padding: InitialPadding) {
with(binding) {
//calling the include toolbar and the root of the toolbar fragment
toolbar.viewImageToolbar.updatePadding(
top = topInset
)
editObjectiveNameRoot.updatePadding(
bottom = insets.systemWindowInsetBottom
)
}
}
Different declaration in onWindowsInsetsChanged:
override fun onWindowsInsetsChanged(view: View, insets: WindowInsets, padding: InitialPadding) {
with(binding) {
//calling root from fragment and instead of calling the include toolbar and its root
fragmentEditObjectiveNameRoot.updatePadding(
top = topInset
)
editObjectiveNameRoot.updatePadding(
bottom = insets.systemWindowInsetBottom
)
}
}
I would like to leave the same color behavior when using findviewbyid, but with view binding, but without losing textView alignment.
Here is the xml that receives the toolbar include:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/fragmentEditObjectiveNameRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/editObjectiveNameRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/bgColorFrameLayout"
android:layout_width="match_parent"
android:layout_height="#dimen/color_frame_layout_height"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/toolbar" />
<androidx.core.widget.NestedScrollView
android:id="#+id/scrollView"
android:layout_width="0dp"
android:layout_height="0dp"
android:fillViewport="true"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/toolbar">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="#dimen/screen_margin_horizontal"
android:layout_marginTop="18dp"
android:layout_marginEnd="#dimen/screen_margin_horizontal"
android:paddingBottom="16dp">
<com.google.android.material.card.MaterialCardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/bari_white"
app:cardCornerRadius="#dimen/card_corner_radius"
app:cardElevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/card_padding">
<TextView
android:id="#+id/titleSubAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/sub_account_control_settings_edit_name"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<br.com.bancobari.core_ui.views.text_input.BariTextInputLayout
android:id="#+id/nameInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:helperIconEnabled="true"
app:helperStart_enabled="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/titleSubAccount">
<br.com.bancobari.core_ui.views.text_input.BariTextInputEditText
android:id="#+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName|textCapWords"
android:maxLength="16"
android:textAlignment="center"
android:textSize="16sp" />
</br.com.bancobari.core_ui.views.text_input.BariTextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<br.com.bancobari.core_ui.views.SubmitButton
android:id="#+id/saveButton"
style="#style/DefaultButton.Icon.Arrow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/objective_settings_save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/cardView"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<include
android:id="#+id/toolbar"
layout="#layout/view_image_toolbar" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Here the toolbar xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/viewImageToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:minHeight="#dimen/custom_toolbar_height"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:layout_width="0dp"
android:layout_height="#dimen/custom_toolbar_height" />
</androidx.appcompat.widget.Toolbar>
<TextView
android:id="#+id/iconEmojiView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="10dp"
android:gravity="bottom"
android:textSize="22.5sp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="#id/toolbarTitleTextView"
app:layout_constraintEnd_toStartOf="#+id/toolbarTitleTextView"
tools:visibility="visible" />
<TextView
android:id="#+id/toolbarTitleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ellipsize="end"
android:fontFamily="#font/nunito_bold"
android:gravity="center"
android:lines="1"
android:textColor="#color/text_neutral_gray_800"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#id/viewImageToolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Teste" />
<ImageView
android:id="#+id/toolbarRightIconImageView"
style="#style/Widget.AppCompat.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_settings"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="#id/toolbarTitleTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#id/toolbarTitleTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
In your first block of code with findViewById, this
val toolbar = fragmentEditObjectiveNameRoot.findViewById<View>(R.id.toolbar)
returns the ConstraintLayout at the root of your included layout with the ID toolbar.
The property toolbar in your binding is another binding for the included layout rather than the root view of that layout. The root property of that included binding in this case is the same ConstraintLayout from above, so you should use instead:
toolbar.root.updatePadding(
top = topInset
)
What you were doing was changing the padding of an inner view of your complete Toolbar layout, the viewImageToolbar element.
so im getting this W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1 spamming the logs, the answer I've read here suggests its a bug that was fixed but is still present, but wondered if anyone has a work around, the user that posted the question found it was an issue when using max lines and ellipsize but im not using either, if anyone can help I'd appreciate it, i have a view pager inside a view pager that displays a card with a bottom navigation view and some tabs (please don't question the design there is a method in this madness) here is my inner view pager layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:attrs="http://schemas.android.com/tools"
android:id="#+id/slide_background"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/title_guideline"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.16" />
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/ThemeOverlay.MyTitleText"
android:text="#string/choose_a_theme"
android:textColor="#color/background_light"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/title_guideline"
android:gravity="center"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/content"
android:layout_width="match_parent"
app:layout_constraintTop_toBottomOf="#id/title_guideline"
app:layout_constraintBottom_toTopOf="#id/spacer"
android:layout_height="0dp">
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/pageIndicatorView"/>
<com.rd.PageIndicatorView
android:id="#+id/pageIndicatorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/view_pager"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="#id/sub_title_guideline"
android:layout_marginTop="16dp"
app:piv_animationType="slide"
app:piv_dynamicCount="true"
app:piv_interactiveAnimation="true"
app:piv_selectedColor="#android:color/white"
app:piv_unselectedColor="#color/accent_yellow"
app:piv_viewPager="#id/view_pager"
attrs:piv_padding="12dp"
attrs:piv_radius="8dp"/>
<androidx.constraintlayout.widget.Guideline
android:id="#+id/sub_title_guideline"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.84" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/ThemeOverlay.MyBodyText"
app:layout_constraintTop_toBottomOf="#id/sub_title_guideline"
app:layout_constraintBottom_toBottomOf="parent"
android:gravity="center"
android:paddingTop="#dimen/activity_horizontal_margin_4dp"
android:paddingBottom="#dimen/activity_horizontal_margin_4dp"
android:paddingStart="#dimen/activity_horizontal_margin_32dp"
android:paddingEnd="#dimen/activity_horizontal_margin_32dp"
android:id="#+id/subTitle"
android:textColor="#color/background_light"
android:text="#string/please_choose_a_theme"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/spacer"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
and here is my view holder that contains a toolbar, tabs and bottom nav bar, the whole thing is essentially a dummy layout to give a user a preview of what a layout will look like given a particular theme, there is nothing in any of my styles except setting colours,
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintWidth_percent="0.8"
app:layout_constraintWidth_max="#dimen/max_create_profile_card"
app:cardElevation="#dimen/activity_horizontal_margin_8dp"
app:cardCornerRadius="8dp"
android:layout_margin="#dimen/activity_horizontal_margin_4dp"
app:cardBackgroundColor="#color/background_light">
<androidx.appcompat.widget.Toolbar
style="?attr/toolbarStyle"
android:layout_width="match_parent"
android:layout_height="56dp"
android:id="#+id/preview_toolbar"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="#+id/sentence_container"
app:layout_constraintTop_toTopOf="parent"/>
<com.google.android.material.tabs.TabLayout
style="?attr/tabLayoutStyle"
android:layout_width="match_parent"
android:layout_height="56dp"
android:id="#+id/preview_tabs"
app:layout_constraintTop_toBottomOf="#id/sentence_container">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.google.android.material.tabs.TabLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="#+id/space"
app:layout_constraintTop_toBottomOf="#id/preview_tabs"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background_light"
style="#style/Widget.MaterialComponents.BottomNavigationView.Colored"
app:layout_constraintTop_toBottomOf="#id/space"
app:menu="#menu/navigation" />
<com.github.sealstudios.fab.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/speak_fab"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="#id/preview_tabs"
android:src="#drawable/ic_play_arrow_white_24dp"
app:fab_size="mini"
android:layout_margin="#dimen/fab_margin"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
i set these attributes to my tablayout
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
and i set tab icons and text like this
private void setUpMaterialTabs(String[] labels,int[] tabIcons){
for (int i = 0; i < labels.length; i++){
int index = i * 2;
Tab tab = tabLayout.getTabAt(i);
if (theme.equals(Constants.THEME_YELLOW)){
Objects.requireNonNull(tab).setIcon(tabIcons[index]);
}else{
Objects.requireNonNull(tab).setIcon(tabIcons[index + 1]);
}
tab.setText(labels[i]);
}
setTabTextColor(theme, getActivity());
}
ok using my own tab avoids the error
for (int i = 0; i < tabLabels.length; i++) {
TextView textView;
int index = i * 2;
if (theme.equals(Constants.THEME_YELLOW)) {
textView = (TextView) LayoutInflater.from(getActivity())
.inflate(R.layout.custom_tab_dark, null);
textView.setCompoundDrawablesWithIntrinsicBounds(0, tabImage[index], 0, 0);
} else {
textView = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.custom_tab, null);
textView.setCompoundDrawablesWithIntrinsicBounds(0, tabImage[index + 1], 0, 0);
}
textView.setText(tabLabels[i]);
tabLayout.getTabAt(i).setCustomView(textView);
}
I have a frame layout that I add different height views to depending on different circumstances.
I would like to know the Y-axis position of the frame layout before and after the view has been added.
So before I add the view I call getY() on the frame layout which returns 1891 which is correct, once the new view has been added it still returns 1891 which is wrong, it should be less as the frame layout is higher up the screen.
I've checked the layout inspector for both circumstances and getY() gives the correct value so why doesn't my code?
I tried to call rootLayout.requestLayout() to redraw the layout after adding the view but that didn't work.
Log.d(TAG, "openActionBar: " + String.valueOf(actionFrameLayout.getY()));
LayoutInflater.from(getContext()).inflate(R.layout.main_menu_3_item, frameLayout, true);
questionRoot.requestLayout();
Log.d(TAG, "openActionBar: " + String.valueOf(actionFrameLayout.getY()));
Here is my XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/questionRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground"
android:clickable="true"
android:focusable="true"
tools:context=".QuestionFragments.Question">
<com.englishquestionstogo.CustomViews.CustomChronometer
android:id="#+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/actionFrameLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/questionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold"
tools:text="Title" />
<TextView
android:id="#+id/questionBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/questionTitle"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:paddingBottom="32dp"
android:textSize="16sp" />
</RelativeLayout>
</ScrollView>
<View
android:id="#+id/topBorder"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#D3D3D3"
app:layout_constraintBottom_toTopOf="#id/frameLayout"
/>
<View
android:id="#+id/bottomBorder"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#D3D3D3"
app:layout_constraintBottom_toBottomOf="parent"
/>
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/white"
app:layout_constraintBottom_toTopOf="#id/bottomBorder"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
<FrameLayout
android:id="#+id/actionFrameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="#id/frameLayout"
>
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
You should use addOnGlobalLayoutListener
actionFrameLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
//Remove the listener before proceeding
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
actionFrameLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
actionFrameLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
// get the absolute coordinates of a view
Log.d(TAG, "openActionBar: " + String.valueOf(actionFrameLayout.getY()));
}
}
);
Current Implementation:
Requirement:
Below is the custom _tab xml which I am inflating into Tablayout and also I need the indicator to take the width of custom square box.I have added the custom_tab.xml layout file.please let me know how to implement one like requirement.
Added tab.xml too
Custom_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="10dp"
app:cardCornerRadius="8dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/round_corners_rectangle"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView_tabs"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_marginTop="-15dp"
android:contentDescription="#string/app_name"
android:src="#drawable/agent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView_tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="rooms"
android:textColor="#color/white"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView_tabs" />
<View
android:id="#+id/underLine"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_marginBottom="5dp"
android:background="#drawable/chat_indicator_line"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_tabs" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
Tab.xml:
<LinearLayout
android:id="#+id/linear_tab"
android:layout_width="match_parent"
android:layout_height="90dp"
android:alpha="0.6">
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#color/black"
android:baselineAligned="false"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
app:tabIndicatorColor="#color/white"
app:tabIndicatorHeight="3dp"
app:tabPaddingEnd="7dp"
app:tabPaddingStart="3dp" />
</LinearLayout>
Below is the code How I am adding customview programmatically:
private void createTabs() {
for (int i = 0; i < tabArray.length; i++) {
View viewCustomTab =
LayoutInflater.from(this).inflate(R.layout.custom_tab, null);//get custom view
viewCustomTab.setBackgroundColor(getResources().getColor(R.color.black));
ImageView imageViewTab = viewCustomTab.findViewById(R.id.imageView_tabs);
imageViewTab.setImageResource(tabIcons[i]);
TextView textViewTab =
viewCustomTab.findViewById(R.id.textView_tabs);
textViewTab.setText(tabArray[i]);
TabLayout.Tab tab = mTabLayout.getTabAt(i);//get tab via position
if (tab != null) {
tab.setCustomView(viewCustomTab);//set custom view
}
}
}
I've attached 3 screenshots of a DialogFragment. In this dialog, I wanna show a RecyclerView. When the dialog opens, the width of first 2 items is shrunk. But after a little bit of scrolling when 3rd item comes up, it is displayed with expected width[Check screenshot 1 & 2]. If I continue scrolling from top to bottom then I see the rest of the items shown normally. Then again I scroll from bottom to top and surprisingly noticed that 1st and 2nd items are also shown as expected.[check screenshot no. 3]
I would like to share my codes.
From my Adapter class:
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new CustomViewHolder(view);
}
item.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingTop="8dp"
android:paddingStart="16dp"
android:paddingEnd="16dp">
<TextView
android:id="#+id/modalityTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#id/modalityTitle"
tools:text="X-Ray"
android:textSize="22sp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"/>
<ImageView
android:id="#+id/statusImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="#id/modalityTextView"
app:layout_constraintBottom_toBottomOf="#id/modalityTextView"
app:layout_constraintRight_toRightOf="parent"
android:src="#drawable/ic_remove_circle_outline_black_24dp"/>
<TextView
android:id="#+id/contrastTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/modalityTextView"
app:layout_constraintLeft_toLeftOf="parent"
android:text="#string/contrast"
android:textStyle="bold"
android:layout_marginTop="4dp"/>
<TextView
android:id="#+id/contrastTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/modalityTextView"
app:layout_constraintLeft_toRightOf="#id/contrastTitle"
tools:text="Oral contrast"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"/>
<TextView
android:id="#+id/bodyPartTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/contrastTitle"
app:layout_constraintLeft_toLeftOf="parent"
android:text="#string/bodyPart"
android:textStyle="bold"
android:layout_marginTop="4dp"/>
<TextView
android:id="#+id/bodyPartTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/contrastTextView"
app:layout_constraintLeft_toRightOf="#id/bodyPartTitle"
tools:text="Chest"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"/>
<TextView
android:id="#+id/procedureTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/bodyPartTitle"
app:layout_constraintLeft_toLeftOf="parent"
android:text="#string/procedureDescription"
android:textStyle="bold"
android:layout_marginTop="4dp"/>
<TextView
android:id="#+id/procedureDescriptionTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/bodyPartTitle"
app:layout_constraintLeft_toRightOf="#id/procedureTitle"
app:layout_constraintRight_toRightOf="parent"
android:text="This is a procedure description. It will be a long text"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"/>
<TextView
android:id="#+id/checkedInAtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/procedureDescriptionTextView"
app:layout_constraintLeft_toLeftOf="parent"
android:text="#string/checked_in_at"
android:textStyle="bold"
android:layout_marginTop="4dp"/>
<TextView
android:id="#+id/checkedInAtTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/procedureDescriptionTextView"
app:layout_constraintLeft_toRightOf="#id/checkedInAtTitle"
tools:text="5:00 PM"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"/>
<View
android:layout_width="0dp"
android:layout_height="2dp"
android:background="#color/grey"
app:layout_constraintTop_toBottomOf="#id/checkedInAtTextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"/>
</android.support.constraint.ConstraintLayout>
DialogFragment xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="400dp"
tools:context="com.alemhealth.ticketcapture.Features.CheckInListShow.StudyListDialog.CheckInDialogFragment">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:id="#+id/toolbarTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/check_in_information"
android:textColor="#color/white"
android:textSize="18sp" />
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:id="#+id/studyListRecyclerView"
android:layout_width="0dp"
android:layout_height="250dp"
app:layout_constraintTop_toBottomOf="#id/toolbar"
app:layout_constraintBottom_toTopOf="#+id/closeButton"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="16dp">
</android.support.v7.widget.RecyclerView>
<Button
android:id="#+id/closeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/studyListRecyclerView"
app:layout_constraintBottom_toBottomOf="parent"
android:background="#drawable/custom_button"
android:textColor="#color/buttonTextColor"
android:text="#string/close"
android:layout_marginBottom="8dp"/>
</android.support.constraint.ConstraintLayout>
Custom theme for DialogFragment:
<style name="DialogStyle" parent="Base.Theme.AppCompat.Dialog">
<item name="android:windowMinWidthMajor">57%</item>
<item name="android:windowMinWidthMinor">57%</item>
<item name="android:textColor">#color/text_color</item>
</style>
onCreateView of DialogFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_check_in_dialog, container, false);
StudyListForDialog studyListForDialog = (StudyListForDialog) getArguments().getSerializable("data");
if(studyListForDialog!=null){
toolbarTextView.setText(studyListForDialog.getPatientName() + " - " + studyListForDialog.getPatientAge() + " - " + studyListForDialog.getPatientGender());
studyListRecyclerViewAdapter = new StudyListRecyclerViewAdapter(studyListForDialog, getActivity(), this);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(studyListRecyclerViewAdapter);
}
return view;
}
AND in this way I open my DialogFragment:
Bundle bundle = new Bundle();
bundle.putSerializable("data", data);
FragmentManager fragmentManager = ((Activity)view.getContext()).getFragmentManager();
CheckInDialogFragment checkInDialogFragment = new CheckInDialogFragment();
checkInDialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
checkInDialogFragment.setArguments(bundle);
checkInDialogFragment.show(fragmentManager, "check-in");
Please help me fix this weird problem.
May be it's a bug of ConstaintLayout.
I updated my DialogFragment XML root with LinearLayout instead of ConstraintLayout. And it solved the shrinking problem.
My updated DialogFragment xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary">
<TextView
android:id="#+id/toolbarTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/check_in_information"
android:textColor="#color/white"
android:textSize="18sp" />
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:id="#+id/studyListRecyclerView"
android:layout_width="match_parent"
android:layout_height="250dp"
app:layout_constraintTop_toBottomOf="#id/toolbar"
app:layout_constraintBottom_toTopOf="#+id/closeButton"
android:layout_marginBottom="16dp">
</android.support.v7.widget.RecyclerView>
<Button
android:id="#+id/closeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_button"
android:textColor="#color/buttonTextColor"
android:text="#string/close"
android:layout_marginBottom="8dp"
android:layout_gravity="center"/>
</LinearLayout>