RecyclerView with CardView is laging and not scrolls smoothly - android

Trying to load around 500 rows of data in recycler view, but it is lagging while scrolling.
Data doesn't include any kind of images, it is just plain text. Thus could not find any reason of laging.
The xml and java code used is as follows:-
xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/markeListCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardPreventCornerOverlap="true"
android:layout_alignParentTop="true"
card_view:cardUseCompatPadding="true"
app:cardCornerRadius="#dimen/_5sdp"
app:cardElevation="#dimen/_5sdp"
android:layout_marginBottom="14dp"
android:paddingBottom="#dimen/_150sdp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/_125sdp"
android:background="#drawable/compltedtlist"
android:layout_gravity="center">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="#dimen/_30sdp"
android:layout_marginTop="#dimen/_10sdp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/_5sdp"
android:text="School Name :"
android:singleLine="true"
android:inputType="textPostalAddress"
android:imeOptions="actionNext"
android:textStyle="bold"
android:textSize="#dimen/_10ssp"
android:textColor="#0E9CE0"
android:paddingLeft="#dimen/_10sdp"
android:id="#+id/scndprsnname" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/_10sdp"
android:maxLines="1"
android:text="Divisional Public School"
android:ems="10"
android:singleLine="true"
android:textColor="#000"
android:imeOptions="actionNext"
android:textSize="#dimen/_10ssp"
android:id="#+id/scholnametxt" >
</TextView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_30sdp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/_5sdp"
android:text="District :"
android:singleLine="true"
android:inputType="textPostalAddress"
android:imeOptions="actionNext"
android:textStyle="bold"
android:textSize="#dimen/_10ssp"
android:textColor="#0E9CE0"
android:paddingLeft="#dimen/_10sdp"
android:id="#+id/detaildadresedit" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/_38sdp"
android:maxLines="1"
android:text="Aleem Ahmed"
android:ems="10"
android:singleLine="true"
android:textColor="#000"
android:imeOptions="actionNext"
android:textSize="#dimen/_10ssp"
android:id="#+id/tehsilname" >
</TextView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="#dimen/_30sdp"
android:layout_marginTop="#dimen/_48sdp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/_5sdp"
android:text="Tehsil :"
android:singleLine="true"
android:inputType="textPostalAddress"
android:imeOptions="actionNext"
android:textStyle="bold"
android:textSize="#dimen/_10ssp"
android:textColor="#0E9CE0"
android:paddingLeft="#dimen/_10sdp"
android:id="#+id/pcname" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/_45sdp"
android:maxLines="1"
android:text="Lahore"
android:ems="10"
android:singleLine="true"
android:textColor="#000"
android:imeOptions="actionNext"
android:textSize="#dimen/_10ssp"
android:id="#+id/citytxt" >
</TextView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="#dimen/_30sdp"
android:layout_marginTop="#dimen/_65sdp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/_5sdp"
android:text="Address :"
android:textStyle="bold"
android:ems="10"
android:singleLine="true"
android:textColor="#0E9CE0"
android:imeOptions="actionNext"
android:textSize="#dimen/_10ssp"
android:paddingLeft="#dimen/_10sdp"
android:id="#+id/provnceedit" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="#dimen/_10sdp"
android:layout_marginLeft="-40dp"
android:maxLines="1"
android:text="h#10 stret no 17 sabzar lahore"
android:ems="30"
android:singleLine="true"
android:textColor="#000"
android:imeOptions="actionNext"
android:textSize="#dimen/_10ssp"
android:id="#+id/addrestxt" >
</TextView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="#dimen/_50sdp"
android:layout_marginTop="#dimen/_70sdp"
android:layout_gravity="center"
android:paddingTop="#dimen/_10sdp"
android:gravity="center"
android:textAlignment="center"
android:layout_marginLeft="#dimen/_170sdp"
android:layout_marginRight="#dimen/_10sdp"
android:orientation="horizontal">
<Button
android:id="#+id/uploadbtn"
android:layout_width="#dimen/_140sdp"
android:layout_height="#dimen/_30sdp"
android:background="#drawable/yelowbtn"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="#dimen/_10sdp"
android:textAlignment="center"
android:layout_marginTop="#dimen/_20sdp"
android:layout_marginBottom="#dimen/_15sdp"
android:gravity="center"
android:text="Stage 2"
android:textColor="#000" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
<!--
</LinearLayout>-->
adapter
import android.animation.Animator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.preference.PreferenceManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.offlinemap.R;
import com.offlinemap.form.FormStage2;
import com.offlinemap.form.FormToUpdateData;
import java.util.ArrayList;
import java.util.List;
public class Stage2RecyclerViewAdapterViewCompleteList extends RecyclerView.Adapter<Stage2RecyclerViewAdapterViewCompleteList.SearchViewHolder> {
private Context mCtx;
private List<ModeClassCompleteListStage2> searcList;
View thumb1View;
private String jobid;
private Animator mCurrentAnimator;
private int mShortAnimationDuration;
private modelclascompletelist model;
String job;
private Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
//ImageView image;
private ArrayList<String> list = new ArrayList<String>();
String pos;
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
/*static final int ZOOM = 2;*/
private static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
String savedItemClicked,scholid,tehsilo ,distrcto ,province ,sgtsID ;
SharedPreferences preferences;
public Stage2RecyclerViewAdapterViewCompleteList(Context mCtx, List<ModeClassCompleteListStage2> searcList)
{
this.mCtx = mCtx;
this.searcList = searcList;
}
#Override
public Stage2RecyclerViewAdapterViewCompleteList.SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.carview_introcomplete, null);
return new Stage2RecyclerViewAdapterViewCompleteList.SearchViewHolder(view);
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(final Stage2RecyclerViewAdapterViewCompleteList.SearchViewHolder holder, final int position)
{
final ModeClassCompleteListStage2 model = searcList.get(position);
holder.adres.setText(model.getDetailsadres());
holder.scholname.setText(model.getScholname());
holder.district.setText(model.getDistrict());
holder.city.setText(model.getTehsil());
/* Glide.with(mCtx)
.load(model.getImage1())
.into(holder.image);*/
if(model.getStg_id().equals("0")){
holder.edit.setText("Add");
}
else {
holder.edit.setText("Update");
}
holder.edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
/* String mtoid = searcList.get(position).get();*/
scholid = searcList.get(position).getScholid();
tehsilo = searcList.get(position).getTehsil();
distrcto = searcList.get(position).getDistrict();
province = searcList.get(position).getProvince();
sgtsID = searcList.get(position).getStg_id();
preferences = PreferenceManager.getDefaultSharedPreferences(mCtx);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("scholid", scholid);
editor.putString("thsillo", tehsilo);
editor.putString("distrctto", distrcto);
editor.putString("prov", province);
editor.putString("sgts",sgtsID);
editor.apply();
if(holder.edit.getText().toString().equals("Add")) {
Intent intent = new Intent(mCtx.getApplicationContext(), FormStage2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
mCtx.getApplicationContext().startActivity(intent);
}
else {
Intent intent = new Intent(mCtx.getApplicationContext(), FormToUpdateData.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
mCtx.getApplicationContext().startActivity(intent);
}
}
});
}
#Override
public int getItemCount() {
/*if (searcList.size() == 0) {
.setVisibility(View.INVISIBLE);
} else {
mRecyclerView.setVisibility(View.VISIBLE);
}*/
return searcList.size();
}
class SearchViewHolder extends RecyclerView.ViewHolder {
/*TextView movingitem, pickup, dropoff,currentbid,date,edit,delete,acceptbid,message,invitetransport,viewquote,similarjob,mesagecount,simlarcount;
ImageView image,largeimage,editimage;
CardView cardView;
ImageView delicon,similaricon,inviteicon,mainimageicon,mesageicon;*/
TextView adres, city, scholname, principalname, secondpersonname, entryno,district;
Button edit;
public SearchViewHolder(View itemView)
{
super(itemView);
adres = itemView.findViewById(R.id.addrestxt);
scholname = itemView.findViewById(R.id.scholnametxt);
edit= itemView.findViewById(R.id.uploadbtn);
district = itemView.findViewById(R.id.tehsilname);
city = itemView.findViewById(R.id.citytxt);
/* movingitem = itemView.findViewById(R.id.MovingItemServlivep);
pickup = itemView.findViewById(R.id.pickupservlivep);
dropoff = itemView.findViewById(R.id.DropOffServlivep);
image = itemView.findViewById(R.id.imagecardlivep);
currentbid = itemView.findViewById(R.id.BidServlivep);
date = itemView.findViewById(R.id.dateservlivep);
edit = itemView.findViewById(R.id.editjob);
delete = itemView.findViewById(R.id.deletejob);
// similarjob = itemView.findViewById(R.id.similarjobtext);
acceptbid = itemView.findViewById(R.id.biddetailslivep);
viewquote = itemView.findViewById(R.id.offerpricelivep);
// invitetransport = itemView.findViewById(R.id.invitetransportertext);
mesagecount = itemView.findViewById(R.id.countermsgs);
// simlarcount = itemView.findViewById(R.id.countersimlar);
editimage= itemView.findViewById(R.id.imageView98);
mesageicon= itemView.findViewById(R.id.imagy98);
delicon= itemView.findViewById(R.id.imagy971);
cardView = itemView.findViewById(R.id.markeListCard);
// similaricon= itemView.findViewById(R.id.imagy981);
// inviteicon= itemView.findViewById(R.id.ima98);
mainimageicon= itemView.findViewById(R.id.imagecardlivep);*/
}
/* public void setFilter(List<UserInfo> newList){
data=new ArrayList<>();
data.addAll(newList);
notifyDataSetChanged();
}*/
}
}

