I´m writing an Android Aplication that is very similar to a binary tree/graph, there are several buttons created dinamically but I can´t figure out how to create lines connecting the buttons.
Here´s an image showing what I need to do:
Here is my XML file:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/vscroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000" >
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hscroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#888888" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativescroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#FFFFFF" >
<!-- Buttons and lines connecting them -->
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
And here is an example of the activity class:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.relativescroll);
RelativeLayout.LayoutParams newParams;
// Botão 1
Button btn1 = new Button(this);
btn1.setId(1);
btn1.setText("Botão 1");
newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
newParams.setMargins(0, 0, 20, 50);
newParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
btn1.setLayoutParams(newParams);
mainLayout.addView(btn1);
// Botão 2
Button btn2 = new Button(this);
btn2.setId(2);
btn2.setText("Botão 2");
newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
newParams.setMargins(0, 0, 20, 50);
newParams.addRule(RelativeLayout.BELOW, 1);
newParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
btn2.setLayoutParams(newParams);
mainLayout.addView(btn2);
// Botão 3
Button btn3 = new Button(this);
btn3.setId(3);
btn3.setText("Botão 3");
newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
newParams.setMargins(0, 0, 20, 50);
newParams.addRule(RelativeLayout.BELOW, 1);
newParams.addRule(RelativeLayout.RIGHT_OF, 2);
btn3.setLayoutParams(newParams);
mainLayout.addView(btn3);
// Botão 4
Button btn4 = new Button(this);
btn4.setId(4);
btn4.setText("Botão 4");
newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
newParams.setMargins(0, 0, 20, 50);
newParams.addRule(RelativeLayout.BELOW, 1);
newParams.addRule(RelativeLayout.RIGHT_OF, 3);
btn4.setLayoutParams(newParams);
mainLayout.addView(btn4);
// Botão 4
Button btn5 = new Button(this);
btn5.setId(5);
btn5.setText("Botão 5");
newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
newParams.setMargins(0, 0, 20, 50);
newParams.addRule(RelativeLayout.BELOW, 1);
newParams.addRule(RelativeLayout.RIGHT_OF, 4);
btn5.setLayoutParams(newParams);
mainLayout.addView(btn5);
// DRAW LINK
NodeLink link = new NodeLink(this);
newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
newParams.addRule(RelativeLayout.BELOW, 1);
newParams.addRule(RelativeLayout.ABOVE, 2);
newParams.addRule(RelativeLayout.RIGHT_OF, 1);
newParams.addRule(RelativeLayout.LEFT_OF, 2);
mainLayout.addView(link);
}
}
Please, any ideas on how can I do that?
You might want to say what NodeLink is doing - custom view class?
Any case, I'd suggest getting the coordinates of "anchors" on your buttons (top-center points, from your diagram), then creating Paths that link associated buttons' anchors. Draw/paint these Paths into a Picture, save it as a PictureDrawable, and then you can set that Drawable as the background of your buttons' parent (the RelativeLayout/mainLayout).
Related
I need dynamically create next xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="a lot of text will be here"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
/>
</LinearLayout>
and here is my code:
LinearLayout ll = new LinearLayout(getActivity());
ll.setLayoutParams(layoutLP);
TextView title = new TextView(getActivity());
title.setText("a lot of text will be here");
title.setLayoutParams(textLP);
EditText editText = new EditText(getActivity());
editText.setLayoutParams(ediLP);
editText.setSingleLine();
ll.addView(title);
ll.addView(editText);
layoutLP = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutLP.gravity = Gravity.CENTER;
textLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
textLP.weight = 2;
textLP.gravity = Gravity.CENTER;
editLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
editLP.weight = 3;
editLP.gravity = Gravity.CENTER;
However, on device I get problem: text inside textView is not wrapped and partly covered by editText.
Why does this happen?
And here is how it must looks like:
Your Activity should be
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
LinearLayout.LayoutParams layoutLP = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutLP.gravity = Gravity.CENTER;
layoutLP.weight = 2;
LinearLayout linearLayout1 = new LinearLayout(this);
linearLayout.setLayoutParams(layoutLP);
linearLayout.setLayoutParams(layoutLP);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams textLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
textLP.weight = 2;
textLP.gravity = Gravity.CENTER;
LinearLayout.LayoutParams editLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
editLP.weight = 3;
editLP.gravity = Gravity.CENTER;
// Add textview 1
TextView textView1 = new TextView(this);
textView1.setLayoutParams(textLP);
textView1.setText("programmatically created TextView1");
// textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
textView1.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
linearLayout1.addView(textView1);
// Add textview 1
EditText editText = new EditText(this);
editText.setLayoutParams(editLP);
editText.setHint("programmatically created TextView1");
// editText.setBackgroundColor(Color.GRAY); // hex color 0xAARRGGBB
editText.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
linearLayout1.addView(editText);
linearLayout.addView(linearLayout1);
Your xml file should be:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout android:id="#+id/ll"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
Try your phone or a phone with similar dpi-resolution properties as yours on the AVD, see if it is the same problem. Maybe the resolution on your phone is the problem.
I am trying to add some buttons to a Relative Layout programatically. I used some examples seen on StackOverflow, but for some reason I cannot understand, my code is not working: I want to have my buttons one above the other, but they end up being one on top of the other.
Here is my layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.android.myApp.MainActivity" >
</RelativeLayout>
And here is my code:
mLayout = (RelativeLayout) findViewById(R.id.mainLayout);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button btnTag = new Button(this);
btnTag.setText("Button0");
btnTag.setId(0);
btnTag.setOnClickListener(mGlobalOnCLickListener);
mLayout.addView(btnTag, lprams);
lprams.addRule(RelativeLayout.BELOW, 0);
btnTag = new Button(this);
btnTag.setText("Button2");
btnTag.setId(1);
btnTag.setOnClickListener(mGlobalOnCLickListener);
mLayout.addView(btnTag, lprams);
Checkout this code....Hope this will help
mLayout = (RelativeLayout) findViewById(R.id.mainLayout);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button btnTag = new Button(this);
btnTag.setText("Button0");
btnTag.setId(10);
btnTag.setOnClickListener(mGlobalOnCLickListener);
mLayout.addView(btnTag, lprams);
lprams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lprams.addRule(RelativeLayout.BELOW, 10);
btnTag = new Button(this);
btnTag.setText("Button2");
btnTag.setId(1);
btnTag.setOnClickListener(mGlobalOnCLickListener);
mLayout.addView(btnTag, lprams);
i want to place one imageview over another. The imageview which i want to place above is getting placed below.
Here's my code:
for (int i = 1; i <= numberofuploads; i++) {
if (i == 1) {
relativeframe1 = new RelativeLayout(this);
RelativeLayout.LayoutParams paramslayout1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT / 2,
RelativeLayout.LayoutParams.MATCH_PARENT);
paramslayout1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
relativeframe1.setLayoutParams(paramslayout1);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
pxrparams1a, pxrparams1b);
imgvwphoto1 = new ImageView(this);
imgvwphoto1.setId(1);
imgvwphoto1.setAlpha(500);
imgvwphoto1.setBackgroundResource(R.drawable.plusicon3);
// params1.setMargins(0, 90, 100, 40);
params1.setMargins(left1, top1, right1, bottom1);
imgvwphoto1.setLayoutParams(params1);
relativeframe1.addView(imgvwphoto1);
imgvwphoto1.setScaleType(ScaleType.MATRIX);
imgvwphoto1.setOnTouchListener(this);
RelativeLayout.LayoutParams paramscomponent1 = new RelativeLayout.LayoutParams(
irpc1, RelativeLayout.LayoutParams.MATCH_PARENT);
paramscomponent1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imgvwcomponent1 = new ImageView(this);
imgvwcomponent1.setAlpha(0);
imgvwcomponent1 .setBackgroundResource(R.drawable.leftpartdoubleheartframe);
imgvwcomponent1.setBackgroundResource(componentname.getImagename());
imgvwcomponent1.setLayoutParams(paramscomponent1);
relativeframe1.addView(imgvwcomponent1);
relativeframe.addView(relativeframe1);
} else {
relativeframe2 = new RelativeLayout(this);
RelativeLayout.LayoutParams paramslayout2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT / 2,
RelativeLayout.LayoutParams.MATCH_PARENT);
paramslayout2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relativeframe1.setLayoutParams(paramslayout2);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
pxrparams2a, pxrparams2b);// 106.67dp, 120dp
imgvwphoto2 = new ImageView(this);
imgvwphoto2.setId(2);
imgvwphoto2.setAlpha(500);
imgvwphoto2.setBackgroundResource(R.drawable.plusicon3);
// params2.setMargins(165, 90, 0, 40);
params2.setMargins(left2, top2, right2, bottom2);
imgvwphoto2.setLayoutParams(params2);
relativeframe2.addView(imgvwphoto2);
imgvwphoto2.setScaleType(ScaleType.MATRIX);
imgvwphoto2.setOnTouchListener(this);
RelativeLayout.LayoutParams paramscomponent2 = new RelativeLayout.LayoutParams(
irpc2, RelativeLayout.LayoutParams.MATCH_PARENT);// 103.33dp
paramscomponent2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imgvwcomponent2 = new ImageView(this);
imgvwcomponent2.setAlpha(500);
imgvwcomponent2
.setBackgroundResource(R.drawable.rightpartdoubleheartframe);
imgvwcomponent2.setLayoutParams(paramscomponent2);
relativeframe2.addView(imgvwcomponent2);
relativeframe.addView(relativeframe2);
}
}
on click of plus icon, image is uploaded on that imgvwphoto,that is overlaping with heart frame, i want it to be viewed inside heart frame only. not outside of heart frame.
You should use two separate layouts and place one at another using <include> statemant like this
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include layout="#layout/lay1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<include layout="#layout/lay2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</merge>
Thats my tab.
I want to align it to center of the screen. (horizontal and vertical)
The table is created dynamically from java.
I tried set
TableLayout t1 = (TableLayout)findViewById(R.id.maintable);
t1.setGravity(Gravity.CENTER);
But it align it only from top.
public class TabActivity extends Activity {
Trny t = Trny.getInstance();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
TableLayout t1 = (TableLayout)findViewById(R.id.maintable);
t1.setGravity(Gravity.CENTER);
// first row of table
TableRow tr = new TableRow(this);
TextView place = new TextView(this);
poradi.setText("P" );
poradi.setPadding(10, 5, 10, 5);
tr.addView(place);
TextView nameOfTeam = new TextView(this);
nazTymu.setText("Nazev" );
nazTymu.setPadding(10, 5, 10, 5);
tr.addView(nameOfTeam);
TextView matchesPlayed = new TextView(this);
odehraneZapasy.setText("Z" );
odehraneZapasy.setPadding(10, 5, 10, 5);
tr.addView(matchesPlayed);
TextView numbefOfWins = new TextView(this);
pocetVyher.setText("V" );
pocetVyher.setPadding(10, 5, 10, 5);
tr.addView(numbefOfWins);
TextView numberOfTies = new TextView(this);
pocetRemiz.setText("R" );
pocetRemiz.setPadding(10, 5, 10, 5);
tr.addView(numberOfTies);
TextView numberOfLoses = new TextView(this);
pocetProher.setText("P" );
pocetProher.setPadding(10, 5, 10, 5);
tr.addView(numberOfLoses);
TextView goalsScored = new TextView(this);
skorePlus.setText("VG" );
skorePlus.setPadding(10, 5, 10, 5);
tr.addView(goalsScored);
TextView goalsGained = new TextView(this);
skoreMinus.setText("OG" );
skoreMinus.setPadding(10, 5, 10, 5);
tr.addView(goalsGained);
TextView numberOfPoints = new TextView(this);
pocBodu.setText("B" );
pocBodu.setPadding(10, 5, 10, 5);
tr.addView(numberOfPoints);
t1.addView(tr);
//more rows dynamically created from java
}
}
XML file
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/maintable" >
</TableLayout>
How can i achieve this? Thanks guys.
change your layout XML to this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/maintable"
android:layout_centerInParent="true" >
</TableLayout>
</RelativeLayout>
I try to add a TextView to a LinearLayout dynamically such as in the following code, but it doesn't appear when I run the application?
setContentView(R.layout.advanced);
m_vwJokeLayout=(LinearLayout) this.findViewById(R.id.m_vwJokeLayout);
m_vwJokeEditText=(EditText) this.findViewById(R.id.m_vwJokeEditText);
m_vwJokeButton=(Button) this.findViewById(R.id.m_vwJokeButton);
TextView tv=new TextView(this);
tv.setText("test");
this.m_vwJokeLayout.addView(tv);
What's the problem?
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("test");
this.m_vwJokeLayout.addView(tv);
You can change lparams according to your needs
Here is a more general answer for future viewers of this question. The layout we will make is below:
Method 1: Add TextView to existing LinearLayout
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamic_linearlayout);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_example);
// Add textview 1
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
textView1.setText("programmatically created TextView1");
textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
textView1.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
linearLayout.addView(textView1);
// Add textview 2
TextView textView2 = new TextView(this);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.RIGHT;
layoutParams.setMargins(10, 10, 10, 10); // (left, top, right, bottom)
textView2.setLayoutParams(layoutParams);
textView2.setText("programmatically created TextView2");
textView2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
textView2.setBackgroundColor(0xffffdbdb); // hex color 0xAARRGGBB
linearLayout.addView(textView2);
}
Note that for LayoutParams you must specify the kind of layout for the import, as in
import android.widget.LinearLayout.LayoutParams;
Otherwise you need to use LinearLayout.LayoutParams in the code.
Here is the xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_example"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff99ccff"
android:orientation="vertical" >
</LinearLayout>
Method 2: Create both LinearLayout and TextView programmatically
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// NOTE: setContentView is below, not here
// Create new LinearLayout
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setBackgroundColor(0xff99ccff);
// Add textviews
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
textView1.setText("programmatically created TextView1");
textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
textView1.setPadding(20, 20, 20, 20); // in pixels (left, top, right, bottom)
linearLayout.addView(textView1);
TextView textView2 = new TextView(this);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.RIGHT;
layoutParams.setMargins(10, 10, 10, 10); // (left, top, right, bottom)
textView2.setLayoutParams(layoutParams);
textView2.setText("programmatically created TextView2");
textView2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
textView2.setBackgroundColor(0xffffdbdb); // hex color 0xAARRGGBB
linearLayout.addView(textView2);
// Set context view
setContentView(linearLayout);
}
Method 3: Programmatically add one xml layout to another xml layout
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamic_linearlayout);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dynamic_linearlayout_item, null);
FrameLayout container = (FrameLayout) findViewById(R.id.flContainer);
container.addView(view);
}
Here is dynamic_linearlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/flContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
And here is the dynamic_linearlayout_item.xml to add:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_example"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff99ccff"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff66ff66"
android:padding="20px"
android:text="programmatically created TextView1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffdbdb"
android:layout_gravity="right"
android:layout_margin="10px"
android:textSize="18sp"
android:text="programmatically created TextView2" />
</LinearLayout>
I customized more #Suragch code. My output looks
I wrote a method to stop code redundancy.
public TextView createATextView(int layout_widh, int layout_height, int align,
String text, int fontSize, int margin, int padding) {
TextView textView_item_name = new TextView(this);
// LayoutParams layoutParams = new LayoutParams(
// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// layoutParams.gravity = Gravity.LEFT;
RelativeLayout.LayoutParams _params = new RelativeLayout.LayoutParams(
layout_widh, layout_height);
_params.setMargins(margin, margin, margin, margin);
_params.addRule(align);
textView_item_name.setLayoutParams(_params);
textView_item_name.setText(text);
textView_item_name.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);
textView_item_name.setTextColor(Color.parseColor("#000000"));
// textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
textView_item_name.setPadding(padding, padding, padding, padding);
return textView_item_name;
}
It can be called like
createATextView(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, RelativeLayout.ALIGN_PARENT_RIGHT,
subTotal.toString(), 20, 10, 20);
Now you can add this to a RelativeLayout dynamically. LinearLayout is also same, just add a orientation.
RelativeLayout primary_layout = new RelativeLayout(this);
LayoutParams layoutParam = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
primary_layout.setLayoutParams(layoutParam);
// FOR LINEAR LAYOUT SET ORIENTATION
// primary_layout.setOrientation(LinearLayout.HORIZONTAL);
// FOR BACKGROUND COLOR
primary_layout.setBackgroundColor(0xff99ccff);
primary_layout.addView(createATextView(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, RelativeLayout.ALIGN_LEFT, list[i],
20, 10, 20));
primary_layout.addView(createATextView(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, RelativeLayout.ALIGN_PARENT_RIGHT,
subTotal.toString(), 20, 10, 20));
TextView rowTextView = (TextView)getLayoutInflater().inflate(R.layout.yourTextView, null);
rowTextView.setText(text);
layout.addView(rowTextView);
This is how I'm using this:
private List<Tag> tags = new ArrayList<>();
if(tags.isEmpty()){
Gson gson = new Gson();
Type listType = new TypeToken<List<Tag>>() {
}.getType();
tags = gson.fromJson(tour.getTagsJSONArray(), listType);
}
if (flowLayout != null) {
if(!tags.isEmpty()) {
Log.e(TAG, "setTags: "+ flowLayout.getChildCount() );
flowLayout.removeAllViews();
for (Tag tag : tags) {
FlowLayout.LayoutParams lparams = new FlowLayout.LayoutParams(FlowLayout.LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.WRAP_CONTENT);
lparams.setMargins(PixelUtil.dpToPx(this, 0), PixelUtil.dpToPx(this, 5), PixelUtil.dpToPx(this, 10), PixelUtil.dpToPx(this, 5));// llp.setMargins(left, top, right, bottom);
TextView rowTextView = (TextView) getLayoutInflater().inflate(R.layout.tag, null);
rowTextView.setText(tag.getLabel());
rowTextView.setLayoutParams(lparams);
flowLayout.addView(rowTextView);
}
}
Log.e(TAG, "setTags: after "+ flowLayout.getChildCount() );
}
And this is my custom TextView named tag:
<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:textAllCaps="true"
fontPath="#string/font_light"
android:background="#drawable/tag_shape"
android:paddingLeft="11dp"
android:paddingTop="6dp"
android:paddingRight="11dp"
android:paddingBottom="6dp">
this is my tag_shape:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#f2f2f2" />
<corners android:radius="15dp" />
</shape>
efect:
In other place I'm adding textviews with language names from dialog with listview:
layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/layoutTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</RelativeLayout>
class file:
setContentView(R.layout.layout_dynamic);
layoutTest=(LinearLayout)findViewById(R.id.layoutTest);
TextView textView = new TextView(getApplicationContext());
textView.setText("testDynamic textView");
layoutTest.addView(textView);
If you are using Linearlayout. its params should be "wrap_content" to add dynamic data in your layout xml. if you use match or fill parent then you cannot see the output.
It Should be like this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>