How can I create a "square" ConstraintLayout that is included in a chain of widgets? - android-constraintlayout

I try to create a "square" ConstraintLayout by extending the ConstraintLayout class as follows:
class SCConstraintLayout extends android.support.constraint.ConstraintLayout {
public SCConstraintLayout(Context context) {
super(context);
}
public SCConstraintLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SCConstraintLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int chosenDimension = 0;
int mode = 0;
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
chosenDimension = MeasureSpec.getSize(heightMeasureSpec);
mode = MeasureSpec.getMode(heightMeasureSpec);
} else {
chosenDimension = MeasureSpec.getSize(widthMeasureSpec);
mode = MeasureSpec.getMode(widthMeasureSpec);
}
int side = MeasureSpec.makeMeasureSpec(chosenDimension, mode);
super.onMeasure(side, side);
setMeasuredDimension(side, side);
}
}
If I include a widget with this custom class into a chain of widgets such as in the following landscape-oriented layout
<?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:id="#+id/scMainFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:layout_optimizationLevel="chains"
tools:context=".MainActivity"
tools:layout_behavior="#string/appbar_scrolling_view_behavior">
<!-- gridFrame -->
<android.support.constraint.ConstraintLayout
android:id="#+id/gridFrame"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
app:layout_optimizationLevel="chains"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/Report"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<!-- column headers -->
<android.support.constraint.ConstraintLayout
android:id="#+id/colHeaders"
android:layout_width="0dp"
android:layout_height="14dp"
android:orientation="horizontal"
app:layout_constraintVertical_chainStyle="spread_inside"
app:layout_optimizationLevel="chains"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/SCFrame"
app:layout_constraintLeft_toLeftOf="#+id/SCFrame"
app:layout_constraintRight_toRightOf="#+id/SCFrame"
>
<TextView
android:id="#+id/textView0"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="A"
android:background="#FF0000"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</android.support.constraint.ConstraintLayout>
<!-- row headers -->
<android.support.constraint.ConstraintLayout
android:id="#+id/rowHeaders"
android:layout_width="14dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_optimizationLevel="chains"
app:layout_constraintTop_toTopOf="#+id/SCFrame"
app:layout_constraintBottom_toBottomOf="#+id/SCFrame"
app:layout_constraintRight_toLeftOf="#+id/SCFrame"
app:layout_constraintLeft_toLeftOf="parent"
>
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="1"
android:background="#FF0000"
app:layout_constraintVertical_chainStyle="spread_inside"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</android.support.constraint.ConstraintLayout>
<be.ema.sclibrary.SCConstraintLayout
android:id="#+id/SCFrame"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#00FF00"
app:layout_constraintLeft_toRightOf="#+id/rowHeaders"
app:layout_constraintTop_toBottomOf="#id/colHeaders"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<TextView
android:id="#+id/A1"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="this is A1"
android:background="#0000FF"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</be.ema.sclibrary.SCConstraintLayout>
</android.support.constraint.ConstraintLayout>
<TextView
android:id="#+id/Report"
android:layout_width="200dp"
android:layout_height="0dp"
android:text="This is the report"
android:background="#FFFF00"
app:layout_constraintLeft_toRightOf="#+id/gridFrame"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</android.support.constraint.ConstraintLayout>
and if I display it on a "Nexus 6" in the emulator of Android Studio for instance it looks like the SCFrame is only partially square:
the A1 TextView is square (i.e. its width is equal to its height)
however the width of the SCFrame itself is different from its height
How should I change the SCConstraintLayout class or the xml lay-out to get the SCFrame view perfectly square (i.e. its width is equal to its height) when displayed on a device?
NB: in my real application, SCFrame has several rows and columns. And there are several other widgets like buttons and textviews instead of the Report textview.

You just need the top level to be a ConstraintLayout. The nested ones and the custom class aren't needed. As for the square-shaped TextView, you can use the aspect ratio attribute.

Related

android constraintlayout, is it possible to set mmaxWidth by percentage

