Adding a textview in a constraint layout inside a recycler view shorten the constraint layout width - android-layout

I have a recycler view (dependency is com.android.support:recyclerview-v7:28.0.0) inside a constraint layout (dependency is androidx.constraintlayout:constraintlayout:2.0.0-beta4) and the item for this recycler view is another constraint layout.
The issue is : when I don't put anything inside the item layout everything is fine and my constraint layout match the width of the recycler view, but when I try to add a text view, the constraint layout width shorten to match the text view width.
I already tried different layout_width combinations for the constraint layout and the text view (match, wrap, 0dp) but nothing work.
Here is the fragment layout containing the recycler view :
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://[enter image description here][1]schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingEnd="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/champions_rv"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="#layout/item_composition"
android:background="#android:color/holo_blue_light"/>
</androidx.constraintlayout.widget.ConstraintLayout>
and this is the item layout :
<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:background="#android:color/holo_orange_dark"
android:paddingStart="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingEnd="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_purple"
android:text="Text view"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the result when the contraint layout is empty - it's good
And this is the result when I try to add the textview - This is not good

Your TextView's width should match its constraint, not wrap its content. This will make the TextView expand to fill the ConstraintLayout instead:
<TextView
android:id="#+id/tv_name"
android:layout_width="0dp"

Related

Android - Button at the end of ScrollView not visible

I have added a button at the end of a ScrollView layout, inside it is a Constraint Layout. Both are inside a Relative layout. However, upon scrolling, the button does not show up on my device. It's stuck at the bottom of the layout and cannot get up. I have tried everything.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.BachelorDetailActivity"
android:id="#+id/layout_fragment_studyplan">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:clipToPadding="false"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/h1_studyplan"
android:textSize="#dimen/h1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_studyplan"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:textColor="#color/color_primary"
android:textStyle="bold"
android:textAllCaps="true"
android:paddingTop="#dimen/margin_default"
android:paddingStart="#dimen/margin_default"/>
<TextView
android:id="#+id/p_studyplan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/h1_studyplan"
app:layout_constraintStart_toStartOf="parent"
android:text="#string/list_studyplan"
android:paddingBottom="#dimen/margin_huge"
android:paddingStart="#dimen/margin_default"
android:paddingTop="#dimen/margin_default"
android:singleLine="false"
android:lineSpacingExtra="#dimen/margin_short"/>
<Button
android:id="#+id/btn_contact_study"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/p_studyplan"
android:layout_width="wrap_content"
android:layout_height="#dimen/button"
android:layout_marginBottom="500dp"
android:text="#string/contact_guide"
android:textColor="#color/white"
android:backgroundTint="#color/color_primary"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</RelativeLayout>
If you are trying to get the button to slide up the screen by 500dp by setting android:layout_marginBottom="500dp" as the button's bottom margin, you will also need to specify a bottom constraint for the button.
app:layout_constraintBottom_toBottomOf="parent"
Margins apply only when there is a constraint in effect for the direction of the margin.

Android Studio & Kotlin: Changing programmatically added images of a list view

