Dan
Dan

Reputation: 11

Android: Changing a textview with in a different Activity

I am trying to change textview properties of a textview that looks like this:

textview properties

In a seperate Activity that looks like this:

Activity

I tried to do this with bundles but I can't get it to work. This is how my BookActivity looks like:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.book_activity);

    //this is where the size property comes in
    Integer size = getIntent().getExtras().getInt("SGRkey");
    TextView test2 = (TextView) findViewById(R.id.booktext);
    test2.setTextSize(size);

    Spinner spinner = (Spinner) findViewById(R.id.kapitelspinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.kapitel_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());


}
    public class MyOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
          Toast.makeText(parent.getContext(),  
              parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();

          final String[] theporn = getResources().getStringArray(R.array.allkapitel);



          TextView text = (TextView) findViewById(R.id.booktext);
          text.setText(theporn[pos]); 


        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }

(i pick the chapter string in the spinner and that works just fine.)

And this is how my SettingsActivity looks like:

public class SettingsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_view);

    Spinner SGRspinner = (Spinner) findViewById(R.id.schriftgroeße_spinner);
    ArrayAdapter<CharSequence> SGRadapter = ArrayAdapter.createFromResource(
            this, R.array.schriftgroesse_list, android.R.layout.simple_spinner_item);
    SGRadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    SGRspinner.setAdapter(SGRadapter); 

}


public class SGROnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {

        Intent answer = new Intent();


      Toast.makeText(parent.getContext(),  
          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();



      final String[] SGRstring = getResources().getStringArray(R.array.schriftgroesse_list);
      int SGRint = Integer.parseInt(SGRstring[pos]);

      Bundle size = new Bundle();
      size.putInt("SGRkey", SGRint);
      Intent nextActivity = new Intent(com.asm.reader.SettingsActivity.this, com.asm.reader.BookActivity.class);
      nextActivity.putExtras(size);

      com.asm.reader.SettingsActivity.this.startActivity(nextActivity);

    }

    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }


}

I get an error when I try this. All activities are declared in the manifest. I really don't know how to go on. I'm pretty new to this, so sorry if this is something simple, but any help would be greatly appreciated!! :-)

Upvotes: 0

Views: 2005

Answers (1)

Emir Kuljanin
Emir Kuljanin

Reputation: 3911

Make your textview static. That is, declare it as a public static class variable. Then you can call it directly from the other activity like this: firstActivity.myTextView.setText("foo");

Upvotes: 5

Related Questions