How to implement custom tabs with indicator below the custom tab - android

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
}
}
}

Related

set transperent background for constraint layout in android

hi I have to make a view like the below image .
But when i make the parent view transperent , default background is showing like this:
a transperent view is showing behind the main view.
my xml starts like :
<?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"
android:layout_margin="#dimen/_8sdp"
android:background="#color/red"
android:clipChildren="false"
android:clipToPadding="false">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/cl2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_50sdp"
android:background="#drawable/rounded_corner_shadow"
android:clipChildren="false"
android:clipToPadding="false"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<FrameLayout
android:id="#+id/progressLayout_frame"
android:layout_width="#dimen/_90sdp"
android:layout_height="#dimen/_90sdp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:clickable="true"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:id="#+id/bgCircle"
android:layout_width="#dimen/_70sdp"
android:layout_height="#dimen/_70sdp"
android:layout_gravity="center"
android:alpha="0.7"
android:background="#color/white"
android:visibility="visible" />
<RelativeLayout
android:id="#+id/progressLayout"
android:layout_width="#dimen/_80sdp"
android:layout_height="#dimen/_80sdp"
android:layout_gravity="center"
android:background="#android:color/transparent"
android:clickable="true"
>
<ProgressBar
android:id="#+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="#dimen/_80sdp"
android:layout_height="#dimen/_80sdp"
android:layout_centerHorizontal="true"
android:background="#drawable/circle_shape"
android:progressDrawable="#drawable/circular_progress_bar" />
<TextView
android:id="#+id/tvTimeCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="60dp"
android:text="00:00"
android:textColor="#FFFFFF"
android:textSize="29sp"
app:customTypeFace="roboto_light" />
how can I hide the view behind . why does this layout come ? any suggestions would be helpful . thanks in advance
To remove default background of the dialog...
Step 1:
Create a custom_dialog.xml
<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"
android:layout_marginHorizontal="#dimen/_20sdp"
android:paddingHorizontal="#dimen/_25sdp"
android:paddingVertical="#dimen/_25sdp" >
<androidx.constraintlayout.widget.Guideline
android:id="#+id/left_vertical_gl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.02" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/right_vertical_gl"
android:layout_width="match_parent"
android:layout_height="1dp"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.98" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/center_horizontal_gl"
android:layout_width="match_parent"
android:layout_height="1dp"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.48" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/top_horizontal_gl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.044" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/end_horizontal_gl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.90" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/segui_black"
android:text="Are You Sure?"
android:textColor="#484A67"
android:textSize="#dimen/_24ssp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/top_horizontal_gl" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_2sdp"
android:layout_marginBottom="12dp"
android:fontFamily="#font/segoe"
android:gravity="center"
android:text="Are you sure you want to sign out?"
android:textColor="#484A67"
android:textSize="#dimen/_11ssp"
app:layout_constraintEnd_toStartOf="#+id/right_vertical_gl"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView9" />
<TextView
android:id="#+id/yesTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_20sdp"
android:fontFamily="#font/segui_semi_bold"
android:padding="#dimen/_5sdp"
android:text="Yes"
android:textColor="#AD7BFF"
app:layout_constraintEnd_toStartOf="#+id/right_vertical_gl"
app:layout_constraintTop_toBottomOf="#+id/textView10" />
<TextView
android:id="#+id/noTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/_30sdp"
android:fontFamily="#font/segoe"
android:padding="#dimen/_5sdp"
android:text="No"
android:textColor="#6D6E85"
app:layout_constraintBottom_toBottomOf="#+id/yesTv"
app:layout_constraintEnd_toStartOf="#+id/yesTv"
app:layout_constraintTop_toTopOf="#+id/yesTv"
tools:layout_editor_absoluteX="143dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 2:
Go to your dialog activity
Add the below line to your dialog code
popUpDialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
Complete code of the dialog
private lateinit var popUpDialog: Dialog
private fun buyLifeLineDialog() {
popUpDialog.setContentView(R.layout.dialog_buy_lifelines)
popUpDialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
val yesTv = popUpDialog.findViewById<TextView>(R.id.yesTv);
val noTv = popUpDialog.findViewById<TextView>(R.id.noTv);
popUpDialog.show()
yesTv.setOnClickListener {
if (isCheatLl) {
mViewModel.buyCheatLifeline(index,lifeLinesPackagesModel)
}
else{
mViewModel.buyRespawnLifeline(index,lifeLinesPackagesModel)
}
popUpDialog.dismiss()
}
noTv.setOnClickListener {
popUpDialog.dismiss()
}
}
I don't know how do you display this window in your app , but I suppose that you display it as custom dialog , so the problem is that you need to add a transparent theme for your dialog to get the same design or your dialog will fill the transparent spaces with this black color or white depends on your app theme , so to solve this problem you need to add this style in your theme.xml file or style.xml
<style name="AlertDialogCustom" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
</style>
and then you create a new custom dialog in your activity like this
//Here i will create new dialog with a custom style which I added in Theme.xml
final AlertDialog dialog2 = new AlertDialog.Builder(MainActivity.this,R.style.AlertDialogCustom).create();
LayoutInflater inflater = getLayoutInflater();
//Here I added my xml file , you can replace test with your own xml file
View convertView = (View) inflater.inflate(R.layout.test, null);
dialog2.setView(convertView);
dialog2.setCancelable(false);
//Here we tell the dialog to display , you can call show() when click a button
dialog2.show();
and here it's my simple design to get a transparent background
<?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="350dp"
android:layout_margin="8dp"
android:background="#android:color/transparent"
android:clipChildren="false"
android:clipToPadding="false">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/cl2"
android:layout_width="350dp"
android:layout_height="350dp"
android:layout_marginTop="50dp"
android:background="#FFFFFF"
android:clipChildren="false"
android:clipToPadding="false"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<FrameLayout
android:id="#+id/progressLayout_frame"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:clickable="true"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:id="#+id/bgCircle"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="#drawable/pers_icon"
android:visibility="visible" />
<RelativeLayout
android:id="#+id/progressLayout"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="#android:color/transparent"
android:clickable="true">
</RelativeLayout>
</FrameLayout>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="218dp"
tools:layout_editor_absoluteY="129dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Now this is the final result , you can make your own design

Android add view blank

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.

W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1

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);
}

getY() value doesnt change after changing the height of view

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()));
}
}
);

Inside a DialogFragment the RecyclerView item width shrink before scrolling

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>

Categories

Resources