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
Related
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)
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().
I'm attempting to make a simple click action which calls a certain number, I'm on the last stage of the code and I cannot see what I'm doing wrong. Currently its the startActivity action which seems to be presenting the error but I don't know why I have watched multiple tutorials and I can see any difference. When above startActivity it informs me that a call permission is required?
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_home);
//On load the program automatically hides the taskbar.
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
Button b = (Button) this.findViewById(R.id.BTNCall);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent PhoneCall = new Intent(Intent.ACTION_CALL);
PhoneCall.setData(Uri.parse("tel:123"));
startActivity(PhoneCall);
}
});
}
I have also added a permission into the android manifest
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
You should set your target SDK version to lvl 22 or under, because for lvl 23 you need to ask user at run time for permission, look there for a better explanation.
Since you are doing it from a OnClickListener, the "this" pointer references to the clickListener class you are implementing, you need to get the reference to your activity just use:
MyActivityClassName.this.startActivity() //Dont know your class name
That should get rid of the red highlight.
If you are still getting a crash we will need the logcat output to solve it.
Hope this helps.
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
I have this line of code:
Button button = (Button) findViewById(R.id.view);
Which gives the following error:
id cannot be resolved or is not a field
That error is logical, because I have nothing what needs an Id in R.java yet, and therefore Id is missing. This will be generated when I execute the code (because I make buttons at onCreate(), and they get Id's). But Eclipse won't let me run before I fix the problem. So is it safe to add this line of code to R.java:
public static final class id {
}
Or maybe there is another sloution?
If you create buttons in onCreate, they aren't added to the resources. The resources are fixed from the moment you build your APK.
Surely if you created the button, you already have the Button object and don't need to look for it in the resources anyway?
No. Anything you add to R.java will get deleted the next time you build. It is an auto-generated file.
You cannot append R.java. But if there is anything that you want to add you can create another file of your own (like Rcustom.java), keep your stuff in it and import it wherever you use it.
findViewById() is used when you are referencing an XML layout that was usually set by setContentView(), or inflated with LayoutInflater.
So if you have a Button in your main.xml layout, with the id of "view", you would do the following in your Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.view);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Respond to click here
}
});
}