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);
Related
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.
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
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.
I want the user to choose that whether they'd like to take picture or choose one from gallery.
I set up an OnClickListener on my Image but when I'm clicking the image, nothing is happening.
Here is my SettingUpUserProfile.java file's code:
public class SettingUpUserProfile extends AppCompatActivity {
public static final int TAKE_PHOTO_REQUEST = 0;
protected ImageView userProfilePicture;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting_up_user_profile);
userProfilePicture = (ImageView) findViewById(R.id.userProfilePicture);
userProfilePicture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(SettingUpUserProfile.this);
builder.setTitle(null);
builder.setItems(R.array.pickImage_options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int position) {
switch (position) {
case 0:
Intent intentCaptureFromCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intentCaptureFromCamera, TAKE_PHOTO_REQUEST);
break;
case 1:
// Choose from gallery.
}
}
});
}
});
}
}
and here is my activity_setting_up_user_profile.xml file's code:
<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="#color/light_purple"
tools:context="com.abc.xyz.SettingUpUserProfile">
<TextView
android:id="#+id/settingUpUserProfileText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="#string/settingUpUserProfileText1"
android:textColor="#color/white"
android:textStyle="bold"
android:textSize="30sp"
android:gravity="center_horizontal|center_vertical"/>
<ImageView
android:id="#+id/userProfilePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/settingUpUserProfileText1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="100dp"
android:clickable="true"
android:src="#drawable/ic_face_white_48dp" />
<TextView
android:id="#+id/settingUpUserProfileText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/userProfilePicture"
android:layout_marginTop="5dp"
android:text="#string/settingUpUserProfileText2"
android:textColor="#color/white"
android:textSize="15sp"
android:gravity="center_horizontal|center_vertical"/>
<EditText
android:id="#+id/userName"
android:background="#drawable/phone_number_edit_text_design"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/settingUpUserProfileText2"
android:layout_marginTop="80dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:gravity="center_horizontal|center_vertical"
android:hint="#string/hint_userName"
android:textColor="#color/white"
android:textColorHint="#E0E0E0"
android:textCursorDrawable="#null"
android:inputType="textPersonName"/>
<Button
android:id="#+id/buttonAllSet"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/userName"
android:layout_marginTop="20dp"
android:text="#string/button_allSet"
android:textStyle="bold"
android:textColor="#color/light_purple"
android:layout_marginEnd="120dp"
android:layout_marginStart="120dp"
android:gravity="center_horizontal|center_vertical"/>
</RelativeLayout>
I'm really unable to figure out what is wrong here.
Kindly, let me know.
I'm new to StackOverflow so please cooperate.
Thanks in advance.
Add Below lines to your onClick() method
AlertDialog alertDialog = builder.create();
alertDialog.show();
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