I'm doing a small project on Android Studio, and I've put a picture as a dark blue background.
I inserted white text on top of this dark blue figure, but the white color in the letters still doesn't show.
The Graphical Interface. See that I put the color white, but it still doesn't show up.
Please, check out:
Please, can anyone help to
[1]: https://i.stack.imgur.com/iTtiS.png
Source-code in the XML:
[2]: https://i.stack.imgur.com/u6AIU.png
Please, what to do to the white text appear on the top of the image in Android Studio?
Whole XML 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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="410dp"
android:layout_height="250dp"
android:color="#color/white"
android:foreground="#drawable/home_bg"
android:textSize="30dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Encontre o seu próximo destino!"
android:textColor="#android:color/white"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.32"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.233" />
<TextView
android:id="#+id/textView2"
android:layout_width="176dp"
android:layout_height="41dp"
android:layout_marginBottom="64dp"
android:text="Procurar Destinos"
android:textColor="#FFFFFF"
android:textColorHighlight="#color/white"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.324"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
app:layout_constraintVertical_bias="0.742" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Ok I think the problem is that you've set the foreground of the constraint layout instead of the background.
Try this:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="410dp"
android:layout_height="250dp"
android:color="#color/white"
android:background="#drawable/home_bg"
android:textSize="30dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
Related
my goal is to make such a blur effect such in this card view around the stroke in with each card with specific color[enter image description here] 1
i searched a lot about it but did not found the intended ui
the last thing i found is blurry but it also not give me like the screen above
https://github.com/wasabeef/Blurry
here is view pager item xml code:
<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">
<com.google.android.material.card.MaterialCardView
android:id="#+id/materialCardViewMain"
android:layout_width="#dimen/_250sdp"
android:layout_height="#dimen/_400sdp"
android:layout_marginTop="#dimen/_15sdp"
android:layout_marginBottom="#dimen/_15sdp"
app:cardBackgroundColor="#color/mo_salah_background_color"
app:cardCornerRadius="#dimen/_20sdp"
app:cardElevation="#dimen/_5sdp"
app:cardUseCompatPadding="true"
app:flow_maxElementsWrap="2"
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">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/playerName"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:autoSizeMaxTextSize="#dimen/_24ssp"
android:autoSizeMinTextSize="#dimen/_12ssp"
android:autoSizeStepGranularity="#dimen/_2ssp"
android:fontFamily="#font/baloo_chettan"
android:text="Mohamed salah"
android:textColor="#color/white"
android:textSize="#dimen/_24ssp"
android:textStyle="bold"
android:transitionName="s.name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.129"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.097" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/playerPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="170M"
android:textColor="#color/white"
android:textSize="17sp"
android:textStyle="bold"
android:transitionName="s.price"
app:layout_constraintBottom_toBottomOf="#+id/playerName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.71"
app:layout_constraintStart_toEndOf="#+id/playerName"
app:layout_constraintTop_toTopOf="#+id/playerName"
app:layout_constraintVertical_bias="0.49" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="#dimen/_320sdp"
android:transitionName="s.image"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/playerName"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/mo_salah2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
I have a custom toolbar in which I set the title and other things programatically. The thing is, even though the title has the setting gravity="center", whenever the navigationIcon appears, it is moved a little bit to the side (like in the picture).
I understand that since the icon is not in the xml it must be moving it but is there a way to make the textView stay in the center no matter the icons? Or is there a way to add an ImageView or Button to the xml and then set the navigationIcon to that button so that the icon is contemplated in the code?
This is my xml, I added the buttons but I don't know a way to set the icons programatically.
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintly"
android:layout_width="match_parent"
android:layout_height="41dp"
android:gravity="center"
android:layout_gravity="center">
<Button
android:id="#+id/navigationIcon"
android:layout_width="24dp"
android:layout_height="24dp"
android:scaleType="center"
android:layout_marginStart="12dp"
android:layout_alignParentLeft="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/NavBar.Title"
android:singleLine="true"
android:ellipsize="end"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginEnd="12dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="Title"/>
<TextView
android:id="#+id/subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginEnd="12dp"
style="#style/NavBar.SubTitle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/centeredTitleTextView"
tools:text="Subtitle"/>
<Button
android:id="#+id/closeIcon"
android:layout_width="24dp"
android:layout_height="24dp"
android:scaleType="center"
android:layout_marginEnd="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
This is the part where the navigationIcon is set but setNavigationIcon cannot take the view.
public void setNavigationIconArrow() {
super.setNavigationIcon(R.drawable.ic_ui_24dp);
}
public void setNavigationIconClose() {
super.setNavigationIcon(R.drawable.ic_24dp);
}
Thank you.
Because of the icon size, your layout starts after that. So you can't use gravity=center. Also, You can't set static left margin or right margin, Because, Android has so much different screen sizes. So you have to do this programmatically.
1- You have to find the phone's screen width size.
2- Convert that size px to dp.
3- Now you can set the left margin to your layout. Which you want in gravity=center.
I hope, I did help you what you want:)
I am trying to have images in my recycler view have rounded corners to look better. I looked online and found out I could not set the background to a cardview but I could set a background to a linear layout so that is what I did. In the design tab of Android Studio I can see that the cardview has rounded corners but when I run the app the corners are not rounded. I tried googling the problem for a few hours but I could not find an answer.
Here is the contrast on what I am seeing in Android studio and the emulator
<?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:orientation="vertical"
android:paddingBottom="5dp">
<androidx.cardview.widget.CardView
android:id="#+id/cvPhoto"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:elevation="0dp"
app:cardBackgroundColor="#CABCBC"
app:cardCornerRadius="15dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="false"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="#+id/llPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/_0443265_10154716579055043_2084238995361491869_o"
android:orientation="horizontal"></LinearLayout>
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/tvName"
android:layout_width="0dp"
android:layout_height="19dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="8dp"
android:text="name"
android:textSize="14sp"
app:layout_constraintBottom_toTopOf="#+id/tvSurname"
app:layout_constraintStart_toEndOf="#+id/cvPhoto"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/tvSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="60dp"
android:text="surname"
android:textColor="#color/gold"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/cvPhoto" />
<TextView
android:id="#+id/tvRank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:text="Rank"
android:textColor="#color/gold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.503" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/next_arrow" />
</androidx.constraintlayout.widget.ConstraintLayout>
I read some comments that suggest to move your linearlayout to have effects that you would like but I suggest to let your linearlayout and instead do like this:
put some padding between your cardview and your linearlayout:
app:cardUseCompatPadding="true"
android:padding="16dp"
don't miss cardUseCompatPadding flag!
put your corners radius and elevation like this:
app:cardElevation="#dimen/cardview_elevation"
app:cardCornerRadius="#dimen/cardview_corners_radius"
set cardBackground to white color and transparent color for the constraintLayout parent;
app:cardBackgroundColor="#color/white" for cardview
android:background="#color/transparent" for parent layout
and you're all done.
I am doing the chat system. So I need to do the UI for showing the message and sent time.
Please see the below images first. The red color is the text view width and blue color is sent time text view width. Now I set the sent time is right of the text view.
I would like to set the sent time text view have a margin. But you can see the text view width is not same. So I am not hard code the margin. So anybody can give me some useful suggestion to me. Thanks a lots.
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.60"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/tvMessage"
android:layout_width="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="#id/guideline"
android:layout_height="wrap_content"
android:textColor="#color/white_color"
android:background="#color/red_color"
android:maxWidth="#dimen/_151sdp"
android:textStyle="bold"
android:minWidth="30dp"/>
<TextView
android:id="#+id/tvDateTime"
android:layout_width="0dp"
app:layout_constraintBottom_toBottomOf="#id/tvMessage"
app:layout_constraintEnd_toEndOf="#id/tvMessage"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/tvMessage"
android:layout_toRightOf="#id/tvMessage"
android:textColor="#color/chat_mag_date_time"/>
</androidx.constraintlayout.widget.ConstraintLayout>
For using a component in another you can use somethings like : CardView,FrameLayout or ConstraintLayout.
ConstraintLayout like this :
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/msg_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/cardview_dark_background">
<TextView
android:id="#+id/tvMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxWidth="151dp"
android:minWidth="30dp"
android:text="fdsfdsfd
dsadas
asd
asd
as"
android:textColor="#color/black"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="8dp"
/>
<TextView
android:id="#+id/tvDateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/tvMessage"
android:layout_toRightOf="#id/tvMessage"
android:text="1234"
android:textColor="#color/purple_700"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/tvMessage"
android:layout_marginStart="8dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Add following alignments to the TextView "tvDateTime"
android:layout_alignBottom="#id/tvMessage"
android:layout_alignRight="#id/tvMessage"
and remove the extra alignment instructions, so your TextView code will become:
<TextView
android:id="#+id/tvDateTime"
android:layout_width="wrap_content"
android:text="12:45"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/tvMessage"
android:layout_alignRight="#id/tvMessage"
android:textColor="#color/grey"/>
I have recently converted over an android project into androidx and I am having issues with trying to stop views going off of the page. My layout is as follows, a Constraint Layout that contains a Card View and a Text View. Within this Card View I have a Constraint Layout that contains the Text View. Outside of the Card View I have a Button which is constrained to the Card View.
The issue with this is that when I run the program the Card View goes off of the screen and the button moves all the way to the top even though they are all constrained properly.
I will show below the code I am using to achieve this and an image of my result on the emulator.
Here is the layout in the editor [1]: https://imgur.com/a/0FLa1IK
Here is the layout shown on the Emulator [2]: https://imgur.com/a/SsvKiOG
Any help would be greatly appreciated.
<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">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/materialButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="Text View Test"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<Button
android:id="#+id/materialButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="256dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Your layout is getting pushed because of this line:
android:layout_marginBottom="256dp"
Different phones got different screen size, in your layout you are using fixed size on your view ("256dp") and it makes your layout non-responsive.
So what may look good on your preview may not look the same on a real phone.
If you want to place some view anywhere on your screen and keep it responsive I recommend using guidelines, something like this:
<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">
<androidx.cardview.widget.CardView
android:id="#+id/cv_text"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/materialButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Text View Test"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.cardview.widget.CardView>
<Button
android:id="#+id/materialButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/guideline13"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent=".5" />
</androidx.constraintlayout.widget.ConstraintLayout>
And it will look like this:
I'm not so sure about android x but the constraint layouts are used for removing the nested layouts so, you should not be using constraint layout inside card view. Try this code as a reference. It might help.
<?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="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:id="#+id/cv_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="Text View Test"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.v7.widget.CardView>
<Button
android:id="#+id/materialButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="280dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cv_text" />
</android.support.constraint.ConstraintLayout>
please remove this line from button attribute :
android:layout_marginBottom="256dp"
your are just pushing to button to the top by setting the margin_bottom , and why it's shown on the preview and not on the device just because of the different screen size