0

In my fragment, I have two linear layout with the same tag "result". Inside theses linear layouts, I have another layout with the tag "toggle" and a button with the tag "toggleButton".

I want that : when I click on my button, it toggle the linear layout "toggle" of its linear layout parent "result"

But when I do this :

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val root = rootLayoutResultSearch
        root.findViewWithTag<Button>("toToggleButton")
        val buttonToggle = view.findViewWithTag<Button>("toToggleButton")

        buttonToggle.setOnClickListener{
            Log.i(TAG, "click")
            val toggle = view.findViewWithTag<LinearLayout>("toToggle")
            if(toggle.visibility == View.GONE){
                toggle.visibility = View.VISIBLE
            }else{
                toggle.visibility = View.GONE
            }
        }
    }

it's working only for my first linear layout "result". When I click on my second button, it's not doing anything

Here the xml of the layouts "result" (I only post the first, the second is the exact same one)

<LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:tag="result"
                    android:contentDescription="result">
                    <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical"
                            android:padding="35dp"
                            android:background="@drawable/whit_bg_and_shadow"
                            android:tag="visibleNotChangeable"
                            >

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="horizontal">

                                <TextView
                                    style="@style/hoursSearch"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_weight=".1"
                                    android:fontFamily="@font/rubik_medium_italic"
                                    android:tag="heureDepart"
                                    android:text="@string/fillHoursRecherche1"
                                    android:textStyle="italic" />

                                <TextView
                                    style="@style/citySearch"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginStart="15dp"
                                    android:layout_weight=".9"
                                    android:fontFamily="@font/rubik_medium"
                                    android:tag="villeDepart"
                                    android:text="@string/fillVilleRecherche1"

                                    android:textSize="18sp" />


                            </LinearLayout>

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="horizontal">

                                <TextView
                                    style="@style/hoursSearch"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_weight=".1"
                                    android:fontFamily="@font/rubik_medium_italic"
                                    android:tag="heureDepart"
                                    android:text="@string/fillHoursRecherche2"
                                    android:textFontWeight="500"
                                    android:textStyle="italic" />

                                <TextView
                                    style="@style/citySearch"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginStart="15dp"
                                    android:layout_weight=".9"
                                    android:fontFamily="@font/rubik_medium"
                                    android:tag="villeDepart"
                                    android:text="@string/fillVilleRecherche2"
                                    android:textSize="18sp" />


                            </LinearLayout>

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="end"
                                android:orientation="horizontal">

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginStart="15dp"
                                    android:fontFamily="@font/roboto"
                                    android:tag="villeArrivee"
                                    android:text="@string/jourCircuRecherche" />


                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:fontFamily="@font/roboto"
                                    android:tag="villeArrivee"
                                    android:text="@string/fillJourCircuRecherche" />
                            </LinearLayout>

                            <View style="@style/HorizontalLine" />

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="horizontal">

                                <ImageView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:src="@drawable/bus" />

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center"
                                    android:layout_marginStart="15dp"
                                    android:background="@drawable/dark_blue_rectangle"
                                    android:text="3"
                                    android:textColor="@color/colorWhite" />

                            </LinearLayout>

                            <View style="@style/HorizontalLine" />

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="horizontal">

                                <ImageView
                                    android:layout_width="20dp"
                                    android:layout_height="20dp"
                                    android:src="@drawable/horaire" />

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center"
                                    android:layout_marginStart="15dp"
                                    android:fontFamily="@font/roboto"
                                    android:text="@string/fillTempsRecherche" />

                            </LinearLayout>


                        </LinearLayout>
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:padding="35dp"
                        android:background="@drawable/gray_bg"
                        android:visibility="gone"
                        android:tag="toToggle"
                        >

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                            <TextView
                                style="@style/hoursSearch"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_weight=".1"
                                android:fontFamily="@font/rubik_medium_italic"
                                android:tag="heureDepart"
                                android:text="@string/fillHoursRecherche1"
                                android:textStyle="italic" />

                            <TextView
                                style="@style/citySearch"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginStart="15dp"
                                android:layout_weight=".9"
                                android:fontFamily="@font/rubik_medium"
                                android:tag="villeDepart"
                                android:text="@string/fillVilleRecherche1"

                                android:textSize="18sp" />


                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                            <TextView
                                style="@style/hoursSearch"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_weight=".1"
                                android:fontFamily="@font/rubik_medium_italic"
                                android:tag="heureDepart"
                                android:text="@string/fillHoursRecherche2"
                                android:textFontWeight="500"
                                android:textStyle="italic" />

                            <TextView
                                style="@style/citySearch"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginStart="15dp"
                                android:layout_weight=".9"
                                android:fontFamily="@font/rubik_medium"
                                android:tag="villeDepart"
                                android:text="@string/fillVilleRecherche2"
                                android:textSize="18sp" />


                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="end"
                            android:orientation="horizontal">

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginStart="15dp"
                                android:fontFamily="@font/roboto"
                                android:tag="villeArrivee"
                                android:text="@string/jourCircuRecherche" />


                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/roboto"
                                android:tag="villeArrivee"
                                android:text="@string/fillJourCircuRecherche" />
                        </LinearLayout>

                        <View style="@style/HorizontalLine" />

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:src="@drawable/bus" />

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center"
                                android:layout_marginStart="15dp"
                                android:background="@drawable/dark_blue_rectangle"
                                android:text="3"
                                android:textColor="@color/colorWhite" />

                        </LinearLayout>

                        <View style="@style/HorizontalLine" />

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                            <ImageView
                                android:layout_width="20dp"
                                android:layout_height="20dp"
                                android:src="@drawable/horaire" />

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center"
                                android:layout_marginStart="15dp"
                                android:fontFamily="@font/roboto"
                                android:text="@string/fillTempsRecherche" />

                        </LinearLayout>

                    </LinearLayout>
                    <Button
                        android:id="@+id/ToggleSearchResult"
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:background="@drawable/arrow_down_blue_circle"
                        android:layout_gravity="center"
                        android:layout_marginTop="-20dp"
                        android:tag="toToggleButton"
                        />
                </LinearLayout>
