How to set textview position when created dynamically in Android Studio - android

I want to dynamically change the position of the textview list dynamically. Here's what I have tried so far:
final TextView[] myTextViews = new TextView[N]; // create an empty array;
TextView rowTextView = new TextView(Oyun.this);
rowTextView = (TextView)findViewById(R.id.parent);
myTextViews[atesSayac].setText("This is row #" + atesSayac);
myTextViews[atesSayac] = rowTextView;
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)myTextViews[atesSayac].getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
RelativeLayout.LayoutParams layoutParams2 = (RelativeLayout.LayoutParams)myTextViews[atesSayac].getLayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
myTextViews[atesSayac].setLayoutParams(layoutParams);
myTextViews[atesSayac].setLayoutParams(layoutParams2);
myTextViews[atesSayac].setTranslationX(locationX);
myTextViews[atesSayac].setTranslationY(locationY);
atesEt.addView(myTextViews[atesSayac]);

Related

Android table layout is not visible

I am using this code to show a table dynamically inside a relative layout.
I can see on debug mode that the relative layout and a child of table layout but it is not visible on the screen.
What am I doing wrong?
The code is:
RelativeLayout relative = (RelativeLayout) findViewById(R.id.my_relative_layout);
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
tableLayout = new TableLayout(MyActivity.this);
tableLayout.setBackgroundColor(Color.BLACK);
TableRow.LayoutParams tableHeaderRowParams = new TableRow.LayoutParams();
tableHeaderRowParams.setMargins(5, 5, 5, 5);
tableHeaderRowParams.weight = 0;
tableHeaderRowParams.span = 2;
TableRow tableHeaderRow = new TableRow(MyActivity.this);
tableHeaderRow.setBackgroundColor(Color.WHITE);
TextView txvHeader = new TextView(MyActivity.this);
txvHeader.setLayoutParams(tableHeaderRowParams);
txvHeader.setBackgroundColor(Color.WHITE);
txvHeader.setGravity(Gravity.CENTER);
txvHeader.setTag(10);
txvHeader.setId(10);
txvHeader.setTextColor(Color.BLUE);
txvHeader.setTextSize(TypedValue.COMPLEX_UNIT_SP, rowTextSize);
txvHeader.setTypeface(null, Typeface.BOLD);
txvHeader.setText(“Header”);
tableHeaderRow.addView(txvHeader, tableHeaderRowParams);
tableLayout.addView(tableHeaderRow, tableLayoutParams);
RelativeLayout.LayoutParams lpTable = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lpTable.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
lpTable.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
lpTable.topMargin = 9 * space + 6 * rowHeight;
relative.addView(tableLayout, lpTable);
Thanks!

How to show textview left alignment in android

