Change theme in Android app [duplicate] - android

This question already has answers here:
Switching application-wide theme programmatically?
(2 answers)
Closed 7 years ago.
I want dynamically change the theme of my app with buttons, so I implemented this:
sharedPreferences = getSharedPreferences("VALUES",MODE_PRIVATE);
int theme = sharedPreferences.getInt("THEME",2);
switch (theme){
case 1: setTheme(R.style.AppTheme);
break;
case 2: setTheme(R.style.AppTheme_AppBarOverlay);
break;
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutotial);
And this is the code of the buttons:
tb1 =(Button) findViewById(R.id.button2);
tb1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sharedPreferences.edit().putInt("THEME",1).apply();
Intent intent = new Intent(tutotial.this, tutotial.class);
startActivity(intent);
}
});
tb2 =(Button) findViewById(R.id.button3);
tb2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sharedPreferences.edit().putInt("THEME",2).apply();
Intent intent = new Intent(tutotial.this, tutotial.class);
startActivity(intent);
Intent intent1 = new Intent(tutotial.this, MainActivity.class);
startActivity(intent1);
}
});
The problem is that code just changes the theme of the activity associated and it does not make the change theme in all the app.

There is an open source podcast player called AntennaPod on github. It contains example code that does this.
The way they do it is to call ContextThemeWrapper.setTheme(int) at the beginning of each Activity.onCreate() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(UserPreferences.getTheme());
super.onCreate(savedInstanceState);
......
}
This could be done in each activity, or by creating a base activity that does this for you on each subclass.
On closer reading of your question, this is exactly what you are doing. So I would say you are on the right track.
It also seems this has been asked before:
Switching application-wide theme programmatically?
Android - Change app Theme on onClick
I want my users to be able to switch from a dark and light theme for my entire app
All offering the same solution.

You can apply a theme to any activity by including android:theme inside activity inside manifest file.
For example:
<activity android:theme="#android:style/Theme.Dialog">
<activity android:theme="#style/CustomTheme">
And if you want to set theme programatically then use setTheme() before calling setContentView() and super.onCreate() method inside onCreate() method.

You can do this by implementing various themes and on click of button change the themes accordingly, refer this change theme programatically for assistance.

Related

Linking buttons to pages using android(Eclipse) - can't get it to work

I'm new to Android development, and to this site!
I have done a few tutorials etc and am working on a project at the moment, and had a good look through other answers to similar questions, but haven't been able to find quite what i'm looking for (but loads of good suggestions!)
I am trying to get buttons on my main screen linking to individual pages. I am using my phone instead of an emulator, but every time i click on a button, the app dies... can you help me?
This is my main Screen code for button1:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declaring and defining the buttons used
Button student1 = (Button) findViewById(R.id.button1);
// Setting the onClickListener for button1
student1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//calling the page1 function
page1(view);
}
});
This is the page1 function:
public void page1(View view) {
Intent intent = new Intent(this, Page1.class);
startActivity(intent);
}
Here is the code for the Page1 class file:
public class Page1 extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_1);
}
}
This is the code for the layout file: (page_1.xml)
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="shannon.white.finalyear.DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
If you need anything else, let me know
Any ideas?
Thanks :)
Your coding looks to be correct.
The next thing to check would be to look inside your AndroidManifest.xml file to ensure you have added the activity to it so the android OS knows it exists. You add it like so:
<activity android:name="Page1" />
If your activity resides in a different package then the one declared inside the manifest file, then you need to specify the full package inside the "name" like so:
<activity android:name="some.other.package.name.Page1" />
Thats about all i can say from the provided code. If you are simply starting another activity which is Page1.class then your code looks correct and you might be missing the manifest declaration as i stated above.
Try moving this following code
// Declaring and defining the buttons used
Button student1 = (Button) findViewById(R.id.button1);
// Setting the onClickListener for button1
student1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
underneath your page1 function so your code looks like this:
public void page1(View view) {
Button student1 = (Button) findViewById(R.id.button1); // Declaring and defining the buttons used
student1.setOnClickListener(new View.OnClickListener() { // Setting the onClickListener for button1
#Override
public void onClick(View v) {
startActivity(intent);
Intent intent = new Intent(this, Page1.class);
}
}
}
and your onCreate look like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
page1(view); // NOTE I'm now declaring it on the onCreate instead of onClick
}
If that doesn't help, well your code still looks cleaner. Could just be my OCD though...
This is a very good tutorial by Mkyong on how to achieve what you are trying to do. If no other answers help, restarting using this tutorial will likely help you succeed. On multiple occasions I've tested his code and its worked.

In Android: How do you show layout twice using intent?

Let's pretend this was my Java Class...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button ScreentwoGameButton = (Button) findViewById(R.id.screentwo);
ScreentwoGameButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent ScreentwoGameIntent = new Intent(Main.this, Screentwo.class);
startActivity(StartGameIntent);
}
});
How do i use this code below but the right way like.
So let's put an example if I click screentwo button the screentwo.xml will show and it will allow me to click inside if any buttons are available. Instead just stare what's in the layout.
I don't want to use the Activity to activity cause the whole point is i'm trying to avoid the flashing looking feel going to another java class.
If you look at the moron test game on Android it says example: press the blue button then red and then green, so if u press the blue button the screen will remain and not flash at all but the image of the blue button will disappear and I'm allowed to click the red and then green.
Hope that helped.
Thanks
Wahid
Button ScreentwoButton = (Button) findViewById(R.id.screentwo);
ScreentwoButton.setOnClickListener(new OnClickListener() {
private Uri Uri;
#Override
public void onClick(View v) {
setContentView(R.layout.Screentwo);
Uri uri=Uri;
Intent i=new Intent(Intent.ACTION_VIEW, uri);
mSoundManager.playSound(1);
}
});
try to use:
setContentView(R.layout.next layout); in your button click.
You could use the viewflipper class and add the different layouts as childs to the viewflipper
and set the active child. Using setcontentView will be trouble some when you use findViewById for a old layout. As findViewById will look in the layout that is specified by setContentView

