Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom layout based on existed layout

I'm trying to create custom layout for android. It normally draws on screen, but without inner views. Draws only my group_box.xml. How i can get access from my custom layout to inner views (TextView with id test) or how to draw they?

main.xml

<my.example.GroupBox
            android:layout_width="match_parent"
            android:layout_height="40sp">
        <TextView android:text="TEST"
                  android:id="@+id/test"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
    </my.example.GroupBox>

group_box.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                style="@style/groupbox">
    <LinearLayout style="@style/groupboxContent"
                  android:id="@+id/content"
                  android:layout_height="match_parent"
                  android:layout_width="match_parent"/>
    <TextView style="@style/groupboxLabel"
              android:id="@+id/caption"
              android:text="@string/visit"/>
</RelativeLayout>

GroupBox.java

public class GroupBox extends LinearLayout {

    public GroupBox(Context context) {
        super(context);
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.group_box, this);
    }

    public GroupBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.group_box, this);
    }
}
like image 275
Dmitriy Tarasov Avatar asked May 13 '26 03:05

Dmitriy Tarasov


1 Answers

you can access to the elements via setters and getters.

put this in your GroupBox.java

caption = (TextView) findViewById(R.id.caption);

 public void setLabel(CharSequence text) {
        caption.setText(text);
    }

add an id to your control in xml:

<my.example.GroupBox
            android:layout_width="match_parent"
            android:layout_height="40sp"
android:id="mycontrol">
        <TextView android:text="TEST"
                  android:id="@+id/test"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
    </my.example.GroupBox>

then find your control in the main activity and do this:

yourcontrol = (GroupBox) findViewById(R.id.mycontrol)

yourcontrol.setLabel("test");
like image 86
passsy Avatar answered May 15 '26 16:05

passsy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!