Textview marquee programmatically - android

Trying to populate an textview(s) from an array. I managed to get the desired effect via XML via the code below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/marque_scrolling_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:padding="16dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Create a Marquee (Scrolling Text) in Android Using TextView. Android Marquee (Scrolling Text) Tutorial with Example"
android:textSize="24sp" />
</LinearLayout>
But I need several of these in an array - so I believe creating the textviews programmatically may be the answer, but cant get them to marquee. This is the code im working on.
String[] strings = {"Mark", "James", "Paul"};
LinearLayout layout = (LinearLayout)findViewById(R.id.linearlayout);
for(String s : strings)
{
TextView newTextView = new TextView(this);
newTextView.setText(s);
newTextView.setSingleLine(true);
newTextView.setEllipsize(TextUtils.TruncateAt.END);
newTextView.setHorizontallyScrolling(true);
newTextView.setLines(1);
newTextView.setMarqueeRepeatLimit(-1);
newTextView.setSelected(true);
newTextView.setTextSize(24);
layout.addView(newTextView);
}

Try this.. It work
TextView testing = (TextView) this.findViewById(R.id.testing);
testing.setEllipsize(TextUtils.TruncateAt.MARQUEE);
testing.setMarqueeRepeatLimit(-1);
testing.setSingleLine(true);
testing.setSelected(true);

Try this.
final RelativeLayout relativeLayout = new RelativeLayout(this);
final RelativeLayout relativeLayoutbotombar = new RelativeLayout(this);
textView = new TextView(this);
textView.setId(1);
RelativeLayout.LayoutParams relativlayparamter = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams relativlaybottombar = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutbotombar.setLayoutParams(relativlaybottombar);
textView.setText("text text text text text, with a long ");
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setSelected(true);
textView.setSingleLine(true);
relativeLayout.addView(relativeLayoutbotombar);
relativeLayoutbotombar.addView(textView);
setContentView(relativeLayout, relativlayparamter);
Another option:
TextView textView = (TextView) this.findViewById(R.id.xxx);
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setText("Text text text text");
textView.setSelected(true);
textView.setSingleLine(true);

Use this code. I improved your code, enjoy copy paste.
val strings = arrayOf("Mark", "James", "Paul")
val layout = findViewById<View>(R.id.linear) as LinearLayout
layout.orientation = LinearLayout.VERTICAL
for (s in strings) {
val newTextView = TextView(this)
newTextView.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
newTextView.text = s
newTextView.isSingleLine = true
newTextView.ellipsize = TextUtils.TruncateAt.END
newTextView.setHorizontallyScrolling(true)
newTextView.setLines(1)
newTextView.marqueeRepeatLimit = -1
newTextView.isSelected = true
newTextView.textSize = 24f
addMarquee(newTextView)
layout.addView(newTextView)
}
Also add this function to your class
fun addMarquee(textView: TextView) {
textView.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val pixels = textView.measuredWidth - 1
val params = textView.layoutParams
params.width = pixels
textView.layoutParams = params
textView.isSelected = true
textView.ellipsize = TextUtils.TruncateAt.MARQUEE
textView.isSingleLine = true
textView.marqueeRepeatLimit = -1
textView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
}

Related

Programmatically generated GridLayout has column being pushed to the right

