Cannot find EditText from FragmentActivity - android

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);
}

Related

button to change layout inside fragment

So i have an app with tabbed activity and i want to change the layout of the first tab if the button(btlink) is clicked:
login(1st fragment/tab) code
public class login extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.login, container, false);
final EditText etUtilizador = (EditText) getView().findViewById(R.id.etUtilizador);
final EditText etPassword = (EditText) getView().findViewById(R.id.etPassword);
final Button btLogin = (Button) getView().findViewById(R.id.btLogin);
Button btlink = (Button) getView().findViewById(R.id.btlink);
btlink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent registerIntent;
registerIntent = new Intent(login.this, registar.class);
login.this.startActivity(registerIntent);
}
});
return rootView;
}
}
but it shows an error at:"registerIntent = new Intent(login.this, registar.class);"
login xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin">
</TextView>
<EditText
android:id="#+id/etUtilizador"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/section_label"
android:layout_marginTop="70dp"
android:ems="10"
android:hint="Utilizador"
android:inputType="textPersonName"
android:textColor="#color/colorPrimaryDark" />
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/etUtilizador"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textColor="#color/colorPrimaryDark" />
<Button
android:id="#+id/btLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/etPassword"
android:layout_marginTop="41dp"
android:background="#color/colorAccent"
android:text="Login"
android:textAppearance="#style/BotaoRegisto" />
<Button
android:id="#+id/btlink"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/btLogin"
android:layout_marginTop="49dp"
android:background="#color/colorAccent"
android:text="Registar"
android:textAppearance="#style/BotaoRegisto" />
<TextView
android:id="#+id/tvregisto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btLogin"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="TextView"
android:textColor="#color/colorPrimary"
tools:text="Se ainda nao tem conta registe-se aqui:" />
I only want the code to change the layout to this when i click on the button:
registar.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin">
</TextView>
<EditText
android:id="#+id/etNome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/section_label"
android:layout_marginTop="70dp"
android:ems="10"
android:hint="Nome"
android:inputType="textPersonName"
android:textColor="#color/colorPrimaryDark" />
<EditText
android:id="#+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/etNome"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress"
android:textColor="#color/colorPrimaryDark" />
<Button
android:id="#+id/btPedido"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/etNumero"
android:layout_marginTop="99dp"
android:background="#color/colorAccent"
android:elevation="0dp"
android:text="Enviar pedido de registo"
android:textAppearance="#style/BotaoRegisto" />
<EditText
android:id="#+id/etNumero"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/etEmail"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="Nº faturação ou série"
android:inputType="textPersonName"
android:textColor="#color/colorPrimaryDark" />
If someone could i would be thankfull.
At first set rootView instead of getView().
final EditText etUtilizador = (EditText) rootView.findViewById(R.id.etUtilizador);
final EditText etPassword = (EditText) rootView.findViewById(R.id.etPassword);
final Button btLogin = (Button) rootView.findViewById(R.id.btLogin);
Button btlink = (Button) rootView.findViewById(R.id.btlink);
And use getActivity().
Return the Activity this fragment is currently associated with.
new Intent(getActivity(), registar.class);
Code Will
Intent registerIntent;
registerIntent = new Intent(getActivity(), registar.class);
getActivity().startActivity(registerIntent);
change
Intent registerIntent;
registerIntent = new Intent(login.this, registar.class);
login.this.startActivity(registerIntent);
To
Intent registerIntent = new Intent(getActivity(), registar.class);
startActivity(registerIntent);
also need to change
final EditText etUtilizador = (EditText) rootView.findViewById(R.id.etUtilizador);
final EditText etPassword = (EditText) rootView.findViewById(R.id.etPassword);
final Button btLogin = (Button) rootView.findViewById(R.id.btLogin);
public class login extends Fragment{
Context context;
#Override
onAttach(Activity activity){
context=(Context)activity;}
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
registerIntent = new Intent(context, registar.class);

Dynamically added EditText is not visible when created

I have been reading about how to dynamically add an EditText field to my Linear Layout, every-time a user clicks a TextView (which has an onClick Listener attached).
I have had some mild success - I know that the EditText field is being created because when the button is clicked, all other elements move up as if something is being added to the screen.
My problem is that the EditText aren't visible and I haven't a clue why that is, so any help would be appreciated.
The the app isn't crashing so nothing to add in terms of the StackTrace and Log, as far as the app is concerned, everything is being created.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/facebookBlue"
android:orientation="vertical"
android:weightSum="1"
tools:context="com.test.practise.AddTeamMembers">
<android.support.design.widget.TextInputEditText
android:id="#+id/tv_teamNames"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/teamName"
android:textColor="#android:color/background_light"
android:textColorLink="#android:color/background_light"
android:textSize="30sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.26"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.47"
android:gravity="center"
android:text="Enter Player Names Below!"
android:textColor="#android:color/background_light"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name1"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name2"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name3"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/editTextGroupLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.07"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/tv_add_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="+ Add Name"
android:textColor="#android:color/background_light"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/facebookBlue"
android:gravity="center"
android:text="Ready to join!"
android:textColor="#android:color/background_light" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Below is the AddTeamMembers Class that calls the above XML
public class AddTeamMembers extends Fragment implements
View.OnClickListener
{
private SharedPreferences pref;
private TextView tv_teamNames, tv_add_name;
private LinearLayout mLayout;
//The below method must be overridden in order to implement a fragment -
this changes the lifecycle method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_add_team_members,
container, false);
initViews(view);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// 0, Sets Shared pref mode to private
pref = getActivity().getPreferences(0);
tv_teamNames.setText(pref.getString(Constants.Team_Name, ""));
}
private void initViews(View view) {
tv_teamNames = (TextView) view.findViewById(R.id.tv_teamNames);
tv_add_name = (TextView) view.findViewById(R.id.tv_add_name);
mLayout = (LinearLayout) view.findViewById(R.id.editTextGroupLayout);
tv_add_name.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_add_name:
createEditTextView();
break;
}
}
#TargetApi(Build.VERSION_CODES.M)
public void createEditTextView() {
try{
//dynamically create new EditText when user clicks to add another
name
//target user using API 22 and above (lollipop and above)
EditText editTextView = null;
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.M) {
editTextView = new EditText(getContext());
}else{
Toast.makeText(getActivity(), "App is not supported on this
device",
Toast.LENGTH_LONG).show();
}
editTextView.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
editTextView.setLayoutParams(params);
mLayout.addView(editTextView);
}catch(Exception e){
Log.d(TAG, "Failed to create new edit text");
}
}
}
[![Before Button is clicked][1]][1]
[![I have clicked 2 add 3 EditText here to make it obvious whats happening]
[2]][2]
[1]: https://i.stack.imgur.com/DGoPd.png
[2]: https://i.stack.imgur.com/3LWcY.png

