How to put one buttom right and another left - java

I try to create dynamically linerlayout and i created two buttoms.
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
TextView titleView = new TextView(this);
titleView.setWidth(LayoutParams.WRAP_CONTENT);
titleView.setHeight(LayoutParams.WRAP_CONTENT);
titleView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
titleView.setText("Hallo Welt!");
layout.addView(titleView);
Button btnConnect = new Button(this);
btnConnect.setText("Connect");
layout.addView(btnConnect);
Button btnDisconnect = new Button(this);
btnDisconnect.setText("Disconnect");
layout.addView(btnDisconnect);
I want to put Connect button to left corner and want to put disconnet button to the right corner. How can i do that?

Have you tried to set layout gravity for your buttons?
LayoutParams params;
Button btnConnect = new Button(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.Left;
btnConnect.setLayoutParams(params);
...
Button btnDisconnect = new Button(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.Right;
btnConnect.setLayoutParams(params);
...
Layout gravity defines where to place a view in its parent (see Gravity and layout_gravity on Android too)
Cheers

You can use RelativeLayout instead of linear layout.
and add rules to the layout params of the
RelativeLayout.Layoutparams params = RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

Related

LinearLayout - adding a TableLayout after a TextView

I'm having a problem with LinearLayout where I want to display a TableLayout after a TextView. I've tried modifying the LayoutParams however I can't seem to make it wrap (which I'm assuming I want it to do).
This is simplified version:
ScrollView sv = (ScrollView) findViewById(R.id.sv);
sv.removeAllViews();
LinearLayout ll = new LinearLayout(this);
sv.addView(ll);
//This is displayed
TextView tv = new TextView(this);
tv.setText("Hello World");
ll.addView(tv);
//This is not displayed - but if I remove the above view it will display...
TableLayout tl = new TableLayout(this);
ll.addView(tl, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
By specifiying Orientation as Vertical it worked for me:
ScrollView sv = (ScrollView) findViewById(R.id.sv);
sv.removeAllViews();
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("Hello World");
ll.addView(tv);
TableLayout tl = new TableLayout(this);
ll.addView(tl);

How to align vertical textview and editText in Linearlayout in android

Currently, I am trying to code a textview and a edittext to align vertically using Linearlayout in android studio. I tried setting out the layout gravity and the other element but I cannot achieve what I want currently this is my code.
LinearLayout params3 = new LinearLayout(this);
params3.setOrientation(LinearLayout.VERTICAL);
params3.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params2.weight = 35;
params3.setLayoutParams(params2);
params2.height = convertDiptoPix(80,context);
//params2.leftMargin = convertDiptoPix(5,context);
TextView textView1 = new TextView(this);
textView1.setId(View.generateViewId());
textView1.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
textView1.setPadding(0,convertDiptoPix(1,context),0,convertDiptoPix(3,context));
textView1.setText("Receipt");
textView1.setTextColor(Color.parseColor("#000000"));
//textView1.setLayoutParams(params2);
textView1.setTextSize(convertDiptoPix(13,context));
EditTextIME editTextIME = new EditTextIME(this);
editTextIME.setId(View.generateViewId());
//editTextIME.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
//editTextIME.setMinimumHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
editTextIME.setGravity(Gravity.BOTTOM | Gravity.CENTER_VERTICAL);
//editTextIME.setLayoutParams(params2);
editTextIME.setFocusableInTouchMode(true);
editTextIME.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzöäüßñéúíóáèùòìàâãåæçëêìíîïôõûýABCDEFGHIJKLMNOPQRSTUVWXYZÖÄÜÕÔÒÔÓÙÚÛÝÑÇÈÉÊËÌÍÎÏÆÅÄÃÂÁÀØ01234567890()&*$%#!+-:;,._?¿/><[]{}|\\^ "));
editTextIME.setHint("Notes");
editTextIME.setInputType(InputType.TYPE_CLASS_TEXT);
editTextIME.setHorizontallyScrolling(true);
editTextIME.setTextSize(convertDiptoPix(13,context));
is anyone had anyone tried to code like vertical align in android studio? Can you share your idea?
You can try this,
LinearLayout parent = new LinearLayout(context);
parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
parent.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView textView1 = new TextView(this);
textView1.setLayoutParams(params2);
textView1.setId(View.generateViewId());
textView1.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
textView1.setText("Receipt");
textView1.setTextColor(Color.parseColor("#000000"));
EditText editTextIME = new EditText(this);
editTextIME.setId(View.generateViewId());
editTextIME.setGravity(Gravity.BOTTOM | Gravity.CENTER_VERTICAL);
editTextIME.setLayoutParams(params2);
editTextIME.setFocusableInTouchMode(true);
editTextIME.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzöäüßñéúíóáèùòìàâãåæçëêìíîïôõûýABCDEFGHIJKLMNOPQRSTUVWXYZÖÄÜÕÔÒÔÓÙÚÛÝÑÇÈÉÊËÌÍÎÏÆÅÄÃÂÁÀØ01234567890()&*$%#!+-:;,._?¿/><[]{}|\\^ "));
editTextIME.setHint("Notes");
editTextIME.setInputType(InputType.TYPE_CLASS_TEXT);
editTextIME.setHorizontallyScrolling(true);
// editTextIME.setTextSize(convertDiptoPix(13,context));
parent.addView(textView1);
parent.addView(editTextIME);

How to align views in a relative layout to far left, center, and far right programmatically?

I have a relative layout that has two buttons and one textview. What i'm trying to have happen is having one button on the far left, the textview in the center, and the other button on the far right. Trying to do this without XML.
Here's my code:
RelativeLayout fm = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
fm.setLayoutParams(lp);
fm.setBackgroundColor(Color.CYAN);
Button done = new Button(this);
done.setId(10);
done.setText("Done");
Button save = new Button(this);
save.setId(12);
save.setText("Save");
TextView formManager = new TextView(this);
formManager.setId(11);
formManager.setText("Form Manager");
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
fm.addView(formManager, lp);
lp.removeRule(RelativeLayout.CENTER_IN_PARENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
fm.addView(done, lp);
lp.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
fm.addView(save, lp);
lp.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
mainLayout.addView(fm);
Problem is...is that the Save button stretches and takes up the whole layout along with being very thin. Basically with this code nothing is happening like I thought. Any ideas on how to achieve this goal?
try this way
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
RelativeLayout fm = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
fm.setLayoutParams(lp);
fm.setBackgroundColor(Color.CYAN);
Button done = new Button(this);
done.setId(10);
done.setText("Done");
Button save = new Button(this);
save.setId(12);
save.setText("Save");
TextView formManager = new TextView(this);
formManager.setId(11);
formManager.setText("Form Manager");
RelativeLayout.LayoutParams lpp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
formManager.setLayoutParams(lpp);
lpp.addRule(RelativeLayout.CENTER_IN_PARENT);
fm.addView(formManager, lpp);
RelativeLayout.LayoutParams doneLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
done.setLayoutParams(doneLayoutParams);
doneLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
fm.addView(done, doneLayoutParams);
RelativeLayout.LayoutParams saveLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
save.setLayoutParams(saveLayoutParams);
saveLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
fm.addView(save, saveLayoutParams);
mainLayout.addView(fm);

How to have dynamically a fixed header with scrollable content in Android?

I have created programmatically a RelativeLayout which contains a button. I have also created a ScrollView which contains a LinearLayout in which are more than 10 TextViews. I want to have the RelativeLayout to be aligned top and fixed. When someone tries to scroll down, i want all the TextViews to go behind the fixed RelativeLayout. I want that button to be always visible. With this code, the RelativeLayout and the button are not displayed. Where am i wrong?
RelativeLayout (fixed)
- Button
LinearLayout
- ScrollView
- TextView
- + other 10 TextViews
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
this.setContentView(relativeLayout);
final Button restartButton = new Button(this);
restartButton.setText(R.string.restartButton);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
restartButton.setLayoutParams(buttonParams);
relativeLayout.addView(restartButton);
ScrollView scrollView = new ScrollView(this);
this.setContentView(scrollView);
final LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(linearLayout.VERTICAL);
scrollView.addView(linearLayout);
TextView textView1 = new TextView(this);
testTitle.setText(R.string.text_view1);
linearLayout.addView(textView1);
// + other 10 text views
}
Thanks!
In your code you replace your RelativeLayout with ScrollView. Just set firstly some LinearLayout as contentView and then put there your RelativeLayout via addView(relativeLayout) and the put there you scrollView also via addView(scrollView)
EDIT:
your new code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LinearLayout mainLinearLayout = new LinearLayout(this);
LinearLayout.LayoutParams mainLinearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
mainLinearLayout.setLayoutParams(mainLinearLayoutParams);
mainLinearLayout.setOrientation(LinearLayout.VERTICAL);
this.setContentView(mainLinearLayout);
final RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
mainLinearLayout.addView(relativeLayout);
final Button restartButton = new Button(this);
restartButton.setText(R.string.restartButton);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
restartButton.setLayoutParams(buttonParams);
relativeLayout.addView(restartButton);
ScrollView scrollView = new ScrollView(this);
mainLinearLayout.addView(scrollView);
final LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(linearLayout.VERTICAL);
scrollView.addView(linearLayout);
TextView textView1 = new TextView(this);
testTitle.setText(R.string.text_view1);
linearLayout.addView(textView1);
// + other 10 text views
}
EDIT 2: renamed first linearLayout to mainLinearLayout according to the comment

Cannot retrieve the integer getHeight()

I have created a Layout 'layout' programmatically, then I created a TextView in the same layout to display the layout height, there are no other methods or anything else in the class. Unfortunately the app closes, and I can't identify why.
LinearLayout layout =new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.setMargins(0, 10, 0, 10);
lparams.gravity= Gravity.CENTER;
TextView fimText = new TextView(this);
fimText.setLayoutParams(lparams);
fimText.setText(layout.getHeight());
layout.addView(fimText, lparams);
setContentView(layout);
Your LinearLayout needs to be added to a container before you can call getHeight on it. Also, getHeight returns an int so you need to convert it to a String.
Try this:
LinearLayout layout =new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.setMargins(0, 10, 0, 10);
lparams.gravity= Gravity.CENTER;
setContentView(layout);
TextView fimText = new TextView(this);
fimText.setLayoutParams(lparams);
fimText.setText(String.valueOf(layout.getHeight()));
layout.addView(fimText, lparams);
This is because layout.getHeight() will not return an CharSequence value. Add .toString() or ""+ to it.

Categories