I am programmatically generating a GridLayout of TextViews. I want the second and third columns (TextViews) of the GridLayout to have equal width, and when there is a lot of text in the TextViews, I want the TextViews to expand vertically downwards instead of occupying more space. I am trying to achieve this by programmatically setting the column weight of the second and third TextViews to be 1. However, when there is lot of text in the second TextView for instance, the second and third TextViews just get pushed to the side. Below is a screenshot:
However, this is what I want my application to look like:
Below is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="1"
android:columnCount="3">
<!-- Difference Table -->
<LinearLayout
android:orientation="vertical"
android:id="#+id/difference_table"
android:layout_width="match_parent"
android:layout_column="0"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_height="wrap_content"></LinearLayout>
</GridLayout>
Below is my Kotlin code:
val diffLayout = findViewById<LinearLayout>(R.id.difference_table)
var diffTable: GridLayout = GridLayout(this)
// Set the dimensions of the grid
diffTable.layoutParams =
ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
diffTable.columnCount = 3;
diffTable.rowCount = 1;
// Create the three columns and add them to the grid
var lineNumber: TextView = TextView(this)
lineNumber.text = "asdf"
lineNumber.setTextColor(Color.parseColor("#000000"))
lineNumber.gravity = Gravity.LEFT
var currentLine1: TextView = TextView(this)
currentLine1.setTextColor(Color.parseColor("#000000"))
currentLine1.text = "asdfasdfasdfadfasdfadfasdfasdfasdfasdfasdfasdf"
lineNumber.gravity = Gravity.LEFT
// 3. Create the TextView representing the current line in Text 2
var currentLine2: TextView = TextView(this)
currentLine2.setTextColor(Color.parseColor("#000000"))
currentLine2.text = "asdfasdfasdfadfasdfadfasdfasdfasdfasdfasdfasdf"
lineNumber.gravity = Gravity.LEFT
// TODO: ENABLE LAYOUT WEIGHT FOR API < 21
diffTable.addView(lineNumber, GridLayout.LayoutParams(
GridLayout.spec(0, GridLayout.CENTER),
GridLayout.spec(0, GridLayout.CENTER, 0.0f)));
// Add the current line in Text 1 to the table
diffTable.addView(currentLine1, GridLayout.LayoutParams(
GridLayout.spec(0, GridLayout.CENTER),
GridLayout.spec(1, GridLayout.CENTER, 1.0f)));
// Add the current line in Text 2 to the table
diffTable.addView(currentLine2, GridLayout.LayoutParams(
GridLayout.spec(0, GridLayout.CENTER),
GridLayout.spec(2, GridLayout.CENTER, 1.0f)));
1) There should be something wrong with flag on extending GridLayouts.
Your both Views inside GridLayout should have weight for vertical and horizontal axis. Try to setup this params for your first and second Views above.
final GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(
GridLayout.UNDEFINED,GridLayout.FILL,1f),
GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f));
2) As an alternative and simple solution, you could replace your both Views above with single LinearLayout container, which could handle weight by it's own. Something like this.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="true"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/view.line.1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView]
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout
android:id="#+id/view.line.2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView]
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout>
I modified a bit in your previous code and added the width = 0property, which may achieve the display effect you want
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_main)
// val diffLayout = findViewById<LinearLayout>(R.id.difference_table)
// val diffTable = findViewById<GridLayout>(R.id.grid_layout);
var diffTable: GridLayout = GridLayout(this)
// Set the dimensions of the grid
diffTable.layoutParams =
ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
diffTable.columnCount = 3
diffTable.rowCount = 1
// Create the three columns and add them to the grid
var lineNumber: TextView = TextView(this)
lineNumber.text = "asdf"
lineNumber.setTextColor(Color.parseColor("#000000"))
lineNumber.gravity = Gravity.LEFT
var currentLine1: TextView = TextView(this)
currentLine1.setTextColor(Color.parseColor("#000000"))
currentLine1.text = "asdfasdfasdfadfasdfadfasdfasdfasdfasdfasdfasdf"
lineNumber.gravity = Gravity.CENTER
// 3. Create the TextView representing the current line in Text 2
var currentLine2: TextView = TextView(this)
currentLine2.setTextColor(Color.parseColor("#000000"))
currentLine2.text = "asdfasdfasdfadfasdfadfasdfasdfasdfasdfasdfasdf"
lineNumber.gravity = Gravity.CENTER
var layoutParams = GridLayout.LayoutParams()
layoutParams.rowSpec = GridLayout.spec(0, GridLayout.FILL)
layoutParams.columnSpec = GridLayout.spec(0, GridLayout.FILL)
// TODO: ENABLE LAYOUT WEIGHT FOR API < 21
diffTable.addView(lineNumber, layoutParams)
var layoutParams1 = GridLayout.LayoutParams()
layoutParams1.rowSpec = GridLayout.spec(0, GridLayout.FILL)
layoutParams1.columnSpec = GridLayout.spec(1, GridLayout.FILL, 1f)
layoutParams1.width = 0
// Add the current line in Text 1 to the table
diffTable.addView(currentLine1, layoutParams1)
// Add the current line in Text 2 to the tableLEFT
var layoutParams2 = GridLayout.LayoutParams()
layoutParams2.rowSpec = GridLayout.spec(0, GridLayout.FILL)
layoutParams2.columnSpec = GridLayout.spec(2, GridLayout.FILL, 1f)
layoutParams2.width = 0
diffTable.addView(currentLine2, layoutParams2)
addContentView(diffTable, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT))
}
My suggestion is to dynamically set the column width for the views. Finally it will align each view with required amount of space.
var diffTable: GridLayout = GridLayout(this)
diffTable.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
diffTable.setColumnCount(3);
diffTable.setRowCount(1);
TextView lineNumber;
for (int i = 0; facilities != null && i < facilities.size(); i++) {
lineNumber = new TextView(getContext());
lineNumber.setText(facilities.get(i));
diffTable.addView(lineNumber, i);
lineNumber.setCompoundDrawablesWithIntrinsicBounds(rightIc, 0, 0, 0);
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.height = LayoutParams.WRAP_CONTENT;
param.width = LayoutParams.WRAP_CONTENT;
param.rightMargin = 5;
param.topMargin = 5;
param.setGravity(Gravity.CENTER);
param.columnSpec = GridLayout.spec(c);
param.rowSpec = GridLayout.spec(r);
lineNumber.setLayoutParams (param);
}
Where c stands for 'column index' and r stands for row index of GridLayout.
Note: The above code may require a bit of change if you want to insert your parameters.

