I wish to get data from database and replicate this format of CardView with different information but I' facing issue regarding the constraints of the constraint layout. I want to set top of next CardView to the bottom of previous one but ids of both would be generated in real time.
At this time I have this code for sample CardView but it appears at (0,0) position right now. Does anyone have an opinion or an alternative solution to do the same.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_appointment);
//activity constraint layout
ConstraintLayout constraintLayout = findViewById(R.id.parentlayout);
//Adding 2 CardViews
for (int i = 1; i <= 2; i++) {
CardView cardview = new CardView(this);
layoutparams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
layoutparams.bottomMargin = 10;
cardview.setLayoutParams(layoutparams);
cardview.setRadius(5);
cardview.setPadding(10, 10, 10, 10);
cardview.setCardBackgroundColor(Color.GRAY);
cardview.setMaxCardElevation(8);
TextView textview = new TextView(this);
textview.setLayoutParams(layoutparams);
textview.setText("CardView Programmatically");
textview.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25);
textview.setTextColor(Color.WHITE);
textview.setPadding(25, 25, 25, 25);
textview.setGravity(Gravity.CENTER);
cardview.addView(textview);
constraintLayout.addView(cardview);
id = i;
}
}
Related
I'm trying to get my button to create a new TextInputEditText field every time the user presses the button. So far I've managed to dynamically add a field but I can't figure out how to increment the hint of the EditText.
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
TextInputEditText et = new TextInputEditText(MainActivity.this);
et.setHint("Enter Member + 1");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(et,lp);
}
});
For example, I want the hint to increase the number to "Enter Number + X" each time the button is clicked.
You can use the LinearLayout's getChildCount api, the demo code could be:
LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
TextInputEditText et = new TextInputEditText(this);
int childNum = layout.getChildCount();
et.setHint(String.format("Enter Member + %d", childNum+1));
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(et,lp);
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'm trying to create button programmatically on a linear layout and i want to have "columns" of buttons but it seems the linear layout automatically put buttons under each other and i can't find a way to make my button position to the top again
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_branch);
LinearLayout linear = (LinearLayout) findViewById(R.id.content);
for(int i =0; i <10; i++) {
Button btn = new Button(this);
btn.setText("Branch1");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(330,200);
layoutParams.setMargins(rowNb*330+5, 3, 0, 0); // left, top, right, bottom
btn.setLayoutParams(layoutParams);
linear.addView(btn);
btnNb++;
if(btnNb>5)
{
rowNb = 1;
}
}
}
here's what i got
Can someone help me out to find a way to make my 2nd columns like the first ? I already tried a btn.SetY or a btn.setGravity
Try GridLayout instead of LinearLayout
https://developer.android.com/reference/android/widget/GridLayout.html
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 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?