How to add onClick for buttons? - android

I'm trying to figure out why my app crashes when my AddThreeToTeamA button crashes my application upon clicking. I have addded my XML and my Java code.
I am following a tutorial however it obviously does not crash and I have tried several different solutions.
Any hint will be appreciated.
package com.themovingmonkey.courtcounter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int score = 0;
int scoreTeamA;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addThreeForTeamA() {
scoreTeamA = score + 3;
displayForTeamA(scoreTeamA);
}
public void addTwoForTeamA() {
scoreTeamA = score + 2;
displayForTeamA(scoreTeamA);
}
public void addOneForTeamA() {
scoreTeamA = score + 1;
displayForTeamA(scoreTeamA);
}
public void displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.Team_A_Score);
scoreView.setText(String.valueOf(scoreTeamA));
}
}
////////////////////////////////////////////////////////////////
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text= "Team A"
android:padding="4dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0"
android:padding="4dp"
android:id="#+id/Team_A_Score"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3 Points"
android:layout_margin="8dp"
android:onClick="addThreeForTeamA"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2 Points"
android:layout_margin="8dp"
android:onClick="addTwoForTeamA"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1 Point"
android:layout_margin="8dp"
android:onClick="addOneForTeamA"
/>
</LinearLayout>

All methods defined using the android:onClick="someMethod" attribute. Then they have to be public and should pass a View as the only parameter, Change your methods to:
public void someMethod(View view){
//Everything else!
}
Your methods do not pass a View as the only parameter at all currently!

Related

Android Initial Login Screen then use fragments to swipe between screens once login is successful