I clicked on my clear button and it doesn't work

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");
}
});
}

EditText prints a letter twice

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.

Android : on changing EditText background color, EditText style getting changed

Hi When I change the Background color of EditText of two EditText they are looking like both merging.
My layout code goes like this `
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Text1"
android:singleLine="true" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Text2"
android:singleLine="true" >
</EditText>
<AutoCompleteTextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionNext"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Text3"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Text4"
android:singleLine="true" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
`
and My Activity code goes like this
public class MainActivity extends Activity {
EditText editText1, editText2, editText3, editText4;
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editText1.setBackgroundColor(getResources().getColor(android.R.color.primary_text_dark_nodisable));
editText3.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
}
});
}
private void init() {
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText4);
button = (Button) findViewById(R.id.button1);
}
}
Refer the attached Screen shots below
Layout before clicking Button
Layout after clicking Button
As you can see here using EditText.setBackgroundColor(any color) will color your outline too. To keep the outline aspect i would recommend to include the edittext in a table row and use margin. Try this in your xml and see the result:
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:orientation="vertical" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_orange_dark"
android:layout_margin="1dip"
android:text="cosmincalistru" />
</TableRow>
You have set the same id for both the edittext. #+id/editText2
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Text3"
android:singleLine="true" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Text4"
android:singleLine="true" />

Categories

Resources