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);
Related
I have button on my activity that show up on the fragment and only the button that show up , i did search about it and find out its a rendering issue but the answers didn't worked for me, here how it looks.
Fragment
Activity
is there any chance that the issue code be from the margin of the button
Fragment.java
public class Smsar extends Fragment {
public Smsar() {
// Required empty public constructor
}
View root;
Button mSmsar,mFinder;
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
root= inflater.inflate(R.layout.fragment_smsar, container, false);
mSmsar=(Button)root.findViewById(R.id.smsar);
mFinder=(Button)root.findViewById(R.id.finder);
mSmsar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().onBackPressed();
}
});
mFinder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Open Discover.
}
});
return root;
}
}
Fragment XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/main_background_difference"
tools:context=".Smsar">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="#+id/smsar"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="59dp"
android:background="#drawable/btn_rounded"
android:fontFamily="#font/cabin"
android:text="Smsar ?"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textSize="18sp"
android:typeface="normal" />
<Button
android:id="#+id/finder"
android:layout_width="204dp"
android:layout_height="63dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="138dp"
android:background="#drawable/btn_rounded"
android:fontFamily="#font/cabin"
android:text="Want Apartment ?"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
</RelativeLayout>
Activity XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical"
tools:context=".MainActivity"
android:id="#+id/mainView"
>
<ImageView
android:id="#+id/logo"
android:layout_width="162dp"
android:layout_height="196dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:adjustViewBounds="true"
android:maxHeight="64dp"
android:maxWidth="64dp"
app:srcCompat="#drawable/logo" />
<Button
android:id="#+id/logIn"
android:layout_width="218dp"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/userName"
android:layout_alignParentBottom="true"
android:layout_marginEnd="31dp"
android:layout_marginBottom="113dp"
android:background="#color/buttons"
android:clickable="true"
android:fontFamily="#font/catamaran"
android:onClick="logIN"
android:text="Login"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textSize="22sp" />
<EditText
android:id="#+id/userName"
android:layout_width="277dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ems="10"
android:fontFamily="#font/catamaran"
android:hint="UserName"
android:inputType="textPersonName"
android:text="" />
<EditText
android:id="#+id/password"
android:layout_width="277dp"
android:layout_height="wrap_content"
android:layout_below="#+id/userName"
android:layout_centerHorizontal="true"
android:fontFamily="#font/catamaran"
android:hint="Password"
android:inputType="textPassword" />
<TextView
android:id="#+id/_new"
android:layout_width="159dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-35dp"
android:layout_marginBottom="0dp"
android:layout_toStartOf="#+id/sginUp"
android:fontFamily="#font/catamaran"
android:text="New ? Start Now By"
android:textAlignment="center"
android:textSize="18sp" />
<TextView
android:id="#+id/sginUp"
android:layout_width="81dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="17dp"
android:layout_marginBottom="0dp"
android:layout_centerHorizontal="true"
android:layout_toEndOf="#id/mError"
android:clickable="true"
android:fontFamily="#font/catamaran"
android:text="SginUp"
android:textAlignment="center"
android:textColor="#color/links"
android:textSize="18sp" />
<TextView
android:id="#+id/mError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="82dp"
android:textColor="#android:color/holo_red_dark"
android:visibility="invisible"
android:text="mError" />
</RelativeLayout>
Activity.Java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_login =(Button)findViewById(R.id.logIn);
pref = getSharedPreferences("user_details", MODE_PRIVATE);
if(pref.contains("username")&&pref.contains("password"))
success();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.mainView, mFrag);
ft.commit();
imageView=(ImageView)findViewById(R.id.logo);
mDBHelper =new DBHelper(this);
mError=(TextView)findViewById(R.id.mError);
//Define the variables
userNameEdit=(EditText)findViewById(R.id.userName);
passwordEdit=(EditText)findViewById(R.id.password);
_signUp=(TextView)findViewById(R.id.sginUp);
//End
_signUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
intent=new Intent(MainActivity.this,Signup.class);
startActivity(intent);
}
});
}
I solve it by surround the button with LinearLayout
<LinearLayout
android:id="#+id/buttonContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/password"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>
<Button
android:id="#+id/logIn"
android:layout_width="186dp"
android:layout_height="wrap_content"
android:layout_marginVertical="12dp"
android:background="#drawable/btn_rounded"
android:minWidth="200dp"
android:onClick="logIN"
android:text="#string/title_login"
android:textAllCaps="false"
android:textColor="#color/ms_white"
android:textSize="18sp" />
</LinearLayout>
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
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 have this DialogFragment. When I show it, it becomes fullscreen, I mean it overlaps the current screen. How to make it small like an AlertDialog?
I guessed it's because of the RelativeLayout so I wrapped the LinearLayout outside of RelativeLayout but nothing changes.
public class LoginDialogFragment extends DialogFragment {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme);
}
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.login_dialog, container, false);
final EditText emailEditText = (EditText)v.findViewById(R.id.emailEditText);
final EditText passwordEditText = (EditText)v.findViewById(R.id.passwordEditText);
final Button loginButton = (Button)v.findViewById(R.id.loginButton);
loginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
checkLogin(emailEditText.getText().toString(), passwordEditText.getText().toString());
}
});
return v;
}
}
Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/loginTitleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/loginDialogTitle"
android:textColor="#color/White"
android:textSize="20sp" />
<EditText
android:id="#+id/emailEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/loginTitleTextView"
android:layout_weight="1"
android:ems="10"
android:inputType="textEmailAddress" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/passwordEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/emailEditText"
android:layout_weight="1"
android:ems="10"
android:inputType="textPassword" />
<TextView
android:id="#+id/passwordTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/passwordEditText"
android:layout_alignBottom="#+id/passwordEditText"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:text="#string/password"
android:textColor="#color/White"
android:textSize="16sp" />
<TextView
android:id="#+id/emailTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/emailEditText"
android:layout_alignBottom="#+id/emailEditText"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:text="#string/email"
android:textColor="#color/White"
android:textSize="16sp" />
<Button
android:id="#+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/passwordEditText"
android:text="#string/loginButtonText" />
</RelativeLayout>
</LinearLayout>
Call setStyle(STYLE_NORMAL, android.R.style.Theme_Dialog); instead of setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme); in onCreate(). Did it help?