Change textview value in listview of android - android

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

Related

How to manipulate the contents of a button without using id; Android?

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
}

Not getting anything from EditText

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

How to get value of a pressed button

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

How to get the id of selected radio button in android?

I am working on quiz application in android. We have created Select.java page which displays the questions and options(with radio buttons) from sqlite database. Also we created a header.java file for displaying buttons i.e back and next buttons for the Select.java page.
Here we need to get the selected radio button id and need to send that to Header class. Because header class consists of the next button onclick action. Once the next button is clicked the selected radio button value has to be stored in arraylist. We created radio buttons in Select.java class. So my question is how to get the selected radio button id into that next button click action. Please help me regarding this.
Thanks in Advance.
Your layout xml file should be like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup
android:orientation="vertical"
android:id="#+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option1"
android:text="Option1"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option2"
android:text="Option2"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option3"
android:text="Option3"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option4"
android:text="Option4"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option5"
android:text="Option5"
/>
</RadioGroup>
</LinearLayout>
Add the below ode in your Activity
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton checkedRadioButton = (RadioButton) findViewById(checkedId);
String text = checkedRadioButton.getText().toString();
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
});
I know it's a old question but i don't see my answer in anywhere and i found it more simple than others..
so here we go:
int myRadioChecked;
if(radioGroup.getCheckedRadioButtonId() == findViewById(R.id.YOUR_RADIO_BUTTON).getId()) {
/**Do Stuff*/
//ex.: myRadioChecked = 1;
}
final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.MyRadioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
int selectedId = radioGroup.getCheckedRadioButtonId();
Log.i("ID", String.valueOf(selectedId));
}
});
In RadioGroup Class you can use method getCheckedRadioButtonId();
RadioGroup rg = findViewById(R.id.radioGroup);
rg.getCheckedRadioButtonId();
Returns the identifier of the selected radio button in this group. Upon empty selection, the returned value is -1.
Hmmm, just add one more member variable in UserBO to store selected answer.
Class UserBO {
private int userID;
private String userName;
private String question;
private String option1;
private String option2;
private String option3;
private int answerID;
//create getter and setters for above member variables
}
then within onclick listener of Adapter class, do like as following
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup,
int radioButtonID) {
switch(radioButtonID) {
case R.id.option1:
listItem.setAnswerID(1);
break;
case R.id.option2:
listItem.setAnswerID(2);
break;
}
}
});
then change your header constructor to receive userarraylist (which contains user details with answer)
ArrayList<USerBO> userList;
Header(Context context, AttributeSet attrs, ArrayList<UserBO> userALt) {
userList = userAL;
}
//on next button click
onclick() {
for(UserBO userObj: userList) {
if (userObj.getAnswerID != 0)
Log.d("AnswerID", userObj.getAnswerID);
}
}
it just like sudo code.. i hope this will help u..
you can get the id of selected button by the following this.Here
int position = group.indexOfChild(radioButton);
will give you the id.Also you can to Toast to see id like this
Toast.makeText(MainActivity.this,"Id of radio button"+position+, Toast.LENGTH_SHORT).show();
This will pop up - "Id of radio button you clicked is 0" if you clicked first button.
This is the best way:
RadioButton button = findViewById(v.getId());

Change EditText by Button

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

Categories

Resources