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>
Related
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
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 have been having an issue with my card view where android studio is putting way too much space in between them. Here is an example screenshot I took from the emulator. For my layout inflater here is the code I used:
#Override
public DriveStatsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
{
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_layout, viewGroup, false);
DriveStatsViewHolder dsvh = new DriveStatsViewHolder(v);
return dsvh;
}
Below is my cardview_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<android.support.v7.widget.CardView
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<TextView
android:id="#+id/FinishTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Drive_Date"
android:layout_toStartOf="#+id/Total_Drive_Time"
android:textAlignment="textEnd"
android:textSize="18sp"
tools:text="Finish Time"/>
<TextView
android:id="#+id/Total_Drive_Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:textAlignment="viewStart"
android:textSize="18sp"
tools:text="Total Drive Time"/>
<TextView
android:id="#+id/Drive_Date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="24sp"
tools:text="Default Text"/>
<TextView
android:id="#+id/Start_Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Drive_Date"
android:textSize="18sp"
tools:text="Start Time"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
I am pretty new to android studio so any help would be appreciated.
Your outer RelativeLayout's height is match_parent, so each item in your list is as tall as your screen. However, the CardView itself only has a height of wrap_content, so most of that space is going to be left empty.
I have a recyclerview using the following xml as a list item for each of its views
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="2dp">
<android.support.constraint.ConstraintLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/item_question_name"
style="?android:attr/textAppearanceLarge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/app_margin"
app:layout_constraintBottom_toTopOf="#+id/item_question_question"
app:layout_constraintEnd_toStartOf="#+id/item_question_needs_sync"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="This is the name" />
<android.support.v7.widget.AppCompatImageView android:id="#+id/item_question_needs_sync"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="#+id/item_question_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/item_question_name"
app:srcCompat="#drawable/ic_sync" />
<TextView android:id="#+id/item_question_question"
style="?android:attr/textAppearanceMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/app_margin"
app:layout_constraintBottom_toTopOf="#+id/item_question_type"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/item_question_name"
app:layout_constraintTop_toBottomOf="#+id/item_question_name"
tools:text="this is the question" />
<TextView android:id="#+id/item_question_type"
style="?android:attr/textAppearanceMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/app_margin"
android:layout_marginTop="#dimen/app_margin"
app:layout_constraintBottom_toTopOf="#+id/item_question_separator"
app:layout_constraintEnd_toStartOf="#+id/item_question_info"
app:layout_constraintHorizontal_weight="5"
app:layout_constraintStart_toStartOf="#+id/item_question_question"
app:layout_constraintTop_toBottomOf="#+id/item_question_question"
tools:text="Four faces which are awesome" />
<TextView android:id="#+id/item_question_info"
style="?android:attr/textAppearanceSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/item_question_type"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="3"
app:layout_constraintStart_toEndOf="#+id/item_question_type"
app:layout_constraintTop_toTopOf="#+id/item_question_type"
tools:text="answered ALL the questions" />
<View android:id="#+id/item_question_separator"
android:layout_width="0dp"
android:layout_height="2dp"
android:background="#color/colorAccent"
app:layout_constraintBottom_toTopOf="#+id/item_question_buttons"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/item_question_info" />
<android.support.constraint.ConstraintLayout android:id="#+id/item_question_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/item_question_separator">
<Button android:id="#+id/item_question_delete"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/item_question_button_delete"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/item_question_edit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button android:id="#+id/item_question_edit"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/item_question_button_edit"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="#+id/item_question_delete"
app:layout_constraintEnd_toStartOf="#+id/item_question_view"
app:layout_constraintStart_toEndOf="#+id/item_question_delete"
app:layout_constraintTop_toTopOf="#+id/item_question_delete" />
<Button android:id="#+id/item_question_view"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/item_question_button_view"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="#+id/item_question_delete"
app:layout_constraintEnd_toStartOf="#+id/item_question_start"
app:layout_constraintStart_toEndOf="#+id/item_question_edit"
app:layout_constraintTop_toTopOf="#+id/item_question_delete" />
<Button android:id="#+id/item_question_start"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/item_question_button_start"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="#+id/item_question_delete"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/item_question_view"
app:layout_constraintTop_toTopOf="#+id/item_question_delete" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
my problem is that when the user clicks on the view I have the viewholder toggling whether the #+id/item_question_buttons will be shown or not like so:
void bind_view(Info info, boolean selected) {
//load the data from info
.....
// _buttons is a View object where I stored the constraint layout with the buttons
if (selected) {
_buttons.setVisibility(VISIBLE);
} else {
_buttons.setVisibility(GONE);
}
}
my problem is that every other time (i.e. the 2nd 4th 6th etc) the user toggles the visibility of the _buttons View the view expands to triple the normal size (which is the same as the max height a button can have).
I solved it by using android:maxLines="1" but I was wandering , why does this happen? and is there a better way to fix this?
thanks in advance for any help you can provide