java.lang.StringIndexOutOfBoundsException: error in Android - java

while I was testing my app using the virtual emulator (don't have a valid device where I can test), I found out a bug. That's the java.lang.StringIndexOutOfBoundsException: length=0; index=2 error, and it pops out when I click on an EditText and try to write in it. The EditText is set to appear when another button is clicked. Here is the XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.luca.cecco.activity.allergie"
android:padding="20dp"
android:orientation="vertical"
android:background="#drawable/possible4_bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hai allergie?"
android:fontFamily="sans-serif"
android:textAllCaps="true"
style="#style/BOLD"
android:textAlignment="center"
android:layout_gravity="center"
android:layout_marginTop="200dp"
android:background="#drawable/round_shape_btn"
android:textColor="#color/blank"
android:padding="10dp"
android:textSize="40sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="105dp">
<Button
android:id="#+id/btn_allergia_si"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_marginRight="10dp"
android:background="#drawable/round_shape_clicked_btn"
android:fontFamily="sans-serif"
android:text="Sì"
android:textAllCaps="true"
style="#style/BOLD"
android:gravity="center"
android:textColor="#color/blank"
android:textSize="30sp" />
<Button android:id="#+id/btn_allergia_no"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="No"
style="#style/BOLD"
android:gravity="center"
android:background="#drawable/round_shape_clicked_btn"
android:fontFamily="sans-serif"
android:textAllCaps="true"
android:textColor="#color/blank"
android:textSize="30sp"
android:layout_marginLeft="110dp"/>
</LinearLayout>
<EditText android:id="#+id/allergia_si"
android:layout_width="285dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginLeft="150dp"
android:hint="Inserisci qui le tue allergie"
android:textAlignment="center"
android:textSize="20sp"
android:textColorHint="#color/blank"
android:background="#drawable/round_shape_clicked_btn"
android:textAllCaps="true"
android:padding="5dp"/>
<Button android:id="#+id/btn_avanti3"
android:layout_marginTop="100dp"
android:layout_marginRight="30dp"
android:layout_width="110dp"
android:textColor="#color/blank"
android:layout_height="85dp"
android:text="Avanti"
android:gravity="center"
android:textAlignment="center"
android:textSize="20sp"
android:layout_gravity="end"
style="#style/AlertDialog.AppCompat"
android:background="#color/blue_button"
android:textStyle="bold"
android:textAllCaps="true"
android:fontFamily="sans-serif"
/>
</LinearLayout>
</RelativeLayout>
And here it is the Java class:
private Button btn_si;
private Button btn_no;
private EditText et_allergia;
private Button btn_avanti;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_allergie);
btn_si = (Button) findViewById(R.id.btn_allergia_si);
btn_no = (Button) findViewById(R.id.btn_allergia_no);
et_allergia = (EditText) findViewById(R.id.allergia_si);
btn_avanti = (Button) findViewById(R.id.btn_avanti3);
et_allergia.setVisibility(View.INVISIBLE);
btn_no.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
btn_no.setBackground(getResources().getDrawable(R.drawable.round_shape_clicked_btn)); //cambia il colore di base
}
});
btn_si.setOnClickListener(new View.OnClickListener() { //cambia il colore di base e rende visibile l'edit text
#Override
public void onClick(View view)
{
btn_si.setBackground(getResources().getDrawable(R.drawable.round_shape_clicked_btn)); //cambia il colore di base
et_allergia.setVisibility(View.VISIBLE);
}
});
btn_avanti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Intent intent = new Intent(allergie.this, Preferenze.class);
startActivity(intent);
}
});
}

You are getting error Because, You have set property 'textAllCaps' in EditText. This property is mostly used for TextView.
Remove This Property and try again.

Related

How to show shopping cart icon in activity?