Related

I'm having trouble with phone number authentication. Verify_otp generates a unique code for any verified number

I'm having trouble with phone number authentication. Verify_otp generates a unique code for any verified number. But the biggest problem is that it only generates the code if the numbers start with 6599 ..... a number 6598 .... it no longer generates the code.
I can not find an outlet for him to accept any phone number.
this is a part of the RegistrationModel.java file
public static class OTP_Details{
/**
* status : 2
* message : Otp Sent to phone for Verification
* otp : 2017
* auto_otp : 1
*/
private int status;
private String message;
private String otp;
private int auto_otp;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getOtp() {
return otp;
}
public void setOtp(String otp) {
this.otp = otp;
}
public int getAuto_otp() {
return auto_otp;
}
public void setAuto_otp(int auto_otp) {
this.auto_otp = auto_otp;
}
}
}
This is Vetify_otp
package com.motofacil.passageiro;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.motofacil.passageiro.accounts.RegistrationModel;
import com.motofacil.passageiro.accounts.ResultCheckMessage;
import com.motofacil.passageiro.accounts.ResultChecker;
import com.motofacil.passageiro.manager.ApiManager;
import com.motofacil.passageiro.manager.SessionManager;
import com.motofacil.passageiro.samwork.Config;
import com.motofacil.passageiro.urls.Apis;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hbb20.CountryCodePicker;
import java.util.HashMap;
public class Verify_OTP extends AppCompatActivity implements ApiManager.APIFETCHER {
ApiManager apiManager ;
GsonBuilder gsonBuilder;
RegistrationModel.OTP_Details otp_details;
private TextView otpError_txt;
private EditText otp_input, edt_enter_phone;
private LinearLayout submit_otp_layout;
SessionManager sessionManager ;
Gson gson;
String code, input_phone_number, otp;
CountryCodePicker codePicker;
private static final int KEY_REGISTER = 110;
Button generate_otp;
LinearLayout submit_otp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gsonBuilder = new GsonBuilder();
gson = gsonBuilder.create();
apiManager = new ApiManager(this , this , this );
sessionManager = new SessionManager(Verify_OTP.this);
otp_details = new RegistrationModel.OTP_Details();
setContentView(R.layout.activity_verify__otp);
gsonBuilder = new GsonBuilder();
gson = gsonBuilder.create();
submit_otp = (LinearLayout) findViewById(R.id.otp_submit);
generate_otp = (Button) findViewById(R.id.generate_otp);
edt_enter_phone = (EditText)findViewById(R.id.edt_enter_phone);
otp_input = (EditText)findViewById(R.id.otp_edt);
otpError_txt = (TextView)findViewById(R.id.otp_verifier_txt);
submit_otp_layout = (LinearLayout)findViewById(R.id.otp_submit);
codePicker = (CountryCodePicker)findViewById(R.id.otp_ccp);
submit_otp.setEnabled(false);
generate_otp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
input_phone_number = edt_enter_phone.getText().toString().trim();
Log.e("input_phone_number====", input_phone_number);
code = codePicker.getSelectedCountryCodeWithPlus();
Log.e("COUNTRY_CODE_PICKER===", code);
if (input_phone_number.equals("")){
Toast.makeText(Verify_OTP.this, R.string.required_field_missing, Toast.LENGTH_SHORT).show();
}else {
getOTP(code+input_phone_number);
}
}
});
submit_otp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (otp_input.getText().toString().equals("")) {
Toast.makeText(Verify_OTP.this, R.string.required_field_missing, Toast.LENGTH_SHORT).show();
} else if (!otp_input.getText().toString().equals(otp)) {
// Toast.makeText(Verify_OTP.this, R.string.invalid_otp, Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.putExtra("phone_number", code + input_phone_number);
setResult(Activity.RESULT_OK, intent);
finish();
} else {
Intent intent = new Intent();
intent.putExtra("phone_number", code + input_phone_number);
setResult(Activity.RESULT_OK, intent);
finish();
}
}
});
}
private void getOTP(String phone) {
HashMap<String , String > bodyparameters = new HashMap<String, String>();
bodyparameters.put("phone" , phone);
bodyparameters.put("flag" ,"1");
apiManager.execution_method_post(Config.ApiKeys.KEY_VERIFY_OTP ,""+ Apis.SEND_OTP, bodyparameters, true,ApiManager.ACTION_SHOW_TOP_BAR);
}
#Override
public void onFetchComplete(Object script, String APINAME) {
if(APINAME.equals(""+Config.ApiKeys.KEY_VERIFY_OTP)){
submit_otp.setEnabled(true);
RegistrationModel.OTP_Details otp_response = gson.fromJson("" + script, RegistrationModel.OTP_Details.class);
// finilalizeActivity();
Log.e("**OTP_SCRIPT-----", String.valueOf(otp_response.getMessage() + otp_response.getOtp() + otp_response.getStatus()));
otp = otp_response.getOtp();
Log.d("otp==normal sign up==", otp);
otp_input.setText(""+otp);
otp_input.requestFocus();
if(otp_response.getAuto_otp() == 1){
otp_input.setText(""+otp);
}
}else {
try{ResultChecker rcheck = gson.fromJson("" + script, ResultChecker.class);
Log.e("**OTP_SCRIPT-----", String.valueOf(script));
if(rcheck.getResult() == 1){
Log.e("**RCHHECKK---", String.valueOf(rcheck.getResult()));
RegistrationModel.OTP_Details otp_response = gson.fromJson("" + script, RegistrationModel.OTP_Details.class);
}else {
ResultCheckMessage rr = gson.fromJson("" + script, ResultCheckMessage.class);
Toast.makeText(this, ""+rr.getMessage(), Toast.LENGTH_SHORT).show();
}
}catch (Exception e){}
}
}
#Override
public void onFetchResultZero(String s) {
}
}
This is Activity_forgotpass_verify_otp.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:orientation="vertical"
tools:context="com.motofacil.passageiro.ForgotPass_Verify_OTP">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/pure_white"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/otp_back"
android:layout_width="50dp"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="invisible">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/ic_left_sort"
android:tint="#color/icons_8_muted_grey"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<com.motofacil.passageiro.accounts.TypefaceDosisRegular
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/verifyOTP"
android:textColor="#color/icons_8_muted_grey"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="50dp"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/icons_8_muted_grey" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/login_banner"></ImageView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bf000000"></RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/icons_8_muted_grey" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|top"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="#+id/phone_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginLeft="15dp"
android:background="#drawable/shapes_white_transparent"
android:layout_marginRight="15dp"
android:gravity="center"
android:orientation="horizontal">
<com.hbb20.CountryCodePicker
android:id="#+id/otp_ccp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:hideNameCode="true"
app:keyboardAutoPopOnSearch="false"
app:showFlag="false"
android:layout_marginLeft="5dp"
app:defaultCode="55"
app:textSize="15dp"/>
<EditText
android:id="#+id/edt_enter_phone"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:background="#android:color/transparent"
android:ems="10"
android:drawableTint="#color/icons_8_muted_grey"
android:gravity="center|left"
android:hint="Enter Phone Number"
android:inputType="phone"
android:maxLength="10"
android:minLines="1"
android:padding="5dp"
android:textColor="#color/pure_black"
android:textSize="17dp" />
</LinearLayout>
<!--
<View
android:layout_width="match_parent"
android:layout_height="0.85dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="6dp"
android:background="#color/icons_8_muted_yellow" />-->
<Button
android:id="#+id/generate_otp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/generateOTP" />
<TextView
android:id="#+id/otp_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="#string/otp_text"
android:textColor="#color/pure_white"
android:textSize="17dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="70dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="30dp"
android:background="#drawable/shapes_white_transparent"
android:orientation="horizontal">
<EditText
android:id="#+id/otp_edt"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
android:drawableTint="#color/icons_8_muted_grey"
android:gravity="center|left"
android:hint="#string/enter_otp"
android:inputType="number"
android:paddingLeft="10dp"
android:textColor="#color/pure_black"
android:textSize="17dp" />
<TextView
android:id="#+id/otp_verifier_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:gravity="center|right"
android:padding="10dp"
android:text="Email not valid"
android:textColor="#color/icons_8_muted_red"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/otp_submit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorPrimary"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submitOTP"
android:textColor="#color/pure_white"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
Ready! I've submitted below the files I've listed. After typing the phone and clicking send otp, the code that comes back is always the code 2017, that is the first code I sent, but it only sends this code 2017 if the numbers start like this: 6599 .... if the number is so for example: 6598 .... it does not generate the code 2017 and can not continue the registration.

