1

I've defined a layout in XML and now I'd like to use it to make a custom button (It just has to show the layout and be clickable, I don't care about different states and stuff). I'm not too sure what to do next though. How to I create this in my main activity?

Thanks!

My custom XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dp"
    android:layout_marginTop="10dp"
    android:textSize="20dp"
    android:text="New Text"
    android:id="@+id/text" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginRight="25dp"
    android:layout_alignParentRight="true"
    android:src="@drawable/left"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:maxHeight="30dp"
    android:maxWidth="30dp"/>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text"
    android:height="1dp"
    android:background="@color/dim_foreground_material_dark"
    android:layout_marginLeft="25dp"
    android:layout_marginTop="10dp"
    android:text=""
    android:id="@+id/horiz" />
</RelativeLayout>
4
  • Try setting an ID to the whole layout and then reference it in your code with findViewById() method. Then attach an onclick event to the object. Commented Dec 27, 2015 at 16:51
  • Yeah thats what I'm having trouble with :P How can I attatch an ID to this layout and use it in my activity.xml? I've tried referencing the layout by file name, but that doesn't work Commented Dec 27, 2015 at 16:52
  • Add the android:id="@+id/layout_id" attribute to your <RelativeLayout> Commented Dec 27, 2015 at 16:58
  • Ah right -_- Now I'd be able to use that in java, but I want to use in it XML in the main activity. Using the file name or the ID doesn't work as a tag name :/ Commented Dec 27, 2015 at 17:01

2 Answers 2

1

Lets start by exploring how we can create custom view:

1)Subclass one of the built-in layouts.

2)Inflate a merge layout in the constructor.

3)Initialize members to point to inner views with findViewById().

4)Add your own APIs to query and update the view state.

So I think you want to use the second option:

public class ButtonView extends Button  {

    public ButtonView (Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ButtonView (Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        LayoutInflater.from(context).inflate(R.layout.custom_xml, this, true);

    }


}

for further reading take a look at:

Custom Layouts on Android

Sign up to request clarification or add additional context in comments.

2 Comments

Are there any functional differences between your solution and mine?
It depends on your requirements. you can do a lot of stuff in java class for example at onDraw method or you can do anything you like. It is very flexible. But if your requirement dose not need that flexibility use your own answer.
0

Figured it out, I can reference the layout in XML by using an <include/> tag.

<include layout="@layout/settings_layout"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/accountStaticUnderline"
    android:id="@+id/termBegin"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.