I made the shopping cart app and I use the navigation drawer and in this navigation drawer I use the shopping cart icon.
Now, When i click on the particular item. It's open in new activity but its not showing the shopping cart icon, So how will it show? So,i see the item in the cart?
ItemDetailsActivity. java (This is the .java file of this image where i am unable to see the shopping cart icon, so i am unable to see how many items add in the cart)
public class ItemDetailsActivity extends AppCompatActivity {
int imagePosition;
String stringImageUri;
TextView textViewshare, textViewmap;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_details);
SimpleDraweeView mImageView = (SimpleDraweeView)findViewById(R.id.image1);
TextView textViewAddToCart = (TextView)findViewById(R.id.text_action_bottom1);
TextView textViewBuyNow = (TextView)findViewById(R.id.text_action_bottom2);
textViewshare = (TextView) findViewById(R.id.text_action1);
textViewmap = (TextView) findViewById(R.id.text_action3);
TextView textViewBuyNowwithpayment = (TextView) findViewById(R.id.text_action_bottom2);
//Getting image uri from previous screen
if (getIntent() != null) {
stringImageUri = getIntent().getStringExtra(ImageListFragment.STRING_IMAGE_URI);
imagePosition = getIntent().getIntExtra(ImageListFragment.STRING_IMAGE_URI,0);
}
Uri uri = Uri.parse(stringImageUri);
mImageView.setImageURI(uri);
mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ItemDetailsActivity.this, ViewPagerActivity.class);
intent.putExtra("position", imagePosition);
startActivity(intent);
}
});
textViewAddToCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ImageUrlUtils imageUrlUtils = new ImageUrlUtils();
imageUrlUtils.addCartListImageUri(stringImageUri);
Toast.makeText(ItemDetailsActivity.this,"Item added to cart.",Toast.LENGTH_SHORT).show();
MainActivity.notificationCountCart++;
NotificationCountSetClass.setNotifyCount(MainActivity.notificationCountCart);
}
});
textViewBuyNow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ImageUrlUtils imageUrlUtils = new ImageUrlUtils();
imageUrlUtils.addCartListImageUri(stringImageUri);
MainActivity.notificationCountCart++;
NotificationCountSetClass.setNotifyCount(MainActivity.notificationCountCart);
startActivity(new Intent(ItemDetailsActivity.this, CartListActivity.class));
}
});
// payment.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// Intent i = new Intent(ItemDetailsActivity.this, PayPalCheckoutActivity.class);
// startActivity(i);
// }
// });
textViewBuyNowwithpayment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ItemDetailsActivity.this, PayPalCheckoutActivity.class);
startActivity(i);
}
});
textViewshare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
intent.putExtra(Intent.EXTRA_TEXT,"Extra Text");
startActivity(intent);
}
});
textViewmap.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(ItemDetailsActivity.this, Placepicker.class);
startActivity(in);
}
});
}
}
activity_item_details.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/activity_item_details"
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"
android:weightSum="10"
tools:context="com.codeexpertise.eshop.product.ItemDetailsActivity">
<ScrollView android:id="#+id/scrollbar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9.5"
android:scrollbars="none"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.facebook.drawee.view.SimpleDraweeView xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:id="#+id/image1"
android:layout_width="match_parent"
android:layout_height="200.0dp"
fresco:placeholderImage="#color/stay_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Denim Shirt"
android:textSize="16dp"
android:textColor="#color/gen_black"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Rs. 1,979"
android:textSize="20dp"
android:textColor="#color/gen_black"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FREE Delivery"
android:textSize="12dp"
android:layout_marginTop="4dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="8dp">
<TextView android:id="#+id/text_ratings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/green_light"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:text="4.3 *"
android:textSize="12dp"
android:textColor="#color/gen_white"
android:textStyle="bold"/>
<TextView android:id="#+id/text_ratings_reviews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:text="50 ratings \u0026 15 reviews"
android:textSize="12dp"/>
</LinearLayout>/
<View android:layout_width="match_parent"
android:layout_height="#dimen/view_width_small"
android:background="#color/grey_light"
android:layout_marginTop="8dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:layout_marginTop="8dp"
android:weightSum="3">
<LinearLayout android:id="#+id/layout_action1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="2">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_share_black_18dp"/>
<TextView android:id="#+id/text_action1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="8dp"
android:text="Share"
android:showAsAction="ifRoom"
android:textSize="12dp"
android:textColor="#color/gen_black"
android:gravity="left"
android:actionProviderClass=
"android.widget.ShareActionProvider"/>
</LinearLayout>
<View android:layout_width="#dimen/view_width_small"
android:layout_height="match_parent"
android:background="#color/grey_light"/>
<LinearLayout android:id="#+id/layout_action2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="2">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_filter_none_black_18dp"/>
<TextView android:id="#+id/text_action2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="8dp"
android:text="Similar"
android:textSize="12dp"
android:textColor="#color/gen_black"
android:gravity="left"/>
</LinearLayout>
<View android:layout_width="#dimen/view_width_small"
android:layout_height="match_parent"
android:background="#color/grey_light"/>
<LinearLayout android:id="#+id/layout_action3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="2">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_favorite_border_black_18dp"/>
<TextView android:id="#+id/text_action3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_marginLeft="8dp"
android:text="Store Locator"
android:textSize="12dp"
android:textColor="#color/gen_black"
android:gravity="left"/>
</LinearLayout>
</LinearLayout>
<View android:layout_width="match_parent"
android:layout_height="#dimen/view_width_small"
android:background="#color/grey_light"
android:layout_marginTop="8dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Details"
android:textSize="16dp"
android:textColor="#color/gen_black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="\u2022 Regular fit, full sleeve"
android:textSize="12dp"
android:textColor="#color/gen_black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="\u2022 Fabric: Cotton"
android:textSize="12dp"
android:textColor="#color/gen_black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="\u2022 Pattern: printed"
android:textSize="12dp"
android:textColor="#color/gen_black"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="horizontal"
android:weightSum="2"
android:elevation="30dp"
android:background="#color/gen_black">
<TextView android:id="#+id/text_action_bottom1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/gen_white"
android:text="ADD TO CART"
android:textSize="14dp"
android:textColor="#color/gen_black"
android:textStyle="bold"
android:gravity="center"/>
<TextView android:id="#+id/text_action_bottom2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#4dc3ff"
android:text="BUY NOW"
android:textSize="14dp"
android:textColor="#color/gen_white"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
Main.xml (In this xml i use the menu item which show in navigation drawer bar)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/ic_search_white_24dp"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="#+id/action_notifications"
android:title="#string/action_notifications"
app:showAsAction="always"
android:icon="#drawable/ic_notifications_white_24dp"/>
<item android:id="#+id/action_cart"
android:title="#string/action_cart"
app:showAsAction="always"
android:icon="#drawable/ic_menu_notifications"/>
</menu>
Please Override this Method in your ItemDetailsActivity to Show Menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
verride this Method in your ItemDetailsActivity to Handle Click Events
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_cart:
dosomething();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