Why pass textInput content to string into ArrayList

I want pass values from a form making with TextInputLayout but Android Studio detects my content as INT.
I put the complete class of my activity as well as the view in .xml to determine the problem
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.prosegma.rso.R;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class AccionActivity extends AppCompatActivity {
ImageView imageView;
private Context activity;
private String[]header={"Campo","Respuesta"};
private String shortText="RSO PROSEGMA";
private String longText="Eliminamos Riesgos Sumamos Valor";
private AccionPdf accionPdf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accion);
Button btnCamera1 = (Button)findViewById(R.id.BtnCamera1);
imageView = (ImageView)findViewById(R.id.reporte1);
btnCamera1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,0);
}
});
Date date = new Date();
String timeStamp = new SimpleDateFormat("dd/MM/yyyy").format(date);
accionPdf=new AccionPdf(getApplicationContext());
accionPdf.openDocument();
accionPdf.addMetaData("Reporte","Acción Insegura","www.prosegma.com");
accionPdf.addTitle("Reporte Acción Insegura","RSO Prosegma"," " +timeStamp);
accionPdf.addParagraph(shortText);
accionPdf.addParagraph(longText);
accionPdf.createTable(header,getClients());
accionPdf.closeDocument();
}
TextInputEditText Nreport = (TextInputEditText) findViewById(R.id.nReporta);
private ArrayList<String[]>getClients(){
ArrayList<String[]>rows=new ArrayList<>();
rows.add(new String[]{"Nombre de quien reporta", Nreport });
rows.add(new String[]{"Nombre del trabajador","No"});
rows.add(new String[]{"Empresa contratista","ju"});
rows.add(new String[]{"Medidas Necesarias","hu"});
return rows;
}
public void pdfAccion(View view)
{
accionPdf.verReporteAccion();
}
public void enviarA(View view) {
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, "receiver_email_address");
email.putExtra(Intent.EXTRA_SUBJECT, "subject");
email.putExtra(Intent.EXTRA_TEXT, "email body");
Uri uri = Uri.fromFile(new File(android.os.Environment.getExternalStorageDirectory(), "pdfFileName"));
email.putExtra(Intent.EXTRA_STREAM, uri);
email.setType("application/pdf");
email.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(email);
}
public Context getActivity() {
return activity;
}
}
I update the publication with the activity, I have tried to change the InputTextLayout by EditText but the situation is the same, it throws me an incompatibility error.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
android:fitsSystemWindows="true">
<include layout="#layout/actionbar_toolbar"/>
<ScrollView
android:id="#+id/reportes0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
>
<LinearLayout
android:id="#+id/reportes1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:weightSum="10"
>
<View
android:layout_width="match_parent"
android:layout_height="7dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"
/>
<View
android:layout_width="match_parent"
android:layout_height="7dp"
/>
<ImageView
android:id="#+id/reporte1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/BtnCamera1"
android:layout_weight="1"
android:layout_height="64sp"
android:layout_width="64sp"
android:background="#drawable/camera"
android:gravity="center"
/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/EditTextBlack"
android:layout_marginTop="4dp"
>
<android.support.design.widget.TextInputEditText
android:id="#+id/nReporta"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="NOMBRE DE QUIEN REPORTA"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/EditTextBlack"
android:layout_marginTop="4dp"
>
<android.support.design.widget.TextInputEditText
android:id="#+id/nTrabajador"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="NOMBRE DEL TRABAJADOR"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/EditTextBlack"
android:layout_marginTop="4dp"
>
<android.support.design.widget.TextInputEditText
android:id="#+id/eContratista"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EMPRESA CONTRATISTA"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/EditTextBlack"
android:layout_marginTop="4dp"
>
<android.support.design.widget.TextInputEditText
android:id="#+id/aMedidas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:hint="ACCIÓN, MEDIDAS CORRECTIVAS INMEDIATAS"
android:inputType="textShortMessage"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/EditTextBlack"
android:layout_marginTop="4dp"
>
</android.support.design.widget.TextInputLayout>
<CheckBox
android:id="#+id/checkAccidente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ACCIDENTE" />
<CheckBox
android:id="#+id/checkImpacto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IMPACTO AMBIENTAL" />
<CheckBox
android:id="#+id/checkEnfermedad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ENFERMEDAR PROFESIONAL" />
<CheckBox
android:id="#+id/checkReincidente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REINCIDENTE" />
<CheckBox
android:id="#+id/checkSancion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="¿APLICA SANCIÓN?"
/>
<Button
android:id="#+id/guardarAccion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GUARDAR REPORTE"
android:theme="#style/RaisedButtonDark"
android:onClick="pdfAccion"
/>
</LinearLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
TextInputEditText Nreport = (TextInputEditText) findViewById(R.id.nReporta);
this line is outside OnCreate(Bundle savedInstanceState) method. Thats why it throws null. It should be like this
public class MainActivity extends AppCompatActivity {
TextInputEditText Nreport;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Button btnCamera1 = (Button)findViewById(R.id.BtnCamera1);
// imageView = (ImageView)findViewById(R.id.reporte1);
Date date = new Date();
String timeStamp = new SimpleDateFormat("dd/MM/yyyy").format(date);
Nreport = (TextInputEditText) findViewById(R.id.nReporta);
getClients();
}
private ArrayList<String[]> getClients(){
ArrayList<String[]>rows=new ArrayList<>();
rows.add(new String[]{"Nombre de quien reporta", Nreport.getText().toString() }); // get text like this
rows.add(new String[]{"Nombre del trabajador","No"});
rows.add(new String[]{"Empresa contratista","ju"});
rows.add(new String[]{"Medidas Necesarias","hu"});
return rows;
}

