I have created dynamic TextViews array list added to FlowLayout. Now I have to add one EditText at left of the each TextView when click on ImageView.
Code :
FlowLayout.LayoutParams params = new FlowLayout.LayoutParams(
FlowLayout.LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
mFlowLayout.removeAllViews();
View[] view = new View[2];
for (int i = 0; i < 2; i++) {
view[i] = LayoutInflater.from(getActivity()).inflate(R.layout.item_rich_element_edit_flow_layout
, mFlowLayout, false);
view[i].setId(i);
view[i].setTag(i);
TextView textView = view[i].findViewById(R.id.text_view_attribute_name);
ImageView imageView = view[i].findViewById(R.id.image_view_left_cursor);
imageView.setTag(i);
imageView.setOnClickListener(view1 -> addView((Integer) view1.getTag(), view1));
textView.setText(element.getElementName());
mFlowLayout.addView(view[i]);
}
Related
I want to build a dynamic table which will have TextView with some content. It could happen that the text is long, in that case, I want my text to be wrapped. But in the code given below it does not work. I tried adding layout parameters to my TextView with weight=1 does not work.
Here is my code:
table = FindViewById<TableLayout>(Resource.Id.main_table);
TableRow tr_head = new TableRow(this);
tr_head.Id = 10;
tr_head.SetBackgroundColor(Color.Gray);
TableRow.LayoutParams paramTableRow = new TableRow.LayoutParams(LayoutParams.MatchParent,
LayoutParams.WrapContent);
tr_head.LayoutParameters = paramTableRow;
LinearLayout.LayoutParams paramcell = new LinearLayout.LayoutParams(LayoutParams.WrapContent,LayoutParams.WrapContent,1.0f);
TextView label_Buyway = new TextView(this);
label_Buyway.Id=20;
label_Buyway.Text ="Buyway";
label_Buyway.SetTextColor(Color.Black);
label_Buyway.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_Buyway);
TextView label_qty = new TextView(this);
label_qty.Id=21;
label_qty.Text="Qty";
label_qty.SetTextColor(Color.Black);
label_qty.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_qty);
TextView label_price = new TextView(this);
label_price.Id = 22;
label_price.Text = "Price";
label_price.SetTextColor(Color.Black);
label_price.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_price);
TextView label_total = new TextView(this);
label_total.Id = 23;
label_total.Text = "Total";
label_total.SetTextColor(Color.Black);
label_total.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_total);
table.AddView(tr_head, new TableLayout.LayoutParams(
LayoutParams.MatchParent,
LayoutParams.WrapContent));
for (int i = 0; i < 10; i++)
{
string date = DateTime.Now.ToString();
Double weight_kg = 1.1;
TableRow tr = new TableRow(this);
tr.Id=(100 + i);
tr.LayoutParameters = paramTableRow;
TextView labelBuyway = new TextView(this);
labelBuyway.Id=(200 + i);
labelBuyway.Text =date;
labelBuyway.SetTextColor(Color.Black);
labelBuyway.SetBackgroundResource(Resource.Drawable.table_row);
labelBuyway.LayoutParameters = paramcell;
tr.AddView(labelBuyway);
TextView labelqty = new TextView(this);
labelqty.Id=(200 + i);
labelqty.Text=(weight_kg.ToString());
labelqty.SetTextColor(Color.Black);
labelqty.SetBackgroundResource(Resource.Drawable.table_row);
tr.AddView(labelqty);
TextView labelPrice = new TextView(this);
labelPrice.Id = (200 + i);
labelPrice.Text = (weight_kg.ToString());
labelPrice.SetTextColor(Color.Black);
labelPrice.SetBackgroundResource(Resource.Drawable.table_row);
tr.AddView(labelPrice);
TextView labelTotal = new TextView(this);
labelTotal.Id = (200 + i);
labelTotal.Text = (weight_kg.ToString());
labelTotal.SetTextColor(Color.Black);
labelTotal.SetBackgroundResource(Resource.Drawable.table_row);
tr.AddView(labelTotal);
table.AddView(tr, new TableLayout.LayoutParams(
LayoutParams.MatchParent,
LayoutParams.WrapContent));
i++;
}
Right now the text gets cut and does not wrap. Any help would be appreciated.
What I guess is these three lines would do the trick :
textViewName.HorizontalScrollBarEnabled = false;
textViewName.SetMinLines(2);
textViewName.InputType = Android.Text.InputTypes.TextFlagMultiLine;
with the weight property set to 1 and these three properties, it should work.
Please revert whether it works.
give your textviews with weight=1 and Wrap content. SO it will expand according to the text size.
Hi i want to create a list of textview with each item clickable like in the image.
In your onClick or in the Event you want to add a TextView try this:
TextView textView = new TextView(Activity.this);
textView.setText();
textView.setTextColor();
textView.setTextSize();
textView.setOnClickListener();
Now suppose you have a horizontal LinearLayout then
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Set your margins here.
params.topMargin = 0;
params.rightMargin = 0;
params.leftMargin = 0;
params.bottomMargin = 0;
textView.setLayoutParams(params);
linearLayout.addView(textView);
Hope this helps.
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.
I have a ScrollView and I want to insert a user specified number of HorizontalScrollViews. So what user says he wants to have a matrix of 5x5 elements, I want to insert 5 HorizontalScrollViews with 5 EditText objects each. My program adds the first line just as it's supposed to, but the rest not.
for (int i = 0; i < number; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(par2);
HorizontalScrollView row = new HorizontalScrollView(this);
row.setLayoutParams(par1);
row.addView(ll);
for (int j = 0; j < number; j++) {
EditText txt = new EditText(this);
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
txt.setHint(i+","+j);
ll.addView(txt);
}
latout_in_scrollview.addView(row);
}
Any ideas why? Thanks!
EDIT:
The 1:1 code im using
LinearLayout dijkstra_rows;
FrameLayout.LayoutParams par1 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams par2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_dijkstra);
dijkstra_rows = (LinearLayout) findViewById(R.id.dijkstra_rows);
Bundle extras = getIntent().getExtras();
number = extras.getInt("vertexes");
for (int i = 0; i < number; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(par2);
HorizontalScrollView row = new HorizontalScrollView(this);
row.setLayoutParams(par1);
row.addView(ll);
for (int j = 0; j < number; j++) {
EditText txt = new EditText(this);
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
txt.setHint(i+","+j);
ll.addView(txt);
}
dijkstra_rows.addView(row);
}
}
ScrollView can contain only one childView. You can put any layout as per your requirement. I generally use Relative Layout...
Then add views dynamically to relative layout
viewLayout = (ViewGroup) mView.findViewById(R.id.YOUR_RELATIVE_LAYOUT_ID);
View lastCard = viewLayout.getChildAt(viewLayout.getChildCount() - 1);
// INFLATE YOUR NEW VIEW YOU WANT TO ADD
CardView cardView = (CardView)
LayoutInflater.from(getContext()).inflate(R.layout.card_nearest_stop, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Set id to view
int id = 125;
if (lastCard != null) {
params.addRule(RelativeLayout.BELOW, lastCard.getId());
id = lastCard.getId() + 125;
}
cardView.setLayoutParams(params);
cardView.setId(id);
viewLayout.addView(cardView);
ScrollView is a single element container.
A ScrollView is a FrameLayout, meaning you should place one child in
it containing the entire contents to scroll; this child may itself be
a layout manager with a complex hierarchy of objects. A child that is
often used is a LinearLayout in a vertical orientation, presenting a
vertical array of top-level items that the user can scroll through.
You are adding multiple LinearLayouts here
for (int i = 0; i < number; i++) {
LinearLayout ll = new LinearLayout(this);
.
.
}
You should have only one out of this loop. Then add this one to your scrollView, in Loop you can add muliple HorizontolScrollViews to this LinearLayout
I have putting such data into LinearView. as like below code:
public void showResult()
{
//db.open();
//c2 = db.getAllTitles(table_name);
List<TextView> textListWord = new ArrayList<TextView>(tempEmployerList.size());
List<TextView> textListAnswer = new ArrayList<TextView>(tempEmployerList.size());
List<TextView> imageListAnswer = new ArrayList<TextView>(tempEmployerList.size());
//for(int i = 0; i < c.getCount(); i++)
//int limit = tempEmployerList.size();
for(int i = 0; i<=tempEmployerList.size()-1; i++)
{
RelativeLayout innerLayout = new RelativeLayout(this);
innerLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
innerLayout.setBackgroundColor(0x00000000);
// set the Multiple TextView
TextView qWordTV = new TextView(getApplicationContext());
TextView aWordTV = new TextView(getApplicationContext());
TextView aImageView = new TextView(getApplicationContext());
qWordTV.setText("\n"+"1");
qWordTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
qWordTV.setTextColor(0xFFFF0000);
qWordTV.setPadding(3, 0, 0, 0);
aWordTV.setText("\n"+"2");
aWordTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
aWordTV.setGravity(Gravity.CENTER);
aWordTV.setTextColor(0xFFFF0000);
aWordTV.setGravity(Gravity.CENTER);
aWordTV.setPadding(40, 0, 0, 0);
aWordTV.setTextColor(0xFFFF0000);
aImageView.setText("\n"+"3");
aImageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
aImageView.setGravity(Gravity.RIGHT);
aImageView.setTextColor(0xFF000000);
aImageView.setPadding(0, 0, 9, 0);
/**** Any other text view setup code ****/
innerLayout.addView(qWordTV);
innerLayout.addView(aWordTV);
innerLayout.addView(aImageView);
myLinearLayout.addView(innerLayout);
textListWord.add(qWordTV);
textListAnswer.add(aWordTV);
imageListAnswer.add(aImageView);
}
//while (limit==0);
}
Now i want to create the Simple View dynamicaly here. . .
As like:
<View android:layout_height="2dip" android:background="#FF000000" />
in xml.
Now how to make such View like line dynamicaly ?
Please help me in this.
Thanks.
View line = new View(getApplicationContext());
line.setBackgroundColor(0xFF000000);
// You may need to convert dip to px here, raw pixels are used in the example:
LayoutParams layoutParams = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, 2);
line.setLayoutParams(layoutParams);