get TextView and EditText from Programatically created

i create 1 LinearLayout inside LinearLayout, that contains 1 EditText and 1 TextView programmatically and i need to get text from EditText and TextView. And this is my code:
main.xml:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" >
<LinearLayout
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
MainActivity.java:
linearLayout = (LinearLayout) view.findViewById(R.id.result);
.......
public void addItem(){
LinearLayout childLayout = new LinearLayout(context);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childLayout.setLayoutParams(linearParams);
childLayout.setTag(item_name);
childLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView item = new TextView(context);
item.setTag(item_name);
item.setText(item_name);
item.setTextSize(16);
item.setGravity(Gravity.LEFT);
LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp1.setMargins(0, 0, 15, 0);
item.setLayoutParams(llp1);
item.setTextColor(context.getResources().getColor(android.R.color.black));
EditText quantity = new EditText(context);
quantity.setText(nama_barang);
quantity.setTextSize(16);
quantity.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp2.setMargins(10, 0, 15, 0);
quantity.setLayoutParams(llp2);
quantity.setTextColor(context.getResources().getColor(android.R.color.black));
childLayout.addView(item);
childLayout.addView(quantity);
linearLayout.addView(childLayout);
}
Help me, please.
I've edit my question
Actually, my case above happened when i clicked addItem(), my app will display new TextView and EditText, so if this process is finished, and move to the next Fragment to display all the text of TextViews and EditTexts like this using StringBuilder:
Items Qtt
Cola......1
Chicken...1
Meat......2
Help me.
Do you want to get like this?
String str = item.getText().toString();
String str2 = quantity.getText().toString();
As stated above, using item.getText().toString(); is all you need, nonetheless I do presume your problem lies in the fact that you are declaring your Views inside the loop, by doing it locally later anywhere on the code you won't be getting any text at all.
Try declaring your Views as global variables or doing it outside of the loop such as;
//....
TextView item;
EditText quantity;
for(int i = 0; i < cartCount; i++) {
LinearLayout childLayout = new LinearLayout(context);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childLayout.setLayoutParams(linearParams);
childLayout.setTag(item_name);
childLayout.setOrientation(LinearLayout.HORIZONTAL);
item = new TextView(context);
item.setTag(item_name);
item.setText(item_name);
item.setTextSize(16);
item.setGravity(Gravity.LEFT);
LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp1.setMargins(0, 0, 15, 0);
item.setLayoutParams(llp1);
item.setTextColor(context.getResources().getColor(android.R.color.black));
quantity = new EditText(context);
quantity.setText(nama_barang);
quantity.setTextSize(16);
quantity.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp2.setMargins(10, 0, 15, 0);
quantity.setLayoutParams(llp2);
quantity.setTextColor(context.getResources().getColor(android.R.color.black));
childLayout.addView(item);
childLayout.addView(quantity);
linearLayout.addView(childLayout);
}
On the other hand, by looking at your code, are you trying to create a list of EditText and TextViews? If so, it would be wise to consider using a ListView with custom items; Here you can find a nice tutorial doing something simmilar.
This feels like you should be looking at an adapter view such as a recyclerview. You might start to see some performance issues using a linearlayout.
Anyway. You could add the views to an list as you add them to the linearlayout.
List<TextView> items = new LinkedList<>();
List<EditText> quantities = new LinkedList<>();
for(int i = 0; i < cartCount; i++) {
...
childLayout.addView(item);
childLayout.addView(quantity);
items.add(item);
quantities.add(quantity);
linearLayout.addView(childLayout);
}
then loop through your views
for (TextView textView : items) {
}
for (EditText editText : quantities) {
}
item.getText().toString()
quantity.getText().toString()