my app crashes whenever i click the button

whenever i click the increment button while running the app on my device my app forces unexpectedly
this is my activity_main.xml code
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".MainActivity"
>
<RelativeLayout
android:padding="20dp"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center_vertical"
>
<EditText
android:id="#+id/name_text_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="textCapWords"
></EditText>
<TextView
android:layout_centerVertical="true"
android:id="#+id/heading1"
style="#style/HeaderTextStyle"
android:text="Toppings"
android:layout_below="#id/name_text_field"
/>
<CheckBox
android:layout_below="#id/heading1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/whipped_cream"
android:textSize="16sp"
android:paddingLeft="24dp"
android:layout_marginTop="20dp"
android:text="Whipped Cream"
/>
<CheckBox
android:layout_marginTop="20dp"
android:layout_below="#id/whipped_cream"
android:id="#+id/chocolate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="chocolate"
android:textSize="16sp"
android:paddingLeft="24dp"
/>
<TextView
android:layout_below="#id/chocolate"
android:id="#+id/quantity_text_view"
style="#style/HeaderTextStyle"
android:text="Quantity"
android:layout_marginBottom="20dp"
/>
<Button
android:layout_below="#id/quantity_text_view"
android:id="#+id/decrement_button"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="decrement"
android:text="-"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="48dp"
android:paddingLeft="10dp"
android:layout_below="#id/quantity_text_view"
android:paddingRight="10dp"
android:textSize="15sp"
android:layout_toRightOf="#id/decrement_button"
android:id="#+id/initial_qty"
android:gravity="center_vertical"
android:text="1"
/>
<Button
android:layout_toRightOf="#id/initial_qty"
android:layout_below="#id/quantity_text_view"
android:id="#+id/increment_button"
android:onClick="increment"
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+"
/>
<Button
android:id="#+id/submit_button"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="order"
android:onClick="submitOrder"
android:gravity="center_vertical"
android:layout_below="#id/decrement_button"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:id="#+id/order_summary_text_view"
android:textAllCaps="true"
android:textStyle="bold"
android:textSize="16sp"
android:layout_below="#id/submit_button"
/>
</RelativeLayout>
</ScrollView>
and this is my MainActivity.java file
package com.orton.birthdaycard;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import java.text.NumberFormat;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int quantity=1;
private void increment(View view)
{
if(quantity==100)
return;
else
quantity++;
displayQuantity(quantity);
}
private void decrement(View view)
{
if(quantity==1)
return;
else
quantity--;
displayQuantity(quantity);
}
private void displayQuantity(int qty)
{
TextView quantityTextView=(TextView) findViewById(R.id.initial_qty);
quantityTextView.setText(String.valueOf(quantity));
Log.v("MainActivity","the quantity is " + quantity);
}
int basePrice;
private int calculatePrice(boolean hasWhippedCream,boolean hasChocolate)
{
basePrice = 5;
if(hasWhippedCream==true)
basePrice += 1;
if(hasChocolate==true)
basePrice +=2 ;
return basePrice=basePrice*quantity;
}
String priceMessage="";
private void submitOrder(View view)
{
EditText nameField = (EditText) findViewById(R.id.name_text_field);
String name = nameField.getText().toString();
CheckBox whippedCream = (CheckBox) findViewById(R.id.whipped_cream);
boolean hasWhippedCream = whippedCream.isChecked();
CheckBox chocolate = (CheckBox) findViewById(R.id.chocolate);
boolean hasChocolate = chocolate.isChecked();
int price=calculatePrice(hasWhippedCream,hasChocolate);
String orderSummary=createOrderSummary(name,price,hasWhippedCream,hasChocolate);
displayMessage(orderSummary);
}
private void displayMessage(String orderSummary)
{
TextView summary= (TextView) findViewById(R.id.order_summary_text_view);
summary.setText(""+orderSummary);
}
private String createOrderSummary(String name, int price, boolean hasWhippedCream, boolean hasChocolate)
{
String priceMessage = "Name :" + name;
if(hasWhippedCream)
priceMessage += "\nWhipped Cream Added" ;
if(hasChocolate)
priceMessage += "\nChocolate Added" ;
priceMessage+="\nQuantity : "+quantity;
priceMessage+="\nTotal : $"+price;
priceMessage+="Thank You!" ;
return priceMessage;
}
}
this is debugging report
05-23 22:14:45.695 29736-29736/com.orton.birthdaycard E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.orton.birthdaycard, PID: 29736
java.lang.IllegalStateException: Could not find method increment(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'increment_button'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Your method must use the public modifier:
public void increment(View view)
{
if(quantity==100)
return;
else
quantity++;
displayQuantity(quantity);
}
Do this
increment_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
instead of android:onClick="increment"

how to make number picker work in fragment with on clicklistener

I have a tab layout, number picker in the fragment doesn't seem to be working, the values remain 0 always.
Basically I am making random number app.
there are three number pickers in my fragment, one gives minimum number, one gives maximum number and one gives quantity of random numbers.
after setting these values in number picker user hits a button to get random numbers.
for me button is not doing anything when the values are set.
Please suggest me how to do this.
My fragment activity
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
public class TabFragment2 extends Fragment {
//declare Spinner object
Spinner generatorType;
Button getRandomNumberBtn;
int minimumNumber;
int maximumNumber;
int quantity1;
TextView randNumbText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_fragment_2, container, false);
getRandomNumberBtn = (Button) view.findViewById(R.id.get_number);
NumberPicker miniText;
final TextView minimumTextView = (TextView) view.findViewById(R.id.textView3);
miniText = (NumberPicker) view.findViewById(R.id.start_number);
miniText.setMinValue(0);
miniText.setMaxValue(10000);
miniText.setWrapSelectorWheel(true);
miniText.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal1, int newVal1) {
//Display the newly selected number from picker
minimumTextView.setText("Min: " + newVal1);
}
});
minimumNumber = miniText.getValue();
NumberPicker maxiText;
final TextView maximumTextView = (TextView) view.findViewById(R.id.textView2);
maxiText = (NumberPicker) view.findViewById(R.id.end_number);
maxiText.setMinValue(0);
maxiText.setMaxValue(10000);
maxiText.setWrapSelectorWheel(true);
maxiText.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal2, int newVal2) {
//Display the newly selected number from picker
maximumTextView.setText("Max: " + newVal2);
}
});
maximumNumber = maxiText.getValue();
NumberPicker quantity;
final TextView quantityTextView = (TextView) view.findViewById(R.id.quantity_text);
quantity = (NumberPicker) view.findViewById(R.id.quantity_picker);
quantity.setMinValue(0);
quantity.setMaxValue(100);
quantity.setWrapSelectorWheel(true);
quantity.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal3, int newVal3) {
//Display the newly selected number from picker
quantityTextView.setText("Quantity: " + newVal3);
}
});
quantity1 = quantity.getValue();
randNumbText = (TextView) view.findViewById(R.id.random_number_display);
getRandomNumberBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int b = maximumNumber + 1;
int a = minimumNumber;
int c = quantity1;
String multipleRandoms = "";
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> list1 = new ArrayList<Integer>();
for (int i = a; i < b; i++) {
list.add(new Integer(i));
}
int k = list.size();
Collections.shuffle(list);
if (k>c) {
for (int i = 1; i <= c; i++) {
list1.add(new Integer(list.get(i)));
multipleRandoms = list1.toString();
}
}else if (k==c) {
multipleRandoms = list.toString();
} else {
Toast toast = Toast.makeText(getActivity(),"Quantity can not be greater than numbers in range", Toast.LENGTH_LONG);
toast.show();
}
randNumbText.setText(multipleRandoms);
}
});
return view;
}
My fragment layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/tab_fragment_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context="com.example.tabstrial2.TabFragment2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="#dimen/activity_vertical_margin">
<LinearLayout
android:id="#+id/linear_layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/generator_type_spinner"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/linear_layout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10px"
android:text="Min" />
<NumberPicker
android:id="#+id/start_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20px"
android:layout_marginTop="20px" />
</LinearLayout>
<LinearLayout
android:layout_width="2px"
android:layout_height="match_parent"
android:background="#android:color/black">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/quantity_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10px"
android:text="Quantity" />
<NumberPicker
android:id="#+id/quantity_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20px"
android:layout_marginTop="20px" />
</LinearLayout>
<LinearLayout
android:layout_width="2px"
android:layout_height="match_parent"
android:background="#android:color/black">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10px"
android:text="Max" />
<NumberPicker
android:id="#+id/end_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView2"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20px"
android:layout_marginTop="20px" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/random_number_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linear_layout1"
android:layout_centerHorizontal="true"
android:layout_margin="10px"
android:textColor="#android:color/black"
android:textSize="40dp" />
<Button
android:id="#+id/get_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/random_number_display"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_orange_light"
android:focusable="true"
android:focusableInTouchMode="true"
android:onClick="getRandomNumber"
android:paddingLeft="20px"
android:paddingRight="20px"
android:text="Get Random Number" />
</RelativeLayout>
</ScrollView>

