Hi I am really new to Android development, I am just developing an app the takes(In EDIT-TEXT) input(integers) converts to centimetersand displays as normal text(In Text-view). But what ever i entered into Edit-Text I am unable to retrieve it i am posting my xml and java code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<EditText
android:id="#+id/edittext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="#string/Hint1"
android:inputType="number" />
<Button
android:id="#+id/Convert"
android:text="#string/Convert"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="30sp"
/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Result"
android:textSize="30sp"
/>
</LinearLayout>
*Java-code:*
public class MainActivity extends Activity
{
TextView textView1 = null;
EditText edittext1 = null;
int no2 = 0;
int centimeters1 = 0;
String text1 ="ssssss";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext1 = (EditText) findViewById(R.id.edittext1);
textView1 = (TextView) findViewById(R.id.textView1);
text1 = edittext1.getEditableText().toString().trim();
System.out.print(text1);
System.out.print(".................................................");
centimeters1= no2* 24 ;
Button button1 = (Button) findViewById(R.id.Convert);
button1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView1.setText("Your Total is:" + text1);
}
});
}
I tried to use "system.out.println" so that I can trace the values but it didnt work(in emulator when I run this after entering values it just displays "Your Total Is:" and nothing else).Thanks in advance, please help me with this.
Your code is correct, but displaced.
You get nothing from the EditText because you're examining its contents right after it's created. The function is called onCreate for a reason :)
Move this:
text1 = edittext1.getEditableText().toString().trim();
Here:
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text1 = edittext1.getEditableText().toString().trim();
textView1.setText("Your Total is:" + text1);
}
});
This way, you pick up the text at the moment of pushing the button, and not on creation, when it's empty.
Move text1 = edittext1.getEditableText().toString().trim(); into your onClick() method instead.
Currently you assign text1 right after your layout is loaded. At this point, your EditText is empty, so it also gets an empty value. Putting it in the onClick() method will make sure it gets a value only when you click the Button.
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text1 = edittext1.getEditableText().toString().trim();
textView1.setText("Your Total is:" + text1);
}
});
text1 = edittext1.getEditableText().toString().trim();
Write these code inside OnClickListener , you get it.
You are getting edittext after setContentView(). Move your text1 = edittext1.getEditableText().toString().trim(); to button's onclick method. So when user types in editext clicks on the button the values is set to textview.
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text1 = edittext1.getEditableText().toString().trim();
textView1.setText("Your Total is:" + text1);
}
});
just change this line:
textView1.setText("Your Total is:" + edittext1.getText().toString());
Try this one and edit text values always be in string.if you want assign that edit text value to integer means you have covert string to integer(just a info to you)..here the answer for your question..
String str = edittext.gettext.tostring;
Textview1.settext(''your total is''+str);
Related
I've coded professionally for many many years in other languages, but I'm pretty new at Android and I'm learning a lot from sites like stack overflow.
After hours of searching I'm struggling with trying to get a reference to an EditText in a Button click method. Both the EditText and Button are part of the same Activity.
Here's a cut down part of my activity_my.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/my_layout"
tools:context=".MyActivity" >
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:singleLine="true"
android:id="#+id/name" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:text="#string/save"
android:id="#+id/save" />
</RelativeLayout>
And a cut down part of my MyActivity.java:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutInflater().inflate(R.layout.activity_my, null));
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
saveClick(view);
}
});
}
public void saveClick(View view)
{
EditText editText = (EditText) view.findViewById(R.id.name); // this is always null
if (editText != null)
{
String name = editText.getText().toString();
}
}
}
I think it's because I'm trying to use the findViewById method on the view parameter. I remember reading somewhere that the parameter to a button click event is the Button itself, not the layout. If that's correct how do I get a reference to the layout in order to use the findViewById method? Or is there something else wrong?
I've seen examples where the onClick event wraps an anonymous method where you can successfully use the findViewById because (I think) you are essentially nested in the onCreate event. However, I'd like to keep my button click event as a separate (non-anonymous) method.
Thanks for any help in advance.
Change this
EditText editText = (EditText) view.findViewById(R.id.name);
to
EditText editText = (EditText)findViewById(R.id.name);
and you can simply use
setContentView(R.layout.activity_my);
directly.
I guess EditText belongs to activity_my.xml
Also there is no need to initialize edittext everytime on button click
EditText editText;// declare as instance variable
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
editText = (EditText) view.findViewById(R.id.name)
public void saveClick(View view)
{
EditText editText = (EditText) view.findViewById(R.id.name); // this is always null
if (editText != null)
{
String name = editText.getText().toString();
}
}
that returns a valid View (an object != null) only if the EditText is inside the Button. You should look for the EditText in the View's hierarchy
use this
EditText editText = (EditText) findViewById(R.id.name);
The View.findViewById() method looks for child views.
In your case, the findViewById call is in the onClickListener of your edittext view. This view does not have a child, it is simple an edittext. That's why you're getting null.
I have an edittext form that can get filled, I want to clear it upon an onClick of a text, so I put an onClick that goes to my clear function in main.
I however, dont know how to properly target the edittext that I want. I want to clear TWO of them.
public void clear(View v) {
#+id/toptext.setText("");
}
This is the XML for that specific text.
<TextView
android:id="#+id/toptext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#303030"
android:text="#string/toptext"
android:textAppearance="?
android:attr/textAppearanceLarge"
android:textColor="#33B5E5"
android:textSize="50sp"
android:onClick="clear" />
Create instances of your EditTexts, using the id of the EditTexts in your xml layout. Then use setText on them and make them blank.
public void clear(View v) {
EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
et.setText("");
et2.setText("");
}
Edit for some reason the method above didn't work. This was the end solution:
// Put one of these in your onCreate method:
TextView tt = (TextView)findViewById(R.id.toptext);
tt.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
et.setText("");
et2.setText("");
return true;
}
});
// or
TextView tt = (TextView)findViewById(R.id.toptext);
tt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
et.setText("");
et2.setText("");
}
});
Try this:
TextView tv = (TextView) findViewById(R.id.toptext);
tv.setText("Whatever you want!");
You can use this code:
public void clear(View v) {
if (v.getId() == R.id.toptext) {
((EditText) v).setText(null);
}
}
Similarly, add another if condition for another EditText.
First, get reference to your TextView by calling findViewById:
TextView textView = (TextView) findViewById(R.id.toptext);
Then use it to clear:
textView.setText("");
I think instead of setting "", u should use null as.
TextView textView = (TextView) findViewById(R.id.toptext);
textView.setText(null);
Use the param passed to the onClick() (v)
public void clear(View v)
{
((TextView)v).setText("");
}
Assuming you want to clear the text in the TextView that you have your onClick attached to. The View passed into clear() is the View that was clicked so you just cast it to the appropriate View (TextView) then you can use it as normal.
So this will clear the text on whichever TextView was clicked.
I want to increment the value in textview on button click event i hv written the code for the same but when i press +Button it increments the textview below it not on same textview.
there r two buttons + and -
on +button value should increment and
on -Button value should decremnt of textview in listview of android
here is my sample code ...
this my xml file
<TextView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/minus"
android:layout_alignBottom="#+id/minus"
android:layout_toLeftOf="#+id/qty"
android:onClick="onClick"
android:text="+"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="20dp"
android:textStyle="bold" />
and the code in adapter is below
text5.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
String qty=text4.getText().toString();
System.out.println("orginal string :"+qty);
int mn=Integer.parseInt(qty);
System.out.println("integer format : "+mn);
int qty1=mn+1;
System.out.println("after increment :"+qty1);
String s = new Integer(qty1).toString();
System.out.println("final string :"+s);
text4.setText(""+qty1);
App.qty=s;
}
});
text6.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
String qty=text4.getText().toString();
int mn=Integer.parseInt(qty);
int qty1=mn--;
text4.setText(""+qty1);
String s = new Integer(qty1).toString();
text4.setText(s);
App.qty=s;
}
});
Any help will be appreciated Thanks.
i used arraylist instead of string array ..that sloved my problem
Hi all I am new to android development, infact its my first application. I want to know
<button
android:text="1" />
in above tag, text is the value of the button? if yes then how i can get this value or store it in a variable. if not, then how to define a value behind any button in android?
yes its a value of Button , use following code to fetch Text of Button.
Button b = (Button)findViewById(R.id.button1);
String buttonText = b.getText().toString();
function call
b.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
function();
}
});
You first need to give the button an id, like so:
<Button
android:id="#+id/buttonId"
android:text="1"
/>
And then in your code do something like:
Button b = (Button)findViewById(R.id.buttonId);
b.getText(); // returns the value of your text.
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ButtonText" />
...
#Override
public void onCreate(Bundle savedInstanceState) {
Button btn = (Button) findViewById(R.id.button1);
String text = btn.getText().toString();
}
I am having EditText and using the numbers inside the EditText for my variables.
I am also having two buttons, one to increase the number in EditText by one and another to decrease it by one.
Can somebody tell me the code to make this possible?
Use the following line in the xml of EditText for setting the input type as number :
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="200"
android:id="#+id/editText1"
android:layout_alignParentLeft="true">
</EditText>
And in source file use the following code to decrease the number by 1:
final EditText ed=(EditText)findViewById(R.id.editText1);
Button b1=(Button)findViewById(R.id.button01);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int a=Integer.parseInt(ed.getText().toString());
int b=a-1;
ed.setText(new Integer(b).toString());
}
});
Similarly add one more button in the xml to increase the number by one.
You need to create a on click listener for each button, doing something like:
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText)findViewById(R.id.edit_text);
int newVal = ... //retrieve the previous val and increment it (or decrement it)
editText.setText(newVal, TextView.BufferType.EDITABLE);
}
});
Simply you have to use the click event of both the buttons, on increase button get the text in edittextbox and increment it and set it in the edittextbox, same way do for decrement button also and decrement the value.
button.setOnClickListener(new OnCLickListener(){
#Override
public void onClick(View arg0) {
if(arg0.equals(button1))
{
String s = editText.getText().toString();
Integer i = Integer.parseInt(s);
i=++i;
s = s.valueOf(i);
editText.setText(s);
}
if(arg0.equals(button2))
{
//decrement
}
}
})