Below is what my layout looks like.
What I want is for the 2 green arrows to be centered vertically in the top left square. Also, the double number sign ("##") needs to be centered vertically and horizontally in the top left square.
Then, the other set of double number signs need to be centered vertically and horizontally in the top right square.
Below is the code.
public class Puzzle extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dividers();
topLeft();
topRight();
setContentView(relativelayout);
}
public void topLeft() {
RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params0.addRule(RelativeLayout.ABOVE, view0.getId());
params0.addRule(RelativeLayout.LEFT_OF, view1.getId());
relativelayout.addView(topLeftRL, params0);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
topLeftRL.addView(puzzle, params1);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.CENTER_VERTICAL);
params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
topLeftRL.addView(left, params2);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.CENTER_VERTICAL);
params3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
topLeftRL.addView(right, params3);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.CENTER_IN_PARENT);
topLeftRL.addView(levelCounter, params4);
left.setImageResource(R.drawable.left_arrow);
right.setImageResource(R.drawable.right_arrow);
puzzle.setText(c.getResources().getString(R.string.puzzlePuzzleTV));
levelCounter.setText("###");
}
public void topRight() {
RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params0.addRule(RelativeLayout.ABOVE, view0.getId());
params0.addRule(RelativeLayout.RIGHT_OF, view1.getId());
relativelayout.addView(topRightRL, params0);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
topRightRL.addView(record, params1);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params2.addRule(RelativeLayout.CENTER_HORIZONTAL);
topRightRL.addView(moves, params2);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.CENTER_IN_PARENT);
topRightRL.addView(movesCounter, params3);
movesCounter.setText("##");
record.setText(c.getResources().getString(R.string.puzzleRecordTV) + " " + "##");
moves.setText(c.getResources().getString(R.string.puzzleMovesTV));
}
public void dividers() {
// vertical line (top of screen)
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(5, height / 6);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
params1.setMargins(0, 10, 0, 10); // left, top, right, bottom
relativelayout.addView(view1, params1);
// horizontal line (top of screen)
RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 5);
params0.addRule(RelativeLayout.BELOW, view1.getId());
params0.setMargins(10, 0, 10, 0); // left, top, right, bottom
relativelayout.addView(view0, params0);
// horizontal line (button of screen)
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 5);
params3.addRule(RelativeLayout.ABOVE, linearlayout.getId());
relativelayout.addView(view3, params3);
}
}
So it looks like when I add multiple addRule() to a param, it doesn't read both of them. I think the problem lies in this line of the topLeft() method: params0.addRule(RelativeLayout.ABOVE, view0.getId());. I've tried switching this line with the other addRule in that param and the result is the same. Why is this?
EDIT #1
- Cleaned up my code to make neater and easier to read.
You are using params that require an anchor. You need to use "addRule (int verb, int anchor)"
so like
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP,1);
I found the answer to my own question. The error was in a part of my onCreate() method that I had not included in my opening post! This was the problem code:
view0.setId(0);
Apparently the id cannot be set to 0. Anyways, I changed the id number to a positive number and everything worked.
Related
Finally found a workaround: I decided to just use a table with a single row and then set stretchAllColumns to true. This means it just mimics a horizontal layout with spread-out views.
I am trying to instantiate a horizontal linear layout at runtime and I would like the elements in the layout to spread out along the full width of the layout. I would also like everything to be center-vertical aligned. This is currently what I've got:
LinearLayout tertiaryLinearLayout = new LinearLayout(getActivity());
tertiaryLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
tertiaryLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
tertiaryLinearLayout.setGravity(Gravity.FILL_HORIZONTAL);
This instantiates the layout but the elements in it don't correctly fill the full width of it. I am also not sure how I can get them to be center-vertical aligned without changing setGravity to
tertiaryLinearLayout.setGravity(Gravity.CENTER_VERTICAL);
This TextView is one of the five elements I add to the layout:
TextView dash = new TextView(getActivity());
dash.setText("-");
Two of the elements are ImageViews and the other three are TextViews. Here is how it looks currently:
https://i.stack.imgur.com/MGLyJ.jpg
EDIT:
The overall structure is a ScollView with a vertical LinearLayout. This LinearLayout holds lots of other vertical LinearLayouts, each with a horizontal LinearLayout. Hope that makes sense.
The parent view of tertiaryLinearLayout is here:
LinearLayout secondaryLinearLayout = new LinearLayout(getActivity());
secondaryLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
secondaryLinearLayout.setOrientation(LinearLayout.VERTICAL);
...
secondaryLinearLayout.addView(tertiaryLinearLayout);
EDIT 2:
Here is my full code:
...
final LinearLayout linearLayout = root.findViewById(R.id.leagueLinearLayout);
List<Game> currentYearCategory = MainActivity.yearCategories.get(0);
for (int i = 0; i < currentYearCategory.size(); i++) {
TextView textView = new TextView(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
textView.setLayoutParams(params);
textView.setText(currentYearCategory.get(i).date);
linearLayout.addView(textView);
}
seekBarChange(linearLayout, root, 0);
// Set up seek bar
SeekBar yearSeekBar = root.findViewById(R.id.yearSeekBar);
yearSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBarChange(linearLayout, root, progress);
}
...
...
private void seekBarChange(LinearLayout linearLayout, View root, int progress){
TextView yearTextView = root.findViewById(R.id.yearTextView);
String newText = (1890 + progress * 10) + " - " + (1899 + progress * 10);
yearTextView.setText(newText);
final List<Game> currentYearCategory = MainActivity.yearCategories.get(progress);
linearLayout.removeAllViews();
for (int i = 0; i < currentYearCategory.size(); i++) {
// Create views
LinearLayout secondaryLinearLayout = new LinearLayout(getActivity());
secondaryLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
secondaryLinearLayout.setOrientation(LinearLayout.VERTICAL);
View separatorView = new View(getActivity());
separatorView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 50));
TextView date = new TextView(getActivity());
date.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
date.setText(currentYearCategory.get(i).date);
LinearLayout tertiaryLinearLayout = new LinearLayout(getActivity());
tertiaryLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
tertiaryLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
tertiaryLinearLayout.setGravity(Gravity.CENTER_VERTICAL);
ImageView homeImage = new ImageView(getActivity());
homeImage.setLayoutParams(new ViewGroup.LayoutParams(150, 150));
if (currentYearCategory.get(i).celticIsHome){
homeImage.setBackgroundResource(R.drawable.celtic_logo);
} else {
homeImage.setBackgroundResource(R.drawable.rangers_logo);
}
TextView homeScore = new TextView(getActivity());
homeScore.setText(currentYearCategory.get(i).fullTimeScore.substring(0, 1));
TextView dash = new TextView(getActivity());
dash.setText("-");
TextView awayScore = new TextView(getActivity());
awayScore.setText(currentYearCategory.get(i).fullTimeScore.substring(2));
ImageView awayImage = new ImageView(getActivity());
awayImage.setLayoutParams(new ViewGroup.LayoutParams(150, 150));
if (currentYearCategory.get(i).celticIsHome){
awayImage.setBackgroundResource(R.drawable.rangers_logo);
} else {
awayImage.setBackgroundResource(R.drawable.celtic_logo);
}
// Add views
tertiaryLinearLayout.addView(homeImage);
tertiaryLinearLayout.addView(homeScore);
tertiaryLinearLayout.addView(dash);
tertiaryLinearLayout.addView(awayScore);
tertiaryLinearLayout.addView(awayImage);
secondaryLinearLayout.addView(separatorView);
secondaryLinearLayout.addView(date);
secondaryLinearLayout.addView(tertiaryLinearLayout);
final int gameNum = i;
secondaryLinearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openGameActivity(currentYearCategory, gameNum);
}
});
linearLayout.addView(secondaryLinearLayout);
}
}
Any help would be greatly appreciated, thanks!
I guess that the PARENT view that the tertiaryLinearLayout will be added to is wrap_content for the layout_width. Try to make its width match_parent. You should have a look on this tutorial at 2:52s
I am new to android development and I am trying to add margins to my view but I have not been able to get it to work.
Here's my code:
ConstraintLayout layout = new ConstraintLayout(this);
final float scale = getResources().getDisplayMetrics().density;
layout.setMinHeight((int)(100*scale));
layout.setMaxHeight((int)(100*scale));
CircleImageView icon = new CircleImageView(this);
icon.setImageResource(image);
icon.setBorderWidth(3);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int)(90*scale), (int)(90*scale));
icon.setLayoutParams(layoutParams);
ViewGroup.MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(icon.getLayoutParams());
marginParams.setMargins(100, 0, 0, 0);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(marginParams);
icon.setLayoutParams(params);
layout.addView(icon);
Why isn't this working? Thanks!
You should do like this:
LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
param.setMargins(left, top, right, bottom);
view.setLayoutParams(param);
After you have created the icon, you should first generate an id for it:
icon.setId(View.generateViewId());
Then, remove the setMargins line and add these lines below layout.addView(icon):
ConstraintSet set = new ConstraintSet();
set.clone(layout);
set.constrainWidth(icon.getId(), (int)(90*scale));
set.constrainHeight(icon.getId(), (int)(90*scale));
set.connect(icon.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
set.connect(icon.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 100);
set.applyTo(layout);
I haven't tested it but it should be a step in the right direction.
Let me know how it goes.
I don't know what is the problem. I am creating the simple phone-book, so i have name of abonent, image and call button under the name. And when i try to set width with the layoutParams parameter for button, it doesn't change anything, btn.setWidth(); doesn't work either.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout linear = (LinearLayout)findViewById(R.id.linear);
LinearLayout.LayoutParams sublparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams childparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams imageparams = new LinearLayout.LayoutParams(200, 200);
linear.setOrientation(LinearLayout.VERTICAL);
for(int i = 0; i < 10 ; i++){
LinearLayout sublinear = new LinearLayout(this);
Button btn = new Button(this);
ImageView img = new ImageView(this);
TextView txt = new TextView(this);
img.setImageResource(R.drawable.img);
txt.setText("Abonent " + (i+1));
btn.setBackgroundColor(getColor(R.color.mygreen));
btn.setText("+3800000");
txt.setLayoutParams(childparams);
btn.setLayoutParams(imageparams);
img.setLayoutParams(imageparams);
linear.addView(txt);
sublinear.addView(img);
sublinear.addView(btn);
linear.addView(sublinear, sublparams);
}
}
}
result of programm, green vertical lines are buttons, that i cannot to make wider
your button is using LayoutParams to set width and heigth so you need to change this properties here (LinearLayout.LayoutParams(widht, height)):
LinearLayout.LayoutParams imageparams = new LinearLayout.LayoutParams(500, 300);
and the size of your button must change, because you are setting this values.
btn.setLayoutParams(imageparams);
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);
I add 2 layout types (on top of the default one), one of which adds some names to the top of some rows and another layout to add buttons to the screen (and a black line).
However I want the bar at the top with the names to remain constant, and to add the buttons in a loop, that I can scroll through. So far the code for this I have written is:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scrollPictures = new ScrollView(this);
RelativeLayout mainApp = new RelativeLayout(this);
LinearLayout appLayout = new LinearLayout(this);
appLayout.setOrientation(LinearLayout.VERTICAL);
appLayout.setClipBounds(null);
for(int x = 1; x <= 15; x++){
View view = LayoutInflater.from(this).inflate(R.layout.button_layout, appLayout, false);
Button StudentSubmission = (Button) view.findViewById(R.id.button1);
StudentSubmission.setText("Button 1");
StudentSubmission.setBackgroundColor(Color.LTGRAY);
Button SoundButton = (Button) view.findViewById(R.id.button2);
SoundButton.setText("Button 2");
SoundButton.setBackgroundColor(Color.LTGRAY);
appLayout.addView(view);
}
scrollPictures.addView(appLayout);
View topmenuview = LayoutInflater.from(this).inflate(R.layout.topofmenu, mainApp, false);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams q = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
q.addRule(RelativeLayout.BELOW, scrollPictures.getId());
mainApp.addView(scrollPictures, p);
mainApp.addView(topmenuview, q);
setContentView(mainApp);
}
However, with this code the bar with the names of columns is placed ON TOP of the list of buttons rather than being ABOVE them, resulting in the top button being cut off. Is there a way to get the buttons to appear UNDER the names of the columns?
q.addRule(RelativeLayout.BELOW, scrollPictures.getId());
mainApp.addView(scrollPictures, p);
mainApp.addView(topmenuview, q);
should be
q.addRule(RelativeLayout.BELOW, topmenuview.getId());
mainApp.addView(topmenuview, p);
mainApp.addView(scrollPictures, q);
right?