Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6060079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:47:04+00:00 2026-05-23T08:47:04+00:00

can anyone explain me these codes what are they doing exactly? I could not

  • 0

can anyone explain me these codes what are they doing exactly? I could not understand how it is adding two buttons (OK and Cancel). I expect some button creation code like new Button() or something like that It is accessing the buttons with id but there are no button with these ids in any xml file. I can just see the R.id.okcancelbar_ok defination in R file.

Thanks.

Original source : http://developer.android.com/resources/articles/layout-tricks-merge.html

Source Code : http://progx.org/users/Gfx/android/MergeLayout.zip

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();
    }}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:47:04+00:00Added an answer on May 23, 2026 at 8:47 am
    LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true);
    

    this line inflate “this” with the layout R.layout.okcancelbar

    ((Button) findViewById(R.id.okcancelbar_ok)).setText(text)
    

    it means that there is a button with id “okcancelbar_ok” in the layout okcancelbar ( inflated earlier)
    Next we assigned to it the text “Ok”

    ((Button) findViewById(R.id.okcancelbar_cancel)).setText(text);
    

    same as above, there is a button with id “okcancelbar_cancel” in the layout okcancelbar

    So this code do :
    1) inflate the view R.layout.okcancelbar
    2) get the button (declared in the previous layout) with id “okcancelbar_ok” and set the text to “Ok”
    3) idem with the button “okcancelbar_cancel” and text “Cancel”

    The “layout/okcancelbar.xml” layout should be like that :

    <?xml version="1.0" encoding="utf-8"?>
    <merge xmlns:android="http://schemas.android.com/apk/res/android">
        <include
            layout="@layout/okcancelbar_button"
            android:id="@+id/okcancelbar_ok" />
    
        <include
            layout="@layout/okcancelbar_button"
            android:id="@+id/okcancelbar_cancel" />
    </merge>
    

    There also should be a “values/attrs.xml” that looks like :

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <declare-styleable name="OkCancelBar">
        <attr name="okLabel" format="string"/>
        <attr name="cancelLabel" format="string"/>
    </declare-styleable>
    </resources>
    

    And finally the “layout/okcancelbar_button” should looks like :

    <?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="fill_parent"
      android:layout_height="fill_parent">    
    </Button>
    

    Hope it helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain what this mod_rewrite rule is doing? I'm trying to comment the
Can anyone explain exactly how the Strategy Pattern relates to Inversion of Control?
Can anyone explain what advantages there are to using a tool like MSBuild (or
Can anyone explain why following code won't compile? At least on g++ 4.2.4. And
Can anyone explain in simple words what First and Second Level caching in Hibernate/NHibernate
Can anyone explain the difference between Server.MapPath(.) , Server.MapPath(~) , Server.MapPath(@\) and Server.MapPath(/) ?
Can anyone explain the meaning of and purposes of the values for the SecurityAction
Can anyone explain to me what this means? Run-Time Check Failure #0 - The
Can anyone explain the differences between Protocols and Categories in Objective-C? When do you
Can anyone explain the difference between the types mentioned above and some sample usage

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.