3 Replies - 487 Views - Last Post: 05 March 2014 - 05:48 AM

#1 steve.v   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-October 12

Need some guidance and small code review

Posted 04 March 2014 - 09:38 PM

Hello,

I've just started to learn the Android SDK. However I am stuck trying to do something involving an image and a text. I'm trying to create an onclick event on an ImageView that will change the existing text of a textview. Therefore if I tap a picture, the text like say "Hello" would change to something like goodbye. This is what I have so far. It doesn't seem to work once I run it through the emulator. This is a small snippet of the code minus the imports and the class initiation.

 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ImageView hotelicon = (ImageView)findViewById(R.id.imageView1);
		hotelicon.setonclickListener(new onclickListener() {
			public void onclick(View v) {
				TextView text = (TextView)findViewById(R.id.textView1);
				text.setText("Hello");
			}
		});
		
		
		
	}



It compiles fine with no errors, but I believe I may have a logic error. Any help is appreciated, thank you.

This post has been edited by steve.v: 04 March 2014 - 09:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Need some guidance and small code review

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12688
  • View blog
  • Posts: 45,877
  • Joined: 27-December 08

Re: Need some guidance and small code review

Posted 04 March 2014 - 10:08 PM

Post your error message from the LogCat if you are getting one. Otherwise, please expand on what is not working. Please be specific.
Was This Post Helpful? 0
  • +
  • -

#3 steve.v   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-October 12

Re: Need some guidance and small code review

Posted 04 March 2014 - 11:14 PM

Thank for replying, it seems I might have partly solved the issue. I was using string references in my xml on the textview; once I hardcoded the string rather than use variable referencing I was able to link the setText call. Clicked on the picture and the text changed.
Was This Post Helpful? 0
  • +
  • -

#4 4this   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 05-March 14

Re: Need some guidance and small code review

Posted 05 March 2014 - 05:48 AM

Hi there - Here's the more professional way to do what you ask for - you can learn for it a thing or two - and i've also add a toogle option


So first of all here's the activity_main.xml code -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Hello" />

</LinearLayout>






And here's the MainActivity.java code -

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.onclickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends Activity implements onclickListener {

	private ImageButton ib;
	private TextView tv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		 tv = (TextView) findViewById(R.id.textView1);
		 ib = (ImageButton) findViewById(R.id.imageButton1);
		
		ib.setonclickListener(this);
		
	}

	@Override
	public void onclick(View v) {
		switch (v.getId()){
			
		
		case R.id.imageButton1:
			
			//This is only to set the text once to Good-Bye
			//tv.setText("Good-Bye");
			
			///The toggle option
			if(tv.getText().toString().equals("Hello")){
				tv.setText("Good-Bye");
			}else{
				tv.setText("Hello");
				
			}
			
			break;
		
		
		}
		
	}



}




Was This Post Helpful? 1
  • +
  • -

Page 1 of 1