I am making an example of dynamic textview, that are created by pressing a button. the format in which they are created is similar to:
1- Textview (fist created)
2- TextView
3- TextView (last created)
I wonder if we can create them in an ascending order, one above the previous TextView.
something like this:
3- TextView (last created)
2- TextView
1- TextView (first created)
this is my example code:
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
mLayout.addView(createNewTextView("TextView"));
count = count +1;
}
};
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(count+ "- " + text);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
return textView;
}}
You can specify the index where TextView has to be added in ViewGroup...
ViewGroup.addView(TextView, 0);
each time you are creating a TextView and adding to mLayout, add with the index 0 which will add on top of previous TextView
mLayout.addView(createNewTextView("TextView"),0);
You could create a LinearLayout and add one below the other, using layout.addView(TextView). I've a psedo code below which explains how it is done.
for(Option option : options){
view = View.inflate(mContext,R.layout.textView,null);
((TextView)view.findViewById(R.id.txt_key)).setText(" SOME TEXT : ");
((TextView)view.findViewById(R.id.txt_value)).setText("SOME VALUE ");
mLlCtr.addView(view);
}
Related
public TextView descriptionTextView(Context context, String text) {
final ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final TextView textView = new EditText(context);
textView.setLayoutParams(lparams);
textView.setTextSize(10);
textView.setTextColor(Color.rgb(0,0,0));
textView.setText(" " + text + "");
textView.setMaxEms(8);
textView.setKeyListener(null);
textView.setFocusableInTouchMode(false);
textView.setEnabled(false);
return textView;
}
Here is the code that I have written for the TextView.
I would like to reference it from another classes, or within the same class, but I cannot find a way to pin it down; as in I would like to change the value based on an input.
Give TextView a id and then access the textView via id like below -
give id like textView.setId(1);
public TextView descriptionTextView(Context context, String text) {
final ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final TextView textView = new EditText(context);
textView.setLayoutParams(lparams);
textView.setTextSize(10);
textView.setId(1);
textView.setTextColor(Color.rgb(0,0,0));
textView.setText(" " + text + "");
textView.setMaxEms(8);
textView.setKeyListener(null);
textView.setFocusableInTouchMode(false);
textView.setEnabled(false);
return textView;
}
let say your layout for textView is LinearLayout.
you can get textView by id by using below code -
TextView tv = (TextView)view.findViewById(1); // view is LinearLayout object
Hope this helps!
This is the code to create TextView Dynamically
LinearLayout layout = findViewById(R.id.linearLayout);
TextView view = descriptionTextView(this, "Sample text");
layout.addView(view);
Below method for calling it from same/different class:
public TextView descriptionTextView(Context context, String text){
ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView (context); //Change this to TextView
textView.setLayoutParams(lparams);
textView.setTextSize(10);
textView.setTextColor(Color.rgb(0,0,0));
textView.setText(" " + text + "");
textView.setMaxEms(8);
textView.setKeyListener(null);
textView.setFocusableInTouchMode(false);
textView.setEnabled(false);
return textView;
}
Creating Views other them XML file is not a preferred method in Android.
The best approach is to make this TextView in XML file and link it using its id.
like this.
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
/>
Futher use this anywhere in you classes and activity by creating an object like this.
TextView textView = findViewById(R.id.textView);
//to change value use this
textView.setText("Value you want to set");
//to change its visibility
textView.setVisibility(textView.GONE);
//to get its string
String value = textView.getText().toString();
If this method is not helpful and you still want to make views in Java file then, make them public and out of function boundaries to make them accessible everywhere.
How can we create a button with a command to create an other object on Android ?
i mean, i press the button on the app and it create in the layout, a new object, for exemple a textView.
how could i proceed ?
thanks !
This is a way:
First you defined into your xml file a layout parent for your TextView and get it in your code:
final LinearLayout parentView = (LinearLayout) findViewById(R.id.parent_view_id);
Then you define create your TextView for example:
final TextView textView = new TextView(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT
); // set height and width
textView.setMargins(left, top, right, bottom); // set margin if necessary
textView.setLayoutParams(layoutParams);
and in your listener :
myButton.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View view) {
parentView.addView(textView);
}
});
Hope this helps.
Sorry for my english.
first of all create a click listener on your button
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
and then this with add a new textview into your target layout
TextView tv = new TextView(this);
tv.setText("hello word!");
tv.setTextSize(18);
tv.setTextColor(Color.BLACK);
tv.setClickable(true);
tv.setPadding(0, 10, 0, 0);
tv.setGravity(Gravity.CENTER);
mainLayout.addView(tv);
in this ex mainLayout is the view that you want to add new object like textview, ....
public void Add_text() {
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView product = new TextView(getActivity());
product.setText(" Product" + 5 + " ");
ll.addView(product);
EditText qty = new EditText(getActivity());
qty.setText(i + "");
qty.setId(i);
qty.setWidth(120);
ll.addView(qty);
Button btn = new Button(getActivity());
ll.addView(btn);
btn.setLayoutParams(params);
btn.setOnClickListener(o);
ly.addView(ll);
i++;
}
I wrote the above code to create the textfields and buttons dynamically; But now I need to remove 2 textfields and a button when the button is clicked. How do I do that?
Try following code.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout linearParent = (LinearLayout) v.getParent().getParent();
LinearLayout linearChild = (LinearLayout) v.getParent();
linearParent.removeView(linearChild);
}
});
Explanation
Here first take "GrandParent" of any view.
Then take its "Parent" view
With reference to "GrandParent" remove that "Parent" view.
this will remove all views which that "Parent" holds. As per your code, your "ll" will be "linearChild" here. And "ly" will be "linearParent" here. So whole "ll" will be removed from "ly" which you have added dynamically.
If you want to permanently remove the views you created.
OnClick(View view){
ly.removeAllViews()
}
If you do not want to permanently remove the views you created.
OnClick(View view){
ly.setVisibility(View.GONE); //This will hide the all views
qty.setVisibility(View.GONE);//This will hide the EditText qty
product .setVisibility(View.GONE);//This will hide the TextView product
}
So use appropriate code line which you want.
EDIT:
Use this code for your situation:
public void Add_text() {
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView product = new TextView(getActivity());
product.setText(" Product" + 5 + " ");
ll.addView(product);
EditText qty = new EditText(getActivity());
qty.setText(i + "");
qty.setId(i);
qty.setWidth(120);
ll.addView(qty);
Button btn = new Button(this);
ll.addView(btn);
btn.setLayoutParams(params);
ly.addView(ll);
i++;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View button) {
qty.setVisibility(View.GONE);//This will hide the EditText qty
product .setVisibility(View.GONE);//This will hide the TextView product
}
});
}
i think you got to use this method on your LinearLayout :
public void removeView (View view)
first you call :
EditText et = (EditText)linearLayout.findViewById(yourEditText.getId());
then call the remove view method :
linearLayout.removeView (et) ;
and to remove all of the Views that are in the LinearLayout do the following :
public void removeAllViews ()
like the following :
linearLayout.removeAllViews()
and give me some feedback
Hope that Helps .
you can simply use qty.setVisibility(View.GONE) on the onClickListener() of the Button of your choice. like this.
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
qty.setVisibility(View.GONE); //for temporary purpose
//or u can also do this
layout.removeView(qty); //removes permanently
}
});
The benefit of using View.GONE is that you can get the View back if you want but layout.removeView(qty) will remove the view permanently and you have to re add the view again.
[EDIT] 1. changed to View.GONE instead of View.INVISIBLE because of reasons explained here
Hope I answered your question. :)
just use index for which you want to remove your view from linear layout
Linearlayout.removeViewAt();
if you want again that view then you can call addViewAt()in same way.
I hope it will help you.
Add this line
mLayout.removeViewAt(mLayout.getChildCount()-1);
This is my Add button code which will get the text from AutoCompleteTextView and list into ListView on each click of Add button. I am so confusing about getting text from all TextView which created by user.
Because I need to compare the user inputs in all TextViewwith symptoms column in database to diagnose the disease. Hope you guys can help me =)
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
mLayout.addView(createNewTextView(mEditText.getText().toString()));
}
};
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
//textView.setText("Symptom: " + text);
textView.setText(text);
return textView;
}
You can maintain a ArrayList where you can store all the getText() if all the text views.
Just define and initialize your arraylist before you use it.
Initialize like below
ArrayList<String> arrayList = new ArrayList<String>();
And in createNewTextView() method you can have the below line to add the names to arraylist
arrayList.add(text)
later you can use these arraylist for reference and get all the text entered by user
Here is my code for how i make adding Dynamically TextView in to Table Layout using LinearLayout for it.
table_personalData.removeAllViews();
int i=0;
for(String header : headerDetail){
TextView label=new TextView(this);
TextView value=new TextView(this);
label.setTextAppearance(this, R.style.TextBase);
value.setTextAppearance(this, R.style.TextBase);
label.setText(String.format("%s:", header));
value.setText(String.format("%s", lead.data[i]==null?"":lead.data[i]));
i++;
LinearLayout rowview=new LinearLayout(this);
rowview.setOrientation(0);
rowview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rowview.addView(label);
rowview.addView(value);
table_personalData.addView(rowview);
}
By Above code i get my table layout with dynamically added textview in that .
here i use Liner Layout for added two textview in One Row .
headerDetail is a ArrayList<String> headerDetail
lead.data[i] i used this to get my values for particular label.
All this work fine for me now what my problem is
Now i want to do one thing that is when we click one of the TextView of my LinearLayout i want that TextView in dynamically.
So finally my question is about to get retrieve textview value when we touch or click any textview which is in Linearlayout and LinearLayout is in TableLayout.
Anyone have idea what should i have to do with this .
i also try something like below but i don't get i can i get TextView Value from below code.
table_personalData.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<table_personalData.getChildCount();i++){
//CharSequence desc=table_personalData.getChildAt(i).getContentDescription();
//Log.v("log_tag", "the values are"+ desc);
}
}
});
EDIT:
if you want the Value of TextView when we Touch on that There is Two way that i write Below :
value.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView txt;
txt=(TextView)findViewById(v.getId());
String tempEmail;
tempEmail=txt.getText().toString().trim();
String aEmailList[]={tempEmail};
pattern=Pattern.compile(EMAIL_PATTERN);
if(validate(tempEmail)){
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,aEmailList );
startActivity(emailIntent);
}else{
pattern=Pattern.compile(PHONE_No_PATTERN);
if(validate(tempEmail)){
tempEmail=tempEmail.replaceAll("[^\\d]", "");
String uri="tel:" +tempEmail;
Intent mIntent=new Intent(Intent.ACTION_CALL);
mIntent.setData(Uri.parse(uri));
startActivity(mIntent);
}else{
//Toast.makeText(LeadDetailActivity.this, "Please Touch on Email or Mo:Number", Toast.LENGTH_SHORT).show();
}
}
}
});
1)Create how many text view u want,but set id for each one,and aslo set same click listener for all
TextView value1=new TextView(this);
value1.setid(1);
value1.setOnClickListener(clickListener);
TextView value2=new TextView(this);
value1.setid(2);
value2.setOnClickListener(clickListener);
TextView value3=new TextView(this);
value3.setid(3);
value3.setOnClickListener(clickListener);
2)second create one onclick listener,inside this listener u will get all cliks. so based on view id u can dynamically do anything
OnClickListener clickListener=new OnClickListener() {
public void onClick(View v) {
int id=v.getId();
}
};