I am trying to create an Android app, then has an initial login screen that detects if a user is Admin or User. Once logged in, the user (depending on their role) will see different tabs to swipe between. I have a basic app that starts with a login activity and the login to decipher the users roles (using static values for now). First part is ok. Now when I try to use Fragments after the login screen, I'm getting stuck.
I have a LoginActivity that has an onClikcListener on a button after user enters credentials. Depending on whether the user is Amin or General Users, it will just to a new activity either activity_vote_admin or activity_vote_user. At this point I am stuck. Any ideas if what I am trying to do is valid. Or is there a better way to do this. At the moment I am getting this error.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.confidencevotefragmentrebuild/com.example.confidencevotefragmentrebuild.activity_vote_admin}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference.
LoginActivity (this is my main activity)
...
package com.example.confidencevotefragmentrebuild;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
private static EditText username;
private static EditText password;
public static TextView attempts;
private static Button submit_button;
int attempt_counter = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginButton();
}
public void loginButton() {
username = (EditText) findViewById(R.id.editText);
password = (EditText) findViewById(R.id.editText2);
attempts = (TextView) findViewById(R.id.textView_Attempts);
//Reference button view
final Button submit_button = findViewById(R.id.submit_button);
// perform click event on button
submit_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (username.getText().toString().equals("admin") &&
password.getText().toString().equals("pass")) {
Toast.makeText(LoginActivity.this, "Welcome " + username.getText(),
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, activity_vote_admin.class);
startActivity(intent);
} else if (username.getText().toString().equals("user") &&
password.getText().toString().equals("pass")) {
Toast.makeText(LoginActivity.this, "Welcome " + username.getText(),
Toast.LENGTH_SHORT).show();
Intent intentUser = new Intent(LoginActivity.this, activity_vote_user.class);
startActivity(intentUser);
} else {
Toast.makeText(LoginActivity.this, "User or Password incorrect",
Toast.LENGTH_SHORT).show();
attempt_counter--;
attempts.setText(Integer.toString(attempt_counter));
if (attempt_counter == 0) {
Toast.makeText(LoginActivity.this, "Too many failed attempts, please close app and try again",
Toast.LENGTH_SHORT).show();
submit_button.setEnabled(false);
}
}
}
}
);
}
}
...
Activity_vote_admin
...
package com.example.confidencevotefragmentrebuild;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
public class activity_vote_admin extends AppCompatActivity {
/**
* Field for selecting the number of confidence votes
*/
RatingBar mResults;
private RatingBar rBar;
Button mBtn;
/**
* Spinner field to enter the project name
*/
private Spinner mProjectSpinner;
/**
* Projects. The possible values are:
* stored locally - Needs to get values from a database. TODO
*/
int mProject = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vote_main_admin);
// Find the view pager that will allow the user to swipe between fragments
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
// Create an adapter that knows which fragment should be shown on each page
AdminFragmentPagerAdapter adapter = new AdminFragmentPagerAdapter(getSupportFragmentManager());
// Set the adapter onto the view pager
viewPager.setAdapter(adapter);
// initiate a rating bar
rBar = findViewById(R.id.rating_bar);
//float vote = rBar.getRating();
// Find all relevant views for user input
mResults = findViewById(R.id.rating_bar);
mProjectSpinner = findViewById(R.id.spinner_project);
// Run getRating method to get rating number from a rating bar
//getRating();
// Initiate button
mBtn = findViewById(R.id.voteButton);
// perform click event on button
mBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// get values and then displayed in a toast
//String totalStars = "Total Stars:: " + RatingBar.getNumStars();
float vote = rBar.getRating();
String rating = "Rating : " + vote;
Toast.makeText(getApplicationContext(), rating + "\n", Toast.LENGTH_LONG).show();
}
});
projectSpinner();
}
// Setup the dropdown spinner that allows the user to select the role.
private void projectSpinner() {
// Create adapter for spinner. The list options are from the String array it will use
// the spinner will use the default layout
ArrayAdapter projectSpinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.projects, android.R.layout.simple_spinner_item);
// Specify dropdown layout style - simple list view with 1 item per line
projectSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
// Apply the adapter to the project spinner
mProjectSpinner.setAdapter(projectSpinnerAdapter);
// Set the integer mSelected to the constant values
mProjectSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selection = (String) parent.getItemAtPosition(position);
if(mProjectSpinner.getSelectedItem() == "This is Hint Text");
if (!TextUtils.isEmpty(selection)) {
if (selection.equals(getString(R.string.nextGEMS1_5))) {
mProject = 0 ;
return;
} else if (selection.equals(getString(R.string.nextGEMS1_6))) {
mProject = 1;
return;
} else if (selection.equals(getString(R.string.nextGEMS1_7))) {
mProject = 2;
return;
} else if (selection.equals(getString(R.string.nextGEMS1_8))) {
mProject = 3;
return;
} else if (selection.equals(getString(R.string.nextGEMS1_9))) {
mProject = 4;
return;
}
else mProject = 0;
}
}
// Because AdapterView is an abstract class, onNothingSelected must be defined
#Override
public void onNothingSelected(AdapterView<?> parent) {
mProject = 0; // User
}
});
}
}
...
activity_vote_main_admin.xml
...
<?xml version="1.0" encoding="utf-8"?>
<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">
<androidx.viewpager.widget.ViewPager
android:name="com.example.confidencevotefragmentrebuild.testFragment1"
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
...
activity_login.xml
...
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".LoginActivity"
tools:showIn="#layout/activity_login">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5fb0c9"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<TextView
android:id="#+id/login_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:gravity="center_horizontal"
android:text="LOGIN"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="500dp"
android:background="#ffffff"
android:elevation="4dp"
android:orientation="vertical"
android:padding="20dp"
android:layout_below="#+id/login_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="80dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="450dp"
android:orientation="vertical"
android:paddingTop="30dp">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:drawableLeft="#drawable/usericon"
android:hint="User Name"
android:inputType="textEmailAddress"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:drawableLeft="#drawable/lock"
android:hint="Password"
android:inputType="textPassword"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingTop="5dp"
android:text="Forgot Password?" />
<Button
android:id="#+id/submit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="22dp"
android:background="#drawable/action_buttom"
android:text="Sign in"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="18sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="20dp"
android:orientation="horizontal"
android:paddingTop="1dp">
<TextView
android:id="#+id/attempts_text"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_gravity="center_horizontal"
android:paddingTop="1dp"
android:text="Number of attempts remaining: " />
<TextView
android:id="#+id/textView_Attempts"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_gravity="center_horizontal"
android:paddingTop="1dp"
android:text="5" />
</LinearLayout>
<TextView
android:id="#+id/lnkRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:gravity="center"
android:paddingTop="30dp"
android:text="Register here"
android:textColor="#5fb0c9"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
<ImageButton
android:id="#+id/conferencevote_logo"
android:layout_width="210dp"
android:layout_height="180dp"
android:layout_below="#+id/login_title"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:elevation="4dp"
android:background="#drawable/rounded_button"
android:src="#drawable/confvotenew"
/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
...
AdminFragemntyPagerAdapter
...
package com.example.confidencevotefragmentrebuild;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
public class AdminFragmentPagerAdapter extends FragmentPagerAdapter {
public AdminFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if (position == 0) {
return new testFragment1();
} else if (position == 1) {
return new testFragment2();
} else {
return new testFragment2();
}
}
#Override
public int getCount() {
return 3;
}
}
...
aactivity_vote_user.xml
...
<?xml version="1.0" encoding="utf-8"?>
<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="#5fb0c9"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<TextView
android:id="#+id/vote_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:gravity="center_horizontal"
android:text="USER VOTE"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="500dp"
android:background="#ffffff"
android:elevation="4dp"
android:orientation="vertical"
android:padding="20dp"
android:layout_below="#+id/vote_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="80dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="450dp"
android:orientation="vertical"
android:paddingTop="70dp">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<Spinner
android:id="#+id/spinner_project"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:hint="#string/spinner_hint"
android:maxLines="1"
android:singleLine="true"
android:backgroundTint="#color/attCobalt"
android:popupBackground="#color/myBlue"
android:dropDownSelector="#color/attBlue"
/>
</com.google.android.material.textfield.TextInputLayout
>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<RatingBar
android:id="#+id/rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stepSize="1.0"
android:layout_marginTop="40dp"
android:theme="#style/rating_bar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="5dp"
android:text="Vote and Submit" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="#+id/voteButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="22dp"
android:background="#drawable/action_buttom"
android:text="Submit"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="18sp" />
/>
</LinearLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<ImageButton
android:id="#+id/conferencevote_logo"
android:layout_width="210dp"
android:layout_height="180dp"
android:layout_below="#+id/login_title"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp"
android:elevation="4dp"
android:background="#drawable/rounded_button"
android:src="#drawable/confvotenew"
/>
</RelativeLayout>
...
My fragments are as follows.
...
package com.example.confidencevotefragmentrebuild;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
public class testFragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_vote_user, container, false);
}
}
...
There are two reason for this issue :
You didn't declare any button on "activity_vote_main_admin.xml", thats why your view not binding with activity and giving null pointer exception.
May be you declare the button on fragment xml and trying to bind it with activity.

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.

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//
¬¬