Having three textView, the content size varies. Want to show ellipsis if the content to to big.
|[aaa][bbb][ccc] |
|[aaaaaaa...][bbbbbb.][ccc]|
|[aaa][bbbbbb.......][c...]|
|[aaaa...][bbbb...][ccc...]|
could not find a solution using constraintLayout for it. This one does not do wanted,
but if it can set the the [a] with a maxWidth by percentage of the parent width, so that it can leave some width for the [b] and [c]
something like (but it does not exist)
android:maxWidth="50%"
Any suggestion?what is
<?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:background="#android:color/white"
android:padding="22dp">
<TextView
android:id="#+id/greenTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#android:color/holo_green_dark"
app:layout_constraintEnd_toStartOf="#id/blueTextView"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Firdsfgsdfgasdsdfasdsdfsds" />
<TextView
android:id="#+id/blueTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#android:color/holo_blue_dark"
app:layout_constrainedWidth="true"
app:layout_constraintBaseline_toBaselineOf="#id/greenTextView"
app:layout_constraintEnd_toStartOf="#id/orangetextView"
app:layout_constraintStart_toEndOf="#id/greenTextView"
app:layout_constraintTop_toTopOf="parent"
tools:text="Secoasdfsdfsdfsdfsdf.." />
<TextView
android:id="#+id/orangetextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#android:color/holo_orange_dark"
app:layout_constraintBaseline_toBaselineOf="#id/blueTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/blueTextView"
tools:text="Third Very Long Text 123456789" />
</androidx.constraintlayout.widget.ConstraintLayout>
It's a simple workaround for that, to achieving the logic of percentage for width we can use two vertical Guideline that can split the screen into three sections:
This is a simple code I wrote for you:
<?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:background="#android:color/white"
android:padding="22dp">
<TextView
android:id="#+id/greenTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#android:color/holo_green_dark"
app:layout_constraintEnd_toStartOf="#+id/guideline"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Firdsfgsdfgasdsdfasdsdfsds" />
<TextView
android:id="#+id/blueTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#android:color/holo_blue_dark"
app:layout_constrainedWidth="true"
app:layout_constraintBaseline_toBaselineOf="#id/greenTextView"
app:layout_constraintEnd_toStartOf="#+id/guideline3"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toTopOf="parent"
tools:text="Secoasdfsdfsdfsdfsdf.." />
<TextView
android:id="#+id/orangetextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#android:color/holo_orange_dark"
app:layout_constraintBaseline_toBaselineOf="#id/blueTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline3"
tools:text="Third Very Long Text 123456789" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.33" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.66" />
</androidx.constraintlayout.widget.ConstraintLayout>
you can customize spaces by margins, any comments or modifications please tell me,cheers

android constraintLayout how to vertical attach to items above which may not visible

