Android example: "Layout tricks: merging layouts" does not work

Just looking for some help. Please let me know if this is too vague.

I am trying to find an example of "Combining Layouts": http://developer.android.com/resources/articles/layout-tricks-merge.html

and i can't get it to work. The initial download on the page does not include all the necessary files. I embed the code below with block comments. When they are not commented, I get a lot of errors. If anyone has a suggestion, before I start inserting errors, that would be great ...

OkCancelBar:

package com.example.android.merge; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.Gravity; import android.view.LayoutInflater; import android.widget.Button; import android.widget.LinearLayout; public class OkCancelBar extends LinearLayout { public OkCancelBar(Context context, AttributeSet attrs) { super(context, attrs); setOrientation(HORIZONTAL); setGravity(Gravity.CENTER); setWeightSum(1.0f); LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true); /* TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 0, 0); String text = array.getString(R.styleable.OkCancelBar_okLabel); if (text == null) text = "Ok"; ((Button) findViewById(R.id.okcancelbar_ok)).setText(text); text = array.getString(R.styleable.OkCancelBar_cancelLabel); if (text == null) text = "Cancel"; ((Button) findViewById(R.id.okcancelbar_cancel)).setText(text); array.recycle(); */ } } 

main.xml:

 <?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:okCancelBar="http://schemas.android.com/apk/res/com.example.android.merge"> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="center" android:src="@drawable/golden_gate" /> <com.example.android.merge.OkCancelBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:paddingTop="8dip" android:gravity="center_horizontal" android:background="#AA000000" <!-- okCancelBar:okLabel="Save" okCancelBar:cancelLabel="Don't save" --> /> </merge> 
+4
source share
3 answers

I looked at the sources in zipped and the res / values ​​/attrs.xml file is missing. This is strange. Create the attrs.xml file and put the following code:

 <resources> <declare-styleable name="OkCancelBar"> <attr name="okLabel" format="string" /> <attr name="cancelLabel" format="string" /> </declare-styleable> </resources> 

Now it should work, but I don’t have time to check it, sorry.

+4
source

In this example, several files are missing. Namely:

In the layout folder: it should have main.xml, okcancelbar.xml and okcancelbar_button.xml. In the values ​​folder: it should have attrs.xml

The contents for main.xml and okcancelbar.xml are shown in the sample article. In okcancelbar_button.xml, you need to define one button as:

 <?xml version="1.0" encoding="utf-8"?> <Button xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/button" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> 

And attrs.xml should provide a label definition:

 <resources> <declare-styleable name="OkCancelBar"> <attr name="okLabel" format="string" /> <attr name="cancelLabel" format="string" /> </declare-styleable> </resources> 

Then everything should come together.

+2
source

pawelzieba and the x-ray response are correct and are studying this modification.
Paste the following code in okcancelbar.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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/okcancelbar_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" > </Button> <Button android:id="@+id/okcancelbar_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" > </Button> </LinearLayout> 


And modify OkCancelBar.java according to the following for a better understanding

 public class OkCancelBar extends LinearLayout { Context mContext; public OkCancelBar(Context context, AttributeSet attrs) { super(context, attrs); setOrientation(HORIZONTAL); setGravity(Gravity.CENTER); setWeightSum(1.0f); mContext = context; LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true); TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 0, 0); String text = array.getString(R.styleable.OkCancelBar_okLabel); if (text == null) text = "Ok"; //((Button) findViewById(R.id.button)).setText(text); ((Button) findViewById(R.id.okcancelbar_ok)).setText(text); text = array.getString(R.styleable.OkCancelBar_cancelLabel); if (text == null) text = "Cancel"; //((Button) findViewById(R.id.button)).setText(text); ((Button) findViewById(R.id.okcancelbar_cancel)).setText(text); Button btnCancel = (Button) findViewById(R.id.okcancelbar_cancel); btnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "Cancel is Clicked...", Toast.LENGTH_LONG).show(); } }); Button btnOk = (Button) findViewById(R.id.okcancelbar_ok); btnOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "OK is pressed...", Toast.LENGTH_LONG).show(); } }); array.recycle(); } } 
+1
source

All Articles