{"id":19260711,"url":"https://github.com/evant/yield-layout","last_synced_at":"2025-04-21T16:32:13.949Z","repository":{"id":18752492,"uuid":"21964699","full_name":"evant/yield-layout","owner":"evant","description":"Combine layouts in Android, opposite of how \u003cinclude/\u003e works.","archived":false,"fork":false,"pushed_at":"2020-06-14T22:53:45.000Z","size":289,"stargazers_count":50,"open_issues_count":1,"forks_count":8,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-01T14:38:19.220Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/evant.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-18T02:19:36.000Z","updated_at":"2024-10-18T14:34:45.000Z","dependencies_parsed_at":"2022-08-30T06:01:11.412Z","dependency_job_id":null,"html_url":"https://github.com/evant/yield-layout","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evant%2Fyield-layout","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evant%2Fyield-layout/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evant%2Fyield-layout/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evant%2Fyield-layout/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/evant","download_url":"https://codeload.github.com/evant/yield-layout/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250091038,"owners_count":21373306,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T19:22:37.392Z","updated_at":"2025-04-21T16:32:13.299Z","avatar_url":"https://github.com/evant.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"YieldLayout\n============\n\nHave you ever had to create two almost-identical layout files because they had a few minor differences? Sure, you can fix it with `\u003cinclude/\u003e` but then you start ending up with layouts all over the place.\n\nYieldLayout to the rescue! It works opposite of `\u003cinclude/\u003e` so you can combine one layout around another instead of inside it. After it does it's magic, you will have 0 extra views in your layout hierarchy, just like `\u003cinclude/\u003e`, so it's like it was never there.\n\n![preview](https://raw.githubusercontent.com/evant/yield-layout/master/images/preview.png)\n\n## Usage\n\nYou can create a layout file that has places to yield content.\n\n```xml\n\u003c!-- yield_view.xml --\u003e\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\n\u003cLinearLayout\n    xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    android:orientation=\"vertical\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\u003e\n\n    \u003cme.tatarka.yieldlayout.Yield\n        android:id=\"@+id/first\"\n        android:layout_weight=\"1\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"/\u003e\n\n    \u003cView\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"1dp\"\n        android:background=\"@color/divider_black\"\n        /\u003e\n\n    \u003cTextView\n        android:layout_weight=\"1\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"@string/between\"\n        android:gravity=\"center\"\n        android:textSize=\"@dimen/text_display_2\"\n        android:textColor=\"@color/text_black\"\n        /\u003e\n\n    \u003cView\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"1dp\"\n        android:background=\"@color/divider_black\"\n        /\u003e\n\n    \u003cme.tatarka.yieldlayout.Yield\n        android:id=\"@+id/second\"\n        android:layout_weight=\"1\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"/\u003e\n\u003c/LinearLayout\u003e\n```\n\nThen you can create another file that uses that layout, filling in the yields.\n\n```xml\n\u003c!-- use_yield.xml --\u003e\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\n\u003cme.tatarka.yieldlayout.YieldLayout\n    xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n    app:yield_layout=\"@layout/yield_linear_layout\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\u003e\n\n    \u003cTextView\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:gravity=\"center\"\n        android:text=\"@string/first\"\n        android:textColor=\"@color/text_black\"\n        android:textSize=\"@dimen/text_display_2\"\n        /\u003e\n\n    \u003cTextView\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"@string/second\"\n        android:textColor=\"@color/text_black\"\n        android:textSize=\"@dimen/text_display_2\"\n        android:gravity=\"center\"\n        /\u003e\n\n\u003c/me.tatarka.yieldlayout.YieldLayout\u003e\n```\n\nBy default child views are matched against `Yield`s in the order they are defined. However, if you\ngive your child views explicit id's, they will be matched against the `Yield` id's.\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\n\u003cme.tatarka.yieldlayout.YieldLayout\n    xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n    app:yield_layout=\"@layout/yield_linear_layout\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\u003e\n\n    \u003c!-- This one will appear second --\u003e\n    \u003cTextView\n        android:id=\"@+id/second\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"@string/second\"\n        android:textColor=\"@color/text_black\"\n        android:textSize=\"@dimen/text_display_2\"\n        android:gravity=\"center\"\n        /\u003e\n\n    \u003c!-- This one will appear first --\u003e\n    \u003cTextView\n        android:id=\"@+id/first\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:gravity=\"center\"\n        android:text=\"@string/first\"\n        android:textColor=\"@color/text_black\"\n        android:textSize=\"@dimen/text_display_2\"\n        /\u003e\n\u003c/me.tatarka.yieldlayout.YieldLayout\u003e\n\n```\n\n### ListViews\n\nIf you want to use this in a list adapter, you can't use the `YieldLayout` directly because it will try to add it's layout to it's parent which a listview doesn't allow. Instead, you can use `YieldLayoutInflater` which will return the final view to add instead of the `YieldLayout`.\n\n```java\n@Override\npublic View getView(int position, View convertView, ViewGroup parent) {\n    if (convertView == null) {\n        int layout = getItemViewType(position) == 0 ? R.layout.list_item_1 : R.layout.list_item_2;  \n        convertView = YieldLayoutInflater.from(mContext).inflate(layout, parent, false);\n    }\n    return convertView;\n}\n```\n\nOr if you are changeing the outer layout instead of the child views,\n\n```java\n@Override\npublic View getView(int position, View convertView, ViewGroup parent) {\n    if (convertView == null) {\n        int layout = getItemViewType(position) == 0 ? R.layout.layout_1 : R.layout.layout_2;  \n        convertView = YieldLayoutInflater.from(mContext).inflate(R.layout.list_item, layout, parent, false);\n    }\n    return convertView;\n}\n```\n\n### Different numbers of children\n\nYou may be wondering what happens if you don't specify as many child views for `YieldLayout` as there are `Yield`s. Too many children is an error because there is no way to know where to put the extra ones. Too few, however, will simply cause the extra `Yield`s to be removed.\n\nSometimes, however, you may depend on the `Yield` to correctly layout your views. For example,\n\n```xml\n\u003cRelativeLayout\n  xmlns:android=\"http://schemas.android.com/apk/res/android\"\n  xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n  android:layout_width=\"match_parent\"\n  android:layout_height=\"wrap_content\"\u003e\n\n    \u003cTextView\n         android:id=\"@+id/content\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:layout_above=\"@+id/footer\"/\u003e\n\n    \u003cme.tatarka.yieldlayout.Yield\n        android:id=\"@+id/footer\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:layout_alignParentBottom=\"true\"/\u003e\n\u003c/RelativeLayout\u003e\n```\n\nIf there is no matching child for `footer`, then it will be removed, and `content` won't lay out correctly. To fix this, you can add\n\n```xml\n    \u003cme.tatarka.yieldlayout.Yield\n      app:yield_keep_if_empty=\"true\"/\u003e\n```\n\nwhich will keep the `Yield` view even if it's not replaced. Note that in this case, the `Yield` view will always have a size of 0.\n\n### Layout Preview\n\nThe layout preview in Android Studio will show `Yield` placholder views so that you can see how they fit in your layout. If you define your `yield_layout` for your `YieldLayout`, that will correctly show as well.\n\nIf you are setting your `yield_layout` dynamically, you would think you could preview it by setting `tools:yield_layout` in your xml. Unfortunately, the `tools` namespace doesn't work with custom attributes. To get around this, you can set `app:tools_yield_layout` instead which will give an equivalent result.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevant%2Fyield-layout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fevant%2Fyield-layout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevant%2Fyield-layout/lists"}