Running a custom animation between Android Activities

So I know you can use your own animation between activities using the overidePendingTransition method. I set up a transition between two activites and it works perfect on my emulator but I see no transition when I flash the app on to my phone. How can this be?
My emulator is running 2.2 as is my phone
Here is my onCreate method
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.close);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(ActivityTransitionActivity.this, ActivityTwo.class);
ActivityTransitionActivity.this.startActivity(myIntent);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
});
}
In your style.xml define your animation
<style name="Animation.CustomAnimation">
<item name="android:activityOpenEnterAnimation">#anim/slide_in_left</item> When opening a new activity, this is the animation that is run on the next activity
<item name="android:activityOpenExitAnimation">#anim/slide_out_right</item>When opening a new activity, this is the animation that is run on the previous activity (which is exiting the screen)
<item name="android:activityCloseEnterAnimation">#anim/slide_in_right</item>When closing the current activity, this is the animation that is run on the next activity (which is entering the screen).
<item name="android:activityCloseExitAnimation">#anim/slide_out_left</item>When closing the current activity, this is the animation that is run on the current activity (which is exiting the screen).
</style>
<style parent="android:style/Theme.Light.NoTitleBar.Fullscreen" name="app_theme">
<item name="android:windowBackground">#drawable/splash</item>
<item name="android:windowAnimationStyle">#style/Animation.CustomAnimation</item>
</style>
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:theme="#style/app_theme">
apply app_theme to your application in android manifest
I had the same problem (on a samsung galaxy s). I found my answer at Activity animation not working in Galaxy Tab
Turns out animations are turned off by default on samsung devices. (It's a setting: Go to Settings -> display -> animations and then turn on the All animations and you will be able to see the animations)
Try this,
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(v.getContext(),
ActivityTwo.class);
startActivityForResult(myIntent, 0);
overridePendingTransition(R.anim.zoomextra, 0);
finish();
}
});

How to connect two activities

I have made one screen with two images and I would like to add a button lower on the page which will navigate to a second page when I click it. Do you know how to code this? I know how to create a button but I don't know how to connect the two screens!
This task is accomplished with the startActivity(); method using Intents.
Intent i = new Intent(FromActivity.this, ToActivity.class);
startActivity(i);
In this case the Intent uses your current Activity as the Context in the first parameter, and the destination Activity in the second parameter.
Make sure that you add your second Activity to the manifest also (it resides in the tag)!
<activity android:name=".ToActivity"
android:label="#string/app_name">
</activity>
To sum it up:
ImageView myImage = (ImageView) findViewById(R.id.image);
myImage.setOnClickListener(new OnClickListener() {
#Override
onClick(View v) {
Intent intent = new Intent(FromActivity.this, ToActivity.class);
startActivity(intent);
}
}
);
Intent intent = new Intent(currentActivity.this,nextActivity.class);
this.finish();
startActivity(intent);
Button start_button=(Button)findViewById(R.id.btnsend);
start_button.setonClickListener(new onClickListener( ){
#override
onClick(View view){
Intent i = new Intent(MainActivity.this, NewActivity.class);
startActivity(i);
}
}
);
Lets break the answer in two parts, XML & JAVA part as every activity has each of these two. Assuming we are having only two activity, 'Activity1' being the one with the button which would redirect user to 'Activity2'.
As we have 2 activity, we would be having 4 files related for these 2 activity.
XML
so lets first do the easy way, as soon as you open the .xml file of Activity1, you should shift to design tab.
After reaching the design part you can insert a button from pallet, now you can select button which is inside your layout. After selection you could see the properties of button in right section of your screen where you can effectly change multiple properties of the button.
Here you shall find the "onClick" option, fill the box next to it with anything very simple, or something which you can remember. For example enter "nextAct"
or
Hard way would be entering the onClick property manually by typing follwing line in button code in XML
android:onClick="nextAct"
This is all on XML part.
JAVA
Open the .java file of Activity1, here you have to make a new method. This method should be named same as in the "onClick" property of button. Over here i would be taking "nextAct" as that is what i had used in XML. You can place this new method anywhere inside the class of the java file, i prefer keeping it at the end of the class as i could easily locate it if any issue in future.
Now you have to write the body of nextAct method. This can be sumed up in these two lines
public void nextAct(View v){
Intent i = new Intent(this, Activity2.class);
startActivity(i);
}
After this both should be connected and working fine.
give id to your button and mention it in your MainActivity.class.Then you can call OnClickListener to listen to your click.
Button mButton = (Button)findViewById(R.id.buttonid);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//you can use anything in place of i
Intent i = new Intent(MainActivity.this, NextActivity.class);
startActivity(i);
}
});

Problems creating a new screen

I am creating a project in which i need to take in some numbers, makes some calculations and then on a new screen create show the answers. I am using an Intent object to go to the new screen:
final Button button = (Button) findViewById(R.id.save);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent();
myIntent.setClass(HelloAndroid.this, screen2.class);
myIntent.putExtra("eFiber", Double.toString(E_fiber));
startActivity(myIntent);
}
});
but when i do this it crashes when i click the button. If i use the same xml file as i do in the first screen then it works just fine, its when i use a different xml file that i have the problems.
Have you registered your second Activity as an Activity in the android-manifest xml?
Under the <application> node, something to the effect of:
<activity android:name=".my.screen2" android:label="#string/app_name"></activity>
With your specific Activity information in place of ".my.screen2"

Categories

Resources