Android Java change theme in program - java

How can I change the the theme of my Android application from light to dark programmatically?
I've tried something like:
setTheme(R.style.Holo_Theme_Light);
but I found nothing which worked for me.

Should be the first line in onCreate, before calling super.onCreate(savedInstanceState); as it is where view processing takes place and your change should be before that to be included in view creation
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Holo_Theme_Light);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
Also, please refer to docs to know where to call your setTheme

You can take a look here: Android - Change app Theme on onClick
I am sorry it's not a comment but I don't have enough reputation :(
EDIT - You can't use the setTheme before the superoncreate, if you will call it before the superoncreate it will work, like here: Change Activity's theme programmatically
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}

See the answer here
It says
As docs say you have to call setTheme before any view output. It seems that super.onCreate() takes part in view processing.
So, to switch between themes dynamically you simply need to call setTheme before super.onCreate like this:
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
So, make sure you have put setTheme(R.style.Holo_Theme_Light); before super.onCreate(savedInstanceState); in your code

Related

I want to change some things in a library it is called .hannesdorfmann:swipeback

I want to change some things in a library it is called .hannesdorfmann:swipeback
first I added :
implementation 'com.hannesdorfmann:swipeback:1.0.4'
this library into my android studio
now I have a problem :
enter image description here
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SwipeBack.attach(this, Position.LEFT)
.setContentView(R.layout.activity_mahsol_activity)
.setSwipeBackView(R.layout.swipeback_default);
I want to remove this.
can you help me?
As docuements say did you try these lines of code and change them to false ?
.setDrawOverlay(true)
.setDivider(drawable)
.setDividerEnabled(true)

Why does the change in position of the setContentView change the behavior of the the app?

When I place "setContentView" above the "NumbersClickListners" line the app works as expected.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NumbersClickListners numbersClickListners = new NumbersClickListners();
TextView numbers = (TextView) findViewById(R.id.numbers);
numbers.setOnClickListener(numbersClickListners);
}
But as soon as the "setContentView" is placed below the three lines starting with "NumbersClickListners" the app crashes. The code looks like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NumbersClickListners numbersClickListners = new NumbersClickListners();
TextView numbers = (TextView) findViewById(R.id.numbers);
numbers.setOnClickListener(numbersClickListners);
setContentView(R.layout.activity_main);
}
I'm pretty much unsure of the reason for this behavior. Can anybody help me with that please?
Let's look at the life of layout.
First of all, you have to create it by declaring XML file, where you do your design in a user-friendly way and name the elements according to your needs.
Now your layout is just a file, that Android doesn't really care about for performance reasons.
Next thing you wanna do is use your layout. To do that your file needs to be converted to internal structure of objects known as ViewGroup and Views.
This process is called inflating. That's the point where the system can find the views for you by calling findViewById().
So in the second snippet you ask activity to find you a button, which is not inflated. That leads to throwing exception.
Generally speaking, first thing you want to do in the onCreate is to call setContentView().

OnClick ButterKnife, nothing happens

I'm trying to use ButterKnife to onClick. I did the code bellow and nothing happens, I've watched tutorials all over the internet, and they do the same thing as I did.
Here is the code
#BindView(R.id.startButton) protected ImageButton mStartButton;
#OnClick(R.id.startButton)
public void startTest(){
Toast.makeText(this, "testing", Toast.LENGTH_LONG).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(MainActivity.this);
}
And if I put the method startTest(); inside the OnCreate, the toast is called when the app runs for the first time, what shows that the ButterKnife is working. But I need that to happen only when the button is clicked.
Thanks
You mentioned that you have compile 'com.jakewharton:butterknife:8.4.0' in your build.gradle file. I think you may be missing the corresponding compiler. Add this to your dependencies section:
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
I think your onCreate method should be public. Try it.
The only two reason i can see are either
'protected' needs to be removed from your ImageView
or
'startButton' is not actually defined in activity_main

Listview need to show the data from the database

I am creating dictionary and I need to display the data from the database in the ListView. Hope someone can help me. Here is the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ls = (ListView) findViewById(R.id.mainlist);
ls.setVisibility(View.VISIBLE);
You will need a custom ArrayAdapter and a method that returns all the values that you need from the database, probably a subclass of the SQLiteOpenHelper class. Read about them in detail and eventually you'll know the answer.

Stuck with Facebook fall back share dialog

When on Android there is not Facebook native app, then in order to share a post from another Android app a web dialog is being opened. Here on step 4 there is a description how to open that dialog (see publishFeedDialog function).
My problems is that all the other code described in this tutorial I have wrote in AppActivity which is a subclass of Activity. Hence, getActivity() method is not defined as far as it is defined for Fragments. To solve this problem I have defined a private variable like this:
private Activity activity;
Assigned a value in onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this;
// .......
}
and used in all places where there was a call of getActivity(). As I not a Java neither an Android expert, I would like to understand whether I have done it correctly.

Categories