I'd like to set the position of the TextView to always be at the bottom of the camera preview.
This is not the case with my layout below. When the user changes the aspect the ratio of the camera preview (ex: from 16:9 to 4:3), the TextView remains in the same position and is no longer at the bottom of the preview.
How do I set the position of the TextView to always be at the bottom (left) of the camera preview?
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activities.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/root"
android:background="#color/black"
tools:context=".ui.activities.MainActivity">
<FrameLayout
android:id="#+id/main_frame"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:paddingTop="8dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/preview_container"
android:layout_height="match_parent"
android:layout_width="match_parent">
<androidx.camera.view.PreviewView
android:id="#+id/preview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,9:16"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/preview"
android:textColor="#ffffffff"
android:text="Text Test" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Try this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activities.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/root"
android:background="#color/black"
android:orientation="vertical"
tools:context=".ui.activities.MainActivity">
<FrameLayout
android:id="#+id/main_frame"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:paddingTop="8dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/preview_container"
android:layout_height="match_parent"
android:layout_width="match_parent">
<androidx.camera.view.PreviewView
android:id="#+id/preview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,9:16"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:textColor="#ffffffff"
android:text="Text Test" />
</FrameLayout>
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Sorry i dont have enough reputation for comment so i will try answer directly
This problem occurs because you set your textview bottom to bottom with camera preview while use ConstraintLayout. I can give you two option for solve this,
first change textview from app:layout_constraintBottom_toBottomOf to app:layout_constraintTop_toBottomOf.
Second try for wrap it with linearlayout and set orientation to vertical.
Related
I want to have textview over image at top left corner of the Imageview similar to this. how can I do this.
I have this code but It doesn't do what i want.
<?xml version="1.0" encoding="utf-8"?>
<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">
<ImageView
android:id="#+id/myImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/myImageSouce"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/spacing_medium"
android:layout_gravity="center"
tools:text="Test"/>
</FrameLayout>
Thanks for help in advance.
Try using a ConstraintLayout. Here's a simple example. For layering, views are layered in the order listed in the xml so if you want the TextView in front put it last in the 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="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
app:srcCompat="#drawable/ic_home_black_24dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Words"
android:padding="20dp"
android:background="#00ff00"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
So I have this code in an XML file:
<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.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:id="#+id/constraintLayout" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_dashboard">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView2">
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView" android:text="#string/Tester" android:layout_alignParentEnd="true"
android:layout_marginEnd="18dp" android:layout_marginStart="10dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp" android:layout_margin="0dp"/>
</RelativeLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
The bottom margin within text view is adding to the top not the bottom, and the text at the bottom of the scroll view gets cut off. Also, when I try to just move the layout instead, if I drag it downward, it moves upward after release, and vice versa for dragging upward. Does anyone know what is happening here???
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="MissingConstraints"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:text="Tester"
android:layout_marginEnd="18dp" android:layout_marginStart="10dp"
android:layout_marginBottom="50dp" android:layout_margin="0dp"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView2">
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
In my app I use a TabLayout under a Toolbar, and a ViewPager filling the rest of the space. I need one of the fragments under the tab to be able to scroll. tried using ScrollView, NestedScrollView and android:fillViewport="true" with no success.
I've looked at other questions about the same problem, tried anything that was suggested in the answers, but they all didn't solve my problem.
the activity that contains the ViewPager's code:
<?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"
android:orientation="vertical"
tools:context=".UserProfile">
<include
android:id="#+id/userToolbar"
layout="#layout/user_info_toolbar_layout" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/userToolbar"
android:background="#android:color/holo_blue_light"
app:tabSelectedTextColor="#android:color/white"
app:tabTextColor="#android:color/black">
<com.google.android.material.tabs.TabItem
android:id="#+id/dashboardTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/dashboard_tab_text_en" />
<com.google.android.material.tabs.TabItem
android:id="#+id/statisticsTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/statistics_tab_text_en" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/userInfoViewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/tabLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
the fragment's code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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/user_dashboard_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
tools:context=".Dashboard">
<androidx.constraintlayout.widget.ConstraintLayout
android:padding="40dp"
android:id="#+id/userInfoContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.github.abdularis.civ.CircleImageView
android:id="#+id/userImage"
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="20dp"
android:scaleType="fitCenter"
android:src="#drawable/male_user_sample1"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/username_sample"
android:textColor="#color/colorPrimaryDark"
android:textSize="30sp"
app:layout_constraintTop_toBottomOf="#id/userImage" />
<com.ramijemli.percentagechartview.PercentageChartView
android:id="#+id/userLevelProgress"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="20dp"
android:outlineAmbientShadowColor="#color/colorPrimary"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/username"
app:pcv_animDuration="1000"
app:pcv_animInterpolator="anticipate_overshoot"
app:pcv_drawBackground="false"
app:pcv_drawBackgroundBar="false"
app:pcv_mode="ring"
app:pcv_orientation="counter_clockwise"
app:pcv_progress="100"
app:pcv_startAngle="270"
app:pcv_textColor="#color/colorPrimary"
app:pcv_textShadowRadius="100"
app:pcv_textSize="50sp" />
<TextView
android:id="#+id/userLevel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/level_sample"
android:textColor="#color/colorPrimary"
android:textSize="40sp"
app:layout_constraintTop_toBottomOf="#id/userLevelProgress" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
I can't find the problem in my code that causes the fragment under the ViewPager being unscrollable.
Rty this
In your ViewPager
<androidx.viewpager.widget.ViewPager
android:id="#+id/userInfoViewPager"
android:layout_width="match_parent"
android:layout_height="0dp" // <- ****
app:layout_constraintBottom_toBottomOf="parent" // <- ****
app:layout_constraintTop_toBottomOf="#id/tablayout"
/>
Then, in fragment xml
<ScrollView
....
android:layout_width="match_parent"
android:layout_height="match_parent" // <- ****
>
<androidx.constraintlayout.widget.ConstraintLayout
android:padding="40dp"
android:id="#+id/userInfoContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" // <- ****
>
it's seems like there some bug with ConstraintLayout inside ScrollView ,Don't forget that If you constraint some view's bottom to constraint layout's bottom. Scrollview could not scroll. Use NestedScrollView with viewport true is working good for me
<android.support.v4.widget.NestedScrollView>
......
......
</android.support.v4.widget.NestedScrollView>
I have an activity with a title, a fragment with a recyclerview full of cards and a bottom navigaton bar.
I cannot manage to show all 3 together without problems, the closest I have been is showing all 3, but with the bar taking space from the cards space.
Right now this is the XML which I thought should be working:
<LinearLayout 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"
tools:context=".habits.HabitsActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="313dp"
android:layout_height="166dp"
android:layout_gravity="center_horizontal"
app:srcCompat="#drawable/goodreasons" />
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom">
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</FrameLayout>
</LinearLayout>
But this gives this output:
This is the xml of the fragment, in case you think it is relevant too.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="5dp"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
android:layout_gravity="center"
/>
</LinearLayout>
Here's your layout, stripped down:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="313dp"
android:layout_height="166dp"/>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Your problem is that the CoordinatorLayout has a match_parent height, so it will take up all of the space that the ImageView doesn't. That leaves no space at all for the FrameLayout, so you won't be able to see anything inside of it.
Probably the best thing to do would be to use layout_weight on the CoordinatorLayout to make it take up as much space as it can while still leaving room for other views. That would look something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="313dp"
android:layout_height="166dp"/>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
The only changes I made were to set the CoordinatorLayout's height to 0dp and the FrameLayout's height to wrap_content, and to add the weight attribute to the CoordinatorLayout.
I have a list of hours created with card view and recycler view. I want to show the changes on some selection of hours. on selection I want to show type of blue box in image. How can I achieve it??
I think I need to add multiple layouts in the parent layout and change the background color of the layouts. Is it a right way?? Or is it possible with any other way???
card layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_light">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/relativeLayout"
android:layout_alignParentTop="true">
<ImageView
android:layout_width="280dp"
android:layout_height="2dp"
android:id="#+id/imageView"
android:background="#drawable/lineh"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/txthour"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Main Layout
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id = "#+id/toolbar_container">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
<FrameLayout
android:id="#+id/nav_contentframe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_light"
android:layout_below="#+id/toolbar">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/nav_menu"
android:background="#android:color/background_light" />
</android.support.v4.widget.DrawerLayout>
Fragment Layout
<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" tools:context="com.example.siddhi.timetablelayout.List"
android:background="#android:color/holo_blue_bright">
<android.support.v7.widget.RecyclerView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/RecyclerView"
android:background="#android:color/white"
android:layout_weight="1.40"
android:layout_gravity="right|bottom" />
</FrameLayout>
Please help...
I would recommend you to use a custom view(or layout that is, obviously, also a view) for this purpose. What you need to do is override onDraw method:
#Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
drawOverlay(canvas);
}
In this case you need to setup all overlay params for your view and after that just draw on canvas a selection. Here is a good article about it.