onFocusChange Doesnt respond from within a view pager

Reposting this as I initially posted it originally with the wrong title and so it got lost in the void of questions.
I'm creating a small timetable app, and am currently creating a screen with some tabs. Each tab is a seperate fragment and each have 20 edit texts inside of them. What I aim to do, is if the user types in one of the edit texts, then clicks off of it and the focus is lost, to send the text from the edit text field to the main activity. Then, when the confirm button is clicked, any text that is in the edit texts is then saved to an SQLite database.
My problem is that the focus listener doesn't seem to be working.
Here is one of the fragment's code:
package com.example.schoolandrevisiontimetable;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.EditText;
public class Input_slessontime_monday extends Fragment implements OnFocusChangeListener{
OnMondayEditTextChangedListener monCallback;
private String mondayString;
private int mondayPeriod;
private int mondayPosition;
public void MondayEditTextChanged(String mondayEditText, int period, int position){
mondayString = mondayEditText;
mondayPeriod = period;
mondayPosition=position;
monCallback.onMondayEditTextChanged(mondayString, mondayPeriod, mondayPosition);
}
public interface OnMondayEditTextChangedListener {
public void onMondayEditTextChanged(String mondayEditText, int period, int position);
}
EditText[][] mondayInput = new EditText[11][2];
String [][] mondayStrings = new String [11][2];
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_input_slessontime_monday,
container, false);
mondayInput[1][0] = (EditText) rootView.findViewById(R.id.mondayslesson1start);
mondayInput[1][1] = (EditText) rootView.findViewById(R.id.mondayslesson1end);
mondayInput[2][0] = (EditText) rootView.findViewById(R.id.mondayslesson2start);
mondayInput[2][1] = (EditText) rootView.findViewById(R.id.mondayslesson2end);
...
//this continues up to 10. Had to delete the rest due to character limitations
Log.v("Monday", "Edit texts all set");
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
monCallback = (OnMondayEditTextChangedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnMondayEditTextChanged");
}
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
Log.v("monday", "focus change!");
String edittext;
if(!hasFocus)
switch(v.getId()){
case R.id.mondayslesson1start:
edittext = mondayInput[1][0].getText().toString();
MondayEditTextChanged(edittext,1,0);
break;
case R.id.mondayslesson1end:
edittext = mondayInput[1][1].getText().toString();
MondayEditTextChanged(edittext,1,1);
break;
case R.id.mondayslesson2start:
edittext = mondayInput[2][0].getText().toString();
MondayEditTextChanged(edittext,2,0);
break;
case R.id.mondayslesson2end:
edittext = mondayInput[2][1].getText().toString();
MondayEditTextChanged(edittext,2,1);
break;
//this continues up to 10. Had to delete the rest due to character limitations
}
}
}
Here is the activity's code:
package com.example.schoolandrevisiontimetable;
import java.util.List;
import com.astuetz.PagerSlidingTabStrip;
import Database.DatabaseHelper;
import Database.MC_sschoolday;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
/**DETECTIVEREPORT:
* onfocuschange does nothing
* but it seems that getCount() is called everytime you click onto another textbox, or scroll. probably unrelated.
* **/
public class Input_slessontime extends FragmentActivity implements
Input_slessontime_monday.OnMondayEditTextChangedListener,
Input_slessontime_tuesday.OnTuesdayEditTextChangedListener,
Input_slessontime_wednesday.OnWednesdayEditTextChangedListener{
//this class uses the pagerslidingtabstrip library from https://github.com/astuetz/PagerSlidingTabStrip to create a tab layout.
MC_sschoolday[] sday = new MC_sschoolday[7]; //new array of schooldays
int amountOfDays;
List<MC_sschoolday> schoolday;
DatabaseHelper db;
public static FragmentManager fgmanger;
private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;
Input_slessontime_monday mondayFragment;
String[][][] slessonTimes = new String[8][11][2];
int numberOfLessonTimes=0;
String[] slessonPointer = new String[140];
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.v("LessonTimeInput", "oncreate");
db = new DatabaseHelper(getApplicationContext());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input_slessontime);
fgmanger = getSupportFragmentManager();
schoolday = db.getAllSSchoolDays();
amountOfDays = getAmountOfDaysUsed();
tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs); //set the tabs
pager = (ViewPager) findViewById(R.id.pager); //set the pager
adapter = new MyPagerAdapter(fgmanger); //set the adapter
pager.setAdapter(adapter); //set the adapter to the pager
final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
.getDisplayMetrics());
pager.setPageMargin(pageMargin);
tabs.setViewPager(pager);
db.close();
}
#Override
public void onMondayEditTextChanged(String mondayEditText, int period, int position) {
slessonTimes[1][period][position] = mondayEditText;
Log.v("LessonTimeInput", "MondayLessontime Added "+ period + " " + position);
slessonPointer[numberOfLessonTimes] = ("1"+period+position);
numberOfLessonTimes++;
}
#Override
public void onTuesdayEditTextChanged(String tuesdayEditText, int period,
int position) {
Log.v("LessonTimeInput", "TuesdayLessontime Added "+ period + " " + position);
slessonTimes[2][period][position] = tuesdayEditText;
slessonPointer[numberOfLessonTimes] = ("2"+period+position);
numberOfLessonTimes++;
}
#Override
public void onWednesdayEditTextChanged(String wednesdayEditText, int period,
int position) {
Log.v("LessonTimeInput", "TuesdayLessontime Added "+ period + " " + position);
slessonTimes[3][period][position] = wednesdayEditText;
slessonPointer[numberOfLessonTimes] = ("3"+period+position);
numberOfLessonTimes++;
}
public void confirmslesson(View v){
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.input_slessontime, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
//this gets the amount of days used, so that the code knows how many tabs to generate
public int getAmountOfDaysUsed(){
db = new DatabaseHelper(getApplicationContext()); //open the database
int amountOfDaysUsed=0; //initialise this int
for (int i =0; i<7; i++){
sday[i]=schoolday.get(i); //stores the lessons in an array
}
for (int i =0; i<7; i++){
if(sday[i].getUsed().toString().equals("y")){ //find how many days are used
amountOfDaysUsed++;
}
}
db.close(); //close the database
Log.v("LessonTimeInput", "amount of days used " + amountOfDaysUsed);
return amountOfDaysUsed; //return the int
}
//small subclass for the pageradapter
public class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
String[] days = new String[7];
db = new DatabaseHelper(getApplicationContext());
int usedCount2=0;
for (int i =0; i< 7; i++){
sday[i]=schoolday.get(i);
if(sday[i].getUsed().toString().equals("y")){
days[usedCount2] = sday[i].getschool_day();
usedCount2++;
}
}
db.close();
Log.v("LessonTimeInput", "getPageTitle");
return days[position];
}
#Override
public int getCount() {
Log.v("LessonTimeInput", "getCount");
return getAmountOfDaysUsed();
}
public Fragment switchFragmentDay(int num) {
DatabaseHelper db;
db = new DatabaseHelper(getApplicationContext());
MC_sschoolday[] sday = new MC_sschoolday[7];
int usedCount1=amountOfDays;
int [] dayID = new int[usedCount1];
int usedCount2=0;
for (int i =0; i< amountOfDays; i++){
sday[i]=schoolday.get(i);
if(sday[i].getUsed().toString().equals("y")){
dayID[usedCount2] = (int) sday[i].getSchool_day_id();
usedCount2++;
}
}
db.close();
switch (dayID[num]){
case 1:
return new Input_slessontime_monday();
case 2:
return new Input_slessontime_tuesday();
case 3:
return new Input_slessontime_wednesday();
case 4:
return new Input_slessontime_thursday();
case 5:
return new Input_slessontime_friday();
case 6:
return new Input_slessontime_saturday();
case 7:
return new Input_slessontime_sunday();
}
return null;
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return switchFragmentDay(0);
case 1:
return switchFragmentDay(1);
case 2:
return switchFragmentDay(2);
case 3:
return switchFragmentDay(3);
case 4:
return switchFragmentDay(4);
case 5:
return switchFragmentDay(5);
case 6:
return switchFragmentDay(6);
}
return null;
}
}
public void StartIntent(Class<?> intentclass) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, intentclass);
startActivity(intent);
}
}
The XML for the fragment:
<ScrollView
android:id="#+id/monday_fragment"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.pipturner.timetable.Input_slessontime_monday"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/mondayslesson1used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="#string/prompt_lesson" />
<EditText
android:id="#+id/mondayslesson1start"
android:inputType="textMultiLine"
android:hint="#string/start"
android:dependency="mondayslesson1used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="#+id/mondayslesson1end"
android:inputType="textMultiLine"
android:hint="#string/end"
android:dependency="mondayslesson2used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/mondayslesson2used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="#string/prompt_lesson" />
<EditText
android:id="#+id/mondayslesson2start"
android:inputType="textMultiLine"
android:hint="#string/start"
android:dependency="mondayslesson1used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="#+id/mondayslesson2end"
android:inputType="textMultiLine"
android:hint="#string/end"
android:dependency="mondayslesson2used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/mondayslesson3used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="#string/prompt_lesson" />
<EditText
android:id="#+id/mondayslesson3start"
android:inputType="textMultiLine"
android:hint="#string/start"
android:dependency="mondayslesson3used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="#+id/mondayslesson3end"
android:inputType="textMultiLine"
android:hint="#string/end"
android:dependency="mondayslesson3used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/mondayslesson4used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="#string/prompt_lesson" />
<EditText
android:id="#+id/mondayslesson4start"
android:inputType="textMultiLine"
android:hint="#string/start"
android:dependency="mondayslesson4used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="#+id/mondayslesson4end"
android:inputType="textMultiLine"
android:hint="#string/end"
android:dependency="mondayslesson4used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/mondayslesson5used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="#string/prompt_lesson" />
<EditText
android:id="#+id/mondayslesson5start"
android:inputType="textMultiLine"
android:hint="#string/start"
android:dependency="mondayslesson5used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="#+id/mondayslesson5end"
android:inputType="textMultiLine"
android:hint="#string/end"
android:dependency="mondayslesson5used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/mondayslesson6used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="#string/prompt_lesson" />
<EditText
android:id="#+id/mondayslesson6start"
android:inputType="textMultiLine"
android:hint="#string/start"
android:dependency="mondayslesson6used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="#+id/mondayslesson6end"
android:inputType="textMultiLine"
android:hint="#string/end"
android:dependency="mondayslesson6used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/mondayslesson7used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="#string/prompt_lesson" />
<EditText
android:id="#+id/mondayslesson7start"
android:inputType="textMultiLine"
android:hint="#string/start"
android:dependency="mondayslesson7used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="#+id/mondayslesson7end"
android:inputType="textMultiLine"
android:hint="#string/end"
android:dependency="mondayslesson7used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/mondayslesson8used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="#string/prompt_lesson" />
<EditText
android:id="#+id/mondayslesson8start"
android:inputType="textMultiLine"
android:hint="#string/start"
android:dependency="mondayslesson8used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="#+id/mondayslesson8end"
android:inputType="textMultiLine"
android:hint="#string/end"
android:dependency="mondayslesson8used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/mondayslesson9used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="#string/prompt_lesson" />
<EditText
android:id="#+id/mondayslesson9start"
android:inputType="textMultiLine"
android:hint="#string/start"
android:dependency="mondayslesson9used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="#+id/mondayslesson9end"
android:inputType="textMultiLine"
android:hint="#string/end"
android:dependency="mondayslesson9used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/mondayslesson10used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="#string/prompt_lesson" />
<EditText
android:id="#+id/mondayslesson10start"
android:inputType="textMultiLine"
android:hint="#string/start"
android:dependency="mondayslesson10used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="#+id/mondayslesson10end"
android:inputType="textMultiLine"
android:hint="#string/end"
android:dependency="mondayslesson10used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
</LinearLayout>
</ScrollView>
and the XML for the activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:background="#drawable/background_tabs" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
tools:context=".Input_slessontime" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
style="?android:attr/borderlessButtonStyle"
android:id="#+id/cancelslessontime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:layout_weight="1" />
<Button
style="?android:attr/borderlessButtonStyle"
android:id="#+id/confirmslessontime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/confirm"
android:layout_weight="1"
android:onClick="confirmslesson" />
</LinearLayout>
</LinearLayout>
Something that may be related, is that when I click on or off an edit text, or scroll the fragment, getCount() is called. Probably unrelated.
Thanks in advance!
needed to set an onFocusListener.
//Thanks for the help guys//
¬¬

Categories

Resources