Android onClick not working on some android versions - android

I have set a couple of TextViews to call a method using the XML OnClick parameter. It works fine on a Lollipop device but it doesn't work on KitKat or older Android versions.
Why does it happen? Is there a way I can solve it without setting listeners for each view id?

Use android:clickable="true" in your textview.
In alternative, you can put your click logic in source code:
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});

Without any code I have to assume that, since you're talking about challenges with pre-lollipop devices, you're having trouble with elevation values.
Check out this link:
Add elevation/shadow on toolbar for pre-lollipop devices

Related

onClickListener in ViewHolder is not called in android pre lollipop

I have a RecyclerView and I define an OnClickListener to the View of the holder. everything works great on android 5 devices but on 4.4.4 an 4.2.2 for example, the onClickListener is never called.
Here is the relevant code:
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClick(getAdapterPosition());
}
});
OK, I solved the problem as there was another layer drawn over it according the SDK version.
Debug GPU overdraw (in the developer options) was very helpful.

My apps won't display "KitKat" toast messages

So I just updated my phone to KitKat android 4.4.2 and the apps I made do not show the new themed KitKat toast. I import android.widget.Toast but still not showing the newly themed one. Is my ROM overriding it? However, Facebook does show the new toast. Not really important, just puzzled me.
Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "This toast is not kit kat", Toast.LENGTH_LONG).show();
}
});
}
Better use YourActivity.this instead of getApplicationContext()
Welp, after much confusion. I finally figured it out! :D
I decided to go to the Google source code for the toast notifications. I then decompiled my ROMs framework-res.apk and found the same source. However, i found an aditional source with a "tw" in front of it. This stands for "TouchWiz".
So simply, Samsung was overriding certain toast messages. I simply went and made both sources match the default source and now all my toasts are the nice neat KitKat toast themed :)
Thanks everyone for chatting with me about it!

Is it possible to change the black background color of android app drawer programmatically?

I'm working on an android apk in which the app drawer background is changed. I have searched through the internet. I have found 2 methods to change the app drawer's black background.
First one is using apktool, which decompiles launcher.apk and builds it back to apk format. In between the programmer do the required changes ( res/values/colors , background="..." ). Then sign and move it back to system/app folder.
Second one is using another application like GO Launcher, but I have to use my own application to change the black background color programmatically.
Is there a way to achieve this ( in eclipse for example ) ?
Thank you for your supports and answers in advance.
Here is an example for your requirement (programatically):
redBtn.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.d("MYAPP", "RED touched..");
setActivityBackgroundColor(Color.RED);
return false;
}
});
The function defination for setActivityBackgroundColor(); is given below:
public void setActivityBackgroundColor(int color) {
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
This worked for me, you can modify same as per your requirement.
Accept the answer if it meets your requirement.
-Regards

OnClickListener Error

I am new to Android and tried to run the sample code of the SDK using eclipse. I created the project (via File → New → Project → Android → Android Sample Project), but when I run the project it shows the following error for all the sample projects:
Multiple markers at this line
The method onClick(View) of type new View.OnClickListener(){} must override a superclass method
implements android.view.View.OnClickListener.onClick
Then I refered to this forum. Here they mentioned a "compiler compliance level". But my compiler compliance level is on 1.7, and I am using Android 4.0.3 with an API level of 15.
So please help me to rectify this error.
Make sure that above your OnClick method you have the annotation #Override like so
#Override
public void OnClick(View v) {
//Code goes here
}
By having #Override, Java knows that you are not creating a new method, in this situation you are letting Java know that you are utilizing that listener.
If you have already done this, check this website http://androidcodemonkey.blogspot.com/2011/10/how-to-solve-must-override-superclass.html
Remove the #Override annotation tag.
For some situation. the eclipse can't recognize.
I just had the same issue.I fixed it by following change of code
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Use View.OnClickListener(){} instead of just OnClickListener(){};
I think this might due to various OnClickListener are there. May be we need to specify which is the relevant one. I would really love to know the real reason if any one knows.

Spinner works in 2.3 but not 3.2

I have been successfully running an app under android 2.2 (api8) using spinners with OnItemSelectedListeners. It was built with tarketSdkVersion = 8 and minSdkVersion = 8. I am now trying to run it on a 3.2 device, but the spinners can not be selected. They do populate with the default array value however, so the adapter appears to be working. Clicking on the spinners results in no reaction. I tried building with tarketSdkVersion = 13 and minSdkVersion = 13, but the spinners are still dead. I am using slightly customized spinner versions to achieve "wrap_content" in a multiline_spinner_dropdown_item.xml file. Is there a compatibility issue with spinners since 2.2?
I tried the .setEnabled(true) but it didn't work. In frustration, I started to remove sections of code from main.java and layout.xml until a single spinner was left and working. As I added the code and controls back, I found that a ScrollView nestled inside a TabHost/LinearLayout/TabWidget/FrameLayout prevented the Spinners from responding. By removing the Scrollview, the Spinners worked in 3.2. For some reason the Scrollview worked in 2.2, but not 3.2.
I had the same problem, I used the spinner in 2.2 ,it was working but the same do not work in 3.2, The problem was with the default theme of 3.2, its holo. because of that the spinner is not shown properly,
Just create a theme in values/style, and apply it to your activity in android.manifest file.
Are you by any chance changing the visibility of the spinner?
I had a similar problem, and it was necessary to call setAdapter() every time the spinner was re-shown, otherwise it became immune to clicks.
You can see the change I made to my project that fixed this problem at https://github.com/nikclayton/android-squeezer/commit/7a148edf5f1b3eaca7718161de18254970290ce0.
Perhaps some visibility issues ?
Are you stacking the ScrollView on top of the area where the spinners' items show up ?
Try to use different states for the spinners by changing their icon or color for example and see if they get the click event. If they do, then it is probably something related with visibility. If they don't, it might something else.
Some code sample could help :)
Have you tried setting the spinner as enabled?
spinner.setEnabled(true);
Although it would be weird that it was disabled on default. Your symptoms do describe a disabled spinner though
Oddly, I tried another app of my that works great on 2.2. When I installed it on a 3.1 tablet or emulator, the onClick events won't fire. No spinners this time, but the listeners don't work either! Here's some of the code:
//In onCreate:
//setup listeners
rbSlab = (RadioButton)findViewById(R.id.rbSlab);
rbBeam = (RadioButton)findViewById(R.id.rbBeam);
rbSlab.setOnClickListener(radio_listener);
rbBeam.setOnClickListener(radio_listener);
etFpc.setOnEditorActionListener(this);
etFy.setOnEditorActionListener(this);
etBw.setOnEditorActionListener(this);
etDp.setOnEditorActionListener(this);
etMu.setOnEditorActionListener(this);
...
}//end onCreate
//In Main body of app:
//for radio buttons
private OnClickListener radio_listener = new View.OnClickListener()
{ //#Override
public void onClick(View v) {
DoCalcs();
}//onClick
};
//for edittext
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{ if ((actionId == EditorInfo.IME_ACTION_DONE) || //if DONE button pushed
((event.getAction()==KeyEvent.ACTION_DOWN) && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))) //if ENTER button pushed
{
//do calcs
return(true);
}
else return(false);
}
I've built this with target API 8 and min API 8, and also with target API 14, min 8, and when installed on a API 8 the listeners work fine, but not with API 14!
I had a similar problem. It's because of the default theme of Android 3.2 and above versions.
The simpler solution is to use a custom spinner, or to make any background colour or picture for the same, like:
android:background="#drawable/spinner"
This link will help you for custom spinner.

Categories

Resources