1. Are you ready for the Galaxy S20? Here is everything we know so far!

Change text in another activity on click. how?

Discussion in 'Android Development' started by rodentooth, Nov 3, 2014.

  1. rodentooth

    rodentooth Lurker
    Thread Starter

    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 (Text):
    1.  
    2.  
    3. import android.app.Activity;
    4. import android.content.Intent;
    5. import android.os.Bundle;
    6. import android.view.Menu;
    7. import android.view.MenuItem;
    8. import android.view.View;
    9. import android.view.View.OnClickListener;
    10. import android.widget.Button;
    11.  
    12.  
    13. public class MainActivity extends Activity implements OnClickListener {
    14.  
    15.    
    16.     Button btn1;
    17.     @Override
    18.     protected void onCreate(Bundle savedInstanceState) {
    19.         super.onCreate(savedInstanceState);
    20.         setContentView(R.layout.activity_main);
    21.    
    22.         btn1 = (Button) findViewById(R.id.buttonSTART);
    23.         btn1.setOnClickListener(this);
    24.        
    25.     [COLOR="red"]    Intent intent = getIntent();
    26.         if( intent != null)
    27.             String inhalt = intent.GetStringExtra("0.1");[/COLOR]
    28.        
    29.        
    30.        
    31.     }
    32.  
    33.  
    34.     @Override
    35.     public boolean onCreateOptionsMenu(Menu menu) {
    36.         // Inflate the menu; this adds items to the action bar if it is present.
    37.         getMenuInflater().inflate(R.menu.main, menu);
    38.         return true;
    39.     }
    40.  
    41.     @Override
    42.     public boolean onOptionsItemSelected(MenuItem item) {
    43.         // Handle action bar item clicks here. The action bar will
    44.         // automatically handle clicks on the Home/Up button, so long
    45.         // as you specify a parent activity in AndroidManifest.xml.
    46.         int id = item.getItemId();
    47.         if (id == R.id.action_settings) {
    48.             return true;
    49.         }
    50.         return super.onOptionsItemSelected(item);
    51.     }
    52.  
    53.  
    54.     @Override
    55.     public void onClick(View v) {
    56.         // TODO Auto-generated method stub
    57.    
    58.         startActivity(new Intent(this,VisusActivity.class));
    59.     }
    60. }
    61.  

    XML Main Activity

    Code (Text):
    1. <RelativeLayout xmlns:android="(external link)"
    2.     xmlns:tools="(external link)"
    3.     android:layout_width="match_parent"
    4.     android:layout_height="match_parent"
    5.     android:paddingBottom="@dimen/activity_vertical_margin"
    6.     android:paddingLeft="@dimen/activity_horizontal_margin"
    7.     android:paddingRight="@dimen/activity_horizontal_margin"
    8.     android:paddingTop="@dimen/activity_vertical_margin"
    9.     tools:context="ch.OptiLab.visustest.MainActivity" >
    10.  
    11.     <TextView
    12.         android:layout_width="wrap_content"
    13.         android:layout_height="wrap_content"
    14.         android:textAppearance="?android:attr/textAppearanceLarge"
    15.         android:text="@string/Text1"
    16.         android:id="@+id/textView"
    17.         android:layout_marginTop="84dp"
    18.         android:layout_alignParentTop="true"
    19.         android:layout_alignParentRight="true"
    20.         android:layout_alignParentEnd="true"
    21.         android:gravity="center_horizontal"
    22.         android:singleLine="false" />
    23.  
    24.     <TextView
    25.         android:layout_width="wrap_content"
    26.         android:layout_height="wrap_content"
    27.         android:textAppearance="?android:attr/textAppearanceMedium"
    28.         android:text="@string/Text2"
    29.         android:id="@+id/textView2"
    30.         android:layout_alignParentTop="true"
    31.         android:layout_centerHorizontal="true"
    32.         android:layout_marginTop="33dp" />
    33.  
    34.     <Button
    35.         android:id="@+id/buttonSTART"
    36.         android:layout_width="200dp"
    37.         android:layout_height="wrap_content"
    38.         android:text="@string/button1"
    39.         android:layout_below="@+id/textView"
    40.         android:layout_centerHorizontal="true"
    41.         android:layout_marginTop="142dp" />
    42.  
    43. </RelativeLayout>
    44.  


    Java 2nd activity
    Code (Text):
    1.  
    2.  
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6. import android.view.Menu;
    7. import android.view.MenuItem;
    8. import android.view.View;
    9. import android.view.View.OnClickListener;
    10. import android.widget.Button;
    11.  
    12. import android.widget.ImageView;
    13. import android.widget.Toast;
    14.  
    15.  
    16.  
    17. public class VisusActivity extends Activity {
    18.     Button next;
    19.     ImageView testanzeige;
    20.     Button finish;
    21.     int i = 0;
    22.  
    23.    
    24.    
    25.  @Override
    26.     protected void onCreate(Bundle savedInstanceState) {
    27.         super.onCreate(savedInstanceState);
    28.         setContentView(R.layout.activity_visus);
    29.         next = (Button)findViewById(R.id.next);
    30.         testanzeige = (ImageView)findViewById(R.id.testanzeige);
    31.         [COLOR="red"]finish = (Button)findViewById(R.id.finish);[/COLOR]
    32.        
    33.    
    34.        
    35.         next.setOnClickListener(new View.OnClickListener() {
    36.            
    37.  @Override
    38.             public void onClick(View arg0) {
    39.                 i++;
    40.                 // To Set Your 1 Image Do it Thorough Layout
    41.            
    42.                
    43.                 if (i == 1 ) {
    44.                     //Your 2 Image
    45.                 testanzeige.setImageResource(R.drawable.visustest);
    46.                
    47.                
    48.                 [COLOR="Red"]finish.setOnClickListener(new View.OnClickListener() {
    49.                
    50.                     Intent i = new Intent(this, MainActivity.class);
    51.                     String content = "You made it until 2nd picture".toString();
    52.                     i.putExtra("0.1", content);
    53.                 }
    54.                 [/COLOR]
    55.                 }
    56.                 else if (i == 2) {
    57.                     //Your 3 Image
    58.                     testanzeige.setImageResource(R.drawable.v2);
    59.                 }
    60.                 else if (i == 3) {
    61.                     //Your 4 Image
    62.                     testanzeige.setImageResource(R.drawable.v3);
    63.                 }
    64.                 else if (i == 4) {
    65.                     // Your 5 Image
    66.                     //Imagey.setImageResource(R.drawable.Your Image);
    67.                 }
    68.                 else if (i == 5 ) {
    69.                     //Your 6 Image
    70.                     //Imagey.setImageResource(R.drawable.Your Image);
    71.                 }
    72.                 else if (i == 6) {
    73.                     //Your 7 Image
    74.                     //Imagey.setImageResource(R.drawable.Your Image);
    75.                 }
    76.                 else if (i == 7 ) {
    77.                     //Your 8 Image
    78.                     //Imagey.setImageResource(R.drawable.Your Image);
    79.                 }
    80.                 else if (i == 8 ) {
    81.                     //Your 9 Image
    82.                     //Imagey.setImageResource(R.drawable.Your Image);
    83.                 }
    84.                 else if (i == 9) {
    85.                     //Your 10 Image
    86.                     testanzeige.setImageResource(R.drawable.visustest);
    87.                 }
    88.                 else if (i == 10) {
    89.                     //Image 11 Image
    90.                
    91.                 }
    92.             }
    93.         });
    94.     }
    95.  
    96. }
    97.  
    98.  

    XML 2nd Activity
    Code (Text):
    1. <RelativeLayout xmlns:android="(external link)"
    2.     (external link)"
    3.     android:layout_width="match_parent"
    4.     android:layout_height="match_parent"
    5.     android:paddingBottom="@dimen/activity_vertical_margin"
    6.     android:paddingLeft="@dimen/activity_horizontal_margin"
    7.     android:paddingRight="@dimen/activity_horizontal_margin"
    8.     android:paddingTop="@dimen/activity_vertical_margin"
    9.     tools:context="ch.OptiLab.visustest.VisusActivity" >
    10.  
    11.     <ImageView
    12.         android:id="@+id/testanzeige"
    13.         android:layout_width="231dp"
    14.         android:layout_height="231dp"
    15.         android:layout_centerHorizontal="true"
    16.         android:layout_centerVertical="true"
    17.         android:src="@drawable/v2" />
    18.  
    19.     <Button
    20.         android:id="@+id/next"
    21.         style="?android:attr/buttonStyleSmall"
    22.         android:layout_width="wrap_content"
    23.         android:layout_height="wrap_content"
    24.         android:layout_alignParentBottom="true"
    25.         android:layout_centerHorizontal="true"
    26.         android:layout_marginBottom="16dp"
    27.         android:text="@string/NEXTPIC" />
    28.  
    29.     <Button
    30.         android:id="@+id/finish"
    31.         android:layout_width="wrap_content"
    32.         android:layout_height="wrap_content"
    33.         android:layout_alignBaseline="@+id/next"
    34.         android:layout_alignBottom="@+id/next"
    35.         android:layout_alignParentLeft="true"
    36.         android:text="@string/cantread" />
    37.    
    38. </RelativeLayout>
    39.  

    The most important strings:
    Code (Text):
    1.  <string name="Text1">Klicke auf Start um zu beginnen.</string>
    2. <string name="cantread">finish</string>
    3.  

    Thank you so much
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Alketi

    Alketi Lurker

    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. :)
     
  3. rodentooth

    rodentooth Lurker
    Thread Starter

    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'.
     
  4. Alketi

    Alketi Lurker

    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.
     
  5. rodentooth

    rodentooth Lurker
    Thread Starter

    well...I really dont get this with the Preferences :'( I've tried this:

    Code (Text):
    1. public class VisusActivity extends Activity {
    2.     Button next;
    3.     ImageView testanzeige;
    4.     Button finish;
    5.     int i = 0;
    6.     [COLOR="Red"]TextView textView;[/COLOR]
    7.  
    8.    
    9.  @Override
    10.     public void onCreate(Bundle savedInstanceState) {
    11.         super.onCreate(savedInstanceState);
    12.         setContentView(R.layout.activity_visus);
    13.         next = (Button)findViewById(R.id.next);
    14.         testanzeige = (ImageView)findViewById(R.id.testanzeige);
    15.         finish = (Button)findViewById(R.id.finish);
    16.         textView = (TextView) findViewById(R.id.textView);
    17.        
    18.    
    19.        
    20.         next.setOnClickListener(new View.OnClickListener() {
    21.            
    22.  @Override
    23.             public void onClick(View arg0) {
    24.                 i++;
    25.                 // To Set Your 1 Image Do it Thorough Layout
    26.            
    27.                
    28.                 if (i == 1 ) {
    29.                     //Your 2 Image
    30.                     testanzeige.setImageResource(R.drawable.visustest);
    31.                    
    32.                     [COLOR="red"]textView.setText("done");[/COLOR]         
    33.                 }
    34.                 else if (i == 2) {
    35.                     //Your 3 Image
    36.                     testanzeige.setImageResource(R.drawable.v2);
    37.                 }
    38.                 else if (i == 3) {
    39.                     //Your 4 Image
    40.                     testanzeige.setImageResource(R.drawable.v3);
    41.                 }
    but doesnt work either

    i tried to use the preference one have a look:

    Code (Text):
    1.  
    2.  @Override
    3.     public void onCreate(Bundle savedInstanceState) {
    4.         super.onCreate(savedInstanceState);
    5.         setContentView(R.layout.activity_visus);
    6.         next = (Button)findViewById(R.id.next);
    7.         testanzeige = (ImageView)findViewById(R.id.testanzeige);
    8.         finish = (Button)findViewById(R.id.finish);
    9.         [COLOR="red"]textView = (TextView)findViewById(R.id.textView);[/COLOR]
    10.        
    11.        
    12.    
    13.        
    14.         next.setOnClickListener(new View.OnClickListener() {
    15.            
    16.  @Override
    17.             public void onClick(View arg0) {
    18.                 i++;
    19.                 // To Set Your 1 Image Do it Thorough Layout
    20.            
    21.                
    22.                 if (i == 1 ) {
    23.                     //Your 2 Image
    24.                     testanzeige.setImageResource(R.drawable.visustest);
    25.                    
    26.                     [COLOR="Red"] textView.getString(PREFS_NAME);[/COLOR]
    27.                      
    28.                 }
    and the main Activity:

    Code (Text):
    1.     [COLOR="red"]  public static final String PREFS_NAME = "done";[/COLOR]
    2.    
    3.         Button btn1;
    4.         @Override
    5.         public void onCreate(Bundle savedInstanceState) {
    6.             super.onCreate(savedInstanceState);
    7.             setContentView(R.layout.activity_main);
    8.        
    9.          
    10.             btn1 = (Button) findViewById(R.id.buttonSTART);
    11.             btn1.setOnClickListener(this);
    12.          
    13.          [COLOR="red"]   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);[/COLOR]
    14.            
    15.            
    16.         }
     
Loading...

Share This Page

Loading...