FlowTexView dynamically added text appear in single line height issue - android

I have added flowtextview jar file in my project lib folder and its working perfectly when i add it in my layout manualy.
I need to add flowtextview dynamically to my LinearLayout and add text and images dynamicaly into it. I can add text and images but the text appearing in sinle line, i thing its because of the flowtexview height setting issue.
please check the attached image , you can see the issue correctly.
code used for adding dynamic views as follows,
contentHolder = (LinearLayout)rootview.findViewById(R.id.contentOuter);
//add LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//params.setMargins(10, 20, 10, 20); // llp.setMargins(left, top, right, bottom);
contentHolder.setOrientation(LinearLayout.VERTICAL);
ftv = new FlowTextView(getActivity());
FlowTextView.LayoutParams fp = new FlowTextView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
fp.setMargins(0, 10, 0, 10);
ftv.setLayoutParams(params);
ftv.setId(110);
ftv.setText("Currently it is placing that image as the background to the view and not the actual image in it.Currently it is placing that image as the background to the view and not the actual image in it.Currently it is placing that image as the background to the view and not the actual image in it..");
ImageView im = new ImageView(getActivity());
im.setBackgroundResource(R.drawable.av1);
im.setId(5);
RelativeLayout rt = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams rparams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rparams.setMargins(0, 10, 0, 10);
rt.setLayoutParams(rparams);
rt.addView(im);
ftv.addView(rt);
ftv.invalidate();
//add the textView and the Button to LinearLayout
contentHolder.addView(ftv);

at last i found the issue, its because of the jar file. its not an updated one. use flowtextview as a library project and add it to your project or use or Gradle:
compile 'uk.co.deanwild:flowtextview:2.0.2#aar'

Related

Align image and text on textview

In my app, I populate a linearlayout with a text and a little icon (like a listview with a custom adapter).
I need the text and the picture to have the same size. Is there a way to do that?
Here's the code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout LayoutPadre = new LinearLayout(this);
TextView Tag = new TextView(this);
ImageView img = new ImageView(this);
Tag.setText(data);
if(stato.equalsIgnoreCase("presente")){
img.setImageResource(R.drawable.ic_presente);
}else{
img.setImageResource(R.drawable.ic_assente);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(75, LayoutParams.WRAP_CONTENT); //Here I need the img to have same size as the text, but Tag.getHeight() returns 0
layoutParams.setMargins(50, 0, 0, 0);
img.setLayoutParams(layoutParams);
LayoutPadre.setLayoutParams(params);
LayoutPadre.addView(Tag);
LayoutPadre.addView(img);
The problem is that the image is not horizontally centered like the img.
Look at the screen
Thanks to all.
keep the layout_height of text and image as Match_Parent and not Wrap_Content... By that
If you are preserving the android:layout_height="wrap_content" or it did not work, you can also do android:gravity="center"

SetMargin Programmatically not working correctly for RelativeLayoutParams

I'm currently programmatically adding RelativeLayout encapsulated in a Linearlayout. The base is a scrollview and i'm trying to add those layouts into scrollview named svbase
LinearLayout llbase = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams llbaseParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); // Verbose!
llbaseParams.setMargins(0, new CommonOpearation().getPixel(10, getApplicationContext()), 0, new CommonOpearation().getPixel(10, getApplicationContext()));
llbase.setLayoutParams(llbaseParams);
llbase.setGravity(Gravity.CENTER_HORIZONTAL);
for(int n =0;n<2;n++)
{
RelativeLayout rLayoutBase = new RelativeLayout(getApplicationContext());
RelativeLayout.LayoutParams rLayoutParms = new RelativeLayout.LayoutParams( new CommonOpearation().getPixel(140, getApplicationContext()), new CommonOpearation().getPixel(125, getApplicationContext()));
**rLayoutParms.setMargins(0, 0, new CommonOpearation().getPixel(5, getApplicationContext()), 0);**
rLayoutBase.setLayoutParams(rLayoutParms);
Drawable drawable = MyCurrentActivity.this.getApplicationContext().getResources().getDrawable(R.drawable.curved_bg); //new Image that was added to the res folder
try {
rLayoutBase.getClass().getMethod(android.os.Build.VERSION.SDK_INT >= 16 ? "setBackground" : "setBackgroundDrawable", Drawable.class).invoke(rLayoutBase, drawable);
} catch (Exception ex) {
}
llbase.addView(rLayoutBase);
}
svBase.addView(llbase);
As you can see I have two relativelayout encapsulated in the linearlayout with a horizontal orientation. I've tried giving margin to each of the relativelayout using setMargin with a right of certain 5dp. However it does not gives margin inbetween the two relativelayout. It worked if I were to do in manually in xml though.
The differences can be seen at the image. the top is a xml specify layout while the bottom two relativelayout are generated programmatically
solved my own.
The solution is already out there! did not google enough! my fault!
Relative Layout ignoring setMargin()

