I can't seem to get findViewById() working with the EditText. It works fine with the button, but I think I am doing something wrong and I can't seem to find a solution anywhere. How does findViewById() work with EditText ?
Here is my class:
public class MainActivity extends ActionBarActivity {
Button sbutton;
EditText book, chapter, verse;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sbutton = (Button) findViewById(R.id.sbutton);
book = (EditText) findViewByID(R.id.book1);
chapter = (EditText) findViewByID(R.id.chapter1);
verse = (EditText) findViewByID(R.id.verse1);
}
}
And here is my XML:
<TextView android:text="Please enter your favorite scripture" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/book1"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/chapter1"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/verse1"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/sbutton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="77dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Book"
android:id="#+id/textView2"
android:layout_alignTop="#+id/book"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chapter"
android:id="#+id/textView3"
android:layout_below="#+id/book"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Verse"
android:id="#+id/textView4"
android:layout_below="#+id/chapter"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
I know this might not be the nicest code because I'm brand new to Android Studio. I would appreciate a little help understanding how this method works and what I might be doing wrong here. Thanks!
You are doing it as "findViewByID()" but its findViewById()
sbutton = (Button) findViewById(R.id.sbutton);
book = (EditText) findViewByID(R.id.book1);
chapter = (EditText) findViewByID(R.id.chapter1);
verse = (EditText) findViewByID(R.id.verse1);
Correct method name is
findViewById()
sbutton = (Button) findViewById(R.id.sbutton);
book = (EditText) findViewById(R.id.book1);
chapter = (EditText) findViewById(R.id.chapter1);
verse = (EditText) findViewById(R.id.verse1);
Related
While running this code error is coming in the logact section.I guess its minor error so its difficult for me to search the error.I tried various things but it didn't worked for me.I don't know where my code went wrong.
this is my NewUserActivity .java code
public class NewUserActivity extends AppCompatActivity {
EditText name;
EditText email;
EditText phone;
EditText usname;
EditText passsword;
Button register;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_user_login);
name = (EditText) findViewById(R.id.name);
email = (EditText) findViewById(R.id.email);
phone = (EditText) findViewById(R.id.phone);
usname = (EditText) findViewById(R.id.usname);
passsword = (EditText) findViewById(R.id.passsword);
register= (Button) findViewById(R.id.textView2);
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String NAME = name.getText().toString().trim();
String EMAIL = email.getText().toString().trim();
String PHONENO =phone.getText().toString().trim();
String username = usname.getText().toString().trim();
String password = passsword.getText().toString().trim();
if (NAME.isEmpty())
Toast.makeText(getApplicationContext(),"Please enter name",Toast.LENGTH_SHORT).show();
else if(EMAIL.isEmpty())
Toast.makeText(getApplicationContext(),"Please enter correct email address",Toast.LENGTH_SHORT).show();
else if(PHONENO.isEmpty())
Toast.makeText(getApplicationContext(),"Please enter correct phone number",Toast.LENGTH_SHORT).show();
else if(username.isEmpty())
Toast.makeText(getApplicationContext(),"Please enter Username",Toast.LENGTH_SHORT).show();
else if(username.length()<8|| username.length()>15)
Toast.makeText(getApplicationContext(),"Username must contain atleast 8 characters",Toast.LENGTH_SHORT).show();
else if(password.isEmpty())
Toast.makeText(getApplicationContext(),"Please enter Password",Toast.LENGTH_SHORT).show();
else if(password.length()<6|| password.length()>10)
Toast.makeText(getApplicationContext(),"Password must contain 6 characters",Toast.LENGTH_SHORT).show();
else {
}
}
});
}
}
this is my xnl file for the same.
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
tools:context=".NewUserActivity"
android:background="#drawable/ic_launcher_background"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/textView4"
android:layout_width="276dp"
android:layout_height="38dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="106dp"
android:layout_marginBottom="581dp"
android:text="REGISTRATION "
android:textColor="#color/WhiteSmoke"
android:textSize="30dp" />
<EditText
android:layout_height="26dp"
android:layout_width="297dp"
android:layout_marginBottom="487dp"
android:layout_marginEnd="65dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:id="#+id/name"/>
<TextView
android:id="#+id/textView5"
android:layout_width="218dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="135dp"
android:layout_marginBottom="530dp"
android:drawableLeft="#drawable/ic_action_name"
android:text="NAME"
android:textColor="#color/WhiteSmoke"
android:textSize="20dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="195dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="161dp"
android:layout_marginBottom="439dp"
android:drawableLeft="#drawable/ic_action_mail"
android:text="EMAIL ID"
android:textColor="#color/WhiteSmoke"
android:textSize="20dp" />
<EditText
android:layout_height="26dp"
android:layout_width="297dp"
android:layout_marginBottom="399dp"
android:layout_marginEnd="67dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:id="#+id/email"/>
<TextView
android:id="#+id/textView7"
android:layout_width="147dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="209dp"
android:layout_marginBottom="351dp"
android:drawableLeft="#drawable/ic_action_phone"
android:text="PHONE NO."
android:textColor="#color/WhiteSmoke"
android:textSize="20dp" />
<EditText
android:layout_width="297dp"
android:layout_height="26dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="63dp"
android:layout_marginBottom="306dp"
android:id="#+id/phone"/>
<TextView
android:layout_height="30dp"
android:layout_width="195dp"
android:textSize="20dp"
android:textColor="#color/WhiteSmoke"
android:text="USERNAME"
android:layout_marginBottom="256dp"
android:layout_marginEnd="160dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:id="#+id/textView8"
android:drawableLeft="#drawable/ic_action_username"/>
<EditText
android:layout_height="26dp"
android:layout_width="297dp"
android:layout_marginBottom="205dp"
android:layout_marginEnd="63dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:id="#+id/usname"/>
<TextView android:layout_height="30dp" android:layout_width="195dp"
android:textSize="20dp" android:textColor="#color/WhiteSmoke"
android:text="PASSWORD" android:layout_marginBottom="160dp"
android:layout_marginEnd="156dp" android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" android:id="#+id/textView9"
android:drawableLeft="#drawable/ic_action_password"/>
<EditText
android:layout_width="297dp"
android:layout_height="26dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="62dp"
android:layout_marginBottom="113dp"
android:id="#+id/passsword"/>
<androidx.cardview.widget.CardView
android:layout_height="56dp" android:layout_width="348dp"
android:layout_marginBottom="40dp" android:layout_marginEnd="20dp"
android:layout_alignParentBottom="true" android:layout_alignParentEnd="true"
app:cardElevation="10dp" app:cardCornerRadius="30dp"
app:cardBackgroundColor="#color/DeepPink" android:layout_marginRight="28dp"
android:layout_marginStart="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true">
<RelativeLayout android:layout_height="73dp" android:layout_width="373dp"
android:layout_marginBottom="100dp" android:layout_marginEnd="50dp">
<Button
android:id="#+id/register"
android:layout_width="132dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="129dp"
android:layout_marginBottom="17dp"
android:fontFamily="sans-serif"
android:freezesText="true"
android:text="REGISTER"
android:background="#android:color/transparent"
android:textColor="#color/Black"
android:textSize="24dp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
There is error in this section of my code.
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String NAME = name.getText().toString().trim();
String EMAIL = email.getText().toString().trim();
String PHONENO =phone.getText().toString().trim();
String username = usname.getText().toString().trim();
String password = passsword.getText().toString().trim();
You are using Wrong id of button which will led to the null pointer exception
Change this
register= (Button) findViewById(R.id.textView2);
to
register= (Button) findViewById(R.id.register);
To know about null pointer exception check this
What is a NullPointerException, and how do I fix it?
You can use View binding to replaces all findviewbyId
https://developer.android.com/topic/libraries/view-binding
name = (EditText) findViewById(R.id.name);
email = (EditText) findViewById(R.id.email);
phone = (EditText) findViewById(R.id.phone);
usname = (EditText) findViewById(R.id.usname);
passsword = (EditText) findViewById(R.id.passsword);
register= (Button) findViewById(R.id.textView2);
here check the register button id you add textView2 id here your mistake
so I have an android app with 2 Activities
1 - Main
2 - Second
Main activity has 1 single checkbox in a RelativeLayout I want to know how to use the checkbox to make a condition where if the check box is checked I want to make the ImageView = VISIBLE and the TextView = GONE in the Second activity and vice versa if its unchecked.
I have tried to use shared preferences and also intent but I keep messing up somewhere where either the checkbox wont matter or the screen just ends up showing blank. One other thing is that in Main activity I have a button that has onClick and already has an intent. I just want to know how to create the if condition with the checkbox for the Second activity. Sadly I would post my entire code but I have scrambled it up so bad trying to figure this out its all over the place and doesn't make sense anymore.
ok I tried to a few things, tried a posted answer but atleast I was able to clean up the code enough to share now but it messes up really badly and closes the app, sorry for any weird formatting, new to this
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="sam.plsnyc.com.plsign.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Company"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/editText"
android:textAlignment="center"
android:textIsSelectable="false" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signage"
android:id="#+id/textView2"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/editText2"
android:textAlignment="center"
android:textIsSelectable="false" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignLeft="#+id/editText"
android:layout_alignStart="#+id/editText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Make Sign"
android:id="#+id/button"
android:layout_centerHorizontal="true"
android:layout_below="#+id/editText2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgView"
android:focusable="true"
android:src="#drawable/logo"
android:scaleType="fitXY"
android:visibility="visible"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Use Logo"
android:id="#+id/logobox"
android:layout_below="#+id/textView2"
android:layout_alignParentStart="true" />
</RelativeLayout>
activity_display_message.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="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0dp"
android:singleLine="false"
android:id="#+id/activity_display_message"
tools:context=".MainActivity"
android:focusableInTouchMode="false"
android:focusable="true"
android:nestedScrollingEnabled="false"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/logo"
android:visibility="gone"
android:id="#+id/iV2" />
<me.grantland.widget.AutofitLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="1"
android:textStyle="bold"
android:textSize="275sp"
android:layout_weight=".5"
android:textAlignment="center"
android:text="LOGO"
android:visibility="invisible"
android:id="#+id/restxttop"
android:elegantTextHeight="false" />
</me.grantland.widget.AutofitLayout>
<me.grantland.widget.AutofitLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:textAlignment="center"
android:maxLines="1"
android:textSize="275sp"
android:text="SIGNAGE"
android:id="#+id/restxtbot"
/>
</me.grantland.widget.AutofitLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
public final static String KEY_CHECKED = "chkd";
CheckBox lbox;
Button button;
EditText etxt;
EditText etxt2;
String toptxt;
String bottxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);{
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
etxt = (EditText) findViewById(R.id.editText);
toptxt = etxt.getText().toString();
etxt2 = (EditText) findViewById(R.id.editText2);
bottxt = etxt2.getText().toString();
Intent i = new Intent(v.getContext(), DisplayMessageActivity.class);
i.putExtra("text1", toptxt);
i.putExtra("text2", bottxt);
startActivity(i);
lbox = (CheckBox) findViewById(R.id.logobox);
Intent i2 = new Intent (MainActivity.this, DisplayMessageActivity.class);
i2.putExtra(KEY_CHECKED, lbox.isChecked());
startActivity(i2);
}
});
}
}
}
DisplayMessageActivity.java
public class DisplayMessageActivity extends Activity {
TextView ttxt;
TextView btxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
btxt = (TextView) findViewById(R.id.restxtbot);
btxt.setText(getIntent().getStringExtra("text2"));
AutofitHelper.create(btxt);
ImageView image = (ImageView) findViewById(R.id.iV2);
Boolean chk = getIntent().getBooleanExtra(MainActivity.KEY_CHECKED, false);
if (chk)
{
ttxt.setVisibility(View.GONE);
image.setVisibility(View.VISIBLE);
}else{
ttxt.setVisibility(View.VISIBLE);
ttxt = (TextView)findViewById(R.id.restxttop);
ttxt.setText(getIntent().getStringExtra("text1"));
AutofitHelper.create(ttxt);
ttxt.setPaintFlags(ttxt.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
}
}
}
If you open the second activity from the main you can put the isChecked value in the intent else in this simple case make the value static
THANK YOU FOR ALL OF YOUR HELP!! I NEED TO READ A TUTORIAL. :)
I know it's probably elementary and as you can tell I'm a newbie. I compared my code to others and don't see anything wrong. Thanks! When I click the clear Button after entering data, my edit fields are not cleared.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button clear = (Button)findViewById(R.id.btn_clear);
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
EditText potsize = (EditText) findViewById(R.id.et_pot_size);
EditText tocall = (EditText) findViewById(R.id.et_to_call);
EditText bepercent = (EditText) findViewById(R.id.et_be_per);
potsize.setText("");
tocall.setText("");
bepercent.setText("");
TextView potoddscalc = (TextView)findViewById(R.id.tv_pot_odds_calc);
potoddscalc.setText("5");
}
});
}
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: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" >
<requestFocus />
<TextView
android:id="#+id/tv_be_per"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_to_call"
android:layout_below="#+id/tv_to_call"
android:layout_marginTop="34dp"
android:text="#string/BE_Per"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_to_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_pot_size"
android:layout_below="#+id/tv_pot_size"
android:layout_marginTop="33dp"
android:text="#string/to_call"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_pot_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="#string/pot_size"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_pot_odds_calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/et_be_per"
android:layout_alignTop="#+id/tv_pot_odds"
android:ems="8" />
<EditText
android:id="#+id/et_pot_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/tv_pot_size"
android:layout_marginLeft="18dp"
android:layout_toRightOf="#+id/tv_pot_odds"
android:ems="10" />
<EditText
android:id="#+id/et_to_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/et_pot_size"
android:layout_alignTop="#+id/tv_to_call"
android:ems="10" />
<EditText
android:id="#+id/et_be_per"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/et_to_call"
android:layout_alignTop="#+id/tv_be_per"
android:ems="10" />
<TextView
android:id="#+id/tv_pot_odds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_be_per"
android:layout_below="#+id/et_be_per"
android:layout_marginTop="20dp"
android:text="#string/pot_odds"
android:textStyle="bold" />
<Button
android:id="#+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/tv_pot_odds_calc"
android:layout_below="#+id/tv_pot_odds_calc"
android:layout_marginTop="24dp"
android:text="#string/clear_button" />
<Button
android:id="#+id/btn_calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn_clear"
android:layout_alignBottom="#+id/btn_clear"
android:layout_alignLeft="#+id/tv_pot_odds_calc"
android:text="#string/calc_button" />
Try this instead. I believe your problem may be arising from creating new EditTexts and TextView objects every time you click the clear button. In the below code, the EditTexts and TextView are declared only once ,in the OnCreate method. Also, getText().clear() is the "official" way of clearing the text from an EditText (not that it really makes a visual difference).
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button clear = (Button)findViewById(R.id.btn_clear);
EditText potsize = (EditText) findViewById(R.id.et_pot_size);
EditText tocall = (EditText) findViewById(R.id.et_to_call);
EditText bepercent = (EditText) findViewById(R.id.et_be_per);
TextView potoddscalc = (TextView)findViewById(R.id.tv_pot_odds_calc);
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
potsize.getText().clear();
tocall.getText().clear();
bepercent.getText().clear();
potoddscalc.setText("5");
}
});
}
// try this way
<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="wrap_content"
android:padding="5dp"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/tv_pot_size"
android:layout_width="0dp"
android:layout_weight="0.20"
android:gravity="right"
android:layout_height="wrap_content"
android:text="pot_size"
android:textStyle="bold" />
<EditText
android:id="#+id/et_pot_size"
android:layout_width="0dp"
android:layout_weight="0.80"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/tv_to_call"
android:layout_width="0dp"
android:layout_weight="0.20"
android:gravity="right"
android:layout_height="wrap_content"
android:text="to_call"
android:textStyle="bold" />
<EditText
android:id="#+id/et_to_call"
android:layout_width="0dp"
android:layout_weight="0.80"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/tv_be_per"
android:layout_width="0dp"
android:layout_weight="0.20"
android:gravity="right"
android:layout_height="wrap_content"
android:text="BE_Per"
android:textStyle="bold" />
<EditText
android:id="#+id/et_be_per"
android:layout_width="0dp"
android:layout_weight="0.80"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/tv_pot_odds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pot_odds"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_pot_odds_calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:ems="8" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center">
<Button
android:id="#+id/btn_calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="calc_button" />
<Button
android:id="#+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="clear_button" />
</LinearLayout>
</LinearLayout>
public class MyActivity extends Activity {
private EditText potsize;
private EditText tocall;
private EditText bepercent;
private Button clear;
private Button calc;
private TextView potoddscalc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clear = (Button)findViewById(R.id.btn_clear);
calc = (Button)findViewById(R.id.btn_calc);
potsize = (EditText) findViewById(R.id.et_pot_size);
tocall = (EditText) findViewById(R.id.et_to_call);
bepercent = (EditText) findViewById(R.id.et_be_per);
potoddscalc = (TextView)findViewById(R.id.tv_pot_odds_calc);
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
clearOrReset();
}
});
}
private void clearOrReset(){
potsize.setText("");
tocall.setText("");
bepercent.setText("");
potoddscalc.setText("5");
}
}
declare all three objects of Edittext in the oncreate method and then try it
enter code here #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button clear = (Button)findViewById(R.id.btn_clear);
EditText potsize = (EditText) findViewById(R.id.et_pot_size);
EditText tocall = (EditText) findViewById(R.id.et_to_call);
EditText bepercent = (EditText) findViewById(R.id.et_be_per);
TextView potoddscalc = (TextView)findViewById(R.id.tv_pot_odds_calc);
public void onClick(View v)
{
swiech(v.getid())
{
case R.id.btn_clear:
potsize.setText("");
tocall.setText("");
bepercent.setText("");
potoddscalc.setText("5");
}
});
}
I am trying to save all the information entered from all EditTexts in each of my 3 Fragments. I am trying to get these information from an option button in my ActionBar.
I noticed that it cannot find the views when I am on a different tab.
My first fragment/ first tab:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="gen_info">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Admin Account"
android:id="#+id/isAdmin" />
<EditText
android:id="#+id/code"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="5dip"
android:singleLine="true"
android:hint="#string/code"
android:ems="10"
android:width="150dip" />
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:singleLine="true"
android:hint="#string/fullname" />
<EditText
android:id="#+id/address"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="#string/address"
android:layout_marginTop="5dip" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:inputType="date"
android:ems="10"
android:id="#+id/birthdate"
android:hint="Birthdate (mm/dd/yyy)" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:inputType="number"
android:ems="10"
android:id="#+id/mobile"
android:hint="Mobile No" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:layout_marginTop="5dip"
android:ems="10"
android:id="#+id/workPhone"
android:hint="Work Phone" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/email"
android:hint="Email" />
<Spinner
android:id="#+id/gender_options"
android:layout_marginTop="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Spinner
android:id="#+id/civil_status"
android:layout_marginTop="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_marginTop="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="10"
android:id="#+id/hired_date"
android:hint="Hired Date (mm/dd/yyyy)" />
<EditText
android:id="#+id/location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:singleLine="true"
android:hint="Location" />
<Spinner
android:id="#+id/position"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Spinner
android:id="#+id/department"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
My 2nd Fragment / tab
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:tag="contacts">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/contactName"
android:hint="Name"
android:paddingTop="5dip" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/contactRelationship"
android:hint="Relationship"
android:paddingTop="5dip" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:ems="10"
android:id="#+id/contactAddress"
android:hint="Address"
android:paddingTop="5dip" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/contactPhoneNo"
android:hint="Phone Number"
android:paddingTop="5dip" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/contactPhoneTypeSpinner"
android:layout_toEndOf="#+id/contactPhoneNo"
android:layout_toRightOf="#+id/contactPhoneNo"
android:paddingTop="5dip" />
</RelativeLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="107dp"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/contactSpecialNotes"
android:paddingTop="5dip"
android:hint="Write special notes here" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add to list"
android:id="#+id/addContact" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Contacts List"
android:id="#+id/contactsHeader"
android:editable="false" />
<TableLayout
android:id="#+id/contactsTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:paddingTop="5dip"></TableLayout>
</LinearLayout>
My 3rdfragment / tab:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="latest_wage">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="10"
android:id="#+id/latestWageDate"
android:hint="Date" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/latestWageNote"
android:hint="Note" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/latestWageRate"
android:hint="Rate (PHP)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Rate"
android:id="#+id/btnAddRate" />
<TableLayout
android:id="#+id/latestWageTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="5dip"></TableLayout>
</LinearLayout>
I declared my first Fragment's elements here:
public static class LaunchpadSectionFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false);
Spinner spinner = (Spinner) rootView.findViewById(R.id.gender_options);
Spinner civilStatusSpinner = (Spinner) rootView.findViewById(R.id.civil_status);
Spinner positionSpinner = (Spinner) rootView.findViewById(R.id.position);
Spinner departmentSpinner = (Spinner) rootView.findViewById(R.id.department);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
Here's the function for my ActionBar:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
case R.id.action_save:
EditText code = (EditText) findViewById(R.id.code);
EditText name = (EditText) findViewById(R.id.name);
EditText address = (EditText) findViewById(R.id.address);
EditText birthDate = (EditText) findViewById(R.id.birthdate);
EditText mobile = (EditText) findViewById(R.id.mobile);
EditText workPhone = (EditText) findViewById(R.id.workPhone);
EditText email = (EditText) findViewById(R.id.email);
Spinner gender = (Spinner) findViewById(R.id.gender_options);
Spinner civilStatus = (Spinner) findViewById(R.id.civil_status);
EditText hiredDate = (EditText) findViewById(R.id.hired_date);
EditText location = (EditText) findViewById(R.id.location);
Spinner departmentId = (Spinner) findViewById(R.id.department);
CheckBox isAdmin = (CheckBox) findViewById(R.id.isAdmin);
return true;
case R.id.action_discard:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
All the Views would return null, these views are found on the first fragment. When I did not make the lat two fragments, I could find the views.
Any help would be greatly appreciated. Thanks!
On approach could be to handle your itemMenuClick in our Fragment itself. So you can handle them in your Fragment and get the Information you need: TUTORIAL
The other approach is to keep a Instance of all of your Fragments in your MainActivity globally and ask your Fragment for the values eg:
MainActivity:
Fragment1 _fragment1;
onCreate(..){
_fragment1 = new Fragment1();
}
onMenuItemSelected(....){
String test = _fragment1.getCustomText();
}
Fragment1:
EditText _test;
onCreate(...){
_test = _root.findViewById(R.id.et_test);
}
public String getCustomText(){
return _test.getText().toString();
}
Try given code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
case R.id.action_save:
EditText code = (EditText) getView().findViewById(R.id.code);
EditText name = (EditText) getView().findViewById(R.id.name);
EditText address = (EditText) getView().findViewById(R.id.address);
EditText birthDate = (EditText) getView().findViewById(R.id.birthdate);
EditText mobile = (EditText) getView().findViewById(R.id.mobile);
EditText workPhone = (EditText) getView().findViewById(R.id.workPhone);
EditText email = (EditText) getView().findViewById(R.id.email);
Spinner gender = (Spinner) getView().findViewById(R.id.gender_options);
Spinner civilStatus = (Spinner) getView().findViewById(R.id.civil_status);
EditText hiredDate = (EditText) getView().findViewById(R.id.hired_date);
EditText location = (EditText) getView().getView().findViewById(R.id.location);
Spinner departmentId = (Spinner) getView().findViewById(R.id.department);
CheckBox isAdmin = (CheckBox) getView().findViewById(R.id.isAdmin);
return true;
case R.id.action_discard:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I know this is kinda silly and weird question, but its really bugging me. As my post subject says, when I enter a letter in the edit text its printing twice. I tried to find the issue, but couldn't. Here is my code,
changecode.xml
<EditText
android:id="#+id/oldcode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/changecodetxt"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:hint="Enter Old Pass Code"
android:inputType="number"
android:password="true" />
<EditText
android:id="#+id/newcode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/oldcode"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:hint="Enter New Pass code"
android:inputType="number"
android:password="true" />
<EditText
android:id="#+id/renewcode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/newcode"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="25dp"
android:hint="Re-Enter New Pass code"
android:inputType="number"
android:password="true" />
<Button
android:id="#+id/savenewcode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#drawable/button_style"
android:text="Update Code" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/savenewcode"
android:layout_below="#id/renewcode"
android:gravity="center" >
<ToggleButton
android:id="#+id/cc_togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textOff="123"
android:textOn="ABC!##" />
</LinearLayout>
In MainActivity.java
oldcode = (EditText) findViewById(R.id.oldcode);
newcode = (EditText) findViewById(R.id.newcode);
renewcode = (EditText) findViewById(R.id.renewcode);
savenewcode = (Button) findViewById(R.id.savenewcode);
cc_toggle = (ToggleButton) findViewById(R.id.cc_togglebutton);
cc_toggle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
boolean on = cc_toggle.isChecked();
if (on) {
oldcode.setInputType(InputType.TYPE_CLASS_TEXT);
newcode.setInputType(InputType.TYPE_CLASS_TEXT);
renewcode.setInputType(InputType.TYPE_CLASS_TEXT);
}
else {
oldcode.setInputType(InputType.TYPE_CLASS_NUMBER);
newcode.setInputType(InputType.TYPE_CLASS_NUMBER);
renewcode.setInputType(InputType.TYPE_CLASS_NUMBER);
}
}
});
Any kind of help or suggestion is much appreciated.