Having a few items above the last row, and they may all not visible or someone still visible.
Here is how they vertically aligned:
[content_part - 'Lorem ipsum...']
[media_image - RED BLOCK - has app:layout_constraintTop_toBottomOf="#+id/content_part"]
[quoted_view - IMG - has app:layout_constraintTop_toBottomOf="#+id/media_image"]
[comment - TEXTVIEW ROW - has app:layout_constraintTop_toBottomOf="#+id/quoted_view"]
when the quoted_view is visible, the comment button row is fine (both landscape and portrait):
but when the quoted_view is NOT visible in landscape the comment row is not displayed right
although in portrait mode it seems fine
How to vertically align to above items which may not visible?
<?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:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="?android:attr/selectableItemBackground"
android:paddingStart="10dp"
android:paddingEnd="10dp">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="52dp" />
<TextView
android:id="#+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="3dp"
android:ellipsize="end"
android:maxLines="1"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toTopOf="parent"
tools:text="re-tweet werwer wer we rwe r wer " />
<ImageView
android:id="#+id/avatar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="3dp"
android:scaleType="fitXY"
android:background="#mipmap/ic_launcher"
app:layout_constraintEnd_toStartOf="#+id/guideline"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title" />
<!-- -->
<TextView
android:id="#+id/content_part"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:autoLink="web"
android:linksClickable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toBottomOf="#+id/title"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. " />
<ImageView
android:id="#+id/media_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:scaleType="matrix"
android:visibility="visible"
app:layout_constraintDimensionRatio="H,15:3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toBottomOf="#+id/content_part"
app:layout_goneMarginBottom="8dp"
android:background="#ff0000" />
<androidx.cardview.widget.CardView
android:id="#+id/quoted_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:background="#color/colorAccent"
app:cardCornerRadius="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toBottomOf="#+id/media_image"
app:layout_goneMarginBottom="8dp"
android:visibility="gone">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="30dp"
android:scaleType="centerCrop"
android:src="#mipmap/ic_launcher_round" />
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#+id/imageView"
android:gravity="center_horizontal"
android:padding="8dp"
android:text="#string/app_name"
android:textColor="#ffffff"
android:textSize="12sp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<!-- quoted -->
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/view"
app:layout_constraintEnd_toStartOf="#+id/response"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toEndOf="#+id/guideline"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toBottomOf="#+id/quoted_view"
android:text="TextView" />
<TextView
android:id="#+id/response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/view"
app:layout_constraintEnd_toStartOf="#+id/like"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/comment"
android:text="TextView" />
<TextView
android:id="#+id/like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/response"
android:text="TextView" />
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#20000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.526"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
update:
adding app:layout_constraintTop_toBottomOf="#+id/comment" to android:id="#+id/view" seems fixed it.
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#20000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.526"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/comment" />
But not really understand why it has to have this constrain, the android:id="#+id/comment" already has constrain app:layout_constraintBottom_toTopOf="#+id/view".
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/view"
app:layout_constraintEnd_toStartOf="#+id/response"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toEndOf="#+id/guideline"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toBottomOf="#+id/quoted_view"
android:text="TextView" />
When you specify app:layout_constraintDimensionRatio="H,15:3" for media_image with a 0dp height and 0dp width, ConstraintLayout will maximize the width of the view and constrain the height of the view such that the resulting aspect ratio is 15:3 (5 x as wide as the height.)
So, given a screen width of, say, 500dp the width of media_image will become 500dp and the height will be 100dp. So, what happens when the actual height of the screen is something less than 100dp plus the height of the TextViews? You will see your problem. (Screen sizes are for example and do not reflect actual screen dimensions that I am aware of.)
The question now depends upon what you want to happen with the layout when the dimensions don't quite work as you have things laid out now.

Customised InfoWindow of Google Map does not render elevation shadow in Android

I'm using InfoWindow to render any information for marker.
In Android, we can define shadow with elevation.
But the shadow defined by elevation is not rendered for InfoWindow.
The layout of infoWindow is like this
<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="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:elevation="4dp"
>
<TextView
android:id="#+id/main_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:textSize="14sp"
android:textStyle="bold"
tools:text="main text"
/>
<TextView
android:id="#+id/sub_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/main_text"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:textSize="11sp"
tools:text="sub text"/>
</RelativeLayout>
Shape is like this
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="500dp"/>
<solid android:color="#color/white"/>
</shape>
Code snippet is like this
googleMap?.setInfoWindowAdapter(object: GoogleMap.InfoWindowAdapter {
override fun getInfoContents(marker: Marker?): View? {
return null
}
override fun getInfoWindow(marker: Marker?): View? {
return layoutInflater.inflate(R.layout.map_info_window, null)
}
})

How do I create a square grid using a ConstraintLayout with labels for the rows that are vertically centered?

