• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Change text in another activity on click. how?

Hi there

I am new to this forum as an User, and I am also new to Java and Android, in developing ways. Sorry if there are any language or other mistakes.

So I am trying to make an app for a 'final' school project, which has the follow use:

The user sees a picture, a 'next' and a 'finish' button. there are 11 pictures (user only sees one) when the user clicks 'next', the pic no. 2 appears, if he clicks another time next, the picture no. 3 appears and so on.
when user clicks next 2 times (so if he made it until the 3rd picture) and then clicks finish, he'll Return to the main activity, and instead of the text 'Klicke auf start um zu beginnen' (click on start to begin) the text 'you made it until the 3rd picture' should show up.
If he clicked Next 5 times (6th picture) and clicks finish, he also returns to the main activity, but the text is 'you made it until the 5th picture'.
And so on with the others.

I've tried this, but I failed.

the red lines are those, which I made at my own but they unfortunately don't work :(

Java Main Activity

Code:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity implements OnClickListener {

	
	Button btn1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        btn1 = (Button) findViewById(R.id.buttonSTART);
        btn1.setOnClickListener(this);
        
    [COLOR="red"]    Intent intent = getIntent();
        if( intent != null)
        	String inhalt = intent.GetStringExtra("0.1");[/COLOR]
        
        
        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	
		startActivity(new Intent(this,VisusActivity.class));
	}
}

XML Main Activity

Code:
<RelativeLayout xmlns:android="(external link)"
    xmlns:tools="(external link)"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="ch.OptiLab.visustest.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/Text1"
        android:id="@+id/textView"
        android:layout_marginTop="84dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:gravity="center_horizontal"
        android:singleLine="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/Text2"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp" />

    <Button
        android:id="@+id/buttonSTART"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/button1"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="142dp" />

</RelativeLayout>


Java 2nd activity
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import android.widget.ImageView;
import android.widget.Toast;



public class VisusActivity extends Activity {
	Button next;
	ImageView testanzeige;
	Button finish;
	int i = 0;

	
	
 @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_visus);
		next = (Button)findViewById(R.id.next);
		testanzeige = (ImageView)findViewById(R.id.testanzeige);
		[COLOR="red"]finish = (Button)findViewById(R.id.finish);[/COLOR]
		
	
		
		next.setOnClickListener(new View.OnClickListener() {
			
 @Override
			public void onClick(View arg0) {
				i++;
				// To Set Your 1 Image Do it Thorough Layout
			
				
				if (i == 1 ) {
					//Your 2 Image
				testanzeige.setImageResource(R.drawable.visustest);
				
				
				[COLOR="Red"]finish.setOnClickListener(new View.OnClickListener() {
				
					Intent i = new Intent(this, MainActivity.class);
					String content = "You made it until 2nd picture".toString();
					i.putExtra("0.1", content);
				}
				[/COLOR]
				}
				else if (i == 2) {
					//Your 3 Image
					testanzeige.setImageResource(R.drawable.v2);
				}
				else if (i == 3) {
					//Your 4 Image
					testanzeige.setImageResource(R.drawable.v3);
				}
				else if (i == 4) {
					// Your 5 Image
					//Imagey.setImageResource(R.drawable.Your Image);
				}
				else if (i == 5 ) {
					//Your 6 Image
					//Imagey.setImageResource(R.drawable.Your Image);
				}
				else if (i == 6) {
					//Your 7 Image
					//Imagey.setImageResource(R.drawable.Your Image);
				}
				else if (i == 7 ) {
					//Your 8 Image
					//Imagey.setImageResource(R.drawable.Your Image);
				}
				else if (i == 8 ) {
					//Your 9 Image
					//Imagey.setImageResource(R.drawable.Your Image);
				}
				else if (i == 9) {
					//Your 10 Image
					testanzeige.setImageResource(R.drawable.visustest);
				}
				else if (i == 10) {
					//Image 11 Image
				
				}
			}
		});
	}

}

XML 2nd Activity
Code:
<RelativeLayout xmlns:android="(external link)"
    (external link)"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="ch.OptiLab.visustest.VisusActivity" >

    <ImageView
        android:id="@+id/testanzeige"
        android:layout_width="231dp"
        android:layout_height="231dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/v2" />

    <Button
        android:id="@+id/next"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"
        android:text="@string/NEXTPIC" />

    <Button
        android:id="@+id/finish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/next"
        android:layout_alignBottom="@+id/next"
        android:layout_alignParentLeft="true"
        android:text="@string/cantread" />
    
</RelativeLayout>

The most important strings:
Code:
 <string name="Text1">Klicke auf Start um zu beginnen.</string>
<string name="cantread">finish</string>

Thank you so much
 
Short answer -- the easiest thing is probably to save the text to shared preferences.

In your 2nd activity, that displays the text, always read from Shared Preferences. The read method has a default field, in case the value is not yet available. You can either set this to blank ("") or some other value.

Good luck. :)
 
hi :) thank you alot for this answer. well I don't really get this with the 'read' one, also, what are the saved preferences? . the text should show up in the main activity as the user returns to it when he clicks 'finish'.
 
Search Google for "android SharedPreferences", it's a private space in storage for your app.

You can write and read values to/from it, which makes it an easy way to share values/strings across activities in an app.

You would read the SharedPreferences key in onResume() of your 2nd activity, after saving the value in your 1st activity.
 
well...I really dont get this with the Preferences :'( I've tried this:

Code:
public class VisusActivity extends Activity {
	Button next;
	ImageView testanzeige;
	Button finish;
	int i = 0;
	[COLOR="Red"]TextView textView;[/COLOR]

	
 @Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_visus);
		next = (Button)findViewById(R.id.next);
		testanzeige = (ImageView)findViewById(R.id.testanzeige);
		finish = (Button)findViewById(R.id.finish);
		textView = (TextView) findViewById(R.id.textView);
		
	
		
		next.setOnClickListener(new View.OnClickListener() {
			
 @Override
			public void onClick(View arg0) {
				i++;
				// To Set Your 1 Image Do it Thorough Layout
			
				
				if (i == 1 ) {
					//Your 2 Image
					testanzeige.setImageResource(R.drawable.visustest);
					
					[COLOR="red"]textView.setText("done");[/COLOR]			
				}
				else if (i == 2) {
					//Your 3 Image
					testanzeige.setImageResource(R.drawable.v2);
				}
				else if (i == 3) {
					//Your 4 Image
					testanzeige.setImageResource(R.drawable.v3);
				}

but doesnt work either

i tried to use the preference one have a look:

Code:
 @Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_visus);
		next = (Button)findViewById(R.id.next);
		testanzeige = (ImageView)findViewById(R.id.testanzeige);
		finish = (Button)findViewById(R.id.finish);
		[COLOR="red"]textView = (TextView)findViewById(R.id.textView);[/COLOR]
		
		
	
		
		next.setOnClickListener(new View.OnClickListener() {
			
 @Override
			public void onClick(View arg0) {
				i++;
				// To Set Your 1 Image Do it Thorough Layout
			
				
				if (i == 1 ) {
					//Your 2 Image
					testanzeige.setImageResource(R.drawable.visustest);
					
					[COLOR="Red"] textView.getString(PREFS_NAME);[/COLOR]
					 
				}

and the main Activity:

Code:
	[COLOR="red"]  public static final String PREFS_NAME = "done";[/COLOR]
	
		Button btn1;
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_main);
	    
	      
	        btn1 = (Button) findViewById(R.id.buttonSTART);
	        btn1.setOnClickListener(this);
	     
	     [COLOR="red"]   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);[/COLOR]
	        
	        
	    }
 
Back
Top Bottom