I am programmatically creating textviews with horizontal lines between each view. Using a drawable created programmatically.
The problem is, the opacity starts off light and gradually increases for each line.
I've logged the opacity (getAlpha()) of the drawable, paint, image view and linear layout at all the points in the two methods provided and from the drawables it's always 255 and the views 1.0. I don't understand why it's not behaving as though this is true. I've also tried setting Alpha, it makes no difference.
Why is it doing this and how do I fix it?
xml:
<LinearLayout android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" .../>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="PaintDashedLines"
android:text="Press Me"/>
</LinearLayout>
java:
static int tvCount = 0;
public void PaintDashedLines(View v) {
LinearLayout ll = (LinearLayout) findViewById(R.id.main);
TextView tv = new TextView(MainActivity.this);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(25);
tv.setPadding(0, 5, 0, 5);
ll.addView(tv);
tv.setText("TextView " + tvCount);
ImageView divider = new ImageView(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ll.getWidth(), 2);
lp.setMargins(0, 5, 0, 5);
divider.setLayoutParams(lp);
divider.setBackground(CreateDashedLined());
ll.addView(divider);
tvCount++;
}
public static Drawable CreateDashedLined() {
ShapeDrawable sd = new ShapeDrawable(new RectShape());
Paint fgPaintSel = sd.getPaint();
fgPaintSel.setColor(Color.BLACK);
fgPaintSel.setStyle(Paint.Style.STROKE);
fgPaintSel.setPathEffect(new DashPathEffect(new float[]{5, 10}, 0));
return sd;
}
To me looks like more some issue with the emulator. You could also try disable the hardware acceleration with
ll.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Please, keep also in mind that you don't need to instantiate a new ImageView every time, just to draw a divider between the TextViews. You could use setDivider. E.g.
In your onCreate
ShapeDrawable sd = new ShapeDrawable(new RectShape());
sd.setIntrinsicHeight(1);
Paint fgPaintSel = sd.getPaint();
fgPaintSel.setARGB(255, 0, 0, 0);
fgPaintSel.setStyle(Paint.Style.STROKE);
fgPaintSel.setPathEffect(new DashPathEffect(new float[]{5, 10}, 0));
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main);
linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE | LinearLayout.SHOW_DIVIDER_END);
linearLayout.setDividerDrawable(sd);
and when you press the button
public void pressMe(View view) {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main);
TextView tv = new TextView(MainActivity.this);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(25);
tv.setPadding(0, 5, 0, 5);
tv.setText("TextView " + linearLayout.getChildCount());
linearLayout.addView(tv);
}
the result is
Related
private LinearLayout clmnView;
LinearLayout clmnView = new LinearLayout(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int margin = DimensionsHelper.getMargins(getContext(), DimensionsHelper.DimensionType.BALL);
params.setMargins(0, margin, 0, margin);
clmnView.setLayoutParams(params);
clmnView.setOrientation(LinearLayout.VERTICAL);
clmnView.setGravity(Gravity.CENTER_VERTICAL);
TextView historyTextView = new TextView(getContext());
historyTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
historyTextView.setText("this is history");
clmnView.addView(historyTextView);
No matter what I do ,the historyTextView is not display inside my clmnView.
Could you please help? Thank you in advance.
Okay everything was working as it should. But I was displaying white text in white background.
Changed the color of the text like this :
historyTextView.setTextColor(Color.parseColor("#000000"));
and my text appeared.
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'm looking for a solution to solve my problem which all my TextViews overlaps on themselves, when are added to Relative Layout. In fact, I need to do put them after each. other I've read existed answers, I followed them but nothing could solve it yet. can someone tell me where I did wrongly?
here is my code:
for (int i=0;i<parts.length;i++)
{
valueTV[i] = new TextView(this);
valueTV[i].setText(parts[i]);
valueTV[i].setId(i);
valueTV[i].setWidth(300);
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
linearLayout_Skills.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
linearLayout_Skills.setBackgroundColor(getResources().getColor(R.color.blue));
if(i>=1)
{
lparams.addRule(RelativeLayout.END_OF, valueTV[i-1].getId());
valueTV[i].setLayoutParams(lparams);
}else {
lparams.addRule(RelativeLayout.ALIGN_PARENT_START);
valueTV[i].setLayoutParams(lparams);
}
linearLayout_Skills.addView(valueTV[i]);
}
XML code:
<RelativeLayout
android:id="#+id/linearSkills"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl"
android:paddingTop="5dp"
</RelativeLayout>
You can achieve this using Relative layout
So this is the main idea , first you define your relation layout
//layout variable is your relative layout
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
Then you define a param variable like this
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
After that you define your textView with an id (in your case this id can be its position in the array)
TextView tv1 = new TextView(this);
tv1.setId(1);
tv1.setText("textView1");
The next textView will be declared like this
TextView tv2 = new TextView(this);
params1.addRule(RelativeLayout.BELOW, tv1.getId());
tv2.setId(2);
tv2.setText("textView2");
Finally you set your view using the params you defined
layout.addView(tv2, params1);
Here is a complete example you can check answer by #AndiM
I'm trying to dynamically build relative layouts consisting of several images.
Theses relative layouts will be display below/rigth of previous relative layouts.
I'm starting with two relative layouts (rl100 and rl200). rl200 is below rl100.
But rl200 isn't below rl100, is "stacked" over rl100.
Can you help me ?
My code :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.rlayout);
RelativeLayout rL100 = rlAdd(rLayout,100,-1,-1);
ImageButton imgBtn101 = imgBtnAdd(rL100,101,R.drawable.apricot,-1,-1);
ImageButton imgBtn102 = imgBtnAdd(rL100,102,R.drawable.banana,-1,101);
ImageButton imgBtn103 = imgBtnAdd(rL100,103,R.drawable.cherry,101,-1);
ImageButton imgBtn104 = imgBtnAdd(rL100,104,R.drawable.strawberry,102,103);
RelativeLayout rL200 = rlAdd(rLayout,200,100,-1);
ImageButton imgBtn201 = imgBtnAdd(rL100,201,R.drawable.pineapple,-1,-1);
ImageButton imgBtn202 = imgBtnAdd(rL100,202,R.drawable.pineapple,-1,201);
ImageButton imgBtn203 = imgBtnAdd(rL100,203,R.drawable.pineapple,201,-1);
ImageButton imgBtn204 = imgBtnAdd(rL100,204,R.drawable.pineapple,202,203);
}
private RelativeLayout rlAdd(RelativeLayout parentContainer, int nId, int nIdBelow,
int nIdRightOf) {
RelativeLayout rLayout = new RelativeLayout(this);
rLayout.setId(nId);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (nIdBelow != -1) {
rlp.addRule(RelativeLayout.BELOW, nIdBelow);
}
if (nIdRightOf != -1) {
rlp.addRule(RelativeLayout.RIGHT_OF, nIdRightOf);
}
rLayout.setLayoutParams(rlp);
parentContainer.addView(rLayout);
return rLayout;
}
private ImageButton imgBtnAdd(RelativeLayout ParentContainer, int ImgId, int ResDrawable,
int imgIdBelow, int imgIdRightOf) {
ImageButton imgBtn = new ImageButton(this);
imgBtn.setId(ImgId);
imgBtn.setImageResource(ResDrawable);
ParentContainer.addView(imgBtn);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) imgBtn.getLayoutParams();
if (imgIdBelow != -1) {
lp.addRule(RelativeLayout.BELOW, imgIdBelow);
}
if (imgIdRightOf != -1) {
lp.addRule(RelativeLayout.RIGHT_OF, imgIdRightOf);
}
imgBtn.setLayoutParams(lp);
return imgBtn;
}
}
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<RelativeLayout android:id="#+id/rlayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal">
</RelativeLayout>
</ScrollView>
You are doing wrong over here
RelativeLayout rL200 = rlAdd(rLayout,100,-1,-1);
// From method
if (nIdBelow != -1) {
rlp.addRule(RelativeLayout.BELOW, nIdBelow);
}
As you are not passing id in nIdBelow field how it is supposed to add layout below. pass above image id or use Linear Layout with vertical orientation.
You should use this line of code to add the relative layout with ID 200 below the relative layout with id 100, am I right?
RelativeLayout rL200 = rlAdd(rLayout, 200, 100, -1);
Thanks for your help.
I make differents errors :
RelativeLayout rL200...
RelativeLayout rL200 = rlAdd(rLayout, 200, 100, -1);
Correct version :
RelativeLayout rL200 = rlAdd(rLayout,200,100,-1);
ImageButton imgBtn20x
ImageButton imgBtn201 = imgBtnAdd(rL100,201,R.drawable.pineapple,-1,-1);
Correct version :
ImageButton imgBtn201 = imgBtnAdd(rL200,201,R.drawable.pineapple,-1,-1);
Same error for imgBtn202 ... imgBtn204
activity_main.xml
Errors 1 and 2 are stunning errors.
Third error in RelativeLayout android:id="#+id/rlayout" ...
Correct version :
<RelativeLayout android:id="#+id/rlayout" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="match_parent">
</RelativeLayout>