In my application i want to show VIEW ALL to left align like below image:-
But I found this type of layout and more thing i also do all these layout programmatically.
My SourceCode:-
private void printFrontCategory(){
for(int i=0;i<main.size();i++) {
View v = new View(MainActivity.this);
v.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT, 5));
v.setBackgroundColor(Color.rgb(51, 51, 51));
/* View v1 = new View(MainActivity.this);
v1.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT,5));
v1.setBackgroundColor(Color.rgb(255, 255, 255));*/
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(MainActivity.this);
horizontalScrollView.setHorizontalScrollBarEnabled(false);
TextView textView = new TextView(MainActivity.this);
String content = front_category_catetory_name.get(i);
content = content.replace("'", "");
content = content.replace("&","");
textView.setText(content);
textView.setGravity(Gravity.RIGHT);
// textView.setGravity(Gravity.CENTER);
textView.setTextSize(20);
TextView textView2 = new TextView(MainActivity.this);
textView2.setText("VIEW ALL");
textView2.setTextColor(Color.BLUE);
textView2.setGravity(Gravity.LEFT);
LinearLayout linearLayout2 = new LinearLayout(MainActivity.this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
linearLayout2.addView(textView,0);
linearLayout2.addView(textView2,1);
LinearLayout linearLayout = new LinearLayout(MainActivity.this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int j = 0; j < main.get(i).size(); j++) {
LinearLayout linearLayout1 = new LinearLayout(MainActivity.this);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
ImageView image = new ImageView(MainActivity.this);
TextView nameProduct = new TextView(MainActivity.this);
TextView priceProduct = new TextView(MainActivity.this);
TextView special_discount = new TextView(MainActivity.this);
/* Log.d("counter val",cnt+"");
Log.d("thumb ",front_category_thumb.size()+"");
Log.d("image", front_category_thumb.get(52));*/
new SetImageView(image).execute(main.get(i).get(j).get(1));
nameProduct.setText(main.get(i).get(j).get(2));
if (!String.valueOf(main.get(i).get(j).get(5)).equals(null)) {
priceProduct.setText(main.get(i).get(j).get(3));
special_discount.setText(main.get(i).get(j).get(5));
priceProduct.setPaintFlags(nameProduct.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
priceProduct.setGravity(Gravity.CENTER);
special_discount.setGravity(Gravity.CENTER);
nameProduct.setGravity(Gravity.CENTER);
linearLayout1.addView(image);
linearLayout1.addView(nameProduct);
linearLayout1.addView(priceProduct);
linearLayout1.addView(special_discount);
} else if (!String.valueOf(main.get(i).get(j).get(4)).equals(null)) {
priceProduct.setText(main.get(i).get(j).get(3));
special_discount.setText(main.get(i).get(j).get(4));
priceProduct.setPaintFlags(nameProduct.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
priceProduct.setGravity(Gravity.CENTER);
special_discount.setGravity(Gravity.CENTER);
nameProduct.setGravity(Gravity.CENTER);
linearLayout1.addView(image);
linearLayout1.addView(nameProduct);
linearLayout1.addView(priceProduct);
linearLayout1.addView(special_discount);
} else {
priceProduct.setText(main.get(i).get(j).get(3));
priceProduct.setGravity(Gravity.CENTER);
nameProduct.setGravity(Gravity.CENTER);
linearLayout1.addView(image);
linearLayout1.addView(nameProduct);
linearLayout1.addView(priceProduct);
}
linearLayout.addView(linearLayout1, j);
}
horizontalScrollView.addView(linearLayout);
// linearLayoutmens.addView(textView);
// linearLayoutmens.addView(v1);
linearLayoutmens.addView(linearLayout2);
linearLayoutmens.addView(horizontalScrollView);
linearLayoutmens.addView(v);
}
}
I am new in android programming. Please help me!
If you haven't found solution yet, then you can try the following
textView.setGravity(Gravity.RIGHT); This basically act as Gravity for text within TextView for more info refer So basically it is counter part of android:Gravity. Hence we need LayoutParam
Your problem it can be solved in two ways using LinearLayout and RelativeLayout change the below code from
TextView textView = new TextView(MainActivity.this);
String content = front_category_catetory_name.get(i);
content = content.replace("'", "");
content = content.replace("&","");
textView.setText(content);
textView.setGravity(Gravity.RIGHT);
// textView.setGravity(Gravity.CENTER);
textView.setTextSize(20);
TextView textView2 = new TextView(MainActivity.this);
textView2.setText("VIEW ALL");
textView2.setTextColor(Color.BLUE);
textView2.setGravity(Gravity.LEFT);
LinearLayout linearLayout2 = new LinearLayout(MainActivity.this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
linearLayout2.addView(textView,0);
linearLayout2.addView(textView2,1);
Solution1 Using LinearLayout
TextView textView = new TextView(MainActivity.this);
String content = front_category_catetory_name.get(i);
content = content.replace("'", "");
content = content.replace("&","");
textView.setText(content);
textView.setGravity(Gravity.START); //Gravity.Left also works
textView.setTextSize(20);
TextView textView2 = new TextView(MainActivity.this);
textView2.setText("VIEW ALL");
textView2.setTextColor(Color.BLUE);
textView2.setGravity(Gravity.END); //Gravity.Right also works
LinearLayout linearLayout2 = new LinearLayout(MainActivity.this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams linearLayoutParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,2);
linearLayout2.setLayoutParams(linearLayoutParam);
LinearLayout.LayoutParams viewParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,1);
linearLayout2.addView(textView, 0, viewParam);
linearLayout2.addView(textView2, 1, viewParam);
In above solution I have assigned weightSum = 2 to LinearLayout and weight=1 to each TextView.
Solution2 Using RelativeLayout
TextView textView = new TextView(MainActivity.this);
String content = front_category_catetory_name.get(i);
content = content.replace("'", "");
content = content.replace("&","");
textView.setText(content);
//textView.setGravity(Gravity.START); //Gravity.Left also works
textView.setTextSize(20);
TextView textView2 = new TextView(MainActivity.this);
textView2.setText("VIEW ALL");
textView2.setTextColor(Color.BLUE);
//textView2.setGravity(Gravity.END); //Gravity.Right also works
RelativeLayout relativeLayout2 = new RelativeLayout(MainActivity.this);
RelativeLayout.LayoutParams relativeLayoutParam1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutParam1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
relativeLayout2.addView(textView, 0, relativeLayoutParam1);
RelativeLayout.LayoutParams relativeLayoutParam2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutParam2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relativeLayout2.addView(textView2, 1, relativeLayoutParam2);
In this case textView.setGravity(Gravity.RIGHT); is optional.
Update Based on comment
If you want underlined text as in SAMPLE below then check one of the answer here
<u>Sample</u>
But if you want a black line then it is view and you need to add it after adding linearlayout2 or relativelayout2 depending upon the solution in the main view.

Dynamically create textview in center

I have a list of items and a relative layout. I want to add as many relative layouts as the no of items in list and textview in each on them.
My problem is, I am not able to position the textview in the centre of the layout.
Below in my code
for(int i = 0; i < Names.size(); i++)
{
textView = new TextView(this);
textView.setText(Names.get(i));
rt=new RelativeLayout(this);
rt.setBackgroundResource(R.drawable.mednamedash);
int curTextViewId = prevTextViewId + 1;
int curRLId = prevRLId + 1;
textView.setId(curTextViewId);
rt.setId(curRLId);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, prevTextViewId);
rt.setLayoutParams(params);
//textView.setLayoutParams(params);
rt.addView(textView);
textView.setGravity(Gravity.CENTER);
prevTextViewId = curTextViewId;
prevRLId=curRLId;
noon.addView(rt, params);
}
Here rt is the dynamiclly created RelativeLayout and textView is the dynamically created TextView... noon is the RelativeLayout I am adding everything into.
Try this:
final RelativeLayout.LayoutParams tvParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
tvParams.addRule(RelativeLayout.CENTER_IN_PARENT);
rt.addView(textView, tvParams);
Try with textView.setGravity(Gravity.CENTER); before its addition to the rt (RelativeLayout)

