This question already has answers here:
Null pointer Exception - findViewById()
(12 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
The error is
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
my java is
package com.doctorfinderapp.doctorfinder;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;
public class DoctorProfileActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
//get data from previous activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Invia una mail a Dottore", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//vado sul feed
RelativeLayout feedback = (RelativeLayout) findViewById(R.id.feedback);
feedback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(DoctorProfileActivity.this, FeedbackItemActivity.class);
startActivity(intent);
}
});
}
}
xml code where is declared the function "feedbackfunction"
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:layout_marginTop="#dimen/activity_horizontal_margin"
android:padding="#dimen/activity_horizontal_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.letsnurture.ln_202.coordinatorlayoutpart1.ScrollingActivity"
tools:showIn="#layout/activity_scrolling">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="74dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="#dimen/activity_horizontal_margin"
android:layout_weight="0.61">
<ImageView
android:id="#+id/ivContactItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:src="#drawable/ic_account_circle_black_24dp" />
<TextView
android:id="#+id/tvNumber1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_toEndOf="#id/ivContactItem1"
android:layout_toRightOf="#id/ivContactItem1"
android:text="Lorenzo Gitto"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#212121" />
<ToggleButton
android:id="#+id/chkState1"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:background="#drawable/ic_account_circle_white_24dp"
android:checked="true"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="#dimen/activity_horizontal_margin">
<ImageView
android:id="#+id/ivContactItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:src="#drawable/ic_info_black_24dp"
android:visibility="visible" />
<TextView
android:id="#+id/tvNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_toEndOf="#id/ivContactItem2"
android:layout_toRightOf="#id/ivContactItem2"
android:text="Chirurgia Toracica"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#212121" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvNumber2"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="8dp"
android:layout_toEndOf="#id/ivContactItem2"
android:layout_toRightOf="#id/ivContactItem2"
android:text="Chirurgia Vascolare"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#727272" />
<ToggleButton
android:id="#+id/chkState2"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:background="#drawable/ic_info_white_24dp"
android:checked="false"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<include layout="#layout/item_divider" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="#dimen/activity_horizontal_margin">
<ImageView
android:id="#+id/ivContactItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:src="#drawable/ic_book_black_24dp" />
<TextView
android:id="#+id/tvNumber3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_toEndOf="#id/ivContactItem3"
android:layout_toLeftOf="#+id/chkState3"
android:layout_toRightOf="#id/ivContactItem3"
android:layout_toStartOf="#+id/chkState3"
android:ellipsize="end"
android:maxLines="1"
android:text="Anni di esperienza: "
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#212121" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvNumber3"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="8dp"
android:layout_toEndOf="#id/ivContactItem3"
android:layout_toRightOf="#id/ivContactItem3"
android:text="11"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#727272" />
<ToggleButton
android:id="#+id/chkState3"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:background="#drawable/ic_info_white_24dp"
android:checked="true"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="#dimen/activity_horizontal_margin">
<ImageView
android:id="#+id/ivContactItem4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:src="#drawable/ic_explore_black_24dp"
android:visibility="visible" />
<TextView
android:id="#+id/tvNumber4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_toEndOf="#id/ivContactItem4"
android:layout_toLeftOf="#+id/chkState4"
android:layout_toRightOf="#id/ivContactItem4"
android:layout_toStartOf="#+id/chkState4"
android:ellipsize="end"
android:maxLines="1"
android:text="Luogo di lavoro"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#212121" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvNumber4"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="8dp"
android:layout_toEndOf="#id/ivContactItem4"
android:layout_toRightOf="#id/ivContactItem4"
android:text="Via del Porto, 21"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#727272" />
<ToggleButton
android:id="#+id/chkState4"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:background="#drawable/ic_info_white_24dp"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<include layout="#layout/item_divider" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:onClick="feedbackfunction"
android:padding="#dimen/activity_horizontal_margin">
<ImageView
android:id="#+id/ivContactItem5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:src="#drawable/ic_exit_to_app_white_24dp" />
<TextView
android:id="#+id/tvNumber5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_toEndOf="#id/ivContactItem5"
android:layout_toLeftOf="#+id/chkState5"
android:layout_toRightOf="#id/ivContactItem5"
android:layout_toStartOf="#+id/chkState5"
android:ellipsize="end"
android:maxLines="1"
android:text="Punteggio Feedback"
android:clickable="true"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#212121"
/>
<ToggleButton
android:id="#+id/chkState5"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:background="#drawable/ic_book_white_24dp"
android:checked="true"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="#dimen/activity_horizontal_margin">
<ImageView
android:id="#+id/ivContactItem6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:src="#drawable/ic_book_black_24dp"
android:visibility="visible" />
<TextView
android:id="#+id/tvNumber6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_toEndOf="#id/ivContactItem6"
android:layout_toLeftOf="#+id/chkState6"
android:layout_toRightOf="#id/ivContactItem6"
android:layout_toStartOf="#+id/chkState6"
android:ellipsize="end"
android:maxLines="1"
android:text="Ulteriori info"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#212121" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvNumber6"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="8dp"
android:layout_toEndOf="#id/ivContactItem6"
android:layout_toRightOf="#id/ivContactItem6"
android:maxLines="10"
android:text="Vai Su linkedin per vedere le mie info e competenze...http://www.linkedin.com/NomeDottore "
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#727272" />
<ToggleButton
android:id="#+id/chkState6"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:background="#drawable/ic_exit_to_app_white_24dp"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<include layout="#layout/item_divider" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prenota appuntamento"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="Videochiama"
/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Can someone help me?? Thanks to all
You don't have any RelativeLayout with id equals feedback.
RelativeLayout feedback = (RelativeLayout) findViewById(R.id.feedback);
So you feedback object is null and you can't execute onClick event on a null object
Solution
Set the RelativeLayout id = feedback
Looks like you're not setting the #id for that RelativeLayout:
android:id="#+id/feedback"
Related
My activity does not sync with its layout xml. I really don't know why the coding seems okay. I'm a beginner dev.
This is my activity using Kotlin in Android Studio:
Code:
package com.pkndegwa.mycarmaintenance
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.work.Data
import androidx.work.ExistingWorkPolicy.REPLACE
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import kotlinx.android.synthetic.service_list_item.*
import com.google.android.material.snackbar.Snackbar.LENGTH_LONG
import com.google.android.material.snackbar.Snackbar.make
import com.pkndegwa.mycarmaintenance.NotifyWork.Companion.NOTIFICATION_ID
import com.pkndegwa.mycarmaintenance.NotifyWork.Companion.NOTIFICATION_WORK
import java.lang.System.currentTimeMillis
import java.text.SimpleDateFormat
import java.util.*
import java.util.Locale.getDefault
import java.util.concurrent.TimeUnit.MILLISECONDS
class AlarmActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.service_list_item)
userInterface()
}
private fun userInterface() {
setSupportActionBar(toolbar)
val titleNotification = getString(R.string.notification_title)
done_fab.setOnClickListener {
val customCalendar = Calendar.getInstance()
customCalendar.set(
date_p.year, date_p.month, date_p.dayOfMonth, time_p.hour, time_p.minute, 0
)
val customTime = customCalendar.timeInMillis
val currentTime = currentTimeMillis()
if (customTime > currentTime) {
val data = Data.Builder().putInt(NOTIFICATION_ID, 0).build()
val delay = customTime - currentTime
scheduleNotification(delay, data)
val titleNotificationSchedule = getString(R.string.notification_schedule_title)
val patternNotificationSchedule = getString(R.string.notification_schedule_pattern)
make(
service_card,
titleNotificationSchedule + SimpleDateFormat(
patternNotificationSchedule, getDefault()
).format(customCalendar.time).toString(),
LENGTH_LONG
).show()
} else {
val errorNotificationSchedule = getString(R.string.notification_schedule_error)
make(service_card, errorNotificationSchedule, LENGTH_LONG).show()
}
}
}
private fun scheduleNotification(delay: Long, data: Data) {
val notificationWork = OneTimeWorkRequest.Builder(NotifyWork::class.java)
.setInitialDelay(delay, MILLISECONDS).setInputData(data).build()
val instanceWorkManager = WorkManager.getInstance(this)
instanceWorkManager.beginUniqueWork(NOTIFICATION_WORK, REPLACE, notificationWork).enqueue()
}
}
This is my layout.xml. This layout been use by two activity which is "ServiceListAdapter" and "AlarmActivity"
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView 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/service_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="8dp"
android:layout_marginVertical="4dp"
android:backgroundTint="#E9E9ED"
android:clickable="true"
android:focusable="true"
android:minHeight="#dimen/button_min_height"
app:cardElevation="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/services_heading_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="12dp"
android:layout_weight="0"
android:importantForAccessibility="no"
android:src="#drawable/ic_baseline_build_circle_24" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/services"
android:textAllCaps="false"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="#color/black"
android:textSize="16sp" />
<TextView
android:id="#+id/services_done_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textSize="14sp"
tools:text="Oil Filer, Spark Plugs, Battery" />
</LinearLayout>
<ImageView
android:id="#+id/services_expand_view"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="12dp"
android:layout_weight="0"
android:contentDescription="#string/expand_more_button"
android:src="#drawable/ic_baseline_expand_more_24" />
</LinearLayout>
<com.google.android.material.divider.MaterialDivider
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/services_details_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="6dp"
android:paddingTop="0dp"
android:paddingBottom="8dp"
android:visibility="gone"
app:layout_constraintEnd_toStartOf="#+id/services_left_layout"
app:layout_constraintStart_toStartOf="#+id/services_left_layout">
<LinearLayout
android:id="#+id/services_left_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="#id/services_right_layout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/service_date"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="#color/black"
android:textSize="16sp" />
<TextView
android:id="#+id/service_date_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textSize="14sp"
tools:text="01 Nov 2022" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="#string/current_mileage"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="#color/black"
android:textSize="16sp" />
<TextView
android:id="#+id/current_mileage_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textSize="14sp"
tools:text="2,000 km" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="#string/next_service_mileage"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="#color/black"
android:textSize="16sp" />
<TextView
android:id="#+id/next_service_mileage_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textSize="14sp"
tools:text="4,000 km" />
</LinearLayout>
<LinearLayout
android:id="#+id/services_right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/services_left_layout"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/next_service_date"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="#color/black"
android:textSize="16sp" />
<TextView
android:id="#+id/next_service_date_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textSize="14sp"
tools:text="01 Mar 2023" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="#string/total_cost"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="#color/black"
android:textSize="16sp" />
<TextView
android:id="#+id/total_cost_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textSize="14sp"
tools:text="1,500 KSH" />
</LinearLayout>
<TextView
android:id="#+id/service_notes_title_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:text="#string/notes"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="#color/black"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/services_left_layout" />
<TextView
android:id="#+id/service_notes_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textAllCaps="false"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/service_notes_title_text_view"
tools:text="Testing out the notes appearance" />
<!-- This section is to create our alarm from different page since Examiner-->
<!-- want the user to separate the alarm from the add service -->
<EditText
android:id="#+id/titleET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/service_notes_text_view"
android:hint="Title for your notification"
tools:context=".ui.AlarmActivity"
android:autofillHints="Which Car" />
<EditText
android:id="#+id/messageET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="You can set your own notification here"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/titleET"
tools:context=".ui.AlarmActivity"
android:autofillHints="what service" />
<DatePicker
android:id="#+id/date_p"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="-30dp"
android:layout_marginRight="-30dp"
android:layout_weight="1"
android:calendarViewShown="false"
android:datePickerMode="spinner"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/messageET"
tools:context=".ui.AlarmActivity"/>
<TimePicker
android:id="#+id/time_p"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="-30dp"
android:layout_marginRight="-30dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:timePickerMode="spinner"
app:layout_constraintTop_toBottomOf="#id/date_p"
tools:context=".ui.AlarmActivity"/>
<Button
android:id="#+id/done_fab"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:text="schedule_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/time_p"
tools:context=".ui.AlarmActivity"/>
<LinearLayout
android:id="#+id/services_actions_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end|center_vertical"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/done_fab">
<Button
android:id="#+id/edit_service_button"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="4dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="4dp"
android:padding="2dp"
android:text="#string/edit"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="#color/black"
app:drawableTopCompat="#drawable/ic_baseline_edit_24" />
<Button
android:id="#+id/delete_service_button"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="2dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="4dp"
android:padding="2dp"
android:text="#string/delete"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="#color/black"
app:drawableTopCompat="#drawable/ic_baseline_delete_24" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
Before onCreate you need to initialize all widget
like
Button done_fab;//in JAVA
private lateinit var done_fab: Button //in Kotlin
//Add other here
and after setContentView you need to initialize the same widget with name like
done_fab=findViewById(R.id.done_fab);//in java
done_fab= findViewById(R.id.done_fab)//in Kotlin
//Add other here
it will be ready to use in overall activity class.
I've got a NestedScrollView inside of a ScrollView because I have an Activity with three hidden fragments that contain text, and when you click one of the fragments it opens to reveal the text. But the thing is that there is quite a bit of text so I'm using NestedScrollView so the reader can read all of it, and the first time it works fine (text doesn't overlap), and then second time when I close the fragment and re-open it the text starts overlapping...
So as mentioned, first time it doesn't happen, and then I close the fragment and reopen it and this happens:
So why is this happening and how can I fix it?
Fragment inside Activity
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="none"
tools:context=".Fragment.PrivacyFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/Grey"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/Grey"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/Grey"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
Activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/button_gradient_two">
<RelativeLayout
android:id="#+id/relative_layout_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="10dp">
</RelativeLayout>
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/arrow_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="25dp"
android:padding="8dp"
android:src="#drawable/icon_search_100" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:padding="8dp"
android:text="Settings"
android:textColor="#color/White"
android:textSize="22sp"
android:textStyle="bold" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/image_profile"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:elevation="16dp"
android:src="#drawable/profile_placeholder" />
<com.google.android.material.card.MaterialCardView
style="#style/CustomCardViewStyle"
android:layout_width="match_parent"
android:layout_height="1000dp"
android:layout_marginTop="150dp"
app:cardElevation="12dp">
<RelativeLayout
android:id="#+id/relative_layout_two"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:gravity="center_horizontal"
android:text="Jack Sparrow"
android:textColor="#color/Black"
android:textSize="32sp"
android:textStyle="bold" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="jacksparrow"
android:textColor="#color/Grey"
android:textSize="22sp"
android:textStyle="" />
<TextView
android:id="#+id/bio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/username"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:gravity="center_horizontal"
android:text="This is my bio!"
android:textColor="#color/Grey"
android:textStyle="" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/bio"
android:orientation="vertical">
<TextView
android:id="#+id/privacy"
style="#style/SelectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="#string/privacy"
android:textColor="#color/colorBlack"
android:textSize="18sp" />
<FrameLayout
android:id="#+id/container_privacy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/security"
style="#style/SelectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="#string/security"
android:textColor="#color/colorBlack"
android:textSize="18sp" />
<FrameLayout
android:id="#+id/container_security"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/help"
style="#style/SelectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="#string/help"
android:textColor="#color/colorBlack"
android:textSize="18sp" />
<FrameLayout
android:id="#+id/container_help"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/about"
style="#style/SelectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="#string/about"
android:textColor="#color/colorBlack"
android:textSize="18sp" />
<FrameLayout
android:id="#+id/container_about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
SettingsActivity
public class SettingsActivity extends AppCompatActivity {
private ImageView mImageProfile, mBackArrow;
private TextView mName, mUsername, mBio, mPrivacy, mSecurity, mHelp, mAbout;
private FirebaseUser mFirebaseUser;
private FrameLayout mPrivacyContainer, mSecurityContainer, mHelpContainer, mAboutContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_tester);
mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
mImageProfile = findViewById(R.id.image_profile);
mName = findViewById(R.id.name);
mUsername = findViewById(R.id.username);
mBio = findViewById(R.id.bio);
mPrivacy = findViewById(R.id.privacy);
mSecurity = findViewById(R.id.security);
mHelp = findViewById(R.id.help);
mAbout = findViewById(R.id.about);
mImageProfile.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_profile_picture));
mName.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_name));
mUsername.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_username));
mBio.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_bio));
mPrivacyContainer = findViewById(R.id.container_privacy);
mSecurityContainer = findViewById(R.id.container_security);
mHelpContainer = findViewById(R.id.container_help);
mAboutContainer = findViewById(R.id.container_about);
mPrivacy.setOnClickListener(v -> {
getSupportFragmentManager().beginTransaction().add(R.id.container_privacy, new PrivacyFragment(), null).addToBackStack(null).commit();
if (mPrivacyContainer.getVisibility() == View.GONE) {
mPrivacyContainer.setVisibility(View.VISIBLE);
} else {
mPrivacyContainer.setVisibility(View.GONE);
}
});
mSecurity.setOnClickListener(v -> {
getSupportFragmentManager().beginTransaction().add(R.id.container_security, new SecurityFragment(), null).addToBackStack(null).commit();
if (mSecurityContainer.getVisibility() == View.GONE) {
mSecurityContainer.setVisibility(View.VISIBLE);
} else {
mSecurityContainer.setVisibility(View.GONE);
}
});
mHelp.setOnClickListener(v -> {
getSupportFragmentManager().beginTransaction().add(R.id.container_help, new HelpFragment(), null).addToBackStack(null).commit();
if (mHelpContainer.getVisibility() == View.GONE) {
mHelpContainer.setVisibility(View.VISIBLE);
} else {
mHelpContainer.setVisibility(View.GONE);
}
});
mAbout.setOnClickListener(v -> {
getSupportFragmentManager().beginTransaction().add(R.id.container_about, new AboutFragment(), null).addToBackStack(null).commit();
if (mAboutContainer.getVisibility() == View.GONE) {
mAboutContainer.setVisibility(View.VISIBLE);
} else {
mAboutContainer.setVisibility(View.GONE);
}
});
getUserInfo();
setBackArrow();
}
private void getUserInfo() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(mFirebaseUser.getUid());
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
if (user != null) {
mName.setText(user.getFullname());
mUsername.setText(user.getUsername());
mBio.setText(user.getBio());
if (user.getImageurl() == null) {
mImageProfile.setImageResource(R.drawable.profile_placeholder);
} else {
Glide.with(getApplicationContext()).load(user.getImageurl()).into(mImageProfile);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void setBackArrow() {
mBackArrow = findViewById(R.id.arrow_back);
mBackArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
You are getting the fragment overlapped because there is a new fragment added each time.
You simply need to do a check if the fragment exits already then there is no need to add a new fragment. You can assign a 'tag' when adding fragment. Code as follows-
if (fragmentManager.findFragmentByTag("First Fragment") == null)
fragmentManager.beginTransaction().add(R.id.fragment, new FirstFragment(), "First Fragment").commit();
OR
Rather then using .add() method use .replace() to make sure fragment got replaced each time.
According to your Activity code, you need to choose the correct way, but make sure there is no fragment getting re-created without destroying previous one.
Happy Coding !!
You are adding two fragments on top of each other.
Instead of add use replace when executing the fragment transaction:
From:
getSupportFragmentManager().beginTransaction().add(R.id.container_privacy, new PrivacyFragment(), null).addToBackStack(null).commit();
if (mPrivacyContainer.getVisibility() == View.GONE) {
mPrivacyContainer.setVisibility(View.VISIBLE);
} else {
mPrivacyContainer.setVisibility(View.GONE);
}
To:
getSupportFragmentManager().beginTransaction().replace(R.id.container_privacy, new PrivacyFragment(), null).addToBackStack(null).commit();
if (mPrivacyContainer.getVisibility() == View.GONE) {
mPrivacyContainer.setVisibility(View.VISIBLE);
} else {
mPrivacyContainer.setVisibility(View.GONE);
}
I want to make pin input with Buttons and TextView in Android.
I have Textview for pin , and 9 Buttons for user to press as digits.
What i want when user press four buttons to change my Textview letter by letter(Button by Button) , and when entered fourth button to start code
Here is image:
Any tips please?
Here is my code:
content_main.xml :
<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:background="#color/white"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.group.digit.razvoj.appointment.MainActivity"
tools:showIn="#layout/app_bar_main">
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/pinLayoutMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:orientation="vertical"
android:paddingRight="#dimen/left_margin_pin">
<LinearLayout
android:id="#+id/pinTvLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/between_margin"
android:layout_marginTop="#dimen/between_margin"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/pinTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/pin"
android:textColor="#android:color/black"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/passLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="50dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/pinEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="****"
android:textColor="#android:color/black"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/firstRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/b1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="1"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/b2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="2"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/b3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="3"
android:textColor="#color/black"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/secondRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/b4"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="4"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/b5"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="5"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/b6"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="6"
android:textColor="#color/black"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/sthirdRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/b7"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="7"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/b8"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="8"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/b9"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="9"
android:textColor="#color/black"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/forthow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="1"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/b0"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="0"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/bback"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="-"
android:textColor="#color/black"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//init buttons
for (int i = 0; i < MAX_PIN_NUM; i++) {
buttonsPinWidget[i] = (Button) findViewById(buttonsPinId[i]);
}
pinentry = (TextView) findViewById(R.id.pinEntry);
}
You should to use one onClickListener for everyone button with switch case inside of it.
In every case of switch case block your should addor delete (if you have the button for deleting) one character it TextView.
After switch case block check the text length in your TextView. If the length of it equals 4, you can try to authorization or login or other actions.
Sorry for bad English.
Very interesting question first of all.
The only solution I can think of is to create a public method which will always check the length of the textview (which I think should be edittext) and add that method for every button. Once the length of the textview is 4, trigger your code you want.
Let me know if you need some more clarification
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String s=txtView.getText().toString();
if(s.length()<3){
s=s+btn.getText().toString();
txtView.setText(""+s);
}else if(s.length()==3){
s=s+btn.getText().toString();
txtView.setText(""+s);
// write your code
}
}
});
I am trying to implement an onclick for the entire layout but it won't work.
Here is my function for my RelativeLayout:
public void updateRange(View v)
{
System.out.println("Hello!");
}
and here is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity"
android:background="#ffffffff"
android:id="#+id/xasdf"
android:onClick="updateRange">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/toptab"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:fontFamily="monospace"
android:textAllCaps="false"
android:layout_weight="0.333333"
android:id="#+id/settingsButton"
android:background="#fffdb64b" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Solutions"
android:fontFamily="monospace"
android:textAllCaps="false"
android:layout_weight="0.333333"
android:id="#+id/solutionsButton"
android:background="#fffdb64b" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mode"
android:fontFamily="monospace"
android:textAllCaps="false"
android:layout_weight="0.333333"
android:id="#+id/modeButton"
android:background="#fffdb64b" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/chartlayout"
android:layout_below="#+id/toptab"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/textView"
android:layout_weight="0.5"
android:background="#ffffffff"
android:fontFamily="monospace"
android:hint="y = ..." />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Graph"
android:fontFamily="monospace"
android:textAllCaps="false"
android:layout_weight="0.25"
android:id="#+id/button"
android:background="#ffd0d0d0"
android:onClick="drawGraph" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Table"
android:fontFamily="monospace"
android:layout_weight="0.25"
android:textAllCaps="false"
android:id="#+id/button2"
android:background="#ffd0d0d0" />
</LinearLayout>
</RelativeLayout>
Try adding the extra line below to your Relative Layout description:
...
android:onClick="updateRange"
android:clickable="true">
Strangely, my listView's onClick no longer works after I added two Buttons: Submit and Show Result.
Here is the list view xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".CulturalActivity"
android:background="#EFEFEF"
android:id="#+id/AdLayout"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="3dp"
>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:divider="#android:color/transparent"
android:descendantFocusability="blocksDescendants"
android:scrollbars="none"
android:dividerHeight="10dp"
android:listSelector="#drawable/list_selector" />
</RelativeLayout>
Here is the layout of each list row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="315dp"
android:clickable="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="7dp"
android:background="#drawable/bg_card"
android:orientation="horizontal"
android:padding="5dip" >
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="-60dp"
android:background="#2093CD"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="52dp"
android:text="Audit"
android:textColor="#android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/relativeLayout2" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Farm/Grp"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_alignTop="#+id/tile"
android:layout_toRightOf="#+id/textView4" />
<!--thumbnail-->
<LinearLayout
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/thumbnail_image"
android:layout_marginTop="35dp"
android:gravity="center"
android:id="#+id/tile">
<ImageView
android:id="#+id/thumbImage"
android:layout_width="50dp"
android:clickable="false"
android:layout_height="50dp"
android:src="#drawable/opened" />
</LinearLayout>
<TextView
android:id="#+id/crophead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Crop"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_alignBottom="#+id/tile"
android:layout_alignLeft="#+id/title" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Certification"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_below="#+id/textView4"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/certification"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView2"
android:layout_marginLeft="22dp"
android:layout_toRightOf="#+id/title"
android:text="Test"
android:textColor="#343434"
android:textSize="17sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Audit Type"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_below="#+id/tile"
android:layout_alignLeft="#+id/textView2"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/audittype"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_alignLeft="#+id/certification"
android:maxLines="2"
android:text="ES"
android:textColor="#343434"
android:textSize="17sp" />
<View
android:layout_width="fill_parent"
android:layout_height="3px"
android:layout_marginTop="200dp"
android:background="#21265b"
android:id="#+id/view" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Date"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_below="#+id/view"
android:layout_alignLeft="#+id/view"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_alignParentBottom="false" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End Date"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_marginRight="15dp"
android:layout_alignTop="#+id/textView6"
android:layout_alignRight="#+id/startdate" />
<TextView
android:id="#+id/startdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#8B1A1A"
android:text="30/05/1992"
android:textSize="17sp"
android:layout_alignTop="#+id/enddate"
android:layout_toRightOf="#+id/relativeLayout2"
android:layout_marginLeft="6dp"
android:layout_alignParentBottom="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#8B1A1A"
android:text="2/13/14"
android:textSize="17sp"
android:id="#+id/enddate"
android:layout_below="#+id/textView7"
android:layout_alignRight="#+id/textView7" />
<TextView
android:id="#+id/farm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/audittype"
android:maxLines="3"
android:layout_alignTop="#+id/title"
android:text="Tea Estate Nagarcoil Tamil Nadu, India "
android:textColor="#343434"
android:textSize="17sp" />
<TextView
android:id="#+id/crop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/farm"
android:layout_alignTop="#+id/crophead"
android:maxLines="2"
android:text="Wheat without its chaff and barn but not brown"
android:textColor="#343434"
android:textSize="17sp" />
<Button
android:id="#+id/upsync"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/blue_thumb"
android:layout_below="#+id/enddate"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Show Results"
android:textColor="#android:color/white" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/upsync"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_below="#+id/enddate"
android:background="#drawable/blue_thumb"
android:text="Submit"
android:textColor="#android:color/white" />
</RelativeLayout>
Here is the click snippet of the list view:
listview.setOnItemClickListener( new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
long audit_id;
try{
audit_id = (new CustomListAdapter(HomeList.this, c).getAuditID(position));
changeTileStatus(audit_id,"opened");
System.out.println("listClicked");
sendAuditAndIntent(audit_id);
}catch(Exception e)
{
System.out.println("ERROR # HOMELIST list onClick: "+e);
}
}
private void sendAuditAndIntent(long audit_id) { //sends audit id and api key
Intent intent = new Intent(HomeList.this,ChapterActivity.class );
Bundle extras= new Bundle();
extras.putString("audit_id",String.valueOf(audit_id));
extras.putString("api_key", api_key);
intent.putExtras(extras);
startActivity(intent);
}
private void changeTileStatus(long audit_id, String value) {
AuditTableManager tile= new AuditTableManager(HomeList.this);
tile.open();
tile.updateEntry(audit_id, value);
tile.close();
}
});
OnItemClickListener is not called if cells contain clickable Views. It is an Android feature. You can handle clicks by OnClickListener then - set it to your two buttons (not the listView itself).
In your adapter:
public View onCreateView(..., final int position){
...
convertView.findViewById(R.id.button1).setOnClickListener(new OnClickListener(){
public void onClick(View view){
itemClickedAction1(position);
}
});
convertView.findViewById(R.id.button2).setOnClickListener(new OnClickListener(){
public void onClick(View view){
itemClickedAction2(position);
}
});
...
}
Try android:Focusable="false" and also android:clickable="false" in the custom row TextView