How to wrap alertdialog in android?

I would like to ask on how to achieve to wrap a content in alertdialog?
Because the current output of my dialog has an excess white field.
Hope someone can help me to understand this problem thanks.
Here is my activity_dialog.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:minWidth="10dp"
android:minHeight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.bloxofcode.toggle.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:background="#color/colorHeaderDialog"
android:textColor="#android:color/white"
android:text="#string/select_gender"
android:padding="15dp"
android:gravity="center_horizontal|left"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:padding="15dp"
android:text="Sample"
android:id="#+id/editText" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ToggleButton
android:text="ToggleButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/toggleButtonMale"
android:textOn=""
android:textOff=""
android:focusable="false"
android:background="#drawable/check_male"
android:layout_weight="1" />
<ToggleButton
android:text="ToggleButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/toggleButtonFemale"
android:textOn=""
android:textOff=""
android:focusable="false"
android:background="#drawable/check_male"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<Button
android:id="#+id/btnCancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:textSize="20dp"
android:padding="30dp"
android:textColor="#android:color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btnAccept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Accept"
android:textSize="20dp"
android:padding="30dp"
android:background="#color/colorDialogOK"
android:textColor="#android:color/white"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Here is some of my implementation in the MainActivity.java:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.activity_dialog, null);
.....
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
Try Creating a dialog like this. this works perfect and then right away your dialog logic is seperated from you activity logic
public class CustomDialog extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public CustomDialog d;
public Button yes, no;
public CustomDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_layout);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//dosomething
}
});
}
}
Hope it is not correct to wrap the content of dialog as it might disrupt the view in tablets and larger devices.
Anyhow, basing upon your requirement.. hope this link works
AlertDialog with custom view: Resize to wrap the view's content

TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead

Always getting this warning TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead. and not finishing Activity for First time. On Second time not getting an warning and activity finishing perfectly.
activity_login.xml
<LinearLayout
android:id="#+id/ll_login_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:visibility="visible">
<android.support.design.widget.TextInputLayout
android:id="#+id/email_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:hint="#string/email_phone"
android:paddingTop="48dp">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:inputType="textEmailAddress"
android:paddingEnd="0dp"
android:paddingStart="8dp" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/password_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/password"
android:paddingBottom="16dp">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:inputType="textPassword"
android:paddingEnd="0dp"
android:paddingStart="8dp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/button_login_social_margin"
android:layout_marginStart="#dimen/button_login_social_margin"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="#string/login"
android:textColor="#android:color/black" />
</LinearLayout>
LoginActivity.java
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final AppCompatEditText etEmailOrPh = (AppCompatEditText) findViewById(R.id.et_email);
final AppCompatEditText etPassword = (AppCompatEditText) findViewById(R.id.et_password);
final Button btnLogin = (Button) findViewById(R.id.btn_login);
assert etEmailOrPh != null;
assert etPassword != null;
assert btnLogin != null;
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String emailOrPhone = etEmailOrPh.getText().toString().trim();
String password = etPassword.getText().toString().trim();
if(emailOrPhone.isEmpty()){
etEmailOrPh.setError(getResources().getString(R.string.email_phone_mandatory));
etEmailOrPh.requestFocus();
} else if(emailOrPhone.contains("#") && CommonUtil.isValidEmail(emailOrPhone)) {
etEmailOrPh.setError(getResources().getString(R.string.email_error));
etEmailOrPh.requestFocus();
} else if(password.isEmpty()) {
etPassword.setError(getResources().getString(R.string.password_mandatory));
etPassword.requestFocus();
} else {
SharedPreferences cache = LoginActivity.this.getSharedPreferences(Constants.SHARED_PREF_NAME, Context.MODE_PRIVATE);
final SharedPreferences.Editor preferenceEditor = cache.edit();
preferenceEditor.putInt(Constants.SHARED_PREF_ITEM_USER_ID, 1);
preferenceEditor.apply();
setResult(RESULT_OK);
finish();
}
}
});
}
}
Change this EditText
android.support.v7.widget.AppCompatEditText
to this
android.support.design.widget.TextInputEditText
Full Code :
<android.support.design.widget.TextInputLayout
android:id="#+id/email_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:hint="#string/email_phone"
android:paddingTop="48dp">
<android.support.design.widget.TextInputEditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:inputType="textEmailAddress"
android:paddingEnd="0dp"
android:paddingStart="8dp"
/>
</android.support.design.widget.TextInputLayout>
Try not using
<android.support.v7.widget.AppCompatEditText
instead use
<EditText
So you will get:
<EditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:inputType="textEmailAddress"
android:paddingEnd="0dp"
android:paddingStart="8dp" />
After doing a bit of research on the issue i found this post:
EditText added is not a TextInputEditText. Please switch to using that class instead
let me know if it helps you.

EditText is declared and LogCat is still giving nullpointerexception

Ok, so I am getting a nullpointerexception in logcat when I am running my app, however I looked at other StackOverflow questions that were similar to mine and found that I had done everything right, so where is my error coming from?
here is my MainActivity:
public double inchSnow, durationStorm, peakSnow, peakTime, finalSnow, windSpeed, snowGround, result, avgSnow;
EditText inSnow, durnStorm, pSnow, pTime, fSnow, wSpeed, sGround;
protected void onCreate(Bundle savedInstanceState) {
final AlertDialog.Builder invalidNumber = new AlertDialog.Builder(actContext);
invalidNumber.setTitle("Blank Field!");
invalidNumber.setMessage("Seems like a field has been left blank, can you fix it?");
invalidNumber.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
inSnow = (EditText) findViewById(R.id.inchesSnow);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getZip = (Button) findViewById(R.id.calculate);
final Intent intent = new Intent(this, com.boreas.snowdaycalculator.result.class);
getZip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try{
inchSnow = new Integer(inSnow.getText().toString()).intValue();
}catch (NumberFormatException ex){
final AlertDialog alertDialog = invalidNumber.create();
invalidNumber.show();
}
Intent myIntent = new Intent(MainActivity.this, result.class);
myIntent.putExtra("inchSnow",inchSnow);
startActivity(myIntent);
}
});
}
My MainActivity 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="521dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="520px"
android:layout_alignTop="#+id/scrollView"
android:layout_alignLeft="#+id/scrollView">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/inchesSnow"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:hint="Inches of Snow"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/durationStorm"
android:layout_below="#+id/inchesSnow"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:hint="Duration of Storm"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/peakRate"
android:layout_below="#+id/durationStorm"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:hint="Peak Snow Rate"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/peakRateTime"
android:layout_below="#+id/peakRate"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:hint="Time of Peak Snow Rate"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:ems="10"
android:id="#+id/finalRate"
android:layout_below="#+id/peakRateTime"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:hint="Final Snow Rate"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:ems="10"
android:id="#+id/windSpeed"
android:layout_below="#+id/finalRate"
android:layout_alignParentLeft="true"
android:hint="Wind Speed"
android:layout_alignParentRight="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/snowGround"
android:layout_below="#+id/windSpeed"
android:layout_alignParentLeft="true"
android:hint="Snow Already on Ground"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</ScrollView>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate Chances"
android:id="#+id/calculate"
android:layout_marginBottom="150dp"
android:layout_alignBottom="#+id/scrollView"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Anyone have any ideas on what I am doing wrong? Thanks.
You need to place
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
before all other calls in method. findViewById() will return null until you set the content view. And more importantly, super.onCreate(savedInstanceState) will create the activity to work with.
Put the line:
inSnow = (EditText) findViewById(R.id.inchesSnow);
after the line:
setContentView(R.layout.activity_main);