Hey fellow programmers,
I was just programming in a listView and implementing actions for tapping a list element until I got to a point where I wanted to keep things recursively.
Taken this ClickListener, I would like to have the image next to the list element changed when tapped.
list.add(Model("firstelement","description 1", R.drawable.button_activated))
list.add(Model("secondelement","description 2",R.drawable.button_activated))
listview.adapter = MyAdapter(this, R.layout.row, list)
var elementArray: IntArray = intArrayOf(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
listview.setOnItemClickListener{ parent: AdapterView<*>, view: View, position:Int, id: Long ->
val currentimage = ? //here I'd like to retrieve the imageview of the tapped list item
val img: ImageView = findViewById(R.id.currentimage)
if (elementArray.get(position)==0){
img.setImageResource(R.drawable.button_activated)
elementArray.set(position,1)
}
else if (elementArray.get(position)==1){
img.setImageResource(R.drawable.button_dectivated)
elementArray.set(position,0)
The images are added to the list together with the text view.
How can I retrieve them to change them? Tried listview.getItemIdAtPosition but that leads to errors.
I bet there is a way - I just don't see it. Thanks in advance for any hint!
In addition, here is my layout row.xml where the list gets loaded into:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
>
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:id="#+id/image0"
android:src="#mipmap/ic_launcher"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:text="first element"
android:textStyle="bold"
android:textSize="20sp"
android:layout_margin="5dp"
android:textColor="#000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:text="second element"
android:textSize="18sp"
android:layout_margin="5dp"
android:textColor="#a9a9a9"
/>
</LinearLayout>
</LinearLayout>
Just resolved it on my own with the help of this solution: Android: Dynamically change Image in Listview
Had to access the lower imageView of the current view: ImageView = view.findViewById(R.id.image)

How to show HereMaps inside an android Linear Layout

Is there anyway to show HereMaps inside a Linear Layout?
This is my layout file and I want to show map in firstMap (LinearLayout).
<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.apploft.tabs.MyTabFragment">
<RelativeLayout
android:id="#+id/confirmed"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/firstMap"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="#color/cardview_shadow_start_color"
android:orientation="horizontal">
<!-- Map Fragment embedded with the map object -->
</LinearLayout>
</FrameLayout>
By adding maoview inside LinearLayout and then calling
<com.here.android.mpa.mapping.MapView
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible"/>
in the fragment
MapView mapColView = (MapView) colTabView.findViewById(R.id.mapview);
map = new com.here.android.mpa.mapping.Map();
map.setCenter(new GeoCoordinate(51.509865, -0.118092, 0.0), com.here.android.mpa.mapping.Map.Animation.NONE);
// Set the zoom level to the average between min and max
map.setZoomLevel((map.getMaxZoomLevel() + map.getMinZoomLevel()) / 2);
mapColView.setMap(map);
Also test/check onResume() onPause().
Use a MapView instead of a MapFragment in this case. The documentation is linked here for starter and here for premium.

how to hide/show a layout when recyclerview scroll (like twitter's new tweet layout)

i want to add a layout which hides/shows when recyclerview scroll. that layout will hide when recyclerview scroll down, will show when recyclerview scroll up, like twitter's new tweet layout. i can do it for appbarlayout but i can not do for that layout. how can i do that?
http://i.hizliresim.com/rVyDq3.png
the purple layout will hide show like actionbar :
http://i.hizliresim.com/gAMp92.png
here is my layout file :
<android.support.v4.widget.DrawerLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp">
<!-- my main content -->
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/tool_bar"
style="#style/ToolBarStyle"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways" />
<LinearLayout
android:orientation="vertical"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/color_accent_pink">
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/newsRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:clickable="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<!-- that layout will hide/show when recyclerview scroll -->
<LinearLayout
android:orientation="horizontal"
android:id="#+id/takimlar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/bg_group_item_swiping_active_state"
android:layout_gravity="bottom" >
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
<!-- my navigation drawer -->
<RelativeLayout
android:id="#+id/container"
android:layout_width="235dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#color/myDrawerBackground">
<TextView
android:id="#+id/merhabaText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="400dp"
android:layout_marginRight="40dp"
android:layout_alignParentRight="true"
android:text="Hello!"/>
</RelativeLayout>
SOLVED
I found the answer there : https://blog.iamsuleiman.com/implement-floating-action-button-fab-part-2/
I have a simple solution for that ..
recyclerview.addOnScrollListener(new HidingScrollListener()
{
#Override
public void onHide()
{
tabss.animate().translationY(tabss.getHeight())
.setInterpolator(new AccelerateInterpolator(2)).start();
filtershow.animate().translationY(-filtershow.getHeight())
.setInterpolator(new AccelerateInterpolator(1));
}
#Override
public void onShow()
{
tabss.animate().translationY(0).setInterpolator(new
DecelerateInterpolator(2)).start();
filtershow.animate().translationY(0).setInterpolator
(new DecelerateInterpolator(1)).start();
}
});
In that Recyclerview i was hiding screen top filters and screen bottom tabs
If you want have doubts about this hiding layout please ask me.
I will provide your required code for that.

ViewSwitcher with EditText leaves extra padding

I'm using a ViewSwitcher to allow switching from a TextView to an EditText element. What I've noticed is that the EditText element generates extra padding at the bottom of the view (i.e. if I remove the EditText everything looks as expected, but adding it, even when visibility is set to gone, generates extra space at the bottom). What's causing this and how should it be resolved?
<ViewSwitcher
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/IngredientsLineSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/IngredientsLineText"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:paddingBottom="0dip"
android:paddingLeft="10dip"
android:paddingTop="0dip"
android:text=""
android:textColor="#color/black"
android:textSize="#dimen/recipe_label_font_size" />
<EditText
android:id="#+id/IngredientsLineEdit"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:paddingBottom="0dip"
android:paddingLeft="10dip"
android:paddingTop="0dip"
android:text=""
android:visibility="gone"
android:textColor="#color/black"
android:textSize="#dimen/recipe_label_font_size" />
</ViewSwitcher>
You should put a RelativeLayout around the TextView or EditText to control their positioning.
-----
android:layout_height="match_parent" >
mlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/IngredientsLineSwitcher"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/IngredientsLineText"
android:layout_height="match_parent"
android:layout_width="match_parent"
----
<EditText
android:id="#+id/IngredientsLineEdit"
android:layout_height="match_parent"
android:layout_width="match_parent"
-----
Change from fill_parent to match_parent in all Views.

Resources