The problem of customization (customization) of the appearance of Android applications often arises before developers. The reason may be the need to follow the interface to the corporate style or the requirement of the customer who wants his application to look special, and not just as a set of standard elements.PackageManager pm = context.getPackageManager(); Resources res = pm.getResourcesForApplication ("package name"); Resources.Theme rstheme = res.newTheme(); package com.mera.detachedthemeslib; import android.app.Activity; import android.content.res.Resources; import android.os.Bundle; public abstract class BaseActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { ActivityManager.setThemeForActivity(this); super.onCreate(savedInstanceState); } @Override public Resources getResources() { return ActivityManager.getResourcesForActivity(this, super.getResources()); } @Override public Resources.Theme getTheme() { return ActivityManager.getThemeForActivity(this, super.getTheme()); } } static List<Theme> getThemes(Context ctx, ThemesConfiguration cfg) static void setTheme(Context ctx, Theme theme) <style name="MainTheme" parent="android:Theme"> <item name="custButtonStyle">@android:style/Widget.Button</item> </style> package com.mera.detachedthemesapp; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import com.mera.detachedthemeslib.BaseActivity; public class MainActivity extends BaseActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String[] items = new String[] {"One", "Two", "Three"}; Spinner spinner = (Spinner) findViewById(R.id.spinner1); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); this.findViewById(R.id.switch_button).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { startActivity(new Intent(MainActivity.this, SwitchThemeActivity.class)); } }); } } package com.mera.detachedthemesapp; import java.util.List; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.TextView; import com.mera.detachedthemeslib.BaseListActivity; import com.mera.detachedthemeslib.ThemesConfiguration; import com.mera.detachedthemeslib.ThemeManager; public class SwitchThemeActivity extends BaseListActivity { private LayoutInflater mInflater; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); List<ThemeManager.Theme> themes = ThemeManager.getThemes(this, new ThemesConfiguration() .addInnerTheme(R.style.CustomTheme2, "Green")); ArrayAdapter<ThemeManager.Theme> adapter = new ArrayAdapter<ThemeManager.Theme>( this, android.R.layout.simple_list_item_1, themes) { @Override public View getView(int position, View convertView, ViewGroup parent) { View row; if (null == convertView) { row = mInflater.inflate( android.R.layout.simple_list_item_1, null); } else { row = convertView; } TextView tv = (TextView) row.findViewById(android.R.id.text1); tv.setText(((ThemeManager.Theme) getItem(position)).mTitle); return row; } }; getListView().setAdapter(adapter); getListView().setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ThemeManager.Theme theme = (ThemeManager.Theme) getListView() .getAdapter().getItem(position); ThemeManager.setTheme(SwitchThemeActivity.this, theme); Intent intent = new Intent(); intent.setClass(SwitchThemeActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); SwitchThemeActivity.this.startActivity(intent); } }); } } 

Source: https://habr.com/ru/post/149564/
All Articles