Reputation: 303
I managed to use a spinner
in my code and wanted to change the textColor of a certain text in the MainActivity
file trough that spinner, but he is located in another class file - Einstellungen
.
Is it possible to change the textColor in the current activity from another activity?
This is the main_activity.xml
where I want to change text color:
<TextView
android:id="@+id/speedtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:gravity="center"
android:singleLine="true"
android:text="TEXT"
android:textColor="@android:color/white"
android:textSize="220sp" />
This is the Einstellungen activity:
public class Einstellungen extends AppCompatActivity {
String[] names = {"Weiß", "Blau", "Rot"};
String[] des = {"Weiß", "Blau", "Rot"};
ArrayAdapter<String> adapter;
Spinner spinner;
TextView description;
public Button button;
public void init() {
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toy = new Intent(Einstellungen.this, MainActivity.class);
startActivity(toy);
}
});
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_einstellungen);
spinner = (Spinner) findViewById(R.id.spinner);
description = (TextView) findViewById(R.id.text);
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, names);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i) {
case 0:
description.setText("" + des[i]);
MainActivity.speed.setTextColor(Color.WHITE);
break;
case 1:
description.setText("" + des[i]);
MainActivity.speed.setTextColor(Color.BLUE);
break;
case 2:
description.setText("" + des[i]);
MainActivity.speed.setTextColor(Color.RED);
break;
}
}
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
init();
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
LocationService myService;
static boolean status;
LocationManager locationManager;
static TextView dist, time, speed;
static long startTime, endTime;
ImageView image;
static ProgressDialog locate;
static int p = 0;
private ServiceConnection sc = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
LocationService.LocalBinder binder = (LocationService.LocalBinder) service;
myService = binder.getService();
status = true;
}
public void onServiceDisconnected(ComponentName name) {
status = false;
}
};
public Button button;
public void init() {
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toy = new Intent(MainActivity.this, Einstellungen.class);
startActivity(toy);
}
});
}
void bindService() {
if (status == true)
return;
Intent i = new Intent(getApplicationContext(), LocationService.class);
bindService(i, sc, BIND_AUTO_CREATE);
status = true;
startTime = System.currentTimeMillis();
}
void unbindService() {
if (status == false)
return;
Intent i = new Intent(getApplicationContext(), LocationService.class);
unbindService(sc);
status = false;
}
protected void onResume() {
super.onResume();
}
protected void onStart() {
super.onStart();
}
protected void onDestroy() {
super.onDestroy();
if (status == true)
unbindService();
}
public void onBackPressed() {
if (status == false)
super.onBackPressed();
else
moveTaskToBack(true);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speed = (TextView) findViewById(R.id.speedtext);
image = (ImageView) findViewById(R.id.image);
start();
init();
}
public void start() {
checkGps();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
return;
}
if (status == false)
bindService();
locate = new ProgressDialog(MainActivity.this);
locate.setIndeterminate(true);
locate.setCancelable(false);
locate.setMessage("Suche GPS-Signal");
locate.show();
}
void checkGps() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
showGPSDisabledAlertToUser();
}
}
private void showGPSDisabledAlertToUser() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Bitte GPS aktivieren")
.setCancelable(false)
.setPositiveButton("GPS aktivieren",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("Abbrechen",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
}
Upvotes: 5
Views: 151
Reputation: 4051
First of all, please, never store View's
into the static field, it leads to Memory leaks. Change:
static ProgressDialog locate;
static TextView dist, time, speed;
to
private ProgressDialog locate;
private TextView dist, time, speed;
Then, for your purposes, you can use SharedPreferences. Let's make it step by step.
Add the next fields to Einstellungen
:
public static final String SHARED_PREFERENCES = "SHARED_PREFS";
public static final String SELECTED_COLOR = "SELECTED_COLOR";
private SharedPreferences preferences;
Get SharedPreferences
inside onCreate()
method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_einstellungen);
preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
...
}
Put selected color to SharedPreferences
:
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i) {
case 0:
description.setText(des[i]);
preferences.edit().putInt(SELECTED_COLOR, Color.WHITE).apply();
break;
case 1:
description.setText(des[i]);
preferences.edit().putInt(SELECTED_COLOR, Color.BLUE).apply();
break;
case 2:
description.setText(des[i]);
preferences.edit().putInt(SELECTED_COLOR, Color.RED).apply();
break;
}
}
In your MainActivity
:
Add the next field:
private SharedPreferences preferences;
Inside onCreate()
method, get selected color and set it to TextView
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speed = findViewById(R.id.speedtext);
image = findViewById(R.id.image);
preferences = getSharedPreferences(Einstellungen.SHARED_PREFERENCES, MODE_PRIVATE);
int color = preferences.getInt(Einstellungen.SELECTED_COLOR, Color.WHITE);
speed.setTextColor(color);
init();
}
Update:
If you want to save spinner state, you can also use SharedPreferences
:
Add another one constant to Einstellungen:
public static final String SELECTED_COLOR_POSITION = "SELECTED_COLOR_POSITION";
Add the next line at the beginning of your onItemSelected()
method, to save selected item position:
preferences.edit().putInt(SELECTED_COLOR_POSITION, i).apply();
Restore state of the spinner in the onCreate()
method, after spinner.setAdapter(adapter)
line:
int position = preferences.getInt(SELECTED_COLOR_POSITION, 0);
spinner.setSelection(position);
Upvotes: 2