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);
}
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
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);
}
I have created edittext and textview without using xml attributes i have defined in java file only but i am unable get values entered in edittext how to get values.i have 2 edittexts if user enters the values i should add the values
public class JavacodeActivity extends Activity {
LinearLayout layout;
TextView view;
EditText edit;
Button btn;
EditText edit1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout=new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
layout.setOrientation(LinearLayout.VERTICAL);
view=new TextView(this);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
view.setText("enter the value");
view.setGravity(Gravity.CENTER);
view.setTextColor(Color.BLUE);
view.setTextSize(20);
layout.addView(view);
edit1=new EditText(this);
edit1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
edit1.setHint("enter the number");
edit1.setGravity(Gravity.CENTER);
edit1.setTextColor(Color.RED);
edit1.setTextSize(20);
edit1.setInputType(InputType.TYPE_CLASS_NUMBER);
layout.addView(edit1);
btn=new Button(this);
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
btn.setText("add");
btn.setGravity(Gravity.CENTER_HORIZONTAL);
btn.setTextColor(Color.GREEN);
btn.setTextSize(20);
layout.addView(btn);
view=new TextView(this);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
view.setText("addition of values");
view.setGravity(Gravity.CENTER);
view.setTextColor(Color.BLUE);
view.setTextSize(20);
layout.addView(view);
I don't know the attribute to retrieve values from edittext if the code is java I am unable to retrieve values. Please help me.
I don't know the attribute to retrieve values from edittext if the code is java I am unable to retrieve values
Read your EditText like any other EditText:
String text = edit.getText().toString();
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);
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();
}
};