how to get values from edit text without using xml code - android

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();

Related

Android dynamic layout

I want to dynamically set the layout. I get a question (String type) as shown below :
Enter any two colors -------- and ---------
So using this string I have to create the view. Here the '---------' must be an EditText and the rest of the string can be a TextView. The blanks, i.e, EditText must appear inline as it appears in the question string. I am new to Android any suggestions would be helpful.
use this class for creating your dynamic views.may be it will help you.
public class MainActivity extends Activity {
LinearLayout l1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1=(LinearLayout)findViewById(R.id.linear_layout);
LinearLayout layout=new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.HORIZONTAL);
TextView text1=new TextView(this);
text1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
text1.setText("Enter any two colors");
EditText edt1=new EditText(this);
edt1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
edt1.setHint("COLOR1");
TextView text2=new TextView(this);
text2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
text2.setText("and");
EditText edt2=new EditText(this);
edt2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
edt2.setHint("COLOR2");
layout.addView(text1);
layout.addView(edt1);
layout.addView(text2);
layout.addView(edt2);
l1.addView(layout);
}
}

Sending data to other activity and display some additional TextViews

I have an Android application in which i send data from one activity to another. I can do that part easily but the problem is i want to display some additional TextViews as well. So when i display the sent data using setContentView i can see only the sent data. I cannot see the additional TextViews. I want both of them to be displayed simultaneously.
I searched and tried hard to find a solution without any luck.I am attaching the code for reference.Thanks!
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
TextView textViewci= new TextView(this);
textViewci.setText(message);
TextView textView1= new TextView(this);
TextView textView2= new TextView(this);
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
When i use this code,i am unable to see anything and the activity remains blank.Actually i am using a TableLayout here so that i can display data in form of tables
You have to add text view on your layout.
View linearLayout = findViewById(R.id.info);
//LinearLayout layout = (LinearLayout) findViewById(R.id.info);
TextView valueTV = new TextView(this);
valueTV.setText("hallo hallo");
valueTV.setId(5);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(valueTV);
You have to add the text view in the parentview. For example, if you have used the text view in the linear layout then add the text view in the linear layout and then use the addview(linearlayout).
Below is the example
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
tv1.setId(23);
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
I have just make a test program for you. first class code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, SecondClass.class);
intent.putExtra("firstValue", "1");
intent.putExtra("secondValue", "2");
startActivity(intent);
}
});
Second class code :
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message1 = intent.getStringExtra("firstValue");
String message2 = intent.getStringExtra("secondValue");
RelativeLayout layout = new RelativeLayout(this);
TextView textView1= new TextView(this);
TextView textView2= new TextView(this);
TextView textViewci= new TextView(this);
textViewci.setText(message1+""+message2);
Log.e("intent value is", ""+message1+""+message2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, textView1.getId());
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
layout.addView(textView1);
layout.addView(textView2);
addContentView(layout, lp);
Try and let me know. YOu have to make related layouts for both classes.
Hope this will help
are you getting any error messages? if yes then please post them here. and the others are right.you need to add yours views to the parent layout.
I have found out the answer by myself.It's fairly easy and there is no need here to use views in the parent layout.I modified the textViews in my xml file and got their values using findViewById.I am attaching the updated code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
textView=(TextView)findViewById(R.id.textViewci);
textView1=(TextView)findViewById(R.id.textViewmd);
textView2=(TextView)findViewById(R.id.textViewso);
textView.setText(message);
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
tf = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
textView.setTypeface(tf);
}

how to display values from dynamically created EditText

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);

creating sms bubbles with alternate positions

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);
}

How to dynamically add edittext to android

I want to add edittext dynamically to the android display. I want to make something like in the android contacts where you can dynamically add fields and remove them if you dont need them.
Thank you for your help
See Everything dynamically
TextView tv=new TextView(this);
tv.setText("TaskID");
TextView tv1=new TextView(this);
task=new EditText(this);
task.setMaxWidth(100);
task.setMinHeight(100);
tv1.setText("Task");
id=new EditText(this);
id.setMaxHeight(10);
TextView tv2=new TextView(this);
name=new EditText(this);
name.setMaxHeight(1);
tv2.setText("Name");
LinearLayout l=new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
l.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
setContentView(l);
l.addView(tv);
l.addView(id);
l.addView(tv1);
l.addView(task);
l.addView(tv2);

Categories

Resources