I have created an application with four tabs loaded with specific webviews. I planned to add advertisement below the tabs. In my code I have set the content view as the ViewGroup created as TabHost. If I am add this Viewgroup to linearlayout the application have been crashed due to the TabHost.add(TabSpec) gets NullPointer exception. Here is the code.
public View addTabBarView(Context context)
{
m_vForm = _createTABForm(context);
return m_vForm;
}
private ViewGroup _createTABForm(Context context) {
sTabHost = new TabHost(context,null);
sTabHost.setLayoutParams(
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
HorizontalScrollView sScrollView = new HorizontalScrollView(context);
LinearLayout.LayoutParams sScrollViewParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
sScrollView.setVerticalScrollBarEnabled(false);
sScrollView.setHorizontalScrollBarEnabled(false);
sScrollView.setScrollBarStyle(TRIM_MEMORY_UI_HIDDEN);
sScrollView.setFillViewport(true);
TabWidget tabWidget = new TabWidget(context);
LinearLayout.LayoutParams sTabWidgetParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tabWidget.setId(android.R.id.tabs);
sScrollView.addView(tabWidget, sTabWidgetParams);
sTabHost.addView(sScrollView, sScrollViewParams);
FrameLayout frameLayout = new FrameLayout(context);
frameLayout.setId(android.R.id.tabcontent);
final float scale = context.getResources().getDisplayMetrics().density;
int paddingtop = (int) (64 * scale + 0.5f);
frameLayout.setPadding(0, paddingtop, 0, 0);
sTabHost.addView(frameLayout, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
sTabHost.setup();
return sTabHost;
}
public addTabItem(final String url, String tabTitle, Drawable tabIcon)
{
TabSpec ts1 = sTabHost.newTabSpec(tabTitle);
if(tabIcon==null)
ts1.setIndicator(tabTitle);
else
ts1.setIndicator(tabTitle,tabIcon);
ts1.setContent(new TabHost.TabContentFactory(){
#SuppressWarnings("deprecation")
public View createTabContent(String tag)
{
//Creating webview inside a layout
}
});
sTabHost.addTab(ts1); //Here throws NullPointer exception.
sTabHost.setOnTabChangedListener(this);
}
How can I achieve my requirement.?
Are you not using an XML layout? It's much more efficient and tidy than what you have above...
You haven't included any logcat so there isnt much to go on but it seems to me that the issue is your addTabItem tries to make something arbitrary and use it as the content when really you want to be using the frameLayout as the content so try passing that into the method and setting it as the content
Related
I need to set margin to dynamically created UI. I want to add margin to LinearLayout.
Below is my code
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
viewPager = (ViewPager) container;
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
this.layoutInflater = inflater;
scrollView = new ScrollView(getActivity());
linearLayout = new LinearLayout(getActivity());
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(15, 15, 15, 15);
scrollView.setLayoutParams(layoutParams); //add this
scrollView.addView(linearLayout, layoutParams);
//adding few UI controllers dynamically by method call to here
return scrollView;
}
I tried many ways but nothing works. Currently, it is not adding space/margin as per given dimensions.
if(layoutParams instanceof MarginLayoutParams)
{
((MarginLayoutParams) layoutParams).topMargin = 15;
((MarginLayoutParams) layoutParams).leftMargin = 15;
//... etc
}
As per this SO answer:
scrollView.setFillViewport(true);
This forces the childview to stretch to the parent scrollview. Then you can set the margins that will add space after filling the scrollview.
If you want to add margins to LinearLayout, you need to create LayoutParams of the same type of the parent, so:
scrollView = new ScrollView(getContext());
linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
ScrollView.LayoutParams layoutParams = new ScrollView.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
// I recommend you to set resolved size as margins, not pixels like you are doing here.
layoutParams.setMargins(15, 15, 15, 15);
scrollView.addView(linearLayout, layoutParams);
EDIT
Remember to add the ScrollView to your layout adding LayoutParams to it.
For example in a Fragment (you are using getActivity() so i suppose you are in a Fragment):
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ScrollView scrollView = new ScrollView(getActivity());
LinearLayout linearLayout = new LinearLayout(getActivity());
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(15, 15, 15, 15);
scrollView.addView(linearLayout, layoutParams);
scrollView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return scrollView;
}
you need to call scrollView.setLayoutParams(layoutParams);
also set Layoutparams to your Scrollview and Inner LinearLayout
in your code :
scrollView = new ScrollView(getActivity());
scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
scrollView.setFillViewport(true);
linearLayout = new LinearLayout(getActivity());
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) scrollView
.getLayoutParams();
layoutParams.setMargins(15, 15, 15, 15);
scrollView.setLayoutParams(layoutParams); //add this
scrollView.addView(linearLayout, layoutParams);
I am working to create fields at run time, like in a relative layout am adding one text field at right corner and one Check-box at the left corner.
For this am getting problem, currently i am using the following code:
ViewGroup hori_layout=new RelativeLayout(getParent());
hori_layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
TextView tv1=new TextView(getParent());
tv1.setText(_medContactNames[i]);
tv1.setTextColor(Color.BLACK);
CheckBox cb = new CheckBox(getApplicationContext());
hori_layout.addView(tv1);
hori_layout.addView(cb);
layout.addView(hori_layout);
*
/**
* GENERATING RELATIVE LAYOUT AT RUNTIME
* */
public class RL extends RelativeLayout {
public RL(Context context,int i,String flag) {
super(context);
//FIRST FIELD OF THE LAYOUT
TextView firstField = new TextView(context);
firstField.setTextColor(Color.BLACK);
if(flag.equalsIgnoreCase("LAW")){
firstField.setText(_lawContactNames[i]);
}else{
firstField.setText(_medContactNames[i]);
}
firstField.setId(1);
//SECOND FIELD OF THE LAYOUT
CheckBox secondField = new CheckBox(context);
secondField.setId(2);
//FIRST LAYOUT WHICH MUST BE PRESENT AT LEFT END == TEXT FIELD
RelativeLayout.LayoutParams lpSecond = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(firstField, lpSecond);
//SECOND LAYOUT AT RIGHT END == CHECK BOX
RelativeLayout.LayoutParams lpFirst = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpFirst.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, secondField.getId());
addView(secondField, lpFirst);
}
}
*
Please help me to fix the code below.
I am making a mousepad view for my Android Remote Application.
public class MousePadView extends RelativeLayout {
private float scale;
// MOUSE BUTTONS LEFT CLICK, MIDDLE CLICK, RIGHT CLICK
private LinearLayout layoutMouseButtonBody;
private RelativeLayout.LayoutParams paramsMouseButtonBody;
private Button left;
private Button middle;
private Button right;
private LinearLayout.LayoutParams paramsButtons;
// BUTTONS FOR MOUSEWHEEL (UP AND DOWN)
private LinearLayout layoutWheelsBody;
private RelativeLayout.LayoutParams paramsWheelBody;
private Button up;
private Button down;
private LinearLayout.LayoutParams paramsWheelButton;
public MousePadView2(Context context, AttributeSet attrs, int defStyle) { // CONSTRUCTOR
super(context, attrs, defStyle);
scale = context.getResources().getDisplayMetrics().density; // GET SCALE FOR CONVERTING DPI TO PIXELS
// MOUSE BUTTON LAYOUT
paramsButtons = new LinearLayout.LayoutParams(DpiToPixels(0), LayoutParams.WRAP_CONTENT, 1);
left = new Button(context);
left.setText("L");
left.setLayoutParams(paramsButtons);
middle = new Button(context);
middle.setText("M");
middle.setLayoutParams(paramsButtons);
right = new Button(context);
right.setText("R");
right.setLayoutParams(paramsButtons);
paramsMouseButtonBody = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
paramsMouseButtonBody.addRule(ALIGN_PARENT_BOTTOM); // RELATIVE LAYOUT RULES
paramsMouseButtonBody.addRule(ALIGN_PARENT_LEFT); // RELATIVE LAYOUT RULES
paramsMouseButtonBody.addRule(ALIGN_PARENT_RIGHT); // RELATIVE LAYOUT RULES
layoutMouseButtonBody = new LinearLayout(context);
layoutMouseButtonBody.setBackgroundResource(android.R.drawable.bottom_bar);
// layoutMouseButtonBody.setPadding(0, DpiToPixels(4), 0, 0);
layoutMouseButtonBody.setOrientation(LinearLayout.HORIZONTAL);
layoutMouseButtonBody.setLayoutParams(paramsMouseButtonBody);
layoutMouseButtonBody.addView(left);
layoutMouseButtonBody.addView(middle);
layoutMouseButtonBody.addView(right);
// WHEELS
paramsWheelButton = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
up = new Button(context);
up.setText("U");
up.setLayoutParams(paramsWheelButton);
down = new Button(context);
down.setText("D");
down.setLayoutParams(paramsWheelButton);
paramsWheelBody = new RelativeLayout.LayoutParams(DpiToPixels(32), LayoutParams.WRAP_CONTENT);
paramsWheelBody.addRule(LEFT_OF, layoutMouseButtonBody.getId());
paramsWheelBody.addRule(ALIGN_PARENT_BOTTOM);
layoutWheelsBody = new LinearLayout(context);
layoutWheelsBody.setOrientation(LinearLayout.VERTICAL);
layoutWheelsBody.setBackgroundResource(android.R.drawable.bottom_bar);
layoutWheelsBody.setLayoutParams(paramsWheelBody);
layoutWheelsBody.addView(up);
layoutWheelsBody.addView(down);
// PARENT
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
addView(layoutMouseButtonBody); // add mousebutton layout in parent (relativelayout)
addView(layoutWheelsBody); // add mousewheel button layout in parent (relativelayout)
}
private int DpiToPixels(int dp) {
return (int)(dp * scale + 0.5f); // converting DPI to Pixels
}
}
The image on the Left is the output generated by Android SDK and The right one is the output that I want.
Please Help me.
I don't want to inflate layout from XML.
I think the only way to achieve what you're looking for is by grouping both your wheel and button layout into another layout. After that, simply align the result to the bottom of the parent.
Add something like this:
LayoutParams paramsTotal = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
paramsTotal.addRule(ALIGN_PARENT_BOTTOM);
LinearLayout layoutTotal = new LinearLayout(context);
layoutTotal.setOrientation(LinearLayout.VERTICAL);
layoutTotal.setLayoutParams(paramsTotal);
layoutTotal.addView(layoutWheelsBody);
layoutTotal.addView(layoutMouseButtonBody);
// PARENT
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
addView(layoutTotal); // add the combined layout
That should place the wheel buttons right above the mouse buttons (or vice versa: the mouse buttons directly below the wheel buttons) and the combination all the way at the bottom.
//Edit: alternatively, you could make the root layout a LinearLayout and add a dummy view with a weight of '1' at the top, which will push the other elements down to the bottom. I'd probably prefer the RelativeLayout option though.
Just out of curiousity: why not just inflate the layout? Personally I find that way more managable.
Your layout management is messed up. I suggest using this code:
// MOUSE BUTTON LAYOUT
paramsButtons = new LinearLayout.LayoutParams(DpiToPixels(0), LayoutParams.WRAP_CONTENT, 1);
left = new Button(context);
left.setText("L");
middle = new Button(context);
middle.setText("M");
right = new Button(context);
right.setText("R");
paramsMouseButtonBody = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
paramsMouseButtonBody.addRule(ALIGN_PARENT_BOTTOM); // RELATIVE LAYOUT RULES
layoutMouseButtonBody = new LinearLayout(context);
layoutMouseButtonBody.setBackgroundResource(android.R.drawable.bottom_bar);
// layoutMouseButtonBody.setPadding(0, DpiToPixels(4), 0, 0);
layoutMouseButtonBody.setOrientation(LinearLayout.HORIZONTAL);
layoutMouseButtonBody.addView(left, paramsButtons );
layoutMouseButtonBody.addView(middle, paramsButtons );
layoutMouseButtonBody.addView(right, paramsButtons );
// WHEELS
paramsWheelButton = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
up = new Button(context);
up.setText("U");
down = new Button(context);
down.setText("D");
paramsWheelBody = new RelativeLayout.LayoutParams(DpiToPixels(32), LayoutParams.WRAP_CONTENT);
paramsWheelBody.addRule(ABOVE, layoutMouseButtonBody.getId());
layoutWheelsBody = new LinearLayout(context);
layoutWheelsBody.setOrientation(LinearLayout.VERTICAL);
layoutWheelsBody.setBackgroundResource(android.R.drawable.bottom_bar);
layoutWheelsBody.addView(up, paramsWheelButton);
layoutWheelsBody.addView(down, paramsWheelButton);
// PARENT
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
addView(layoutMouseButtonBody, paramsMouseButtonBody ); // add mousebutton layout in parent (relativelayout)
addView(layoutWheelsBody, paramsWheelBody); // add mousewheel button layout in parent (relativelayout)
I need to layout the views from the RIGHT side in a Fragment in Android, however, android did not layout the sub views as what I thought.
I tried to add a TableLayout and an ImageView to a LINEARLAYOUT, the width of the ImageView was fixed and the width of TableLayout is dynamic. Furthermore, the ImageView need to be located on the right side.
Part of the source code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
Context c = getActivity().getApplicationContext();
LinearLayout l = new LinearLayout(c);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 0);
params.gravity = Gravity.RIGHT;
l.setLayoutParams(params);
// l.setGravity(Gravity.RIGHT);
PankouAttachmentView pav = new PankouAttachmentView(c, null);
pav.setLayoutParams(params);
l.addView(pav);
ImageView iv = new ImageView(c);
iv.setClickable(true);
iv.setFocusable(true);
iv.setImageResource(R.drawable.testarrow);
iv.setMaxWidth(BUTTON_WIDTH);
iv.setLayoutParams(new LayoutParams(BUTTON_WIDTH,
LayoutParams.MATCH_PARENT));
l.addView(iv);
return l;
}
Any help will be appreciated.THX :)
I'm not sure I completely understand your question but have you tried using a relative layout? Relative layout lets you accomplish much easier than a linear layout. See the android hello views tutorial.
I created a custom view. In it, theres a line, a textview, another line. beneath the bottom line, i wanted to put a new horizontally oriented linearlayout. when i run it, this nested linearlayout doesnt seem to show up at all. Instead, i can see the test button right underneath the bottom line. what am i doing wrong?
public class MyView extends LinearLayout {
public MyView(Context context, Question question) {
super(context);
// this.setLayoutParams(params);
this.setOrientation(VERTICAL);
this.setBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 2);
View topLine = new View(context);
lineParams.setMargins(0, 15, 0, 0);
topLine.setBackgroundColor(Color.argb(255, 0, 159, 218));
topLine.setLayoutParams(lineParams);
this.addView(topLine);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
//Challenge Question
TextView questionText = new TextView(context);
questionText.setTextColor(Color.BLACK);
questionText.setTextSize(14);
questionText.setLayoutParams(params);
questionText.setGravity(Gravity.CENTER_HORIZONTAL);
questionText.setText(question.getQuestion());
this.addView(questionText);
View bottomLine = new View(context);
bottomLine.setBackgroundColor(Color.argb(255, 0, 159, 218));
bottomLine.setLayoutParams(lineParams);
this.addView(bottomLine);
LinearLayout innerLayout = new LinearLayout(context);
LinearLayout.LayoutParams innerLayoutParams = new LinearLayout.LayoutParams(300, LayoutParams.WRAP_CONTENT);
innerLayout.setLayoutParams(innerLayoutParams);
innerLayout.setBackgroundColor(Color.RED);
innerLayout.setOrientation(HORIZONTAL);
//TableLayout for the multiple choices
TableLayout tableLayout = new TableLayout(context);
LayoutParams tableLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// tableLayoutParams.weight = .8f;
tableLayout.setBackgroundColor(Color.RED);
tableLayout.setLayoutParams(tableLayoutParams);
innerLayout.addView(tableLayout);
this.addView(innerLayout);
Button button = new Button(context);
button.setLayoutParams(params);
button.setText("testing 123");
this.addView(button);
}
Note that I pasted the code without all the stuff that I added to the tablelayout. I probably should have pasted that too. But it didn't work when I did that either. but either way, if i set the nested linearlayout to 300 width and set a background color of red to it, i should at least see it, no?
Think about what the height of the inner layout should be. Right now it is wrap_content and contains a TableLayout (with no rows) with its height also set to wrap_content. There doesn't seem to be anything in that inner layout giving it a height dimension, so that may be why it is not being displayed.
Trying the following will make your layout visible:
LinearLayout.LayoutParams innerLayoutParams = new LinearLayout.LayoutParams(300, 300);
More usefully, you can try adding something with a real width/height to the TableLayout.
Also consider writing your layout in XML to better separate your application logic and the presentation.