1

I'm trying to make an android app and need some help, i want to make imagebutton1 that have a src1 already to change its src1 and a textbox1 text "a" when clicked to src2 and the textbox1 text become "b" if clicked again change to src1 and the text to "a" etc... i used to do this in vb.net by

 Private Sub units_Click(sender As Object, e As EventArgs) Handles units.Click

    counter = counter + 1
    If (-1) ^ counter < 0 Then
        units.Image = WindowsApplication1.My.Resources.Resources.lup
        Label3.Text = "a"

    Else
        units.Image = WindowsApplication1.My.Resources.Resources.ldown
        Label3.Text = "b"

    End If
End Sub

please help and mention what each line do because I'm new to java

Avi K.
  • 1,734
  • 2
  • 18
  • 28
tamim abweini
  • 459
  • 6
  • 21
  • 1
    This is `vb.net` ***not Java***. Are you wanting us to turn this into `Java`? This will ***not*** translate to `Java` at all also we help with specific issues and or problems, not translate code. – Trevor Mar 10 '16 at 14:56

3 Answers3

1

I think you need to take a look into a Java book, there are a lot of them, Thinking in Java for example. And read the Android documentation, there is a lot of information there This question it's very basic after you read a little bit.

textView.setOnClickListener(new View.OnClickListener() {
                public int counter;

                @Override
                public void onClick(View v) {
                    counter += 1;
                    if (counter % 2 == 0) {
                        imageView.setImageResource(R.drawable.ic_one);
                        textView.setText("Text 1");
                    } else {
                        imageView.setImageResource(R.drawable.ic_two);
                        textView.setText("Text 2");
                    }
                }
            });
0

If i understand right, you want to change an image and text pressing the button? What you should do is to make a listener in your Activity: To load Image from URL to ImageView, Please visit this topic:

https://stackoverflow.com/a/18953695/5223744


Using the class above, your code should look like this:

    imgCBButton = (ImageButton) findViewById(R.id.imgBTN);
    txtCBText = (TextView) findViewById(R.id.txt);

    txtCBText.setText("a");
    final String firstURL = "www.yourFirstURL"; // Setting the firs URL
    final String secondURL = "www.yourSecondURL"; // Setting the second URL
    new ImageLoadTask(firstURL, imgCBButton).execute(); // Making a new ImageLoadTask class and passing the default values


          imgCBButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) { // Setting Listener on the imgButton
                if(indicator == false){  
                    txtCBText.setText("b"); // If the boolean == false change the text to "b"
                    new ImageLoadTask(secondURL, imgCBButton).execute(); // change the IMG URL
                    indicator = true; // Changing the boolean to true

                }else {
                    txtCBText.setText("a");
                    new ImageLoadTask(firstURL, imgCBButton).execute();
                    indicator = false;
                }

              }
          });

}

Don't forget to set a permission in the Manifest:

<uses-permission android:name="android.permission.INTERNET" />
Community
  • 1
  • 1
Igor Fridman
  • 1,267
  • 1
  • 16
  • 30
-1

If you are working inside an activity this code should work :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_view);
    yourButton = (ImageButton) findViewById(R.id.your_button);
    yourTextView = (TextView) findViewById(R.id.your_textview);
    yourButton.setTag(R.drawable.src1);
    yourButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (yourButton.getTag() == R.drawable.src1) {
                yourButton.setImageResource(R.drawable.src2);
                yourTextView.setText("b");
                yourButton.setTag(R.drawable.src2);
            } else {
                yourButton.setImageResource(R.drawable.src1);
                yourTextView.setText("a");
                yourButton.setTag(R.drawable.src1);
            }
        }
    });
}
youssef1700
  • 109
  • 11
  • Please ***do not*** feed these type's of question's. – Trevor Mar 10 '16 at 14:52
  • thanks for the down vote but I didn't see the user mentioned the word "translate" in his question he added a code in VB.Net to explain what he want and probably because stackoverflow didn't allowed him to ask a question without adding some code. – youssef1700 Mar 10 '16 at 15:32
  • You are welcome, `trying to make an android app and need some help` I think that's pretty clear as his code is in `.net` not `Java`... This still would be a `translate` correct, if not I don't know what is... Also please explain ***what your suggested code does*** he is **new** to `Java`, remember? – Trevor Mar 10 '16 at 15:38
  • codexer i don't understand why you are upset, i need to make something clear, I am not a programmer I never studied any programming language but i made a simple program in vb.net as a graduated project for civil engineering by learning just the basic stuff and now i m planning to make a very simple android app and need help to understand the java basics. and you are telling people not to "feed type's of question's." you are a very nice guy. – tamim abweini Mar 12 '16 at 15:13