I have this Button:
<Button
android:id="#+id/button1"
android:layout_width="130dp"
android:layout_height="35dp"
android:layout_alignRight="#+id/editText2"
android:layout_alignTop="#+id/imageView1"
android:layout_marginTop="23dp"
android:text="Increase" />
How can i make it so that whenever the button is clicked, the count in a textview goes up, for example if the number is first 0, and then you click the button it changes to 1?
Attach a listener to the button to handle the click event.
When you click on it, increment a counter variable and update the text of the textview.
Hint: Use String.valueOf(myCounter); to convert the int to a String
You must add a listener to the Button via the Button's setOnClickListener method, passing in an OnClickListener object.
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = Integer.parseInt(myTextView.getText().toString());
myTextView.setText(String.valueOf(i + 1));
}
});
where myTextView is the TextView with the counter, obtained with a call to findViewById. Be sure to pass a String to setText, otherwise the method overload with the integer parameter will be called, which has a different meaning (the integer represents a resource id).
it is very simple ! use this code :
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Increase"
android:onClick="inc"/>
<TextView
android:id="#+id/text"
..../>
and in your activity :
public void inc(View v){
TextView t = (TextView) findViewById(R.id.text);
t.setText(String.valueOf(Integer.parseInt(t.getText)+1));
}
Related
I know these type of question asked many time .but still nobody gave perfect answer for that.
I have question :
I want to move from EditText1 ** to another **EditText2 .
I had already detect to editText1 but how to move cursor to editText2.?
In short I had to move my cursor position from one editText1 to another EditText2 directly.
I faced this type of issue and found the solution as below.
Here I have two editText, if I press "a", my cursor will move to next step. I used below code for doing it.
final EditText editText = (EditText) findViewById(R.id.editText1);
editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v , int keyCode , KeyEvent event) {
EditText editText2 = (EditText) findViewById(R.id.editText2);
// TODO Auto-generated method stub
if (keyCode == event.KEYCODE_A) {
Selection.setSelection((Editable) editText2.getText(),editText.getSelectionStart());
editText2.requestFocus();
}
return true;
}
});
Let me know if you are facing any error regarding this.
For this, all you need to do is...add below two properties to your EditText tag in xml, except the last EditText(In case, you add it to the last EditText also, then the cursor control will again go to the first EditText when you press enter/next from the keypad)
<EditText
.
.
android:singleLine="true"
android:imeOptions="actionNext"
.
.
/>
Hope this helps
Here is a working example, hope this helps.
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<EditText
android:id="#android:id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<Button
android:id="#android:id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
</LinearLayout>
Class:
public class Example extends Activity {
TextView text1;
TextView text2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text1 = (EditText) findViewById(android.R.id.text1);
text2 = (EditText) findViewById(android.R.id.text2);
Button button = (Button) findViewById(android.R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Selection.setSelection((Editable) text2.getText(), text1.getSelectionStart());
text2.requestFocus();
}
});
}
}
Set onKeyListener to detect the key pressed on every key pressed checked your condition and when your condition will be fulfilled set edittext property edittext2.requestFocus();
I have tested all the previous code segments, and find all are working fine. But I find just calling "requestFocus()" with the proper edittext object is also working. As per as ques asked, the ans can be:
edittext2.requestFocus();
which is working fine for me. Please correct me if I am wrong.
I'm trying to make an application on Android Studio. I have 2 buttons. a plus and a minus. I need to know how to make the text smaller and bigger everytime I click on one of these buttons.
This is my MainActivity.java:
Button Min = (Button)findViewById(R.id.Min);
Min.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView t = (TextView)findViewById(R.id.DeText);
t.setTextSize(-5);
}
});
This is my .xml:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Plus"
android:layout_gravity="top|left"
android:text="+"
android:textSize="50dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Min"
android:layout_gravity="top|right"
android:text="-"
android:textSize="50dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="50dp"
android:id="#+id/DeText"
android:text="Text"/>
I also have made a screenshot of the page I'm working on. Hopefully this gives some more information about my end product:
You simply set the text size by getting the current size and adding/subtracting the value you want.
Button minButton = (Button)findViewById(R.id.Min);
minButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView t = (TextView)findViewById(R.id.DeText);
t.setTextSize(COMPLEX_UNIT_PX, t.getTextSize() - 5f);
}
});
It would be better to check if the current text size is not too small before reducing the size.
You should use sp instead of dp for text sizes. From documentation, you can use setTextSize method and it includes two arguments.
void setTextSize (int unit,
float size)
So, you can specify the text size after button click as:
Button button = (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView text = (TextView)findViewById(R.id.DeText);
text.setTextSize(TypedValue.COMPLEX_UNIT_SP,text.getTextSize()-1);
}
});
I'm a novice android developper and was wondering:
Could I have 2 buttons to be linked into the same, 1 onClick method (which i'll presumably override to accept 2 extra parameter, int btnId and View targetTextView for instance) in order to decide which button is calling the method and then which TextView text to update?
For Example:
btn1 will update the text on text_view_1
and btn2 will update text_view_2.
Except they we will be linked to the same method:
public void generalOnClick(View view, String btnId, String textViewId){...}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:onClick="btnClick"/>
<TextView
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Two"
android:onClick="btnClick"/>
</LinearLayout>
Button Click Function in your Activity
public void btnClick(View view) {
TextView tv = (TextView)view;
int id = tv.getId();
if(id==R.id.one) {
tv.setText("One Clicked");
} else if(id==R.id.two){
tv.setText("Two Clicked");
}
}
set an tag to button and verify it with onClick method that which buttons click event has been triggered , if it doesn't work then follows following trick,
define an common method for the functionality which you are going to execute on button click, make it as common function.
define independent onclick event for both button and then while calling the common function which created above pass some unique param and verify it.
use following code:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View.OnClickListener clickListener = new View.OnClickListener() {
public void onClick(View v) {
if(v.getId().Equals(btn1.getId())){
//do your work here
}
else{
//work for 2nd button
}
};
btn1 = (Button)findViewById(R.id.btn_1);
btn2 = (Button)findViewById(R.id.btn_2);
btn1.setOnClickListener(clickListener);
btn2.setOnClickListener(clickListener);
}
I am Developing my first android Calculator app. I'm stuck with a single defect. I have added few buttons and on clicking those buttons, it will put the respectives text on the EditText Field. The Main Problem is described below,
When on running the project, the buttons have to be clicked twice to put the respective text on the EditText field for the first time. For example, Button1 puts '1' on the EditText field on click. When on run, First click on that button does nothing. Only on the second click it puts '1' on the EditText field.
The Code follows,
XML Button and EditField,
<EditText
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ellipsize="end"
android:ems="10"
android:gravity="right"
android:hint="#string/textView1" />
<Button
android:id="#+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
android:hint="#string/button1"
android:onClick="set1" />
MainActivity.java
The respective function for Button onClick,
public void set1(View v){
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
EditText tv = (EditText) findViewById(R.id.textView1);
String text=tv.getText().toString();
tv.setText(text+"1");
}
});
}
Change your set1() method as follows,
public void set1(View v)
{
EditText tv = (EditText) findViewById(R.id.textView1);
String text=tv.getText().toString();
tv.setText(text+"1");
}
If you are calling your set1() method onClick of button in xml then you don't need to find ID for button in that method again. So simply it looks like,
public void set1(View v)
{
EditText tv = (EditText) findViewById(R.id.textView1);
String text=tv.getText().toString();
tv.setText(text+"1");
}
Now in your xml for button will be
android:onClick="set1"
I cannot figure out why but I keep getting E/AndroidRuntime(709): java.lang.NullPointerException at any point I try to call the buttons titled yes and no. The buttons are created in an xml layout and called using the findViewById method, a similar technique I've used in several properly working buttons in the same program. The only difference between these buttons and the rest is that their xml code is kept in a separate layout file that is not the main.xml.
If I only want to show the buttons and don't call them within the java code they will show up just as they should. The error occurs when I try to apply anything to them in the java code, such as setClickable() and setOnClickListener().
The code below outlines the creation of the button and layout in the xml and the java code shows a method that creates a popup when called.
Java Code:
private void getLogoutOption() {
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.logoutoption, null);
final PopupWindow logoutoption = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button yes = (Button) findViewById (R.id.yes);
yes.setClickable(true);
yes.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
logoutoption.dismiss();
switchActivity(8);
}
});
Button no = (Button) findViewById (R.id.no);
no.setClickable(true);
no.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
logoutoption.dismiss();
}
});
logoutoption.showAsDropDown(search, 50, -30);
}
XML CODE:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent" >"
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/menu_default" >
<TextView
android:id="#+id/logoutmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Are you sure you would like to logout?"
android:textColor="#color/gold" />
<Button
android:id="#+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/logoutmessage"
android:layout_centerInParent="true"
android:text="YES"
android:textColor="#color/green" />
<Button
android:id="#+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/logoutmessage"
android:layout_toRightOf="#id/yes"
android:text="NO"
android:textColor="#color/red" />
</RelativeLayout>
</RelativeLayout>
Button yes = (Button) findViewById (R.id.yes);
should be
Button yes = (Button) popupView.findViewById (R.id.yes);
By default findViewById() will be searching your content view for the view with the specified ID (e.g. if you used setContentView(R.layout.main), it will be searching through the main.xml structure for those views). If you're searching an inflated layout, you have to call findViewById() on that particular inflated layout.
Change:
Button yes = (Button) findViewById (R.id.yes);
Button no = (Button) findViewById (R.id.no);
To:
Button yes = (Button) popupView.findViewById (R.id.yes);
Button no = (Button) popupView.findViewById (R.id.no);
Hi The problem is you are getting id as you take in Activity.In Activity view created by setContentView(R.layout.main).In Your code you are inflating new view so you should use
Button yes = (Button)popupView.findViewById (R.id.yes);