I am trying to programmatically (without using XML files) to create custom subtitles in Android (this is what I call it in iOS), which basically is a series of basic views (shortcuts, buttons, text fields, etc.) combined into a reusable subview class, so I can use it inside my UIViewControllers or Activity in Android.
I do not know what the correct terminology is in Android. There seem to be a million different terms.
Custom View, ViewGroups, Layouts, Widgets, Components, everything you want to name.
On iOS, this is done as follows:
CustomView.h
@interface CustomView : UIView @property (nonatomic, strong) UILabel *message; @property (nonatomic, strong) UIButton *button; @end
CustomView.m
@implementation CustomView -(id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if(self) { [self initViews]; [self initConstraints]; } return self; } -(void)initViews { self.message = [[UILabel alloc] init]; self.button = [[UIButton alloc] init]; [self addSubview:self.message]; [self addSubview:self.button]; } -(void)initConstraints { id views = @{ @"message": self.message, @"button": self.button }; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[message]|" options:0 metrics:nil views:views]]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button]|" options:0 metrics:nil views:views]]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[message][button]|" options:0 metrics:nil views:views]]; } @end
Now I can reuse this custom view in any ViewController (Android Activity ) that I have selected.
How can one achieve something like this in Android?
I look around and from what I'm going to in Android to add subviews, I add them to Layouts :
RelativeLayout relativeLayout = new RelativeLayout(...); TextView textView = new TextView(...); relativeLayout.addSubview(textView);
Does this mean that I need to extend RelativeLayout or ViewGroup ?
Looking at this page: http://developer.android.com/reference/android/view/ViewGroup.html
It seems that we need to write some really complex logic for the layout of the user view, for example:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int count = getChildCount(); // These keep track of the space we are using on the left and right for // views positioned there; we need member variables so we can also use // these for layout later. mLeftWidth = 0; mRightWidth = 0; // Measurement will ultimately be computing these values. int maxHeight = 0; int maxWidth = 0; int childState = 0; // Iterate through all children, measuring them and computing our dimensions // from their size. for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() != GONE) { // Measure the child. measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); // Update our size information based on the layout params. Children // that asked to be positioned on the left or right go in those gutters. final LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (lp.position == LayoutParams.POSITION_LEFT) { mLeftWidth += Math.max(maxWidth, child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin); } else if (lp.position == LayoutParams.POSITION_RIGHT) { mRightWidth += Math.max(maxWidth, child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin); } else { maxWidth = Math.max(maxWidth, child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin); } maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin); childState = combineMeasuredStates(childState, child.getMeasuredState()); } } // Total width is the maximum width of all inner children plus the gutters. maxWidth += mLeftWidth + mRightWidth; // Check against our minimum height and width maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); // Report our final dimensions. setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState), resolveSizeAndState(maxHeight, heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT)); }
All I'm trying to do is use a few basic android shortcuts, views, buttons in a custom view, such as the iOS example above, why is it so complicated in Android?
I was hoping for something simple:
public class CustomView extends View { public RelativeLayout mainLayout; public TextView message; public Button button;
I'm sorry that I am sincerely trying to learn and create a basic Android application, rather than trying to bash Android to do something.
I know how to add and compose objects within an Activity and have been doing this for the last two days, but not inside the user group View / View Group / Layout. I don’t want to design the same subview for each of my actions in an Android application that just contradicts good coding practice ?: D
You just need to learn a little from others who have done both iOS and Android development.
Edit
It seems that what I'm looking for is called Compound Control: http://developer.android.com/guide/topics/ui/custom-components.html
I will continue to dig and hope to get the result that I will: D
You just need to develop this Inflater business.