I'm working on having a "Keep me on Logged in" state on my app. How should i do it?

Here's my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<TextView android:id="#+id/welcome_text"
android:text = "Log-In"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="40dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/username_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="20dip"
android:layout_marginTop="100dip"
android:layout_marginLeft="30dip"
android:text="Username:"
android:textColor="#000000"/>
<EditText
android:id="#+id/txt_username"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_toRightOf="#id/username_text"
android:layout_alignTop="#id/username_text"/>
<TextView
android:id="#+id/password_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/username_text"
android:layout_marginRight="20dip"
android:layout_marginTop="30dip"
android:layout_marginLeft="30dip"
android:text="Password:"
android:textColor="#000000"/>
<EditText
android:id="#+id/txt_password"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_toRightOf="#id/password_text"
android:layout_alignTop="#id/password_text"
android:layout_below="#id/txt_username"
android:layout_marginLeft="5dip"
android:password="true" />
<Button
android:id="#+id/login_button"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_below="#+id/txt_password"
android:layout_alignParentLeft="true"
android:layout_marginTop="35dip"
android:layout_marginLeft="110dip"
android:text="Submit" />
<TextView
android:id="#+id/tv_error"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:textSize="5pt"
android:layout_alignParentLeft="true"
android:layout_below="#id/txt_password"
android:layout_marginRight="9dip"
android:layout_marginTop="9dip"
android:layout_marginLeft="15dip"
android:textColor="#FF0000"
android:text=""/>
<TextView android:id="#+id/tv_login"
android:text = "#string/Login"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="40dp"
android:layout_below="#id/login_button"
android:layout_centerHorizontal="true"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_login"
android:orientation="horizontal"
android:layout_centerHorizontal="true">
<RadioButton android:id="#+id/on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dip"
android:text="On"
android:textColor="#000000"/>
<RadioButton android:id="#+id/off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dip"
android:layout_marginLeft="20dip"
android:text="Off"
android:textColor="#000000"/>
</RadioGroup>
</RelativeLayout>
and here's my java:
public class LogInActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginactivity);
final RadioButton radio_on = (RadioButton) findViewById(R.id.on);
final RadioButton radio_off = (RadioButton) findViewById(R.id.off);
Button launch = (Button)findViewById(R.id.login_button);
launch.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
EditText usernameEditText = (EditText) findViewById(R.id.txt_username);
EditText passwordEditText = (EditText) findViewById(R.id.txt_password);
TextView loginTextView = (TextView)findViewById(R.id.tv_error);
String sUserName = usernameEditText.getText().toString();
String sPassword = passwordEditText.getText().toString();
if(sUserName.equals("numlock") && sPassword.equals("numlock")) {
Intent intent = new Intent(LogInActivity.this, HomeActivity.class);
startActivity(intent);
}
else {
loginTextView.setText("Login failed. Username and/or password doesn't match.");
}
}
});
}
}
I want to assign the keep me logged in state on the radio buttons. Any help?
you should save the login credentials in sharedPreferences or Sqlite and on login activity check if you find any saved data then login with the saved data else ask user for credentials..
You can do it by using SharedPreference, Static variables or by Application class which will helps you in keeping the state of the User

Categories