I have a image button and i want that image button when pressed it changes the text in a textbox and it changes a image to a different image how do i do this?
You need an OnClickListener: http://developer.android.com/reference/android/view/View.OnClickListener.html
When clicked your text can be changed with (something like) text.setText("new text");
I'll find a link in a minute which will help more.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class TestesetetActivity extends Activity {
/** Called when the activity is first created. */
TextView textview = null;
ImageButton buttonResume = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonResume = (ImageButton) findViewById(R.id.imageButton1);
textview = (TextView) findViewById(R.id.textView1);
buttonResume.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textview.setText("test");
buttonResume.setImageResource(R.drawable.push_pin);
}
});
}
}
In the xml file, you can give the image an onClick attribute, which calls a method in the java class, which calls the xml resource, and passes it the id of the ImageView.
In that java method, you can use
((ImageView) view).setImageResource(int id)
or
setImageDrawable(Drawable d)
to change the image.
Likewise, you can identify the TextView you wish to change using, for example,
TextView tv = (TextView) findViewById( id )
with id being the id of the TextView you wish to find.
You can then use
tv.setText(String s)
to set the text in this view.
In your OnClickListener, you could use
button.setBackgroundResource(YOUR_BUTTON_ID);
or
button.setImageResource(YOUR_BUTTON_ID);
Related
I am new to android programming and am building a quiz app in which a question has 4 options and if the user clicks on one of the options the other options should be unclickable. I am currently able to only make a single button unclickable. Here is the java code.
package com.example.android.quiz;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//This method is called when option 1 of question 1 is selected
public void verifyQuestion1Option1(View view) {
Button QuestionOption1 = (Button) findViewById(R.id.question1_option1);
QuestionOption1.setBackgroundColor(getResources().getColor(R.color.solid_red));
question1Answer();
}
public void verifyQuestion1Option2(View view) {
Button Question1Option2 = (Button) findViewById(R.id.question1_option2);
Question1Option2.setBackgroundColor(getResources().getColor(R.color.solid_red));//solid red is not a predefined colour. It is declared in colors.xml
question1Answer();
}
public void verifyQuestion1Option3(View view) {
Button Question1Option3 = (Button) findViewById(R.id.question1_option3);
Question1Option3.setBackgroundColor(getResources().getColor(R.color.solid_green));
question1Answer();
}
public void verifyQuestion1Option4(View view) {
Button Question1Option4 = (Button) findViewById(R.id.question1_option4);
Question1Option4.setBackgroundColor(getResources().getColor(R.color.solid_red));//We call the getResources() method because R.colour.solid_red passed the id of the color not the actual colour value.
question1Answer();
}
public void question1Answer() {
TextView q1Answer = (TextView) findViewById(R.id.question1_answer);
String answer = "Rajinish Kumar is the current Chairman of SBI who took over after Arundhati Bhattacharya retired on 6 October.Shikha Sharma is the Managing Director and CEO of Axis Bank and Chanda Kochhar is the managing director and CEO of ICICI Bank";
q1Answer.setText(answer);
}
}
Either you can use a buttongroup which will have only 1 active button at any point of time or else, you need to disable other button programatically.
To disable the button you can use the following code:
Button button = (Button) findViewById(R.id.button);
button.setEnabled(false);
You can use .setEnabled(false); to disable a button. That button will grey out and does not respond to click events any more.
To disable all buttons, get the handle to each button and set them to disabled.
Button Question1Option1 = (Button) findViewById(R.id.question1_option1);
Button Question1Option2 = (Button) findViewById(R.id.question1_option2);
Button Question1Option3 = (Button) findViewById(R.id.question1_option3);
Button Question1Option4 = (Button) findViewById(R.id.question1_option4);
Question1Option1.setEnabled(false);
Question1Option2.setEnabled(false);
Question1Option3.setEnabled(false);
Question1Option4.setEnabled(false);
That way, all buttons of this question become disabled.
You can also come up with a solution where you save that the button is already pressed and ignore further click events. You could introduce some sort of variable bool question1answered = false; that is set to true as soon as the onClick event is fired.
public void verifyQuestion1Option4(View view) {
if (question1Answered == true) {return;}
question1Answered =true;
//Do the rest of your checks here
}
Two tips for for programming Java:
Java (in contrast to i.e C#) used lower letter variables as convention.
Button question1Option1 = (Button) findViewById(R.id.question1_option1); would be a better way.
If you have more questions, it would make sense to put them in some sort of array and reuse the same four buttons multiple times. That would save you much programming overhead and code rewrites if you have to change something. And it keeps the code cleaner.
Below is my main activity file. I am stuck with the bottom area (commented out). I want to make it so that when I hit button b1, the text changes from what it is for default. So say it says "hello" by default, I want to switch the text to something like "how are you" when you press button b1. I have tried many things, but I always get errors. Can someone help me?
package com.wdmc85.donottouch;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class DoNotTouchActivity extends Activity
implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) this.findViewById(R.id.b1);
b1.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.b1:
//WHAT DO I PUT IN HERE?? I WANT IT SO WHEN YOU HIT BUTTON B1, THE
//TEXT CHANGES FROM WHAT IT IS FOR DEFAULT. SO SAY IT SAYS "HELLO"
//AS DEFAULT, HOW DO I MAKE IT SO WHEN YOU PRESS BUTTON B1 THE TEXT
//CHANGES FROM "HELLO" TO "HOW ARE YOU?" I HAVE TRIED ALL SORTS OF
//THINGS BUT NOTHING HAS WORKED
break;
}
}
}
if want to change text of the button itself
switch(v.getId())
{
case R.id.b1:
Button b1 = (Button) v;
b1.setText("HOW ARE YOU?");
break;
}
if want to change text of the any other Textview
switch(v.getId())
{
case R.id.b1:
TextView tv = (TextView ) this.findViewById(R.id.<tvid>);
tv .setText("HOW ARE YOU?");
break;
}
You should use below method to change text of text view.
textView.setText("your text");
reference link is >> textview>>setText()
can use same for button and other views.
first I would like to say thank you to every one out here as i am a nOOb and have learned a lot just by reading questions and answers that you post. I am trying to pass a great deal of text to the end users and while being able to do this with new classes and .xml files this is becoming cumbersome. i thought of stream lining the app by just having a single xml layout for a particular set of text strings and just change the #string/????? via button onclick and setText but have learned that I can not change the initial value of #string in an xml file. question is that TRUE? and is there a more efficient way to do this ie (setting android:text to a var and setting var in java to a particular string) or do i need a new xml layout for each string? (that's a lots of waste if you ask me) and a little insight, there are at this time approx 250 different strings with min 5 paragraphs and growing.
here is my code thus far.
snippet of first java
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Monlt extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.monolt);
final MediaPlayer buttonsound = MediaPlayer.create(Monlt.this, (R.raw.buttonclick));
Button button1 = (Button) findViewById(id.button1);
Button button2 = (Button) findViewById(id.button2);
Button button3 = (Button) findViewById(id.button3);
Button button4 = (Button) findViewById(id.button4);
Button button5 = (Button) findViewById(id.button5);
Button button6 = (Button) findViewById(id.button6);
Button button7 = (Button) findViewById(id.button7);
Button button8 = (Button) findViewById(id.button8);
Button button9 = (Button) findViewById(id.button9);
Button button11 = (Button) findViewById(id.button11);
Button button12 = (Button) findViewById(id.button12);
Button button13 = (Button) findViewById(id.button13);
Button button14 = (Button) findViewById(id.button14);
Button button15 = (Button) findViewById(id.button15);
Button button16 = (Button) findViewById(id.button16);
Button button17 = (Button) findViewById(id.button17);
Button button18 = (Button) findViewById(id.button18);
Button button19 = (Button) findViewById(id.button19);
Button button21 = (Button) findViewById(id.button21);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
buttonsound.start();
final TextView mview = (TextView) findViewById(R.id.solayout2);
mview.setText("mono1"); //this was my first string to pass
startActivity(new Intent("com.nvar.Sorders.Mono.ASO1"));
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
buttonsound.start();
startActivity(new Intent("com.nvar.Sorders.ASO1"));
final TextView mview = (TextView) findViewById(R.id.solayout2);
mview.setText("#string/mono2");/this is the second string to pass
}
});
`
now this code works when i remove the 2 text view lines in the onclick
so then it call another class Aso1 that I would like to keep in place for later use.
Aso1 java code
`
package com.nvar.Sorders.Mono;
import com.nvar.Sorders.R;
import android.app.Activity;
import android.os.Bundle;
public class Aso1 extends Activity {
// Called when the activity is first created.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.solayout2);
}
}
`
and then the first xml
`
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/solayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mview"/>
<!-- android:text="#string/monoaso1"/>
-->
<!-- this is were i was playing with the strings />
-->
</LinearLayout>
</ScrollView>
`
any help in this matter would be greatly appreciated, and remember "noob" to java / android! so if there is a sample of what i could or should be looking at, don't hesitate to smack me in the head and point me in the right direction. i don't mind reading :)
thanks again.
When displaying the Strings, What you could use is a TextView and have multiple lines and then just update the text value of it in code. It doesn't even need default text
<ScrollView
...some params...>
<TextView
android:id="#+id/my_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="10"/>
</ScrollView>
This will give you a TextView 10 lines long. Then in code you can do something like this to update the value:
String longText = getVeryLongText();
((TextView)findViewById(R.id.my_text)).setText(longText);
Then you'll have something that looks like a scrollable paragraph of text
*#string/string_name* in xml are not designed to change the value. They are there to help you with localization. Currently that xml is located here http://developer.android.com/guide/topics/resources/localization.html
res/values/string.xml
you can have another string xml with different language like in the following location, for example france
res/values-fr/strings.xml
you can read more about that over here.
Now, let's move on to how to reference that string_name from java
Resources res = Monlt.this.getResources();
mview.setText(res.getString(R.string.string_name));
//do something
mview.setText(res.getString(R.string.mono2));
I used to have some experience with developing for android but I started up again after 6 months and forgot most of it. I am now using a macbook to do my developing on and had to set up Eclipse, the Android SDK and AVD all over again and I'm worried I messed something up.
When I start a new project with the default activity that displays "Hello World" on my screen the app runs fine. I then tried to put in two buttons that cause the text in a new TextView to change. But whenever I include the textView part I get a runtime error. When I comment it out, the app runs but obviously nothing happens. Based upon the tutorials I've been reading, this is the appropriate place and way to declare/create the textView but I can't figure out what's wrong. Any suggestions?
[Edit] I was messing around and found that I can make the mytext a field instead of a TextView and that worked. So in my onCreate(), I put
mytext = (TextField)findViewById(R.id.TextView1);
but that doesn't seem the right way to do things.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class IntroActivity extends Activity {
TextView myText = (TextView)findViewById(R.id.textView1);
//i've tried this with final added on to it as well (recommended by eclipse)
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setButtonClickListener();
}
private void setButtonClickListener() {
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myText.setText("Hello");
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// myText.setText("Goodbye");
}
});
}
}
This:
TextView myText = (TextView)findViewById(R.id.textView1);
should be separated. The declaration should be at the same place:
private TextView myText;
But the assignment should come only after setContentView:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView)findViewById(R.id.textView1);
This is done since before setContentView, Dalvik doesn't know from which layout to take the view that matchs the id R.id.textView1
Describing the functionality of my application: I have put in a Relative Layout a TextView, an EditText and a button. All I am trying to do is: when the user writes something in the EditText and push the button, then the content of the EditText is appeared in the TextView(just like a chat-virtual chat). Everything works perfectly, but when the EditText is empty,and the button get pushed, an empty line is appeared in the TextView(and i don't want to..). Although I've tried to solve it using an if the empty line is still appearing in the TextView. I would be really greatfull, if you could help!!! Than you in advance!
Here is my code:
package teiath.android.appliacation;
import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class M_chat extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**Code for the scroll bars in the TextView. */
final TextView tv = (TextView)findViewById(R.id.TEXT_VIEW);
tv.setMovementMethod(new ScrollingMovementMethod());//for the scroll bars
/** Code for the scroll bars in the EditText. */
final EditText wr = (EditText)findViewById(R.id.EDIT_TEXT);
wr.setMovementMethod(new ScrollingMovementMethod());//for the scroll bars
final Button button = (Button) findViewById(R.id.btn1);//find the button by id in main.xml
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String wrcontent = wr.getText().toString();//gets the text of the EditText and put it in "tvcontent" variable.
String tvcontent = tv.getText().toString();//gets the text of the textView and put it in "tvcontent" variable.
if (wrcontent!="")//if the EditText is not empty
{
//check if the TextView is empty or not
if (tvcontent!="")//If it is not empty...
{
tv.setText(tvcontent + "\n" + wrcontent);//add its current(TextView's text) text, new line and the text of the EditText as the new text of TextView.
//tv.setVisibility(0);//makes visible the textView with the cloud1.png background
wr.setText("");//set the text of the Edit Text as empty
//wrcontent = "";
}
else//if the TextView is empty...
{
tv.setText(wrcontent);//add the text of the editText as the new text of the TextView
wr.setText("");
}
}
/**if (wrcontent=="")
{
}*/
//finish();
}
});
}
}
Don't use !="" for String comparison. To check for empty text, use something like
if ( wrcontent != null && wrcontent.trim().length() == 0 ) {
Better yet, include Guava libraries in your code.