Projet Sin
  • 337
  • 8
  • 20
  • When posting a code sample, it would probably be more helpful to clean it up to the point where the person seeking an answer for you can just paste it into a project without having to clean it up themselves. – Andre Artus Nov 30 '18 at 16:10
  • Keep in mind that `findViewWithTag` only returns the first view with the given tag. – Andre Artus Nov 30 '18 at 16:37
  • @AndreArtus ok, so that's the reason of my "problem" ^^. And sorry for the clean up, I didn't see that when I created my question. – Projet Sin Dec 03 '18 at 08:42

1 Answers1

1

Okay, I've thrown together a quick idea of what I think you want to accomplish. It excludes all the error and consistency checking one would normally want to do.

package com.example.toggler

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val list = arrayListOf<View>()
        root_View.findViewsWithText(list, "toToggleButton", View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION)
        list.forEach { button ->
            button.setOnClickListener { v: View ->
                val viewParent = v.parent
                if (viewParent is LinearLayout) {
                    val taggedView = viewParent.findViewWithTag<View>("toggle")

                    taggedView.visibility = when {
                        taggedView.visibility == View.GONE -> View.VISIBLE
                        else -> View.GONE
                    }
                }
            }
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_View"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:contentDescription="toToggleButton"
            android:tag="toToggleButton"
            android:text="toggle"
            tools:ignore="HardcodedText" />

        <View
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#F44336"
            android:tag="toggle" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:contentDescription="toToggleButton"
            android:tag="toToggleButton"
            android:text="toggle"
            tools:ignore="HardcodedText" />

        <View
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#9C27B0"
            android:tag="toggle" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:contentDescription="toToggleButton"
            android:tag="toToggleButton"
            android:text="toggle"
            tools:ignore="HardcodedText" />

        <View
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#FFEB3B"
            android:tag="toggle" />
    </LinearLayout>
</LinearLayout>

[EDIT] Or, if you prefer to use tags:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        root_View.findViewsWithTag("toToggleButton").forEach { button ->
            button.setOnClickListener { v: View ->
                val viewParent = v.parent
                if (viewParent is LinearLayout) {
                    val taggedView = viewParent.findViewWithTag<View>("toggle")

                    taggedView.visibility = when {
                        taggedView.visibility == View.GONE -> View.VISIBLE
                        else -> View.GONE
                    }
                }
            }
        }
    }
}

private fun ViewGroup.findViewsWithTag(tag: String): Sequence<View> {
    return sequence {
        for (index in 0 until childCount) {
            val child = getChildAt(index)
            when (child) {
                is ViewGroup -> yieldAll(child.findViewsWithTag(tag))
                is View -> if (child.tag == tag) yield(child)
            }
        }
    }
}
Andre Artus
  • 1,850
  • 15
  • 21