Skip to content
This repository was archived by the owner on Jun 8, 2025. It is now read-only.

Content View Binding

Steven Lewi edited this page Apr 9, 2015 · 1 revision

@ContentView can be used to automatically set content view of Activities, Fragments and Dialogs. Just put this annotation on top of your Activity/Fragment/Dialog class and framework will do the job for you (inflating/applying the layout).

See this example below.

Common Activity class

public class MainActivity extends Activity {

    @Override
    protected void onCreate() {
        setContentView(R.layout.activity_main);
    }
}

Activity using Annotation Binding

@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity {

    @Override
    protected void onContentViewCreated() {
    
    }
}

Common Fragment class

public class MainFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

Fragment using Annotation Binding

@ContentView(R.layout.activity_main)
public class MainFragment extends BaseFragment {

    @Override
    protected void onContentViewCreated() {
        
    }
}

Common Custom Dialog class

public class CustomDialog extends Dialog {

    public CustomDialog(Context context) {
        super(context);
        initLayout();
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
        initLayout();
    }

    protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        initLayout();
    }

    private void initLayout() {
        setContentView(R.layout.dialog1);
    }
}

Custom Dialog using Annotation Binding

@ContentView(R.layout.dialog1)
public class CustomDialog extends BaseDialog {

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

    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onContentViewCreated() {

    }
}

Clone this wiki locally