Relative Layout dynamic textView

RelativeLayout layout = (RelativeLayout)findViewById(R.id.rl);
for(int i = 1; i <= count; i++)
{
final TextView textViewUtente = new TextView(mContext);
final TextView textViewMessaggio = new TextView(mContext);
final TextView textViewData = new TextView(mContext);
textViewUtente.setText(sepUser[i]);
textViewMessaggio.setText(sepMessage[i]);
textViewData.setText(sepDate[i]);
int curTextViewIdUtente = 1000+i;
int curTextViewIdMessaggio = 2000+i;
int curTextViewIdData = 3000+i;
textViewUtente.setId(curTextViewIdUtente);
textViewMessaggio.setId(curTextViewIdMessaggio);
textViewData.setId(curTextViewIdData);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.topMargin= 100+i*150;
textViewUtente.setLayoutParams(params);
layout.addView(textViewUtente, params);
params.topMargin= 250+i*150;
textViewMessaggio.setLayoutParams(params);
layout.addView(textViewMessaggio, params);
params.topMargin= 100+i*150;
params.rightMargin=200;
textViewData.setLayoutParams(params);
layout.addView(textViewData, params);
}
I need to create 3 textview (that create the post on the blog from the user) every for loop like this:
Post 1:
TextViewUSer TextViewDate
TextViewMessage
Post 2:
TextViewUSer TextViewDate
TextViewMessage
I'm able to make one blog's post(a group of text view) above the other.
I just can position them, like this, from eachother. they stuck one above the other.
I also trued with addRules the .ABOVE and others but it's even worst cause they stuck all on the top left corner
You can add Rules to your textviews like this
tv1 = new TextView(this);
tv1.setId((int)System.currentTimeMillis());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.BELOW, recent.getId());
tv1.setText("Time: "+System.currentTimeMillis());
rr.addView(tv1, lp);
Egs.
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
lp.addRule(RelativeLayout.BELOW, someOtherView.getId())
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT)

how to set the width and height of ImageView and TextView here?

here are my text and image and text views(i1, t1, t2). how can i format the width and height of these views dynamically in my class file.
ImageView i1 = new ImageView(this);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
RelativeLayout rl1 = new RelativeLayout(this);
RelativeLayout.LayoutParams innerLP1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
innerLP1.addRule(RelativeLayout.CENTER_IN_PARENT);
innerLP1.topMargin=(30);
t1.setText(name);
rl1.setLayoutParams(innerLP1);
rl1.addView(t1);
set layout parameters to those views
RelativeLayout.LayoutParams view_params = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imageview.setLayoutParams(view_params );
textview.setLayoutParams(view_params );
Use the txt.SetHeight() method.
Use setHeight() & setWidth() methods. For example:
ImageView i1 = new ImageView(this);
i1.setHeight(xxx);
i1.setWidth(xxx);

Categories

Resources