I want to show a PDF in a custom Dialog. I fetch PDF from a service as a ByteArray. I pass the ByteArray to PDFView object and then inflate the Dialog. All the views in the dialog are visible except the pdfview.
Here is my activity code:
val fileName = "filename.pdf"
val dialog = Dialog(this)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(true)
dialog.setContentView(R.layout.activity_pdf_view)
val pdfView = dialog.findViewById(R.id.pdfView) as PDFView
val closeBtn = dialog.findViewById(R.id.closeBtn) as ImageButton
val downloadBtn = dialog.findViewById(R.id.downloadBtn) as Button
pdfView.fromBytes(byteArray).load()
dialog.show()
Below is the xml activity_pdf_view.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/ticketLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#drawable/bg_ticket_layout">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Ticket"
android:textSize="15sp"
android:textColor="#color/black"
android:layout_margin="25dp"/>
<com.github.barteksc.pdfviewer.PDFView
android:id="#+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="#id/title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="39dp"
android:layout_marginBottom="8dp"
android:layout_marginHorizontal="8dp"/>
<ImageButton
android:id="#+id/closeBtn"
android:layout_width="11dp"
android:layout_height="11dp"
android:background="#color/white"
android:contentDescription="#string/close"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_cancel_icon"
android:layout_marginTop="20dp"
android:layout_marginEnd="24dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="#+id/downloadBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/submit_btn_bg"
android:drawableStart="#drawable/ic_file_download_white"
android:text="#string/download_pdf"
android:textColor="#color/white"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="#id/ticketLayout"
app:layout_constraintStart_toStartOf="#id/ticketLayout"
app:layout_constraintTop_toBottomOf="#id/ticketLayout"
android:paddingHorizontal="19dp"
android:drawablePadding="10dp"
android:layout_marginTop="15dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
This is how the view looks:
I debugged the code and confirmed that byteArray is not empty. All the other views like the close button and download button function properly as expected. Only the pdfview doesn't show anything.
I had tried many things first like AlertDialog.Builder, an activity with Dialog theme and many more ways and finally broke the code down to the simplest possible way. Still no change in view.
Please help me find out the issue here. Thanks a lot in advance :)
Update:
The issue is that if I use wrap_content for parent ConstraintLayout, it shows this small view as in screenshot. But when I use match_parent, it shows PDFView correctly (but stretched throughout the screen that looks ugly though). So what I did is hardcoded the height of parent constraint layout. I did some digging on the internet and found that the PDF inside the PDFView is not loaded when the parent ConstraintLayout draws its child layouts. So does anyone know any way where I can redraw the layout after all data is loaded?
As we all know Android Deprecated to show PDF via webview here you can find useful link. https://github.com/eduxcellence/PDFViewer/find/master
Related
enter image description hereI want to put the processbar under the id iv_logo, but the progressbar is located at the top center, how can I do that?
Additionally, I am using databinding. Please let me know if there is any problem here.
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/activity_start"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="#+id/progress"
android:layout_width="60dp"
android:layout_height="60dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_logo" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/iv_logo"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="#drawable/Logo"
android:layout_marginTop="40dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/iv_alogo"
android:layout_width="62dp"
android:layout_height="30dp"
android:src="#drawable/logo_pnt"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
It seems like you need to rethink your layout structure completely.
In addition, your iv_alogo has no vertical constraints, so it is not known exactly where it will be placed on the layout.
I offer you two options here:
Refuse include and place two AppCompatImageView in the main layout.
Place the progress bar inside the included layout.
In this case, it is not clear why you using include here.
your XML attribute is pointing on some View with tv_logo id
app:layout_constraintTop_toBottomOf="#+id/tv_logo"
but in second XML snippet (assuming part of #layout/activity_start) there is no View with such id... maybe try with one of these ids, which are placed in activity_start layout, so iv_logo or iv_alogo?
app:layout_constraintTop_toBottomOf="#+id/iv_logo"
//or
app:layout_constraintTop_toBottomOf="#+id/iv_alogo"
edit: due to posted image - it looks like you are looking for centering, not placing below another View. for centering use
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
I want to have a ProgressBar inside of an EditText, but I want it to be right inside it and, preferably, in the center of the EditText. All the solutions I've come across so far do not really allow the bar to be inside of the borders. Also, I'm looking for as clean solution as possible, not just putting over 9000 constraints in ConstraintLayout.
It should look like this (picture is taken from android progressbar inside edittext):
But the actual result is either this:
or this (if I use smaller style for the bar):
My code is:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center_vertical"/>
</FrameLayout>
Of course, I could just put some hardcoded values for height, but it's definitely a bad fix of the problem. Also, if I change the style of the bar to the smaller one, it's not a fix, since I want to have some control over it (and it doesn't look perfect, either). I'm planning to add something like error and success icons also, so the solution that fulfills this requirement is extremely welcome! So, if you have any clue about how to put it properly inside of the input field, I'll be grateful.
Thanks in advance, guys!
You can try:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<EditText
android:id="#+id/edit_text"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ProgressBar
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You can achive like this, Whenever you want to position children inside a layout FrameLayout is not a good layout to use. consider using RelativeLayout or better ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#null"
android:hint="Phone number goes here" />
<ProgressBar
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:padding="5dp" />
</RelativeLayout>
Happy coding!!
I would like to create custom component in Android Studio. This should be a button but with other components like ImageView or ChceckBox inside. How to do this?
I'm already tried create a Compound Control (LinearView with components inside) but I can't set onClickListener to catch click event of this and it's not so elegant solution.
I would like to avoid creating my own component nearly from scratch (extend View and override onDraw method) but I don't know how I can do this in other way.
You can use ConstraintLayout to achieve view over/inside another view, something like this:
?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="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:scaleType="fitXY"
tools:srcCompat="#tools:sample/avatars[10]" />
<CheckBox
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="check"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="#+id/imageView"
app:layout_constraintStart_toStartOf="#+id/imageView"
app:layout_constraintTop_toTopOf="#+id/imageView" />
</android.support.constraint.ConstraintLayout>
This will look like this:
And about your click listeners - if you want to do the same thing when any of your views are clicked just add a click listener to your parent layout.
If you want to do different things for different view clicks with ConstraintLayout you can simply attach click listener for every view that you have.
everyone
I am a beginner. I tried to use ConstraintLayout with Android studio 3.1.1 and genymotion. when I run the project on genymotion emulator, It is different from what I have coded. Images FYI.
Each component needs to be constrained to be able to display correctly. I think you should do it. Because some TextView components seem to have no constraints
For Constrain Layout, You can to pull all point from the box of your TextView (for example)to top, bottom, left and right to remain it in the position. It could takes sometimes if have lots of button or textView or what so ever.
ConstrainLayout
sampleCode
<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="match_parent">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.32" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="16dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
You have to pull the line from the dot of the box to everyside so it could stay at the position.
Since you are a beginner, I think You can consider LinearLayout.
As LinearLayout, it is align those textView and button or anything inside in arrangement. As long as You it in vertical or horizontal.
Example
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
....
//Edit your layout
</LinearLayout>
Another good layout for beginner is RelativeLayout
RelativeLayout you can drag and drop the textView or button or what so ever to the layout.
Example
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
....
//Edit your layout
</RelativeLayout>
U have to keep edit and arrange until the design is what you like.
However, I believe there is a lot of tutorial You can learn from Youtube. Check it Out.
First of all, take a look at this screeshot:
I want to place a close button in this modal like this:
But I can't figure out how to do that, here is going my layout file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="br.com.makadu.makaduevento.ui.fragments.wizardInteligenceArtificial.WizardAIFifthStep">
<android.support.constraint.ConstraintLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv_title_subjects_schedule_suggestion"
android:paddingTop="#dimen/screen_title_top_bot_padding"
android:paddingBottom="#dimen/screen_title_top_bot_padding"
android:background="#color/Verde2"
android:gravity="center_horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="#dimen/txt_medium"
android:text="#string/str_screen_title_subjects"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:text="#string/str_according_to_your_interests_title"
style="#style/Theme.DefaultTextView.PrimaryDark.Bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
android:id="#+id/tv_according_interests"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/tv_title_subjects_schedule_suggestion" />
<ExpandableListView
android:id="#+id/elv_suggested_schedule"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/tv_according_interests"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/b_confirm_schedule">
</ExpandableListView>
<Button
android:id="#+id/b_confirm_schedule"
style="#style/Theme.DefaultButton.Purple.Radius"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/str_confirm_schedule_button"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginEnd="8dp" />
</android.support.constraint.ConstraintLayout>
Thanks in advance.
Well stackoverflow is asking me to explain better my problem, but i guess the pictures explain it selfs, so I'm now just writing any no sense text.
The first solution, it will work 100% but I think not beautiful:
You can make root layout transparent and put in it needed visible views. U can set OnClickListener for root view to hide dialog on tap out visible views.
The second solution, maybe it will work I can't check now:
You can turn off clipPadding for root view and move yo close button out of visible.
P.S. I wrote from phone so it has not good view format and without samples.
I runed on similar issue, u should Create a LinearLayout With a gravity center and margin with background transparent, the add a RelativeLayout with the content of your dialog.
In the RelativeLayout u can use the android:layout_alignParentRight="true" & android:layout_alignParentTop="true" for the ImageView containing the close button (probably u will need to use some margins too) so it will be displayed above the textview containing the title of your dialog.
I hope this helped you, tomorrow i will try to send u a code for this !