Android - set textview dynamically in row wise

I have to create textview with json dynamically. For now I successfullyClick Here create but not in proper way.
Like this image but not able to create. I done like that, Click Here
Here is xml code --
<LinearLayout
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:id="#+id/layout_top_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
Here is java code
try {
JSONObject object = new JSONObject(response);
boolean status = object.getBoolean("status");
if (status) {
JSONArray array = object.getJSONArray("topsearches");
for (int i = 0; i < array.length(); i++) {
final TextView textView;
JSONObject jsonObject = array.getJSONObject(i);
String keyword = jsonObject.getString("search-keyord");
String cat_id = jsonObject.getString("category_id");
textView = new TextView(getActivity());
textView.setText(keyword);
textView.setId(i);
textView.setBackgroundResource(R.drawable.border_gray);
textView.setPadding(10, 10, 10, 10);
textView.setTextSize(14);
textView.setLayoutParams(new LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
getLayout_top_search().addView(textView);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String query = textView.getText().toString();
}
});
}
}
} catch (Exception e) {
e.printStackTrace();
}
Please help.
All you want is Flow Layout
<com.wefika.flowlayout.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start|top">
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum" />
</com.wefika.flowlayout.FlowLayout>
Instead of LinearLayout you can use Flow Layout Which helps you to add textview in a row. If the row has no enough space it will extend a new row. And then the layout will become like this.
For more Information: Please read this and this
Please Feel free to ask me.if you are facing any issue.
TextView tvContent = new TextView(getActivity());
medicineName.add(tvContent);
LinearLayout.LayoutParams tvParam = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tvContent.setLayoutParams(tvParam);
tvContent.setGravity(Gravity.LEFT);
tvContent.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
tvContent.setText(name);
tvContent.setTextSize(16);
layout.addView(tvContent);
use this in onPostExecute() method
LinearLayout layout = new LinearLayout(getActivity());
LinearLayout.LayoutParams lLayoutlayoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lLayoutlayoutParams.setMargins(0, 8, 8, 8);
layout.setLayoutParams(lLayoutlayoutParams);
layout.setPadding((int) (fDimRatio * 8), (int) (fDimRatio * 8),
(int) (fDimRatio * 8), (int) (fDimRatio * 8));
layout.setOrientation(LinearLayout.VERTICAL);
TextView tvContent = new TextView(getActivity());
medicineName.add(tvContent);
LinearLayout.LayoutParams tvParam = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tvContent.setLayoutParams(tvParam);
tvContent.setGravity(Gravity.LEFT);
tvContent.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
tvContent.setText(name);
tvContent.setTextSize(16);
layout.addView(tvContent);
linearLayout.addView(layout);
linearLayout is id of linearlayout in xml
linearLayout = (LinearLayout) view.findViewById(R.id.container);
fDimRatio value is
private float fDimRatio = 1.0f; // /xml
If you want like image 1 then change android:orientation="horizontal"

Programmatically align a Text view in a RelativeLayout [duplicate]