I need to create a square grid with labels for the rows on the left side. The grid should use the entire space available on the device while keeping width=height. Because this grid has several rows and several columns automatically adjusted, I thought that using a customized ConstraintLayout would fit that requirement.
In the code below, which is used in a "portrait" layout, if I set the layout_width of the SCFrame view to
match_parent: the size of the grid is as expected, but the gravity of the textView10 and textView11 fields is not applied
wrap_content: the grid collapses to 0dpx0dp
0dp: same as wrap_content, which in my opinion is not in line with the documentation of the ConstraintLayout that says "Using 0dp, which is the equivalent of MATCH_CONSTRAINT"
xxxdp: the SCFrame grid is square with width=xxxdp and height=xxxdp, and the gravity of the textView10 and textView11 fields is correctly applied
What should I change in the following code to get a grid that is square and uses the entire width of the screen (whatever the used device and allowing space for the labels column) and with the correct gravity for the labels?
<?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:id="#+id/scMainFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintVertical_chainStyle="spread_inside"
app:layout_optimizationLevel="chains"
tools:context=".MainActivity"
tools:layout_behavior="#string/appbar_scrolling_view_behavior">
<TextView
android:id="#+id/textView10"
android:layout_width="14dp"
android:layout_height="0dp"
android:gravity="center_vertical"
android:text="A"
android:background="#FF0000"
app:layout_constraintTop_toTopOf="#+id/SCFrame"
app:layout_constraintRight_toLeftOf="#+id/SCFrame"
app:layout_constraintBottom_toTopOf="#+id/textView11" />
<TextView
android:id="#+id/textView11"
android:layout_width="14dp"
android:layout_height="0dp"
android:gravity="center_vertical"
android:text="B"
android:background="#FF0000"
app:layout_constraintTop_toBottomOf="#+id/textView10"
app:layout_constraintRight_toLeftOf="#+id/SCFrame"
app:layout_constraintBottom_toBottomOf="#+id/SCFrame" />
<be.ema.sclibrary.SCConstraintLayout
android:id="#+id/SCFrame"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:background="#0000FF"
app:layout_constraintLeft_toRightOf="#+id/textView10"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:id="#+id/A1"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="this is A1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="#+id/A2"
app:layout_constraintBottom_toTopOf="#+id/B1" />
<TextView
android:id="#+id/A2"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="this is A2"
app:layout_constraintLeft_toRightOf="#+id/A1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="#+id/B2" />
<TextView
android:id="#+id/B1"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="this is B1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/A1"
app:layout_constraintRight_toLeftOf="#+id/B2"
app:layout_constraintBottom_toBottomOf="parent" />
<TextView
android:id="#+id/B2"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="this is B2"
app:layout_constraintLeft_toRightOf="#+id/B1"
app:layout_constraintTop_toBottomOf="#+id/A1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</be.ema.sclibrary.SCConstraintLayout>
</android.support.constraint.ConstraintLayout>
This is the code for the customized ConstraintLayout:
class SCConstraintLayout extends android.support.constraint.ConstraintLayout {
public SCConstraintLayout(Context context) {
super(context);
}
public SCConstraintLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SCConstraintLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int chosenDimension = 0;
int mode = 0;
if (ScMain.isLandscapeOriented) {
chosenDimension = MeasureSpec.getSize(heightMeasureSpec);
mode = MeasureSpec.getMode(heightMeasureSpec);
} else {
chosenDimension = MeasureSpec.getSize(widthMeasureSpec);
mode = MeasureSpec.getMode(widthMeasureSpec);
}
int width = MeasureSpec.makeMeasureSpec(chosenDimension, mode);
int height = MeasureSpec.makeMeasureSpec(chosenDimension, mode);
// setMeasuredDimension(width, height);
super.onMeasure(width, height);
}
}

Captured image overwrites previous picture when i take picture after closing and reopening the app

