karex
karex

Reputation: 217

Android Custom View declared in xml

I want to make a View that consists of few other standard elements. There are ProgressBar, TextViews and ImageView. I declared how to layout them in res/layout/myView.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_marginRight="10dp"
    tools:ignore="HardcodedText,ContentDescription" >

    <ImageView
        android:id="@+id/qItemImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/qItemNameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@id/qItemImageView"
        android:gravity="center"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ProgressBar
        android:id="@+id/qProgressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/qItemNameTextView"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/qItemNameTextView"
        android:max="100"
        android:progress="50" />

    <TextView
        android:id="@+id/qLevelTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/qItemImageView"
        android:layout_alignTop="@id/qItemImageView"
        android:text="7"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/qTimeTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/qProgressBar"
        android:layout_alignLeft="@id/qProgressBar"
        android:layout_alignRight="@id/qProgressBar"
        android:layout_alignTop="@id/qProgressBar"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="01:01:01"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/black" />

</RelativeLayout>

I also created a new class, but do not know how can I bind it to this resource file...

import android.content.Context;
import android.view.View;

public class QueueRowView extends View {
    public QueueRowView(Context context) {
        super(context);
    }
}

How can I make my control look like specified in layout file?

Upvotes: 1

Views: 117

Answers (1)

SimonSays
SimonSays

Reputation: 10977

Is there a reason why you need a separate class for this layout? You could just include this layout file into another one. If you really want the extra class, then look at Fragments.

Upvotes: 1

Related Questions