How to convert textview as link in android?

I have created a login page which consists of a username password login button and a signup button which should be a link. I want to convert this signup(text view) to a link that can be navigated to the next page. How would I do that? For example if I click on don't have an account? sigup,it should be navigated to welcome.java..
Please help....
activity_main.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="match_parent"
android:background="#drawable/bg_gradient"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:text="#string/welcome"
android:textColor="#color/white"
android:textSize="45dp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_form_rounded"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#null"
android:hint="#string/email"
android:padding="5dp"
android:singleLine="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#null"
android:hint="#string/password"
android:inputType="textPassword"
android:padding="5dp" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#drawable/bg_button_rounded"
android:text="#string/login"
android:textColor="#color/white" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="25dp"
android:gravity="center_horizontal"
android:text="#string/signup"
android:autoLink="web"
android:textColor="#color/white" />
</RelativeLayout>
mainactivity.java
package com.example.internationalization12;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getActionBar().hide();
}
}
Try this :
package com.example.internationalization12;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
TextView txtSignUp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSignUp=(TextView)findViewById(R.id.txt_sign_up);
txtSignUp.setText(Html.fromHtml("<u>Dont have account?SignUp</u>")
txtSignUp.setOnClickListener(signUpListener);
//getActionBar().hide();
}
public OnClickListener signUpListener=new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,Welcome.class));
}
};
}
You have to setOnTouchListener to the TextView. Here I give you an example:
public class MainActivity extends Activity implements View.OnTouchListener {
private TextView tv1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
tv1.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// HERE YOU HAVE TO CHECK WHICH ID IS SELECTED, IF YOU HAVE MORE THAN ONE,
// AND WHICH EVENT HAS THE USER DONE.
// IN YOUR CASE.. OPEN WEBSITE LIKE THIS:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
return true;
}
}
you can try Linkify here
textview.setText("www.androiddeveloper.com");
Linkify.addLinks(textview, Linkify.WEB_URLS);

