Reputation: 3230
I am trying to create a customview which will allow me to add images and text. The reason I am doing this is because in the app I am creating I am repeating the same code over and over in order to achieve this and I want to try and wrap it up into its own custom view so I can just use setters and position the view relative to one another, placing all this code in one simple to use class.
I can add text views and imageviews to the view however when I try to position them in relation to one another I always get a NPE crash.
Here is the code I have so far, it simply adds a TextView and then atempts to center the TextView inside my custom view.
public class MultiView extends RelativeLayout {
Context cx;
int images = 0;
public MultiView(Context context) {
super(context);
cx = context;
}
public void addText(String textParam) {
TextView tv = new TextView(cx);
tv.setText(textParam);
tv.setTextColor(Color.WHITE);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv
.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
// tv.setLayoutParams(layoutParams); // the app crashes when I add this line
this.addView(tv);
}
}
Upvotes: 1
Views: 6182
Reputation: 6179
Full class:
public class MultiView extends RelativeLayout {
Context cx;
int images = 0;
public MultiView(Context context) {
super(context);
cx = context;
}
public void addText(String textParam) {
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(cx);
tv.setText(textParam);
tv.setTextColor(Color.WHITE);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
tv.setLayoutParams(layoutParams);
this.addView(tv);
}
}
Upvotes: 2
Reputation: 256
I think you get a NPE
because tv.getLayoutParams();
returns null
.
Try the following :
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
tv.setLayoutParams(layoutParams); // the app crashes when I add this line
this.addView(tv);
Upvotes: 1
Reputation: 1523
It is very strange that you get crash on that line:
tv.setLayoutParams(layoutParams);
I think you that you get it on that line because created View don't have any LayoutParams (besides, if it RelativeLayout.LayoutParams):
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
I think that you can improve addText() method by this video from DevBytes: http://www.youtube.com/watch?v=55wLsaWpQ4g
But if you want to make your code work you need to replace your steps like this:
TextView tv = new TextView(cx);
tv.setText(textParam);
tv.setTextColor(Color.WHITE);
this.addView(tv); // Programatically created View get LayoutParams when we add it to a ViewGroup
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
tv.setLayoutParams(layoutParams);
Or create your layout params by self:
TextView tv = new TextView(cx);
tv.setText(textParam);
tv.setTextColor(Color.WHITE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
tv.setLayoutParams(layoutParams);
this.addView(tv);
Upvotes: 1