Sorry by my english.. this is google traductor
I create (java) for example 10 LLseccion.
Each LLseccionhas multiple objects inside (imageview and 2 textview)
i want read the first textview of a layout clicked
LinearLayout LLseccion = new LinearLayout(getApplicationContext());
LLseccion.setOrientation(LinearLayout.HORIZONTAL);
LLseccion.setId(i);
LLseccion.setClickable(true);
LLseccion.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
TextView tvBIG=new TextView(getApplicationContext());
tvBIG.setText("i want read this");
tvBIG.setTypeface(FontNormal);
tvBIG.setTextSize(bigSize);
tvBIG.setTextColor(getResources().getColor(R.color.GREEN));
tvBIG.setSingleLine(true);
LLseccion.addView(tvBIG);
TextView tvSMALL=new TextView(getApplicationContext());
tvSMALL.setText("other text");
tvSMALL.setTypeface(FontNormal);
tvSMALL.setTextSize(smallSize);
tvSMALL.setTextColor(getResources().getColor(R.color.GREEN));
tvSMALL.setPadding(30, 0, 0, 0);
tvSMALL.setSingleLine(true);
LLseccion.addView(tvSMALL);
Related
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, ....
I have this FlowLayout where I have a set of TextView's which I build programatically. After getting the wanted names, I create a TextView for each name inside the layout.
What I want to do, if I click on the TextView, I want to move it into another layout. I manage to do that but I also want to move it back. I could also do that until I program it to, but I can't program it to be like a infinite loop.
This is a piece of code which will make you understand better what I'm talking about hopefully.
TextView tv = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tv.setText("Test");
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tvSelected = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tvSelected.setText(tv.getText().toString());
tvSelected.setLayoutParams(params);
tv.setVisibility(View.GONE);
filteredLayout.addView(tvSelected);
}
});
unfilteredLayout.addView(tv);
Is it possible to make it work? Thanks.
LE: As you can see in the onClickListener event of the TextView, I create the other TextView I add in the other layout, to move it back I could also add an onClickListener event to this TextView but this is not the solution.
Try following:
boolean isInFilterLayout = false; //Class variable
TextView tv = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tv.setText("Test");
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isInFilterLayout){
filteredLayout.remove(tv);
unfilteredLayout.addView(tv);
isInFilterLayout = false;
}else{
unfilteredLayout.remove(tv);
filteredLayout.addView(tv);
isInFilterLayout = true;
}
}
});
unfilteredLayout.addView(tv);
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);
My android app is populating a table layout which is created programmatically from the sqlite database.the last column of the table layout has two buttons. those two buttons are assigned to one column by a linear layout.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
35, 35);
params.setMargins(10, 0, 10, 0);
seventhCol1=new Button(this);
seventhCol1.setText("A");
seventhCol1.setLayoutParams(params);
//seventhCol1.setLayoutParams(new LayoutParams(30, 30));
seventhCol1.setId(71);
seventhCol1.setOnClickListener(mListener);
seventhCol2=new Button(this);
seventhCol2.setText("C");
seventhCol2.setLayoutParams(params);
seventhCol2.setId(72);
seventhCol2.setOnClickListener(nListener);
btn_linear=new LinearLayout(this);
btn_linear.setOrientation(LinearLayout.HORIZONTAL);
btn_linear.addView(seventhCol1);
btn_linear.addView(seventhCol2);
btn_linear.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
btn_linear.setGravity(Gravity.CENTER);
tr.addView(btn_linear);
on click of the button seventhCol1 i want to get the data from the particular row from where that button was clicked...how can I do that????
I tried doing it by this method:
tr.setOnClickListener(rowclickListner);
private OnClickListener rowclickListner=new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TableLayout tl=(TableLayout) v.getParent();
/*TableRow tr=(TableRow) tl.getParent();*/
TableRow tr=(TableRow) tl.getParent();
TextView tv_mobile=(TextView)tr.getChildAt(2);
String mobile=tv_mobile.getText().toString();
Toast.makeText(LeadManagement.this, mobile, Toast.LENGTH_LONG).show();
/*ArrayList<String> leaddetails=mDbHelper.getTableDetails("lead_table", mobile);
prefEditor.putBoolean("leadbundle", true);
prefEditor.commit();
startActivity(new Intent(LeadManagement.this,CreateLead.class).putExtra("leadlist", leaddetails));*/
}
};
but it is showing an error!! how can i do it?
Don't register an onClickListener as a class member, but rather as an anonymous object. Something like:
Button tr = // your button corresponding to N-th row
final RowData rowData = ... // get the data from row N
tr.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
someMethodCall(rowData)
}
});
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();
}
};