Onclick in Textview gets crashed [duplicate] - android

This question already has answers here:
Could not find method in parent or ancestor context
(3 answers)
Closed 4 years ago.
I have a TextView which contains onclick in xml.The onclick works sometimes and sometimes the app crashes.I renamed the TextView in xml to android.support.v7.widget.AppCompatTextView,but it didn't solved the issue.I found similar questions but none of solved mine.
code
public void customerDetails(View view){
Intent intent=new Intent(Customers.this,CustomerDetails.class);
intent.putExtra(Common.CUSTOMERID,view.getTag().toString());
startActivity(intent);
}
My XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
app:cardCornerRadius="4dp"
app:cardElevation="1dp"
app:cardMaxElevation="0dp"
app:cardUseCompatPadding="true"
android:animateLayoutChanges="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="#+id/customer_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/call_button"
android:layout_toLeftOf="#+id/amount"
android:layout_toRightOf="#+id/call_button"
android:layout_toStartOf="#+id/amount"
android:onClick="customerDetails"
android:text="Customer Name"
android:textColor="#color/colorAccent"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/customer_name"
android:text="ContactPersonName"
android:textColor="#color/primary_text"
android:textSize="12sp"
android:id="#+id/contact_person_name"
android:layout_toLeftOf="#+id/amount"
android:layout_toStartOf="#+id/amount"
android:layout_toRightOf="#+id/call_button"
android:layout_toEndOf="#+id/call_button"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Error is
java.lang.IllegalStateException: Could not find method customerDetails(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatTextView with id 'customer_name'

You have to use your own class in xml, as standrt TextView classes do not contain your custom method

Try Adding this line to the root tag of the xml:
tools:context="<yourpackaggename.activityname>"

You should add your listeners in your Activity onCreate method. By using lambdas, it won't grow your code more than a line for every listener:
findViewById(R.id.textView).setOnClickListener(v -> customerDetails(v));
And you can change customerDetails method visibility to private now.
This is a better practice, and you will have a better control on your listeners if you need anything more complex.

Related

TextView is gone and Button is visible

Textview items are not visible. The Button below them is.
Tried changing the LinearLayout with RelativeLayout, changed the match_parent and content with 0dp. None of them worked. Only the button shows in the right spot as though the TextViews are there but invisible. These views are called in the onCreate() of the Activity.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".EnterData">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvDateIn"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
tools:text="Date"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvDescIn"
android:layout_marginTop="15dp"
android:layout_marginStart="15dp"
tools:text="Description"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvAmountIn"
tools:text="California"
android:layout_marginTop="15dp"
android:layout_marginStart="15dp"
android:textColor="#000"
android:textSize="17sp"/>
<Button
android:id="#+id/btnSendData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="Send Data"/>
</LinearLayout>
You try to use android:text instead of tools:text. tools:textonly visible in the Android Studio.
Or you should use yourTextView.setText("yourText") if you are using tools:text.
Instead of using tool:text in xml use android:text="abc" in textView.
As tool:text is used for android studio layout preview . It does not show text when you run your application .
While android:text is used to set text to textview, button etc.
Yep, it was all working and just placing android:text showed it.
Thank you very much.

layout_weight is not working when using data binding text

First of all, sorry about my poor english
I have a recyclerview with xml layout like this
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="township"
type="com.test.feature.Area" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="horizontal"
android:paddingTop="#dimen/_10sdp"
android:paddingBottom="#dimen/_10sdp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#{township.name}"
android:textColor="#color/agent_list_gray_1"
android:textSize="#dimen/_15ssp" />
<android.support.v7.widget.AppCompatRadioButton
android:id="#+id/btnRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"/>
</LinearLayout>
I set text to the textview in Adapter class, and this is what I want
what I want
But if I use data binding with this line instead
android:text="#{township.name}"
I got this
layout_weight is incorrect
So does anyone know what's wrong with my code ?
There are another ways to archive this kind of UI but I really want to know why this won't work
Could you not use RelativeLayout and set the Textview to parentLeft and the EditTextview to parentRight for the spacing?
It's weird! because I implemented your code and it worked. Maybe try wrapping it in a parent view same as below (I did it this way) :
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/backgroundOrange"
android:orientation="horizontal"
android:paddingTop="#dimen/_10sdp"
android:paddingBottom="#dimen/_10sdp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#{township.name}"
android:textColor="#color/textPrimary"
android:textSize="15dp"/>
<android.support.v7.widget.AppCompatRadioButton
android:id="#+id/btnRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Let me know if it worked.
Solution is Simple.
When inflating your layout if your layout is inside another layout pass container layout as parent in the inflate method.
If you don't, all attributes starting with layout will not work.
Your inflating code should look like this.
binding = FragmentTestBinding.inflate(layoutInflator,parent,false)

#id is not working inside Relative Layout

As far as I know, the difference between #+id and #id is to create a resource id first time and reuse that already existed resource id in different places. For instance, If we have a Relative layout having two textViews one below another, we shall use #resourceId for the second textView which refers to the first TextView.
The problem is, after updating the android studio to 3.0, #resourceId is not working anymore.To place second textView below the first one, I need to use #+firstTextViewId instead of #firstTextViewId. More specifically I need to use,
android:layout_below="#+id/totalQty"
instead of
android:layout_below="#id/totalQty"
Here is the code
<RelativeLayout
android:id="#+id/relBottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:id="#+id/totalQty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abcdef"/>
<TextView
android:id="#+id/totalPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/totalQty"
android:text="saasdfdsdfsdf"/>
<TextView
android:id="#+id/totalNetPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/totalPrice"
android:text="abcdsadfsafddgfdgfgdef"
/>
</RelativeLayout>
Is it an understanding issue? or a problem from any end? Can anyone please explain?
I just remove + sign at #+id from your code. Here's the updated code
<RelativeLayout android:id="#+id/relBottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/totalQty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abcdef"/>
<TextView
android:id="#+id/totalPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/totalQty"
android:text="saasdfdsdfsdf"/>
<TextView
android:id="#+id/totalNetPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/totalPrice"
android:text="abcdsadfsafddgfdgfgdef"
/>
</RelativeLayout>

Android TextView is not clickable

I have this small piece of code, and I started using relative layouts and the textview is no longer clickable. I am posting the XML below.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/fav_list_selector"
android:orientation="horizontal"
android:padding="5dip">
<ImageButton
android:id="#+id/pinImage"
android:layout_alignParentLeft="true"
android:src="#drawable/ic_9_av_make_available_offline"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<TextView
android:id="#+id/favBusItem"
android:layout_toRightOf="#+id/pinImage"
android:layout_toLeftOf="#+id/transpoImage"
android:layout_centerVertical="true"
android:textSize="12sp"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:clickable = "true"
android:textColor="#000000">
</TextView>
<ImageButton
android:id="#+id/transpoImage"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/pinImage"
android:src="#drawable/ic_9_av_make_available_offline"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</RelativeLayout>
I suspect that the textview is not clickable because of the ImageButton.
Well, I figured it out myself.. Anyway here is the answer in case anyone else hits the same issue:
add this attribute for your imagebutton
android:focusableInTouchMode="false"
also add
android:descendantFocusability="blocksDescendants"
for your parent layout
This worked for me
use android:clickable="true", in this part of your code:
<ImageButton
android:id="#+id/transpoImage"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/pinImage"
android:src="#drawable/ic_9_av_make_available_offline"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
Use an OnClickListener in your class and hook it up to your TextView.

This RelativeLayout layout or its LinearLayout parent is useless

I'm developing an Android 3.1. and above application.
On one activity I generate its layout dynamically. I'm using formdetail.xml as contentview:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.formdetail);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
currentFormId = extras.getString("FormId");
}
}
formdetail.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/downloadFormsButton"
android:enabled="false"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/download_forms_button" />
<TextView
android:id="#+id/formErrorMsg"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
</RelativeLayout>
</LinearLayout>
downloadFormsButton and formErrorMsg must be always in form.
But using that xml I get this warning:
This RelativeLayout layout or its LinearLayout parent is useless
I need linearLayout to add TableLayouts programmatically.
How can I solve this warning?
Lint is correct in it's warning, your LinearLayout isn't doing anything useful, since its only child is a RelativeLayout. Remove the LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="#+id/downloadFormsButton"
android:enabled="false"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/download_forms_button" />
<TextView
android:id="#+id/formErrorMsg"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" />
</RelativeLayout>
You either ignore it or right click your project. Click Build Path->Configure Build Path.... Under Android Lint Preferences look for UselessParent and set it's severity to ignore or click Ignore All.
EDIT:
I take that last part back. I think Ignore All will wipe all Lint warnings and errors.
I was also having the same issue with my app layout.
I don't know how and it is right way or not, but this get resolved after setting background attribute to both layouts.
Now there is no warning and working perfect. Hope it will also work for you.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:background="#drawable/bg"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/bg2"
android:layout_marginTop="10dp" >
<ImageView
android:id="#+id/img_BookImage"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitXY"
android:contentDescription="#string/India"
android:src="#drawable/india" />
</FrameLayout>
</FrameLayout>
That is just an warning from lint. Lint doesn't know you would later append views to this layout and it will warn you that you added an unnecessary extra view(increasing your layout depth).
You should just ignore the warning(If it really bothers you here is a link on how to remove warnings from lint).

Categories

Resources