1

I have one RelativeLayout that has TextView (first label) , EditText(for input), TextView (second label). I have this in at least 10 activities in my project. How I can extract view and make my own. So, if I want to change textSize , I will have to change it on just one place, not 10.

For example I would like to have this

<RelativeLayout
  android:width="match_parent"
  android:height="wrap_content"
>
  <TextView
    android:id="firstTextView"
    ...
    android:text="I like">
  <EditText
     android:id="edittextColor" 
     hint="type some color here"
     ... >
<TextView
    android:id="secondTextView"
    ...
    android:text="car.">
    </RelativeLayout>

So, I need something like this on a lot of place. What I would like to have is:

<MySpecialView
   firstText="I like"
   colorEditTextHint="type color here"
   secondText="car"/>
Mladen Rakonjac
  • 9,562
  • 7
  • 42
  • 55

4 Answers4

1

Inflaters

Let's suppose that your RelativeLayout file is called reusable_layout. This means that you could access it as R.layout.reusable_layout (considering that you have this file stored in the layouts folder of your project).

In your usual override of onCreate() add these variables at the start: LayoutInflater inflater = getSystemService(LAYOUT_INFLATER_SERVICE); RelativeLayout layout = inflater.inflate(R.layout.reusable_layout, null);

Afterwards, call setContentView(layout);

If you want to edit the children you can call layout.getChildAt(int childNumber); This would return you a View

An example of editing the first TextView child:

TextView tv = (TextView) layout.getChildAt(0);
tv.setText("Example String");

UPDATE: Another way to do what you want!

Creating a custom view may do the job!

A good tutorial on these is included here: https://developer.android.com/training/custom-views/create-view.html#subclassview

I think all you need to know is included in that. Another possibly useful source would be included here: how to add views inside a custom View?

Hope I helped,

-Daniel

Community
  • 1
  • 1
BlazeDev
  • 37
  • 5
0

You can create one common layout and include in all the 10 activities layout like this

common_layout.xml

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


  <TextView
    android:id="@+id/label1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Label1"/>

  <EditText
    android:id="@+id/input1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/label1"
    android:text="Input1"/>

 </RelativeLayout>

activity_layout

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

        <include layout="@layout/common_layout"/>

        <TextView
          android:id="@+id/textinactivity_tv"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Activity text"/>

    </LinearLayout>

I hope this is what you wanted.

Abhishek Jaiswal
  • 628
  • 6
  • 17
0

Although Android offers a variety of widgets to provide small and re-usable interactive elements, you might also need to re-use larger components that require a special layout. To efficiently re-use complete layouts, you can use the include and merge tags to embed another layout inside the current layout. https://developer.android.com/training/improving-layouts/reusing-layouts.html

What about <include> create you your_base_layout.xml and <include> it in any other xml in the place where you want to add it

your_base_layout.xml

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:id="@+id/some_other_id">
   <Button
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:id="@+id/button1" />
 </LinearLayout>

<include
   android:id="@+id/include_id"
   layout="@layout/your_base_layout" />

example of usage: another_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

      <include
         android:id="@+id/include_id"
         layout="@layout/your_base_layout" />

    <TextView android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="@string/hello"
              android:padding="10dp" />

    ...

</LinearLayout>

This is how you access views in it,

View includedLayout = findViewById(R.id.some_id_if_needed);
Button buttonInsideTheIncludedLayout = (Button) includedLayout.findViewById(R.id.button1); // if there is a button in your base layout that you included access like this

find great answers >here

Community
  • 1
  • 1
Charuක
  • 12,953
  • 5
  • 50
  • 88
0

You can define your own control with specified attributes.

Save ButtonPlus.java into your package.

e.g.

public class ButtonPlus extends Button {

    public ButtonPlus(Context context) {
        super(context);
    }

    public ButtonPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        CustomFontHelper.setCustomFont(this, context, attrs);
    }

    public ButtonPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        CustomFontHelper.setCustomFont(this, context, attrs);
    }
}

And you can use inside your layout XML file.

Naitik
  • 796
  • 11
  • 32