Android RelativeLayout My Own MousePadView - android

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)

Related

How to make two buttons on the same row in eclipse programmatically?

LinearLayout table = (LinearLayout)findViewById(R.id.linearlayout);
btnControl = new Button(this);
btnControl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
btnControl.setPadding(0,40,0,40);//x,y,w,h
btnControl.setBackgroundColor(Color.parseColor("#1C344E"));
btnControl.setTextColor(Color.WHITE);
setMargins(btnControl, 50, 50, 50, 50);
btnLogs = new Button(this);
btnLogs.setLayoutParams(new LayoutParams(380, LayoutParams.WRAP_CONTENT));
btnLogs.setPadding(0,40,0,40);//x,y,w,h
btnLogs.setBackgroundColor(Color.parseColor("#1C344E"));
btnLogs.setTextColor(Color.WHITE);
setMargins(btnLogs, 50, 50, 50, 50);
btnLogs.setId(5);
btnLogs.setOnClickListener(this);
btnTermLogs = new Button(this);
btnTermLogs.setLayoutParams(new LayoutParams(380, LayoutParams.WRAP_CONTENT));
btnTermLogs.setPadding(0,40,0,40);//x,y,w,h
btnTermLogs.setBackgroundColor(Color.parseColor("#CC6675"));
btnTermLogs.setTextColor(Color.WHITE);
setMargins(btnLogs, 50, 50, 50, 50);
btnTermLogs.setId(6);
btnTermLogs.setOnClickListener(this);
btnLogout = new Button(this);
btnLogout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
btnLogout.setPadding(0,40,0,40);//x,y,w,h
btnLogout.setBackgroundColor(Color.parseColor("#CC6675"));
btnLogout.setTextColor(Color.WHITE);
table.addView(btnLogs);
table.addView(btnTermLogs);
I want the two buttons to be aligned together horizontally. However it just align themselves vertically which i dont want.
My buttons look like this:
what i want is to look like this:
Try this below function. It will help
public void addButtons(){
LinearLayout table = (LinearLayout)findViewById(R.id.linearlayout);
LinearLayout linearLayout1 = new LinearLayout(this);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams LayoutParamsview = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Button button1 = new Button(this);
button1.setText("Btn 1");
button1.setLayoutParams(LayoutParamsview);
linearLayout1.addView(button1);
table.addView(linearLayout1);
//
LinearLayout linearLayout2 = new LinearLayout(this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
Button button2 = new Button(this);
button2.setText("Btn 2");
button2.setLayoutParams(LayoutParamsview);
Button button3 = new Button(this);
button3.setText("Btn 3");
button3.setLayoutParams(LayoutParamsview);
linearLayout2.addView(button2);
linearLayout2.addView(button3);
table.addView(linearLayout2);
//
LinearLayout linearLayout3 = new LinearLayout(this);
linearLayout3.setOrientation(LinearLayout.VERTICAL);
Button button4 = new Button(this);
button4.setText("Btn 4");
button4.setLayoutParams(LayoutParamsview);
linearLayout3.addView(button4);
table.addView(linearLayout3);
}
Horizontal linear layout supports left to right widgets approach, When
app developer selects horizontal orientation in linear layout then it
will automatically start adding components and widgets to left to
right side after adding them. So here is the complete step by step
tutorial for Creating horizontal linearlayout programmatically
android.
Write this code
//Creating LinearLayout.
LinearLayout Horizontallinearlayout = new LinearLayout(this);
//Setting up LinearLayout Orientation
Horizontallinearlayout.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams Horizontallinearlayoutlayoutparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
setContentView(Horizontallinearlayout, Horizontallinearlayoutlayoutparams);
LayoutParams LayoutParamsview = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//Creating textview .
Button button1 = new Button(this);
Button button2 = new Button(this);
//Adding text to TextView.
button1.setText("First Button");
button2.setText("Second Button");
button1.setLayoutParams(LayoutParamsview);
button2.setLayoutParams(LayoutParamsview);
//Adding textview to linear layout using Add View function.
Horizontallinearlayout.addView(button1);
Horizontallinearlayout.addView(button2);
Hope this helps you
You should use setLayoutParams
Add setOrientation(LinearLayout.HORIZONTAL);
Set the layout parameters associated with this view. These supply
parameters to the parent of this view specifying how it should be
arranged. There are many subclasses of ViewGroup.LayoutParams, and
these correspond to the different subclasses of ViewGroup that are
responsible for arranging their children.
Code Structure
LinearLayout table = (LinearLayout)findViewById(R.id.linearlayout);
table.setOrientation(LinearLayout.HORIZONTAL);
Button Btn1 = new Button(Activity.this);
Btn1.setText("Button 1");
LinearLayout.LayoutParams childParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam.weight = 0.5f;
Btn1.setLayoutParams(childParam);
Button Btn2 = new Button(Activity.this);
Btn2.setText("Button 2");
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
childParam2.weight = 0.5f;
Btn2.setLayoutParams(childParam2);
table.addView(Btn1);
table.addView(Btn2);
Respected OP
You can create another XML file, that contains a horizontal LinearLayout, then inflate it and insert the two buttons into it, and then add the whole horizontal LinearLayout to the vertical LinearLayout:
Create an XML file e.g. horizontal_layout.xml:
<LinearLayout
android:id="#+id/horizontal_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
// You will insert the buttons programmatically.
</LinearLayout>
In your Activity use this method to inflate the Layout:
LinearLayout verticalLinearLayout = rootView.findViewById(R.id.vertical_linear_layout);
// Add the first vertical Button regularly.
Button firstVerticalButton = new Button(Activity.this);
firstVerticalButton.setText("Vertical");
verticalLinearLayout.addView(firstVerticalButton);
// Inflate the horizontal LinearLayout and add the two horizontal buttons.
LinearLayout horizontalLinearLayout = getLayoutInflater().inflate(R.layout.horizontal_layout,
linearLayout, false);
Button horizontalOne = new Button(Activity.this);
horizontalOne.setText("Horizontal");
LinearLayout.LayoutParams childParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.5f;
horizontalOne.setLayoutParams(childParam);
Button horizontalTwo = new Button(Activity.this);
horizontalTwo.setText("Horizontal");
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.5f;
horizontalTwo.setLayoutParams(childParam2);
horizontalLinearLayout.addView(horizontalOne);
horizontalLinearLayout.addView(horizontalTwo);
// Insert the inflated layout to the main layout.
verticalLinearLayout.addView(horizontalLinearLayout);
// Add the second vertical Button
Button secondVerticalButton = new Button(Activity.this);
secondVerticalButton.setText("Vertical");
verticalLinearLayout.addView(secondVerticalButton);
use table.setOrientation(LinearLayout.HORIZONTAL); .
Hope it Helps.

Adding Buttons dynamically in RelativeLayout to LinearLayout

When the user inputs a word, he creates a number of Buttons equal to the length of the word. For example: if user inputs "aaaa" he will create 4 Buttons, side by side, in the first row. Then if the user enters "bb" he will create 2 Buttons, side by side, in the second row. And "ccc" he creates 3 Buttons...
Image to demonstrate:
I dynamically create a RelativeLayout, then dynamically add Buttons to that layout. And finally I add the RelativeLayout to my existing LinearLayout. But the problem is, only one Button is added per row. And my program currently looks like this:
Can someone please me fix this problem?
CODE:
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
RelativeLayout relativeLayout = new RelativeLayout(view.getContext());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length(); //the user input number of buttons
int id = 1;
for (int i=0; i<size; i++)
{
Button myButton = new Button(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
myButton.setId(id);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
relativeLayout.addView(myButton, rlp);
id++;
}
linearLayout.addView(relativeLayout, llp);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
This line says that myButton should be added to right of myButton, which doesn't make any sense.
simple way to resolve this is to use the following line instead
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId()-1);
But this isn't the best way to do this, you should use LinearLayout with horizontal orientation instead.
The structure should be simple
Just need to add your buttons in 3 different linear layout with orientation horizontal.
Like
<Relative layout>{
<LinearLayout global container with vertical orientation >{
<LinearLayout for 'a' type buttons container with horizontal orientation>
<LinearLayout for 'b' type buttons container with horizontal orientation>
<LinearLayout for 'c' type buttons container with horizontal orientation>
}
}
You guys are right. It is much easier using a LinearLayout. For those interested
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
LinearLayout linearLayout2 = new LinearLayout(view.getContext());
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length();
for (int i=0; i<size; i++)
{
Button myButton = new Button(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
linearLayout2.addView(myButton, rlp);
}
linearLayout.addView(linearLayout2, llp);

How to set margin dynamically in Android?

I am currently doing an android application that contains customize alert dialog. It contains a button , but i can't set the margin for the button . the code is given below. setmargin method is not working
AlertDialog.Builder myDialog = new AlertDialog.Builder(Login.this);
Button button = new Button(Login.this);
button.setText("Send");
LayoutParams buttonLayoutParams
= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(buttonLayoutParams);
resetPassword=editText.getText().toString();
LinearLayout layout = new LinearLayout(Login.this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView);
layout.addView(editText);
layout.addView(button);
myDialog.setView(layout);
Write below code to set margin, it may help you.
AlertDialog.Builder myDialog = new AlertDialog.Builder(Login.this);
Button button = new Button(Login.this);
EditText editText = new EditText(Login.this);
TextView textView = new TextView(Login.this);
button.setText("Send");
LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonLayoutParams.setMargins(50, 10, 0, 0);
button.setLayoutParams(buttonLayoutParams);
String resetPassword = editText.getText().toString();
LinearLayout layout = new LinearLayout(Login.this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView);
layout.addView(editText);
layout.addView(button);
myDialog.setView(layout);
myDialog.show();
Use LinearLayout.LayoutParams or RelativeLayout.LayoutParams according to parent layout of the child view
Just sharing a slightly different approach.
Instead of casting to LinearLayout.LayoutParams, RelativeLayout.LayoutParams, etc, you can just cast to MarginLayoutParams.
Casting to MarginLayoutParams is better because you can later update your layout and you don't need to return to your java code to change from LinearLayout to RelativeLayout or any other Layout type
You can do something like:
MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
// Set bottom margin
layoutParams.bottomMargin = x;
// Set top margin
layoutParams.topMargin = x;
// Set left margin
// This won't have effect if you set any relative margin (start) previously or in the layout.xml
layoutParams.leftMargin = x;
// Set left margin
// This won't have effect if you set any relative margin (end) previously or in the layout.xml
layoutParams.rightMargin = x;
// Set start margin
layoutParams.setMarginStart(x);
// Set end margin
layoutParams.setMarginStart(x);
// Set all left, top, right, bottom margins at once
// Note that here, left and right margins are set (not start/end).
// So, if you have used start/end margin before (or via layout.xml),
// setting left/right here won't have any effect.
layoutParams.setMargins(left, top, end, bottom)
// Then re-apply the layout params again to force the view to be re-draw
// This step may not be necessary because depending where you set the margin,
// view is already scheduled to be drawn
// For any case, to ensure the view will apply the new margin, call:
view.setLayoutParams(layoutParams);
buttonLayoutParams.bottomMargin
buttonLayoutParams.topMargin
buttonLayoutParams.leftMargin
buttonLayoutParams.rightMargin
can be used to set margins
The setMargin() method is available if you're using LinearLayout.LayoutParams but not if you're using ViewGroup.LayoutParams. Dipak Keshariya alludes to this but doesn't say it in so many words.
You can set in LinearLayout margin
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
Button okButton=new Button(this);
okButton.setText("some text");
ll.addView(okButton, layoutParams);
This works for me (The support library (need to use AndroidX):
Kotlin
val params = LinearLayoutCompat.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
).apply {
setMargins(0,16,0,16)
}
Java
LinearLayoutCompat.LayoutParams params = LinearLayoutCompat.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
params.setMargins(0,16,0,16)

Android button layout

I have recently coded my button to be aligned to the right of the screen and it seems to not want to work.
I have also checked to see if my parent layout is set to cover the whole width of the screen and it is.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Vertical Layout - For the layout of the newSheetLayout.
newSheetLayoutV = new LinearLayout(this);
newSheetLayoutV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in Vertical layout. Used for the next, back and save buttons.
newSheetButtonLayoutH = new LinearLayout(this);
newSheetButtonLayoutH.setOrientation(LinearLayout.HORIZONTAL);
//Vertical Layout - For the layout of the newSheetLayout.
dataShowV = new LinearLayout(this);
dataShowV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in the dataShow V. Used for the data entry objects.
dataShowH = new LinearLayout(this);
dataShowH.setOrientation(LinearLayout.HORIZONTAL);
//Next button.
nextButton = new Button(this);
nextButton.setText("NEXT >");
nextButton.setOnClickListener(nextListener);
//Back button.
backButton = new Button(this);
backButton.setText("< BACK");
backButton.setOnClickListener(backListener);
backButton.setEnabled(false);
//Save button.
saveButton = new Button(this);
saveButton.setText("Save");
saveButton.setOnClickListener(saveListener);
saveButton.setEnabled(false);
//Addition of the buttons to the button view in order that needs to be seen.
newSheetButtonLayoutH.addView(backButton);
newSheetButtonLayoutH.addView(nextButton);
newSheetButtonLayoutH.addView(saveButton);
//Layout Param's for the buttons.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
newSheetButtonLayoutH.setLayoutParams(params);
//Set save to anchor right.
LinearLayout.LayoutParams saveButtonParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
saveButtonParam.gravity = Gravity.RIGHT;
saveButton.setLayoutParams(saveButtonParam);
//Add all the layout Views to the main layout view.
newSheetLayoutV.addView(dataShowV);
newSheetLayoutV.addView(newSheetButtonLayoutH);
newSheetLayoutV.setGravity(Gravity.BOTTOM);
setContentView(newSheetLayoutV);
}
If it is something simple and I'm very blind to the solution, I will punish myself accordingly.
Thank you for your help.
try
saveButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,Gravity.RIGHT);
if you are using xml and your need is not to do it in dynamic way then do
android:layout_gravity="right"
Edit:
i think this is what you want
LinearLayout newSheetLayoutV,dataShowV,dataShowH,newSheetButtonLayoutH,newlayout;
Button nextButton,backButton,saveButton;
//RelativeLayout ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
newSheetLayoutV = new LinearLayout(this);
newSheetLayoutV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in Vertical layout. Used for the next, back and save buttons.
newSheetButtonLayoutH = new LinearLayout(this);
newSheetButtonLayoutH.setOrientation(LinearLayout.HORIZONTAL);
newlayout = new LinearLayout(this);
newlayout.setOrientation(LinearLayout.VERTICAL);
newlayout.setGravity(Gravity.RIGHT);
newlayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//newSheetButtonLayoutH = new RelativeLayout(this);
//Vertical Layout - For the layout of the newSheetLayout.
dataShowV = new LinearLayout(this);
dataShowV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in the dataShow V. Used for the data entry objects.
dataShowH = new LinearLayout(this);
dataShowH.setOrientation(LinearLayout.HORIZONTAL);
//Next button.
nextButton = new Button(this);
nextButton.setText("NEXT >");
//nextButton.setOnClickListener(nextListener);
//Back button.
backButton = new Button(this);
backButton.setText("< BACK");
//backButton.setOnClickListener(backListener);
backButton.setEnabled(false);
//Save button.
saveButton = new Button(this);
saveButton.setText("Save");
//saveButton.setOnClickListener(saveListener);
saveButton.setEnabled(false);
LinearLayout.LayoutParams saveButtonParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//saveButtonParam.gravity = Gravity.RIGHT;
saveButton.setLayoutParams(saveButtonParam);
//Addition of the buttons to the button view in order that needs to be seen.
newSheetButtonLayoutH.addView(backButton);
newSheetButtonLayoutH.addView(nextButton);
newlayout.addView(saveButton);
newSheetButtonLayoutH.addView(newlayout);
//Layout Param's for the buttons.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
newSheetButtonLayoutH.setLayoutParams(params);
//Set save to anchor right.
//Add all the layout Views to the main layout view.
newSheetLayoutV.addView(dataShowV);
newSheetLayoutV.addView(newSheetButtonLayoutH);
newSheetLayoutV.setGravity(Gravity.BOTTOM);
setContentView(newSheetLayoutV);
}
Try giving RelativeLayout params
RelativeLayout.LayoutParams rel_lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel_lp.addRule(RelativeLayout.ALIGN_RIGHT);
rel_lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
btn.setLayoutParams(rel_lp);
Try setting the layout_gravity of LinearLayout instead of your Button.

Custom view: nested linearlayout not showing up

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.

Categories

Resources