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();
}
Related
I'm new in android world and I have a problem, well, I'm making a project very simple it is about an activity where i have a button and an EditText. The button has an event onClick in XML.
My problem is it: I need the button value and send this value to
EditText but my button don't have a id. Help me I don't know how manipulate a element if it dont have a id.
XML Code:
<View
android:layout_height="#dimen/cell_height"
android:background="#color/red"/>
<Button
android:layout_marginLeft="#dimen/button_margin"
android:layout_gravity="center_vertical"
android:text="#string/hex_red"
android:textColor="#color/red"
android:onClick="copy"/>`
Java code:
public void copy(View boton){
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
String color = boton; <-- here need the button value
txtSelected.setText(color);
}
I need your help, Thanks
you can say boton.getText().toString()
Modify your copy() function like this:
public void copy(View boton) {
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
Button btn = (Button) boton; // << key point.
String color = btn.getText().toString();
txtSelected.setText(color);
}`
public void onClickBtn(View view) {
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
Button btn = (Button)(view);
String value = (String) btn.getText();
txtSelected.setText(value);
txtSelected.setSelection(value.length()); // cursor will be at the end of the text
}
I have a text view in which the user can enter data at run time using the custom buttons that I have created.
My delete button is able to delete one character at a time but if i hold the button then it stops.
I want the text field to get cleared when i hold the button.
Is there any solution to this....??
Please help me out.
This is my xml,
<RelativeLayout
android:id="#+id/btnClear"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
>
<ImageView
android:id="#+id/imgClear"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/img_clear" />
</RelativeLayout>
This is my code,
imgClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String getNumber;
if(isFirstNum){
getNumber = txtFirstNumber.getText().toString();
if(getNumber.length() > 0)
txtFirstNumber.setText(getNumber.substring(0, getNumber.length()-1));
} else if(!isFirstNum){
getNumber = txtSecondNumber.getText().toString();
if(getNumber.length() > 0)
txtSecondNumber.setText(getNumber.substring(0, getNumber.length()-1));
}
}
});
You can use the onLongClickListener to check if the delete button is pressed for a long time
imgClear.setOnLongClickListener(new OnLongClickListener()
{
#Override
public boolean onLongClick(View v)
{
txtFirstNumber.setText("");
return true;
}
});
This will set your txtFirstNumber to blank but the onClickListener will not be called.
Your question is not clear. But as per my understanding you want to clear the text of the text view then in your button click listener just set the text for textview to empty.
eg:
public void onClick(View v){
textview.setText("");
}
I have two overlapping webviews with in a frame layout. One contains a map and the other (on top of the map) contains the details. I want to let user to on and off the webview contains details with a toggle button. How can I do that?
First get the value of your switch button and check what is user prefer to do.
String val= (String) (switchVal.isChecked()? switchVal.getTextOn() : switchVal.getTextOff());
If he selected to be off the detail view then set not to be visible (setKeepScreenOn(boolean)) your detail webview.
For Toggle Button simply you have to use:
<ToggleButton
android:id="#+id/toggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="#string/toggle_turn_on"
android:textOff="#string/toggle_turn_off"
android:checked="true" />
Now, you have ID for toggle. Start work on MainActivity.
public void addListenerOnButton() {
ToggleButton toggleButton2 = (ToggleButton) findViewById(R.id.toggleButton2);
Button btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("\ntoggleButton2 : ").append(toggleButton2.getText());
Toast.makeText(MyAndroidAppActivity.this, result.toString(),
Toast.LENGTH_SHORT).show();
}
});
For Instance have a look there, if toogle is ON that would display like this:
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);
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
}
}
})