multiply with zero, double no number

I am trying to build a calculator for android, i am trying to multiply two numbers.
If i give number 1 and leave number 2 open, i want to get zero as an answer.
I first thought it could be the problem of not having a value for the double.
So i tried: if (Double.isNaN(number1)) { number1=0; }
But it did not work.
How do i make this happen?
R.layout.main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 1 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="number:" />
<EditText
android:id="#+id/number1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="0"
android:inputType="number"
android:layout_marginLeft="20dp" />
</LinearLayout>
<!-- 2 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="number:" />
<EditText
android:id="#+id/number2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="0"
android:inputType="number"
android:layout_marginLeft="20dp" />
</LinearLayout>
<!-- multiply -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="multiply:" />
<TextView
android:id="#+id/multiplydisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="20dp" />
</LinearLayout>
<Button
android:id="#+id/btncalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"/>
<Button
android:id="#+id/btnreset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset" />
</LinearLayout>
TipcalcActivity.java
package com.tip.calc;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class TipcalcActivity extends Activity {
private EditText number1;
private EditText number2;
private TextView multiplydisplay;
private Button btncalculate;
private Button btnreset;
private double number1calc = 0;
private double number2calc = 0;
private double multiply = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
private void initControls() {
number1 = (EditText)findViewById(R.id.number1);
number2 = (EditText)findViewById(R.id.number2);
multiplydisplay = (TextView)findViewById(R.id.multiplydisplay);
btncalculate = (Button)findViewById(R.id.btncalculate);
btnreset = (Button)findViewById(R.id.btnreset);
btncalculate.setOnClickListener(new Button.OnClickListener() { public void
onClick (View v){ calculate(); }});
btnreset.setOnClickListener(new Button.OnClickListener() { public void
onClick (View v){ reset(); }});
}
private void calculate() {
number1calc=Double.parseDouble(number1.getText().toString());
number2calc=Double.parseDouble(number2.getText().toString());
multiply=(number1calc*number2calc);
multiplydisplay.setText(Double.toString(multiply));
}
private void reset() {
multiplydisplay.setText("");
number1.setText("");
number2.setText("");
}
}
you should just check that the content of the textfield of the value is different than "". If not, just assign the value 0.
In calculate :
private void calculate() {
number1calc=(number1.getText().toString() != "" ) ? Double.parseDouble(number1.getText().toString()) : 0;
number2calc=(number2.getText().toString() != "" ) ? Double.parseDouble(number2.getText().toString()) : 0;
multiply=(number1calc*number2calc);
multiplydisplay.setText(Double.toString(multiply));
}
if(numberOne.getText().toString().trim().length() < 1 || numberTwo.getText().toString().length() < 1){
multiplyDisplay.setText("0");
}
else{
//default handling
}
If the edittext is left blank, this is obviously not a number(NaN). Doing any operation with NaN in Java will cause the result of this operation to be NaN.

Categories

Resources