1

I've got 2 activities (well actually 4 but only 2 matter for now), the main activity and the font changing activity. The main activity sends text that the user has typed in to the font changing activity using an intent and startActivityForResult. Within the font changing activity the user can change the font of the text, as expected. What I'm trying to do is after the user has changed the font, send the text and the new font back to the main activity. But I'm not sure how to do this. I'm thinking of using some kind of bundle but I am stuck. If anyone could help me that would be greatly appreciated.

Here's my (relevant) Main Activity code:

public class MainActivity extends AppCompatActivity
{
    RelativeLayout move_group;

    ImageView view;
    String backgroundImageName;
    SeekBar seekSize;
    TextView user_text;
    Button button;

    SeekBar tiltChange;

    String temp;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        user_text = (TextView) findViewById(R.id.user_text);

    static final int REQUEST_FONT = 4;
    public void FontChange(View view)
    {
        Intent fontIntent = new Intent(this, Main4Activity.class);
        fontIntent.putExtra("Text", temp);
        startActivityForResult(fontIntent, REQUEST_FONT);
    }

    //There's supposed to be an accompanying onActivityResult method as well
    //but i haven't figured that part out yet

}

Here's my (relevant) Font Activity code:

public class Main4Activity extends AppCompatActivity
{
    TextView user_font_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        user_font_text = (TextView) findViewById(R.id.user_font_text);
    }

    public void boldText(View view)
    {
        Typeface custom_font = getResources().getFont(R.font.avenir_next_bold);
        user_font_text.setTypeface(custom_font);
    }

    public void italicText(View view)
    {
        Typeface custom_font = getResources().getFont(R.font.avenir_next_italic);
        user_font_text.setTypeface(custom_font);
    }

    public void regularText(View view)
    {
        Typeface custom_font = getResources().getFont(R.font.avenir_next_regular);
        user_font_text.setTypeface(custom_font);
    }

    public void thinText(View view)
    {
        Typeface custom_font = getResources().getFont(R.font.avenir_next_thin);
        user_font_text.setTypeface(custom_font);
    }

    public void OK(View view)
    {
        //The intent and/or bundle would go here but I don't know what to do
    }
}
Marc Karam
  • 445
  • 6
  • 22

1 Answers1

0
public void FontChange(View view)
    {
        temp = user_text.getText().toString();
        Intent fontIntent = new Intent(this, Main4Activity.class);
        fontIntent.putExtra("Text", temp);
        startActivityForResult(fontIntent, REQUEST_FONT);
    }

this will fetch your string from textview to fontActivity.

Now in your FontActivity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        Intent intent = getIntent();
        String text = intent.getStringExtra("temp");
        user_font_text = (TextView) findViewById(R.id.user_font_text);
        user_font_text.setText(text);
    }

   public void OK(View view)
    {
          Intent returnIntent = new Intent();
                returnIntent.putExtra("result", 
                    user_font_text.getText().toString());
                returnIntent.putExtra("font", 
                    ypur_font_type);
                 setResult(Activity.RESULT_OK, returnIntent);
    }

Remaining part of setting font style can be done using SpannableStringBuilder. here you can check SpannableStringBuilder . Hope this helps.

Nirmal Prajapat
  • 1,735
  • 1
  • 12
  • 23