I'm trying to achieve the following programmatically (rather than declaratively via XML):
<RelativeLayout...>
<TextView ...
android:id="#+id/label1" />
<TextView ...
android:id="#+id/label2"
android:layout_below: "#id/label1" />
</RelativeLayout>
In other words, how do I make the second TextView appear below the first one, but I want to do it in code:
RelativeLayout layout = new RelativeLayout(this);
TextView label1 = new TextView(this);
TextView label2 = new TextView(this);
...
layout.addView(label1);
layout.addView(label2);
setContentView(layout);
Update:
Thanks, TreeUK. I understand the general direction, but it still doesn't work - "B" overlaps "A". What am I doing wrong?
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
From what I've been able to piece together, you have to add the view using LayoutParams.
LinearLayout linearLayout = new LinearLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
parentView.addView(linearLayout, relativeParams);
All credit to sechastain, to relatively position your items programmatically you have to assign ids to them.
TextView tv1 = new TextView(this);
tv1.setId(1);
TextView tv2 = new TextView(this);
tv2.setId(2);
Then addRule(RelativeLayout.RIGHT_OF, tv1.getId());
Cut the long story short:
With relative layout you position elements inside the layout.
create a new RelativeLayout.LayoutParams
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(...)
(whatever... fill parent or wrap content, absolute numbers if you must, or reference to an XML resource)
Add rules:
Rules refer to the parent or to other "brothers" in the hierarchy.
lp.addRule(RelativeLayout.BELOW, someOtherView.getId())
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
Just apply the layout params: The most 'healthy' way to do that is:
parentLayout.addView(myView, lp)
Watch out: Don't change layout from the layout callbacks. It is tempting to do so because this is when views get their actual sizes. However, in that case, unexpected results are expected.
Just spent 4 hours with this problem. Finally realized that you must not use zero as view id. You would think that it is allowed as NO_ID == -1, but things tend to go haywire if you give it to your view...
Android 22 minimal runnable example
Source:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RelativeLayout relativeLayout = new RelativeLayout(this);
final TextView tv1;
tv1 = new TextView(this);
tv1.setText("tv1");
// Setting an ID is mandatory.
tv1.setId(View.generateViewId());
relativeLayout.addView(tv1);
// tv2.
final TextView tv2;
tv2 = new TextView(this);
tv2.setText("tv2");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.BELOW, tv1.getId());
relativeLayout.addView(tv2, lp);
// tv3.
final TextView tv3;
tv3 = new TextView(this);
tv3.setText("tv3");
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
lp2.addRule(RelativeLayout.BELOW, tv2.getId());
relativeLayout.addView(tv3, lp2);
this.setContentView(relativeLayout);
}
}
Works with the default project generated by android create project .... GitHub repository with minimal build code.
call
tv1.setId(1)
after
tv1.setText("A");
Try:
EditText edt = (EditText) findViewById(R.id.YourEditText);
RelativeLayout.LayoutParams lp =
new RelativeLayout.LayoutParams
(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT
);
lp.setMargins(25, 0, 0, 0); // move 25 px to right (increase left margin)
edt.setLayoutParams(lp); // lp.setMargins(left, top, right, bottom);
This approach with ViewGroup.MarginLayoutParams worked for me:
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.my_layout);
TextView someTextView = ...
int leftMargin = Util.getXPos();
int topMargin = Util.getYPos();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
lp.setMargins(leftMargin, topMargin, 0, 0);
myLayout.addView(someTextView, lp);
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
final RelativeLayout relativeLayout = new RelativeLayout(this);
final TextView tv1 = new TextView(this);
tv1.setText("tv1 is here");
// Setting an ID is mandatory.
tv1.setId(View.generateViewId());
relativeLayout.addView(tv1);
final TextView tv2 = new TextView(this);
tv2.setText("tv2 is here");
// We are defining layout params for tv2 which will be added to its parent relativelayout.
// The type of the LayoutParams depends on the parent type.
RelativeLayout.LayoutParams tv2LayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//Also, we want tv2 to appear below tv1, so we are adding rule to tv2LayoutParams.
tv2LayoutParams.addRule(RelativeLayout.BELOW, tv1.getId());
//Now, adding the child view tv2 to relativelayout, and setting tv2LayoutParams to be set on view tv2.
relativeLayout.addView(tv2);
tv2.setLayoutParams(tv2LayoutParams);
//Or we can combined the above two steps in one line of code
//relativeLayout.addView(tv2, tv2LayoutParams);
this.setContentView(relativeLayout);
}
}
If you really want to layout manually, i'd suggest not to use a standard layout at all. Do it all on your own, here a kotlin example:
class ProgrammaticalLayout #JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : ViewGroup(context, attrs, defStyleAttr) {
private val firstTextView = TextView(context).apply {
test = "First Text"
}
private val secondTextView = TextView(context).apply {
text = "Second Text"
}
init {
addView(firstTextView)
addView(secondTextView)
}
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
// center the views verticaly and horizontaly
val firstTextLeft = (measuredWidth - firstTextView.measuredWidth) / 2
val firstTextTop = (measuredHeight - (firstTextView.measuredHeight + secondTextView.measuredHeight)) / 2
firstTextView.layout(firstTextLeft,firstTextTop, firstTextLeft + firstTextView.measuredWidth,firstTextTop + firstTextView.measuredHeight)
val secondTextLeft = (measuredWidth - secondTextView.measuredWidth) / 2
val secondTextTop = firstTextView.bottom
secondTextView.layout(secondTextLeft,secondTextTop, secondTextLeft + secondTextView.measuredWidth,secondTextTop + secondTextView.measuredHeight)
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
// just assume we`re getting measured exactly by the parent
val measuredWidth = MeasureSpec.getSize(widthMeasureSpec)
val measuredHeight = MeasureSpec.getSize(heightMeasureSpec)
firstTextView.measures(MeasureSpec.makeMeasureSpec(meeasuredWidth, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))
secondTextView.measures(MeasureSpec.makeMeasureSpec(meeasuredWidth, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))
setMeasuredDimension(measuredWidth, measuredHeight)
}
}
This might give you an idea how this could work

