I have a setOnClickListener() for a Google Pay Button in the OnCreate() in a fragment. I'm using viewbinding to get access to it.
override fun onCreate(savedInstanceState: Bundle?) {
setHasOptionsMenu(true)
super.onCreate(savedInstanceState)
binding = AddJobScreenBinding.inflate(layoutInflater)
paymentsClient = PaymentsUtil.createPaymentsClient(activity as Activity)
possiblyShowGooglePayButton()
binding.include.googlePayButton.setOnClickListener( { requestPayment() })
The PayButton is included in the xml of my fragment here:
<include
android:id="#+id/include"
layout="#layout/googlepay_button"
android:layout_width="378dp"
android:layout_height="69dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
this is the button itself:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:clickable="true"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="48sp"
android:background="#drawable/googlepay_button_no_shadow_background"
android:paddingTop="2sp"
android:contentDescription="#string/googlepay_button_content_description">
<LinearLayout
android:duplicateParentState="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="fitCenter"
android:duplicateParentState="true"
android:src="#drawable/googlepay_button_content"/>
</LinearLayout>
<ImageView
android:id="#+id/googlePayButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:duplicateParentState="true"
android:scaleType="fitXY"
android:src="#drawable/googlepay_button_overlay" />
</RelativeLayout>
When the button gets clicked though the callback method requestPayment doesn't get called.
Why is this?
It seems that you are setting the click listener to an ImageView inside of the button. Try setting it to the button element itself:
binding.include.googlePayButton.setOnClickListener( { requestPayment() })
It'd be useful to give the element a more descriptive name too (eg.: #+id/googlePayButton).
Related
On the Java side, I get Famous error: on a null object object In the event that definition conditions "findVIewById" are correct .
It is interesting that textView With id :#+id/txt_detail_pPrice is identified but #+id/txt_detail_Price NO and throw :on a null object
why findVIewById does not work just for #+id/txt_detail_Price and "price" in java side become
null??
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_details)
val imgProduct=findViewById< MyImageView >(R.id.img_details_showpicProduct)
val txt_title=findViewById<TextView >(R.id.txt_detail_showTitle)
val waranty=findViewById< TextView >(R.id.txt_details_waranty)
val pPrice=findViewById< TextView >(R.id.txt_detail_pPrice)
val price=findViewById<TextView>(R.id.txt_detail_Price)
val color=findViewById< TextView >(R.id.txt_details_color)
val detailIntroduction=findViewById<TextView>(R.id.txt_details_introduction)
val ratingBar=findViewById<RatingBar>(R.id.details_ratingBar_showratingProduct)
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="match_parent"
tools:context=".Details.DetailsActivity">
<com.google.android.material.appbar.AppBarLayout...>
<androidx.core.widget.NestedScrollView...>
<RelativeLayout
android:layout_width="match_parent"
android:layout_gravity="bottom"
android:background="#color/purple_500"
android:layout_height="75dp">
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:backgroundTint="#color/greenIcon"
android:text="افزودن به سبد خرید"
android:textColor="#color/white" />
<TextView
android:id="#+id/txt_detail_pPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="10000000میلیون"
android:textColor="#color/teal_700" />
<TextView
android:id="#+id/txt_detail_Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="20000000میلیون"
android:textColor="#color/black" />
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
If you are quite certain the corresponding view that it can't find is in the layout you passed to setContentView, I think the most likely cause for this issue is that you have multiple versions of this same layout for different screen configurations, and forgot to include a view with this ID in all of them. When the screen is in a configuration where the view is not included, it cannot be found.
I am trying to make a layout with an Edit Text field for email requests. The design makes it so that the keyboard is always opened over the editText View which makes it impossible to know what you are entering. I have tried many suggestions on stack overflow but most of them are for API <24 (though I don't think this has much effect on this).
In the Activity file I have tried the following:
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN or WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN or WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
In Android Manifest I have also tried the following, both in conjunction and separately:
android:windowSoftInputMode="adjustPan"
android:windowSoftInputMode="stateHidden|adjustPan"
android:windowSoftInputMode="adjustResize"
I also wrapped my layout in a ScrollView as described by Heena
here : https://stackoverflow.com/a/39867438/5635144
Below is my XML and the accompanying activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="match_parent"
tools:context=".EmailResetActivity">
<RelativeLayout
android:id="#+id/bg"
android:layout_width="match_parent"
android:layout_height="419dp"
android:background="#drawable/signin_up_anim"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
<EditText
android:id="#+id/e_request"
android:layout_width="305dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="48dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="#string/email_address"
android:inputType="textEmailAddress"
android:paddingStart="10dp"
android:paddingBottom="15dp"
android:textColorHint="#color/hintgrey"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.522"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/bg" />
<LinearLayout
android:layout_width="wrap_content"
android:id="#+id/buttonsContainer"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/e_request">
<Button
android:id="#+id/sendRequestBtn"
android:layout_width="150dp"
android:layout_height="45dp"
android:background="#drawable/large_btn"
android:text="#string/submit"
android:textColor="#color/white" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
class EmailResetActivity : AppCompatActivity() {
lateinit private var auth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_email_reset)
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN or WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
auth = FirebaseAuth.getInstance()
val animDrawable = resetPassInfoBg.background as AnimationDrawable
animDrawable.setEnterFadeDuration(2500)
animDrawable.setExitFadeDuration(1500)
animDrawable.start()
sendRequestBtn.setOnClickListener(View.OnClickListener {
val email = e_request.getText().toString().trim()
if (TextUtils.isEmpty(email)) {
Toast.makeText(application, "Enter your email ", Toast.LENGTH_SHORT).show()
return#OnClickListener
}
})
}
}
I would like for the layout to be moved up so the editText Field is visible but currently nothing is making it work.
Remove this line:
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
With FLAG_LAYOUT_NO_LIMITS the application is not able to calculate how to scroll your view, because your layout has no limits.
I have a problem with my layout of my App. I have 2 Fragments which must be displayed in an Activity. It works fine, but the Layout somehome displays an EditText twice and also behind each other. You can see the hint of the EditText is darker and once I type something in the TextEdit I can see the other EditText independently still visible in the background. Also the TextView and Switch is displayed twice as you can see from the darkness of the appearance:
The Layout files:
activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".QuickSteuerActivity">
<fragment
android:id="#+id/calculation_fragment"
android:name="de.immozukunft.quicksteuer.CalculationFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<fragment
android:id="#+id/settings_overview_fragment"
android:name="de.immozukunft.quicksteuer.SettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/> </LinearLayout>
fragment_calculation.xml (This is where the EditText and Switch etc. is)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_calculation"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/turnover"
android:defaultValue="0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/turnover_tax"
android:inputType="numberDecimal"
android:textSize="36sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/turnover_vattxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="#string/turnover_vattxt"
android:visibility="visible" />
<Switch
android:id="#+id/turnover_vat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:foregroundGravity="top"
android:title="#string/industrial_tax"
android:visibility="visible" />
</LinearLayout>
<EditText
android:id="#+id/costs"
android:defaultValue="0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/costs"
android:inputType="numberDecimal"
android:textSize="36sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/costs_vattxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="#string/turnover_vattxt"
android:visibility="visible" />
<Switch
android:id="#+id/costs_vat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:title="#string/industrial_tax"
android:visibility="visible" />
</LinearLayout>
<Button
android:id="#+id/btncalculate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/calculate"
android:textSize="24sp" />
</LinearLayout>
</FrameLayout>
fragment_settings_overview.xml (2.Fragment)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="#+id/fragment_settings_overview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/current_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:padding="10dp"
android:text="#string/overview_settings"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</FrameLayout>
I can also provide the Java-files if necessary or any additional files. I just didn't want to bloat the post.
If anybody has got any other additional advices (coding style etc.) please let me know, I am relatively new to Android programming :)
thanks in advance
THE-E
EDIT:
I think I am messing up the layout with the fragment manager. But I am not sure. Because now I see even some PreferenceFragment.
onCreate() of the Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //Layout
manager = getSupportFragmentManager(); //Fragment-Manager
CalculationFragment calculationFragment = new CalculationFragment(); //create calculation fragment
SettingsOverviewFragment settingsOverviewFragment = new SettingsOverviewFragment(); //create settings overview fragment
calculationFragment.setArguments(getIntent().getExtras()); //I don't know what happens here
settingsOverviewFragment.setArguments(getIntent().getExtras()); //I don't know what happens here either
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.calculation_fragment, calculationFragment); //add calculation fragment to the fragment transaction
transaction.add(R.id.settings_overview_fragment, settingsOverviewFragment); //add settings overview fragment to the fragment transaction
transaction.commit(); //commit the transaction
}
Change height and width of both the fragments to wrap_content and see if it works for you ?
I've used Hint in the second text and TEXT in the first
android:hint="#string/turnover_tax"
To display the text dimly use the Hint instead of the Netscape
android:hint="Your String"
android:text="Your String"
I've used Hint in the second text and TEXT in the first
android:hint="#string/turnover_tax"
To display the text dimly use the Hint instead of the Netscape
android:hint="Your String"
android:text="Your String"
Using the Hint means a tip which must be written in the EditText
When you type Netscape in the Netscape EditText, the embedded grid will appear in the EditText and can be set or changed
But when you write a hint it will disappear once you start typing in the EditText that contains it
I found my mistake. I was adding in the onCreate of the main Activity a Fragment on top of the already set Fragment using the FragmentManager. The Fragment was already set in the layout ( itself, that is why I don't need to use a FragmentManager (fragment_calculation.xml).
All I had to do is set the layout of the activity and I was done.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //Layout
}
this was my onCreate()-Method.
thanks for your help! :)
I've got a FrameLayout with two nested LinearLayouts. I want an onClickListener() for FrameLayout. After a search I got a solution clickable="false".
This is my Layout file:
<FrameLayout
android:id="#+id/flOuter"
android:paddingLeft="#dimen/half_margin"
android:paddingRight="#dimen/half_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:clickable="false"
android:gravity="center"
android:id="#+id/flAddIdProof"
android:background="#drawable/add_background"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:clickable="false"
android:id="#+id/ivIcon"
android:src="#drawable/ic_add_black_48dp"
android:background="#drawable/add_button_border"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:clickable="false"
android:id="#+id/llLinear"
android:alpha="0.5"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:enabled="false"
android:clickable="false"
android:drawablePadding="#dimen/half_margin"
android:drawableLeft="#drawable/ic_devices_black_24dp"
android:hint="Identity Proof"
android:inputType="none"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:enabled="false"
android:clickable="false"
android:drawableLeft="#drawable/ic_border_color_black_24dp"
android:hint="Identity Number"
android:drawablePadding="#dimen/half_margin"
android:inputType="none"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
Here is my click listener:
flOuter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(DeviceInMeeting.this, "detect", Toast.LENGTH_SHORT).show();
}
});
I don't know how to debug for onClick listeners, clickable="false" is not working in my case.
Try implementing android:clickable="true" in Following code snippet for FrameLayout
<FrameLayout
android:id="#+id/flOuter"
android:clickable="true"
android:focusable="true"
android:foreground="attr/selectableItemBackground"
android:paddingLeft="#dimen/half_margin"
android:paddingRight="#dimen/half_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
and adding android:clickable="false" in other nested layout.
<LinearLayout
android:id="#+id/flOuter"
android:clickable="false" />
You are using it wrong. Try using:
<FrameLayout
android:id="#+id/flOuter"
android:paddingLeft="#dimen/half_margin"
android:paddingRight="#dimen/half_margin"
android:layout_width="match_parent"
android:clickable="true"
android:layout_height="wrap_content">
// Other layouts there
</FrameLayout>
For fields you don't want to be clicked add clickable=false to them.
And for fields for which you wan't click event add clickable=true for them.
One thing to remember here is that if your child elements contain some clickable elements like buttons and edittexts and then you want to get click event on parent layout then add clickable false to the child and true to the parent.
You need to set OnTouchListener instead onClickListener. In onTouch method you can decide if touch event is pass to children (by returning false) or consume event.
On your case you should do your logic and return true.
I tried a lot but, my Linearlayout as below not getting clicked :
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/ll_top_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="false"
android:orientation="vertical">
<LinearLayout
android:focusableInTouchMode="true"
android:clickable="true"
android:id="#+id/ll_Promos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/vv_divider4"
android:layout_marginTop="#dimen/dimen_5"
android:orientation="horizontal"
android:padding="#dimen/dimen_10">
<ImageView
android:id="#+id/iv_restaurant_promo"
android:layout_width="#dimen/dimen_25"
android:layout_height="#dimen/dimen_25"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/dimen_5"
android:layout_marginRight="#dimen/dimen_5"
android:src="#drawable/promos" />
<com.app.thelist.view.CustomTextView
android:id="#+id/txt_restaurant_promo"
style="#style/RegularFont"
android:layout_gravity="center"
android:layout_marginRight="#dimen/dimen_5"
android:layout_weight="1"
android:padding="#dimen/dimen_5"
android:text="#string/promo"
android:textColor="#color/txt_sub_title"
android:textSize="#dimen/sp_15" />
<ImageView
android:id="#+id/iv_restaurant_promo_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/dimen_5"
android:src="#mipmap/app_icon"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</ScrollView>
The above is my xml file, and I have make my LinearLayout named : ll_Promos not getting clicked. I have implemented my onClick() as below in java file :
ll_promos = (LinearLayout) findViewById(R.id.ll_Promos);
ll_promos.setOnClickListener(this);
case R.id.ll_Promos:
Intent intentProm = new Intent(Activity1.this, Activity2.class);
startActivity(intentProm);
break;
Remove
android:focusableInTouchMode="false" // Problem starts from here
Set whether this view can receive focus while in touch mode. Setting
this to true will also ensure that this view is focusable.
And Add
android:focusableInTouchMode="true"
android:clickable="true"