Could not find method android.view.View.getParentForAccessibility - android

Getting the following error onclick of minus remove button:
Could not find method android.view.View.getParentForAccessibility,
referenced from method com.wassap.main.UserFragment$12.onClick
Relevant code:
/////////////////////// minus remove button click
imgRemove.setOnClickListener(new OnClickListener()
{
#SuppressLint("NewApi")
public void onClick(View vv)
{
View parent = (View) vv.getParentForAccessibility();
}
});

getParentForAccessibility is available since Android 16. So you need a device with at least that api level to run it

Related

show NextButton but hide PrevButton in MediaController

is there a way to show just the NextButton, and hide the PrevButton in mediaController?
I can add both the next and prev buttons using the following codes but there does not seem to be any way to add just one button.
(using retroLambda)
mediaController.setPrevNextListener(v->handleNextVideoClicked(),null);
or
mediaController.setPrevNextListeners(new OnClickListener() {
#Override
public void onClick(View v) {
handleNextVideoClicked();
}
}, null);
any help is appreciated.
The easiest way to just hide the previous button I can think of is this, create a class extend MediaController and override the setAnchorView method but call the super method before doing anything in this method, then just hide the previous button.
#Override
public void setAnchorView(View view) {
super.setAnchorView(view);
findViewById(com.android.internal.R.id.prev).setVisibility(GONE);
}
I haven't got the time to test this, but you can give this a try

error in OnLongClickListener() method

akashButton.setOnLongClickListener(
new Button.OnLongClickListener(){
public boolean onClick(View v){
TextView akashHacked = (TextView)findViewById(R.id.akashHacked);
akashHacked.setText("Wow, that was a long one");
return true;
}
}
);
On writing this code in android studio I am getting a red line under Button.OnLongClickListener() and the error says
" class'Anonymous class derived from OnLongClickListener' must either be declared abstract or implement abstract method 'OnLongClick(View)' in 'OnLongClickListener' "`
Please help me in fixing this error.
First you will have to retrieve your button from your layout :
Button myButton = (Button) findViewById(R.id.myButtonId);
then, you will have to set your button's listener :
myButton.setOnLongClickListener(new Button.OnLongClickListener(){
public boolean onLongClick(View v){
// do whatever you want here
}
});
Have you implemented OnItemClickListener (or AdapterView.OnItemClickListener)? If so, remove it.

Android - In a button On click event, button goes missing

I am trying to change the text color of button on click event. But while the button click event is fired, button goes missing. Code mentioned below.
Button design in Layout XML file
<Button
android:id="#+id/btnCategory1"
android:background="#000000"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:layout_margin="10dp"
>
</Button>
Activity.java file
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_expense);
btnType1 = (Button)findViewById(R.id.btnCategory1);
btnType1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == (View)btnType1)
{
btnType1.setTextColor(R.color.darkorange);
}
}
Tried the below Option also. Still Button Goes missing. Log statement is fired successfully.
btnType1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.v("AAAAAAAAAAA","BBBBBBBBBBB");
// TODO Auto-generated method stub
btnType1.setTextColor(R.color.orange);
}
});
If someone can find the reason, kindly share it.
You cannot use just the R.color integer when calling setTextColor. You need to call getResources().getColor(R.color.YOURCOLOR) to set a color properly.
Make your button as below
Button bOne = (Button) findViewById(R.id.btnCategory1);
bOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
bOne.setTextColor(getResources().getColor(R.color.YOURCOLOR));
}
});
Hmmmmm. I'm not seeing a good reason as to why that would happen.
I do think that there is maybe a better/cleaner way to do something so simple, and so I'll tell you it - go ahead and try it. This was should work.
Get rid of the btnType1.setOnClickListener(this); line from your java.
Then, go into your xml and add this to your button:
android:onClick="methodName"
Now, if you go into your java and create a method called methodName that takes a view as an argument:
public void methodName(View v) {
btnType1.setTextColor(R.color.darkorgange);
}
The color should be updated!
EDIT: Just looked again and the reason the previous code wasn't working was because you were trying to update btnType2, not btnType1. Still, try the method I just gave you. It is good practie, and a cleaner and easier way to do things for the future.
EDIT2:
All right, the mystery is solved. Here is your issue. When you do set color, you need to pass in the actual color, not just the id. Here is what you need to change that line to:
btnType1.setTextColor(getResources().getColor(R.color.darkorange));

Change View Text Color by code

Well, I developed a menu with eight Buttons for an App. So, every time the user clicks on in one of the buttons, such button changes its background. And I would would like to change its color as well. But I got now idea how, since setTextColor does not work with Views.
I'm using View because its part of onClick method that I override in order to achieve what I want. So, here go the code:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.setBackgroundResource(R.drawable.degrade_menu);
}
So, how could I change the text color?
Cheers,
Cast your v to TextView and then set the text color. Do not forget to read color from resourse
((TextView)v).setTextColor(getResources().getColor(R.color.errorColor));
quick solution:
final Button button = (Button) findViewById(<id>);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
button.setTextColor(<color>);
}
};
better solution: use states
Cast the view to a button. Then you can use settextcolor

android how to write click event of an ImageView

I have an alarm symbol on my interface. I want it like if anyone clicks on this then he/she can get another interface.
so how to write a click event of Imageview. plz give me answer. Thank you
You can add an OnClickListener to ImageView using setOnClickListener method on your ImageView and write your code in the listener's onClick method.
imageView.setOnClickListener(clickListener);
OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
if (v.equals(imageView)) {
// Write your awesome code here
}
}
};
Faster.
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Your code.
}
});
with lambda expression
img_drawer_open_icon.setOnClickListener(v -> Home.drawerLayout.openDrawer(GravityCompat.START));
Also just do it in some easy steps visually:
1- Click your Image
2- From Attribute, Select OnClick and Type a Name Such as onCkick_btnST
3- In code of Your xml, find and select the above verb
4- using yellow bulb menu, select "Create OnClick Event Handler"
5- Fill your Code in created area.
The benefit of this methode is that U`LL see that event title in structure tab of Android Studio!

Categories

Resources