issue with clear content in RelativeLayout - android

I am facing one issue for updating TextView content in RelativeLayout. When I update in the second time, I need to clear the previous text from RelativeLayout.
I have one RelativeLayout in my app activity_calendar.xml
<RelativeLayout
android:id="#+id/rel_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/linearLayout1"
android:layout_toRightOf="#+id/linearLayout1">
</RelativeLayout>
This is the Main Code :
public void listall () {
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rel_container);
int prevTextViewId = 0;
for (int i = 0; i < titleCalendar.length; i++) {
final TextView textView = new TextView(this);
textView.setText(titleCalendar[i]);
cnvrttime = hourCalendar[i] - 3;
addcnvrtTime = 60*cnvrttime;
curTextViewId = prevTextViewId + 1;
textView.setId(curTextViewId);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, addcnvrtTime);
params.setMargins(0, convertDPtoInt((float)addcnvrtTime), 0, 0);
textView.setLayoutParams(params);
prevTextViewId = curTextViewId;
relativeLayout.addView(textView, params);
}

You need to give the TextView an id so you can find it later:
textView.setId(1234);
Then later:
TextView textView = findViewById(1234);
textView.setText("New Text");

If there aren't any child Views inside the relative layout that you want to keep, call relativeLayout.removeAllViews() before your for loop. Otherwise, use removeView(View) or removeViewAt(int) to remove the ones you don't want.

Related

How to add textview dynamically with clickable each item like in image?

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.

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 dynamic array of imageviews in relative layout 3 per row

Well, I would like to seek your help regarding how to show imageviews dynamically 3 per row in relative layout. What I tried so far is I am able to show imageviews one below other because of which I can add maximum 10 images in screen.
Instead I want to add 3 images per row.
Here is my source code:
int prevTextViewId = 0;
for(int i = 0; i < 1; i++)
{
{
final TextView textView = new TextView(context);
textView.setText(savedListCont[K]);
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(null, bd,null , null);
textView.setPadding(0, 20, 0, 20);
textView.setTextColor(Color.WHITE);
textView.setMaxLines(1);
textView.setEllipsize(TextUtils.TruncateAt.MIDDLE);
int curTextViewId = prevTextViewId + 1;
textView.setId(curTextViewId);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, prevTextViewId);
textView.setLayoutParams(params);
prevTextViewId = curTextViewId;
ll.addView(textView, params);
Actually, I can use gridview or gridlayout for same but these both doesn't suit my project requirement.
My layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/home_one"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
Thanks & Regards,
Aditya.J

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"

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)

Categories

Resources