Dynamically-generated LinearLayouts overlapping

I'm a newbie to Android development, and I'm very much still learning Java too so be gentle!
I am creating an app that can take information about a task (I'm basing it around a sort of homework planner), store that info and then display it in a list. The program must be able to dynamically generate the list from the background files. I have managed all of this so far, but when I create a basic output for each task, containing the "subject" and "details" variables using a LinearLayout they appear on the screen overlapping. They all seem to be creating correctly, but they are all being put in the same place. Are there attributes I can set to make them display in a vertical list???
Here is the piece of code where I generate the viewgroups and display them. This is called from a loop in another part of the program which finds the number of files in internal storage.
TextView subjView;
TextView detailView;
RelativeLayout displayLayout;
LinearLayout taskDisplay = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
subjView = new TextView(this);
detailView = new TextView(this);
displayLayout = (RelativeLayout) findViewById(R.id.relative_display_layout);
subjView.setText(subject);
detailView.setText(details);
layoutParams.setMargins(0, 0, 0, 0);
taskDisplay.addView(subjView, layoutParams);
layoutParams.setMargins(10, 0, 0, 0);
taskDisplay.addView(detailView, layoutParams);
displayLayout.addView(taskDisplay);
If I understand correctly, I think your issue is only that you are declaring and then changing the layoutParams margins which sets them both to the same, which is overlapping your TextViews.
Edit
Okay, I am still not 100% sure how you are doing all of this so my example may need to be tweaked. I tried to throw this together quickly so forgive me for any minor mistakes.
New mock up for dynamic layouts:
TextView subjView, detailView;
RelativeLayout displayLayout, rl;
// I am assuming this is your main layout
displayLayout = (RelativeLayout) findViewById(R.id.relative_display_layout);
// Just using a for loop as an example of a loop event, not sure how you are accomplishing this
for(int i = 0; i < data.length(); i++) {
RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 100);
if (i > 0) {
int rePositionRule = i;
rllp.addRule(RelativeLayout.BELOW, rePositionRule);
}
RelativeLayout taskDisplay = new RelativeLayout(this);
taskDisplay.setLayoutParams(rllp);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 0);
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams2.setMargins(10, 0, 0, 0);
subjView = new TextView(this);
detailView = new TextView(this);
subjView.setText(subject);
subjView.setLayoutParams(layoutParams);
detailView.setText(details);
detailView.setLayoutParams(layoutParams2);
taskDisplay.addView(subjView);
taskDisplay.addView(detailView);
displayLayout.addView(taskDisplay);
}
Your displayLayout is a relativeLayout.. A relative layout, as the name implies, places element relative to each other. Normally you'd say "element A should go below element B" etc. Since you aren't providing any of these rules for the items you are creating they are just going to all go to the default position in a relative layout (which is the top of the screen.. hence the overlap)
If you don't want to deal with the hassle of changing your code to place things relatively simply switch your displayLayout to a LinearLayout in your xml and code and set its orientation to vertical. You'll probably want to wrap that in a scroll view if it runs off the screen
However, it sounds like what you really want is a ListView...

How to get ImageView left-aligned with java coding?

I have searched the solution about ImageView alignment. But most of the solutions are using layout.xml. To make it flexible, I'm using java code to add ImageView to layout. Following is my code. It always shows in the center. I need it go to the left.
String uri="/xxx/xxx.png";
File f = new File(uri);
ImageView iv=new ImageView(cntxt);
if (f.exists())
iv.setImageURI(Uri.parse(uri));
else
iv.setImageResource(R.drawable.missing);
iv.setMaxWidth(400);
iv.setScaleType(ScaleType.CENTER_INSIDE);
LinearLayout.LayoutParams p22 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
p22.gravity=Gravity.LEFT;
linenearlayout.addView(iv, p22);

android how to give width n height to an image programatically

In my activity i am creating a Imageview dynamically. Every time it fetch the image from server create an imageview and set that image.
All images size also not same , so i want to put all images in a particular size.
But its not working. Here is my code bellow,
ImageView imageView = new ImageView(getActivity());
imageView.setImageResource(mContent);
imageView.setPadding(20, 20, 20, 20);
imageView.setMaxWidth(400);
imageView.setMaxHeight(400);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.setGravity(Gravity.CENTER);
layout.addView(imageView);
Simply do this,
imageview.getLayoutParams().height=100;
imageview.getLayoutParams().width=100;

Categories

Resources