How to lay out Views in RelativeLayout programmatically?

I'm trying to achieve the following programmatically (rather than declaratively via XML):
<RelativeLayout...>
<TextView ...
android:id="#+id/label1" />
<TextView ...
android:id="#+id/label2"
android:layout_below: "#id/label1" />
</RelativeLayout>
In other words, how do I make the second TextView appear below the first one, but I want to do it in code:
RelativeLayout layout = new RelativeLayout(this);
TextView label1 = new TextView(this);
TextView label2 = new TextView(this);
...
layout.addView(label1);
layout.addView(label2);
setContentView(layout);
Update:
Thanks, TreeUK. I understand the general direction, but it still doesn't work - "B" overlaps "A". What am I doing wrong?
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
From what I've been able to piece together, you have to add the view using LayoutParams.
LinearLayout linearLayout = new LinearLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
parentView.addView(linearLayout, relativeParams);
All credit to sechastain, to relatively position your items programmatically you have to assign ids to them.
TextView tv1 = new TextView(this);
tv1.setId(1);
TextView tv2 = new TextView(this);
tv2.setId(2);
Then addRule(RelativeLayout.RIGHT_OF, tv1.getId());
Cut the long story short:
With relative layout you position elements inside the layout.
create a new RelativeLayout.LayoutParams
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(...)
(whatever... fill parent or wrap content, absolute numbers if you must, or reference to an XML resource)
Add rules:
Rules refer to the parent or to other "brothers" in the hierarchy.
lp.addRule(RelativeLayout.BELOW, someOtherView.getId())
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
Just apply the layout params: The most 'healthy' way to do that is:
parentLayout.addView(myView, lp)
Watch out: Don't change layout from the layout callbacks. It is tempting to do so because this is when views get their actual sizes. However, in that case, unexpected results are expected.
Just spent 4 hours with this problem. Finally realized that you must not use zero as view id. You would think that it is allowed as NO_ID == -1, but things tend to go haywire if you give it to your view...
Android 22 minimal runnable example
Source:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RelativeLayout relativeLayout = new RelativeLayout(this);
final TextView tv1;
tv1 = new TextView(this);
tv1.setText("tv1");
// Setting an ID is mandatory.
tv1.setId(View.generateViewId());
relativeLayout.addView(tv1);
// tv2.
final TextView tv2;
tv2 = new TextView(this);
tv2.setText("tv2");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.BELOW, tv1.getId());
relativeLayout.addView(tv2, lp);
// tv3.
final TextView tv3;
tv3 = new TextView(this);
tv3.setText("tv3");
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
lp2.addRule(RelativeLayout.BELOW, tv2.getId());
relativeLayout.addView(tv3, lp2);
this.setContentView(relativeLayout);
}
}
Works with the default project generated by android create project .... GitHub repository with minimal build code.
call
tv1.setId(1)
after
tv1.setText("A");
Try:
EditText edt = (EditText) findViewById(R.id.YourEditText);
RelativeLayout.LayoutParams lp =
new RelativeLayout.LayoutParams
(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT
);
lp.setMargins(25, 0, 0, 0); // move 25 px to right (increase left margin)
edt.setLayoutParams(lp); // lp.setMargins(left, top, right, bottom);
This approach with ViewGroup.MarginLayoutParams worked for me:
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.my_layout);
TextView someTextView = ...
int leftMargin = Util.getXPos();
int topMargin = Util.getYPos();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
lp.setMargins(leftMargin, topMargin, 0, 0);
myLayout.addView(someTextView, lp);
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
final RelativeLayout relativeLayout = new RelativeLayout(this);
final TextView tv1 = new TextView(this);
tv1.setText("tv1 is here");
// Setting an ID is mandatory.
tv1.setId(View.generateViewId());
relativeLayout.addView(tv1);
final TextView tv2 = new TextView(this);
tv2.setText("tv2 is here");
// We are defining layout params for tv2 which will be added to its parent relativelayout.
// The type of the LayoutParams depends on the parent type.
RelativeLayout.LayoutParams tv2LayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//Also, we want tv2 to appear below tv1, so we are adding rule to tv2LayoutParams.
tv2LayoutParams.addRule(RelativeLayout.BELOW, tv1.getId());
//Now, adding the child view tv2 to relativelayout, and setting tv2LayoutParams to be set on view tv2.
relativeLayout.addView(tv2);
tv2.setLayoutParams(tv2LayoutParams);
//Or we can combined the above two steps in one line of code
//relativeLayout.addView(tv2, tv2LayoutParams);
this.setContentView(relativeLayout);
}
}
If you really want to layout manually, i'd suggest not to use a standard layout at all. Do it all on your own, here a kotlin example:
class ProgrammaticalLayout #JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : ViewGroup(context, attrs, defStyleAttr) {
private val firstTextView = TextView(context).apply {
test = "First Text"
}
private val secondTextView = TextView(context).apply {
text = "Second Text"
}
init {
addView(firstTextView)
addView(secondTextView)
}
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
// center the views verticaly and horizontaly
val firstTextLeft = (measuredWidth - firstTextView.measuredWidth) / 2
val firstTextTop = (measuredHeight - (firstTextView.measuredHeight + secondTextView.measuredHeight)) / 2
firstTextView.layout(firstTextLeft,firstTextTop, firstTextLeft + firstTextView.measuredWidth,firstTextTop + firstTextView.measuredHeight)
val secondTextLeft = (measuredWidth - secondTextView.measuredWidth) / 2
val secondTextTop = firstTextView.bottom
secondTextView.layout(secondTextLeft,secondTextTop, secondTextLeft + secondTextView.measuredWidth,secondTextTop + secondTextView.measuredHeight)
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
// just assume we`re getting measured exactly by the parent
val measuredWidth = MeasureSpec.getSize(widthMeasureSpec)
val measuredHeight = MeasureSpec.getSize(heightMeasureSpec)
firstTextView.measures(MeasureSpec.makeMeasureSpec(meeasuredWidth, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))
secondTextView.measures(MeasureSpec.makeMeasureSpec(meeasuredWidth, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))
setMeasuredDimension(measuredWidth, measuredHeight)
}
}
This might give you an idea how this could work

Categories

Resources