final LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
scrl.addView(ll);
Button add_btn=new Button(this);
add_btn.setText("Click to add TextViiews and EditTexts");
ll.addView(add_btn);
add_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//String str;
TextView tv=(TextView)findViewById(R.id.tv1);
EditText et2=new EditText(getApplicationContext());
String s=et2.getText().toString();
tv.setText(s);
ll.addView(et2);
I have created a button when i click on this button i will get edittext dynamically if i enter a value i should display the value I don't know how to display value and make use of it if suppose i want to add entered values in dynamically created edit text.
You cannot get text if you haven't added the view.Also you add textview on click of button. You can remove textview creation from onClick() and add it outside of it
EDIT(something like this):
final LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
scrl.addView(ll);
Button add_btn=new Button(this);
add_btn.setText("Click to add TextViiews and EditTexts");
ll.addView(add_btn);
add_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String s=et2.getText().toString();
tv.setText(s);
}
});
TextView tv=(TextView)findViewById(R.id.tv1);
EditText et2=new EditText(getApplicationContext());
ll.addView(et2);
Related
I need a button that, when clicked, would add a TextView below it. Also, the TextView cannot be made invisible and appear on click in this case, it must add the TextView. Was looking for this and couldn't find anything that suits my needs.
Set a listener on the button and for each click, create a new TextView and add it to the layout.
final LinearLayout theLayout = (LinearLayout)findViewById(R.id.thelayout);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello");
tv.setTextColor(Color.BLACK);
theLayout.addView(tv);
}
});
Update: Added text color above
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);
I'm trying to add a TextView each time a button is pressed. But the thing is i want it to be added as in sms app.
The first time I click thebutton the TextView will be at the left of the screen. The next time the button is clicked the newly created TextView should be at the right side.
I tried with the following code, but it didn't work.
public void sendMessage(View view){
EditText editText=(EditText)findViewById(R.id.edit_message);
String message=editText.getText().toString();
LinearLayout layout=(LinearLayout)findViewById(R.id.layout01);
TextView text=new TextView(this);
text.setText(message);
text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
if(flag){
text.setGravity(Gravity.LEFT);
}
else
text.setGravity(Gravity.RIGHT);
flag=!flag;
layout.addView(text);
}
'flag' is of boolean type which is declared at the beginning of the class.
EDIT -
Is it possible that i create two more layouts(one for displaying TextView on left and the other to display on right). But I don't know how to use different layouts in the same screen.
Thanks for your help
public void sendMessage(View view){
EditText editText=(EditText)findViewById(R.id.edit_message);
String message=editText.getText().toString();
LinearLayout layout=(LinearLayout)findViewById(R.id.layout01);
TextView text=new TextView(this);
text.setText(message);
text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
if(flag){
text.setGravity(Gravity.LEFT);
flag=!flag;
}
else
{
text.setGravity(Gravity.RIGHT);
flag=!flag;
}
layout.addView(text);
}
public void onClick_addContact(View v)
{
LinearLayout layout = (LinearLayout) findViewById(R.id.layoutLinear);
layout.addView(linearlayout(_intMyLineCount));
_intMyLineCount++;
}
private EditText editText(int _intID) {
EditText editText = new EditText(this);
editText.setId(_intID);
editText.setHint("My lines");
editText.setWidth(180);
editTextList.add(editText);
return editText;
}
private TextView textView(int _intID)
{
TextView txtviewAll=new TextView(this);
txtviewAll.setId(_intID);
txtviewAll.setText("My lines:");
textviewList.add(txtviewAll);
return txtviewAll;
}
private Button button(int _intID)
{
Button btn = new Button(this);
btn.setId(_intID);
btn.setTag("but1");
btn.setOnClickListener(newContact);
return btn;
}
OnClickListener newContact = new OnClickListener() {
//onClick view
public void onClick(View v) {
LinearLayout layout = (LinearLayout)v.getParent();
layout.removeViewInLayout(v);
// setContentView(layout);
_intMyLineCount--;
}
};
private LinearLayout linearlayout(int _intID)
{
LinearLayout LLMain=new LinearLayout(this);
LLMain.setId(_intID);
LLMain.addView(textView(_intID));
LLMain.addView(editText(_intID));
LLMain.addView(button(_intID));
LLMain.setOrientation(LinearLayout.HORIZONTAL);
linearlayoutList.add(LLMain);
return LLMain;
}
In the newOnContact Listener, only the child button gets deleted, but not the entire linear layout that includes the textView and EditText.
How to do it?
You can use layout.setVisibility(View.INVISIBLE); or
layout.setVisibility(View.Gone); to remove the layout from UI.
View.INVISIBLE will hide the view, but it'll still take the space whereas View.GONE will remove the view as well as it won't take up any space in the UI.
Refer http://developer.android.com/reference/android/view/View.html#attr_android:visibility
Simply use GONE constant:
layout.setVisibility(LinearLayout.GONE)
then your linear layout will be invisible and it won't allocate space also.
Instead of setting the newContact onClickListener to the Button, try setting to LLMain LinearLayout. So the view that you receive in the onClick is the LinearLayout you want to remove, if I ve not misunderstood you.
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();
}
};