I have posted java code and the snapshot of the activity. The camera tab has a button and imagepreview widget. The camera is functioning properly. How to save the picture in a particular folder called pictures. And I'm not getting preview of the image.
Here's my java code:
public class activities extends AppCompatActivity {
public static final int CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE = 1777;
ImageView imgPreview;
Button btnCapturePicture;
File file;
static int count=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activities);
TabHost th=(TabHost)findViewById(R.id.tabHost);
th.setup();
TabHost.TabSpec specs=th.newTabSpec("Tag1");
specs.setContent(R.id.camera);
specs.setIndicator("",getResources().getDrawable(R.drawable.camera));
th.addTab(specs);
specs=th.newTabSpec("Tag2");
specs.setContent(R.id.chat);
specs.setIndicator("",getResources().getDrawable(R.drawable.chat15));
th.addTab(specs);
specs=th.newTabSpec("Tag3");
specs.setContent(R.id.profile);
specs.setIndicator("",getResources().getDrawable(R.drawable.ic_person_black_24dp));
th.addTab(specs);
specs=th.newTabSpec("Tag4");
specs.setContent(R.id.history);
specs.setIndicator("",getResources().getDrawable(R.drawable.history2));
th.addTab(specs);
specs=th.newTabSpec("Tag5");
specs.setContent(R.id.settings);
specs.setIndicator("",getResources().getDrawable(R.drawable.settings5));
th.addTab(specs);
imgPreview = (ImageView)findViewById(R.id.imageview6);
btnCapturePicture=(Button)findViewById(R.id.btnCapturePicture);
btnCapturePicture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory()+ File.separator + "IMG_"+count+++".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
//Check that request code matches ours:
if (requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE)
{
//Get our saved file into a bitmap object:
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "IMG_"+count+++".jpg");
Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
imgPreview.setImageBitmap(bitmap);
}
}
public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight)
{ // BEST QUALITY MATCH
//First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize, Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
options.inPreferredConfig = Bitmap.Config.RGB_565;
int inSampleSize = 1;
if (height > reqHeight)
{
inSampleSize = Math.round((float)height / (float)reqHeight);
}
int expectedWidth = width / inSampleSize;
if (expectedWidth > reqWidth)
{
//if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
inSampleSize = Math.round((float)width / (float)reqWidth);
}
options.inSampleSize = inSampleSize;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
}
Here's my xml layout:
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#eeeeee"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<!--<android.support.v7.widget.Toolbar
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:id="#+id/toolbar"/>-->
</android.support.design.widget.AppBarLayout>
<TabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabHost"
android:layout_gravity="center_horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/camera"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:id="#+id/imageview6" />
<Button
android:id="#+id/btnCapturePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take a Picture"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/profile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView3"
android:layout_gravity="center_horizontal"
android:layout_marginTop="46dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Name"
android:id="#+id/textView13"
android:layout_gravity="center_vertical" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:layout_marginStart="20dp"
android:layout_gravity="center_vertical"
android:id="#+id/textView18" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/view1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#bdbdbd">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Grade"
android:textSize="18sp"
android:id="#+id/textView14"
android:layout_gravity="center_vertical" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:layout_marginStart="20dp"
android:layout_gravity="center_vertical"
android:gravity="end"
android:id="#+id/textView19" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/view2"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#bdbdbd">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Credit Balance"
android:layout_gravity="center_vertical"
android:id="#+id/textView15"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:layout_gravity="center_vertical"
android:gravity="end"
android:layout_marginStart="20dp"
android:id="#+id/textView20" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/view3"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#bdbdbd">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Invite Friends"
android:layout_gravity="center_vertical"
android:id="#+id/textView16"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:src="#drawable/invite"
android:layout_marginStart="213dp"
android:layout_gravity="center_vertical"
android:foregroundGravity="right"
android:layout_marginEnd="3dp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/view4"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#bdbdbd">
</View>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Password"
android:id="#+id/textView17"
android:textSize="18sp"
android:layout_gravity="center_vertical" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView5"
android:src="#drawable/change_passg"
android:layout_gravity="center_vertical"
android:layout_marginEnd="0dp"
android:layout_marginStart="170dp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/view5"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#bdbdbd">
</View>
</LinearLayout>
<LinearLayout
android:id="#+id/history"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/settings"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp">
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/view6"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#bdbdbd">
</View>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center"
android:textSize="18sp"
android:text="Add Credits"
android:id="#+id/textView21" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/view7"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#bdbdbd">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center"
android:textSize="18sp"
android:text="Report Issue"
android:id="#+id/textView22" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/view8"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#bdbdbd">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="Rate Us"
android:textSize="18sp"
android:id="#+id/textView23" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/view9"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#bdbdbd">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center"
android:textSize="18sp"
android:text="FAQ"
android:id="#+id/textView24" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/view10"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#bdbdbd">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="Feedback"
android:textSize="18sp"
android:id="#+id/textView25" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/view11"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#bdbdbd">
</View>
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="Logout"
android:textColor="#FFFFFF"
android:background="#color/colorPrimary"
android:textSize="18sp"
android:id="#+id/button6" />
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
screenshot: