-3

I'm trying to create a settings page for my app which has the radioGroup for 3 options to help users change the colour of the text in differrent layout/xml file. But in mysettings page I cant call the view from different layout, any idea?

Below are my codes:

Settings.Java

    package com.example.sunny.mynote;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Toast;
    import android.widget.EditText;



    /**
     * Created by Sunny on 19/04/2015.
     */
    public class Settings extends Activity {

        private RadioGroup RadioGroup1;
        private RadioButton rdbRed, rdbBlue, rdbOrange;
        private Button btnSave;
        private EditText editText;


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

            RadioGroup1 = (RadioGroup) findViewById(R.id.RadioGroup1);

            RadioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(checkedId == R.id.rdbRed)
                    {
                        Toast.makeText(getApplicationContext(), "choice: Red",
                                Toast.LENGTH_SHORT).show();
                    }
                    else if(checkedId == R.id.rdbBlue)
                    {
                        Toast.makeText(getApplicationContext(), "choice: Blue",
                                Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "choice: Orange",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });

            rdbRed = (RadioButton) findViewById(R.id.rdbRed);
            rdbBlue = (RadioButton) findViewById(R.id.rdbBlue);
            rdbOrange = (RadioButton) findViewById(R.id.rdbOrange);
            editText = (EditText) findViewById(R.id.editText);

            btnSave = (Button)findViewById(R.id.btn_Save);
            btnSave.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int selectedId = RadioGroup1.getCheckedRadioButtonId();
                    if(selectedId == rdbRed.getId()) {
                        //textView.setText("You chose 'Sound' option");
                    } else if(selectedId == rdbBlue.getId()) {
                        //textView.setText("You chose 'Vibration' option");
                    } else {
                        //textView.setText("You chose 'Silent' option");
                    }
                    finish();
                }

            });




        }
  @Override
        public void onBackPressed() {

        }
    }

and I want to FindViewbyID from this layout, an EditText from this view to be exactly

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/noteText"
        android:singleLine="false"
        android:gravity="top"
        android:inputType="textImeMultiLine"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

2 Answers2

0

I think you can create an intent with extras like this:

Intent i=new Intent(context,Class.class);
i.putExtra("ColorChoice",color);

And then extract this data from your main class with:

intent.getStringExtra("ColorChoice");
CAS
  • 535
  • 1
  • 8
  • 15
user3624383
  • 87
  • 3
  • 13
  • Sorry I dont understand. In my if statement I want to do sth like this if(selectedId == rdbRed.getId()) { textView.setTextColor(MyColor); But the problem is I cant get the textView from anorther layout. – Harper Mina Apr 20 '15 at 12:32
0

You cannot do that in your Settings.java activity if your editText is not in this activity.

You can either use intent with putExtra and getExtra like @user3624383 mentioned. (more on this here)

Or a better way would be to use SharedPreferences to save your settings even if the user exits your app and return to it later

CAS
  • 535
  • 1
  • 8
  • 15
  • so now if i do this Intent intent = new Intent(this, NoteEditorActivity.class); what should i put in my if statement so when the rdbRed is checked the text of the EditText in NoteEditorActivity will change its color to Red – Harper Mina Apr 20 '15 at 12:40
  • with `intent.putExtra`, yes it can work but I wouldn't recommend them. `SharedPreferences` are meant for these things – CAS Apr 20 '15 at 12:43
  • Can you please show me how to use intent to do this? – Harper Mina Apr 20 '15 at 12:47
  • There are many examples online. [Here](http://stackoverflow.com/questions/4233873/how-to-get-extra-data-from-intent-in-android) is one... – CAS Apr 20 '15 at 12:49
  • So intent basiclly is to get data from another activity. But what if I dont want to get data from another activity, I want to change data from another activity. – Harper Mina Apr 20 '15 at 12:52
  • Use `SharedPreferences`. Check [here](http://stackoverflow.com/questions/23024831/android-shared-preferences-example) for an example – CAS Apr 20 '15 at 12:54