I've been trying to create a calculator app which uses Navigation Drawer. I've 6 fragments and each fragments have EditTexts for getting inputs from user.
But when I ran the app and type some values inside EditText (any fragments doesn't matter) and change the fragment and return to previous fragment where I entered a value before, the entered value is gone and restores default value.
How can I manage this problem?
MainActivity.java
package com.example.testnavigation;
import android.content.Intent;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import android.view.MenuItem;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.core.view.GravityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
public class MainActivity extends AppCompatActivity implements MyListener{
private AppBarConfiguration mAppBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_gubreler, R.id.nav_sonuc, R.id.nav_hedef, R.id.nav_oranlar, R.id.nav_tankbilgileri, R.id.nav_cevirme)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
TankBilgileriFragment
package com.example.testnavigation;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class TankBilgileriFragment extends Fragment {
EditText tet1, tet2;
double tet1v, tet2v;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_tankbilgileri, container, false);
tet1=(EditText) v.findViewById(R.id.tet1); tet1v=Double.parseDouble(tet1.getText().toString()); tet2=(EditText) v.findViewById(R.id.tet2); double tet2v=Double.parseDouble(tet2.getText().toString());
return v;
}
}
GubrelerFragment
package com.example.testnavigation;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import java.text.DecimalFormat;
public class GubrelerFragment extends Fragment{
Button gbtnSifirla, gbtnHesapla, gbtnKaydet;
EditText get1, get2, get3, get4, get5, get6, get7, get8, get9, get10, get11, get12, get13, get14, get15, get16;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_gubreler, container, false);
gbtnHesapla=(Button) v.findViewById(R.id.gbtnHesapla); gbtnSifirla=(Button) v.findViewById(R.id.gbtnSifirla); gbtnKaydet=(Button) v.findViewById(R.id.gbtnKaydet);
get1=(EditText) v.findViewById(R.id.get1); get2=(EditText) v.findViewById(R.id.get2); get3=(EditText) v.findViewById(R.id.get3); get4=(EditText) v.findViewById(R.id.get4); get5=(EditText) v.findViewById(R.id.get5);
get6=(EditText) v.findViewById(R.id.get6); get7=(EditText) v.findViewById(R.id.get7); get8=(EditText) v.findViewById(R.id.get8); get9=(EditText) v.findViewById(R.id.get9); get10=(EditText) v.findViewById(R.id.get10);
get11=(EditText) v.findViewById(R.id.get11); get12=(EditText) v.findViewById(R.id.get12); get13=(EditText) v.findViewById(R.id.get13); get14=(EditText) v.findViewById(R.id.get14); get15=(EditText) v.findViewById(R.id.get15);
get16=(EditText) v.findViewById(R.id.get16);
return v;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState!=null){
get1.setText(savedInstanceState.getString("key1"));}
}
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("key1",get1.getText().toString());
}
}
fragment_gubreler.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:columnCount="3"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/kalnit"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="L"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="L"
android:textSize="20sp"></TextView>:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potnit"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="kg"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="kg"
android:textSize="20sp"></TextView>:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="string/potsul"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="kg"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="kg"
android:textSize="20sp"></TextView>:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potklr"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="kg"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="kg"
android:textSize="20sp"></TextView>:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mkp"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="kg"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="kg"
android:textSize="20sp"></TextView>:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/magnit"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="kg"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="kg"
android:textSize="20sp"></TextView>:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/magsul"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="kg"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="kg"
android:textSize="20sp"></TextView>:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fasit"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="L"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="kg"
android:textSize="20sp"></TextView>:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Peters Professional 10-52-10:"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="kg"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="kg"
android:textSize="20sp"></TextView>:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Peters Professional 30-10-10:"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="kg"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="kg"
android:textSize="20sp"></TextView>:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fe"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="kg"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="kg"
android:textSize="20sp"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bor"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="g"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="g"
android:textSize="20sp"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mn"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="g"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="g"
android:textSize="20sp"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/zn"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="g"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="g"
android:textSize="20sp"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cu"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="g"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="g"
android:textSize="20sp"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mo"
android:textSize="20sp"></TextView>
<EditText
android:id="#+id/get16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60dp"
android:layout_marginLeft="10dp"
android:hint="g"
android:inputType="numberDecimal"
android:gravity="center"
android:textSize="20sp"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="g"
android:textSize="20sp"></TextView> </GridLayout> </LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/gbtnSifirla"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sfr"></Button>
<Button
android:id="#+id/gbtnHesapla"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hesap"></Button>
<Button
android:id="#+id/gbtnKaydet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/kaydet"></Button> </LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
You need to learn about "saveInstance"
#Override public void onSaveInstanceState(Bundle outState)
{ super.onSaveInstanceState(outState);
outState.putString("dataGotFromServer", dataGotFromServer);
}
#Override public void onActivityCreated(Bundle savedInstanceState)
{ super.onActivityCreated(savedInstanceState);
dataGotFromServer = savedInstanceState.getString("dataGotFromServer");
}
I managed my problem with SharedPreferences. I added a button in my Fragment and onClick method to save values with SharedPreferences and restored values inside onCreateView (before return v;) so even I close the app or change any other fragment values still remain.
Related
I have an activity and when I click on the "Register" TextView a fragment will open and in that fragment all the Registration detail is there.But when I click on the textview the Fragment is loaded successfully but it did'nt cover up the entire screen.
Here is my code:
MainActivity.java
Register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
Fragments fragments=new Fragments();
fragmentTransaction.replace(R.id.Fragment_Register,fragments);
fragmentTransaction.commit();
fragmentTransaction.addToBackStack(null);
}
});
}
MainActivity.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"
tools:context="com.example.user.attendance.MainActivity"
android:background="#drawable/blurbackgound"
android:padding="10dp"
android:focusableInTouchMode="true"
>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="WELCOME"
android:textColor="#color/white"
android:textStyle="bold"
android:fontFamily="monospace"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"/>
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:id="#+id/login_mobile_number"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:background="#drawable/edit_text_border"
android:padding="15dp"
android:textColor="#color/white"
android:inputType="number"
android:hint="Enter mobile number"
android:gravity="center"
android:maxLength="10"
android:textColorHint="#color/hint_color"
/>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/ic_local_phone_black_24dp"
android:layout_below="#id/title"
android:layout_marginLeft="30dp"
android:layout_marginTop="73dp"
android:id="#+id/phonelogo"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mb_counter"
android:layout_below="#id/title"
android:layout_marginLeft="300dp"
android:layout_alignBaseline="#id/login_mobile_number"
android:text="abssh"
android:textColor="#color/white"
/>
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/login_mobile_number"
android:layout_marginTop="25dp"
android:background="#drawable/edit_text_border"
android:paddingRight="50dp"
android:hint="Password"
android:gravity="center"
android:textColorHint="#color/hint_color"
android:inputType="textPassword"
android:textColor="#color/white"
android:id="#+id/login_password"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="1dp"
android:layout_below="#id/login_password"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#color/white"
android:text="Student"
android:textColor="#color/white"
android:id="#+id/Radio_button_student"
android:layout_marginLeft="19dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:buttonTint="#color/white"
android:text="Teacher"
android:textColor="#color/white"
android:id="#+id/Radio_button_teacher"/>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHOW"
android:textColor="#color/white"
android:textStyle="bold"
android:layout_alignRight="#id/login_password"
android:layout_alignBaseline="#id/login_password"
android:id="#+id/password_visibility"
android:layout_marginRight="10dp"
/>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/ic_lock_outline_black_50dp"
android:layout_marginLeft="30dp"
android:layout_below="#id/phonelogo"
android:layout_marginTop="39dp"
/>
<Button
android:stateListAnimator="#null"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="Login"
android:id="#+id/login"
android:background="#drawable/login_buttom"
android:textColorHint="#color/hint_color"
android:layout_below="#id/login_password"
android:layout_marginTop="50dp"
android:layout_marginLeft="35dp"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FORGOT PASSWORD"
android:textColor="#color/white"
android:layout_below="#id/login"
android:layout_marginTop="18dp"
android:layout_marginLeft="48dp"
android:id="#+id/forgot_password"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:textColor="#color/white"
android:layout_alignBaseline="#id/forgot_password"
android:layout_marginLeft="170dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REGISTER"
android:textColor="#color/white"
android:layout_below="#id/login"
android:layout_marginLeft="185dp"
android:layout_marginTop="20dp"
android:id="#+id/Register"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Fragment_Register"></RelativeLayout>
</RelativeLayout>
Fragments.java
package com.example.user.attendance;
import android.app.FragmentManager;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import de.hdodenhof.circleimageview.CircleImageView;
public class Fragments extends Fragment {
Button Register_Cancel_fullname, Register_Cancel_email, Register_Cancel_phone,
Register_Cancel_password, Sign_up,Register_Cancel_Confirm_Password;
String Passcode,Confirm_Passcode;
EditText Full_Name, Email_Address, Phone_number, Password_register,Confirm_Password_register;
CircleImageView Register_add_photo;
LinearLayout select_photo_from_camera,select_photo_from_gallery;
TextView textView,hint_textview_circular_image_view;
// RadioButton Category_teacher,Category_student;
RadioGroup Teacher_student;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myfragmentVieew = inflater.inflate(R.layout.fragment_signup, container, false);
//Edit Text
Full_Name = (EditText) myfragmentVieew.findViewById(R.id.Register_name_and_surname);
Email_Address = (EditText) myfragmentVieew.findViewById(R.id.Register_Email_address);
Phone_number = (EditText) myfragmentVieew.findViewById(R.id.Register_Phone);
Password_register = (EditText) myfragmentVieew.findViewById(R.id.Register_Password);
Confirm_Password_register=(EditText)myfragmentVieew.findViewById(R.id
.Confirm_Password_Register);
//Button
Sign_up = (Button) myfragmentVieew.findViewById(R.id.Register_Signup);
Register_Cancel_fullname = (Button) myfragmentVieew.findViewById(R.id.Register_First_Cancel_Button);
Register_Cancel_email = (Button) myfragmentVieew.findViewById(R.id.Register_Second_Cancel_Button);
Register_Cancel_phone = (Button) myfragmentVieew.findViewById(R.id.Register_Third_Cancel_Button);
Register_Cancel_password = (Button) myfragmentVieew.findViewById(R.id.Register_fourth_Cancel_Button);
Register_Cancel_Confirm_Password=(Button)myfragmentVieew.findViewById(R.id
.Register_Cancel_Confirm_Password_Button);
Register_add_photo=(CircleImageView)myfragmentVieew.findViewById(R.id.Register_add_photo);
//TextView
textView = (TextView) myfragmentVieew.findViewById(R.id.Register_textview);
hint_textview_circular_image_view=(TextView)myfragmentVieew.findViewById(R.id
.hint_of_image_view);
return myfragmentVieew;
}
}
Here is what I am getting:
Remove this line from root layout.
android:padding="10dp"
This will solve the problem.
HI I'm making GymExercises App and every Exercise is written in different Fragment
so i want to add Video to show how exercise to do,it compiles but doesn't show anything can someone help me ?
PS i don't need play,stop button and anything other just to show the video
Here is the code that i add
Example:
BenchFragment.java
package com.Hristijan.Aleksandar.GymAssistant.Exercises;
import android.media.session.MediaController;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.VideoView;
import java.net.URL;
/**
* A simple {#link Fragment} subclass.
*/
public class BenchFragment extends Fragment {
public BenchFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_bench, container, false);
VideoView view = (VideoView)rootView.findViewById(R.id.video_view);
String path = "android.resource://" + getActivity().getPackageName() + "/" + R.raw.bench1;
view.setVideoURI(Uri.parse(path));
view.start();
return inflater.inflate(R.layout.fragment_bench, container, false);
}
}
fragment_bench.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:orientation="vertical"
android:background="#000000"
tools:context="com.Hristijan.Aleksandar.GymAssistant.Exercises.BenchFragment">
<VideoView
android:id="#+id/video_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
/>
<TableLayout
android:layout_marginTop="20dp"
android:layout_below="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*">
<TableRow>
<TextView
android:text="#string/group"
android:background="#color/colorPrimary"
android:layout_width="500dp"
android:textColor="#ffffff"
android:textStyle="italic"
android:fontFamily="Arial"
android:textSize="17sp"
android:padding="5dip" />
</TableRow>
<TableRow>
<TextView
android:text="#string/bencbody"
android:textStyle="italic"
android:fontFamily="Arial"
android:textSize="17sp"
android:textColor="#ffffff"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="#string/description"
android:textStyle="italic"
android:fontFamily="Arial"
android:textSize="19sp"
android:background="#color/colorPrimary"
android:textColor="#ffffff"
android:padding="5dip" />
</TableRow>
<TableRow>
<TextView
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:fontFamily="Arial"
android:gravity="left"
android:layout_marginTop="5dp"
android:text="#string/stepone"
android:textColor="#android:color/white"
android:textSize="15sp"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:fontFamily="Arial"
android:layout_marginTop="5dp"
android:gravity="left"
android:text="#string/steptwo"
android:textColor="#android:color/white"
android:textSize="16sp"
android:padding="3dip" />
</TableRow>
</TableLayout>
</RelativeLayout>
I am trying to develop a simple app to find the day for a given date
xml code
<?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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.dayfinder.MainActivity">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/bg" />
<LinearLayout
android:id="#+id/l1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:fontFamily="sans-serif-condensed"
android:text="Date:"
android:textColor="#android:color/black"
android:textSize="35dp" />
<TextView
android:id="#+id/month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:fontFamily="sans-serif-condensed"
android:text="Month:"
android:textColor="#android:color/black"
android:textSize="35dp" />
<TextView
android:id="#+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:fontFamily="sans-serif-condensed"
android:text="Year:"
android:textColor="#android:color/black"
android:textSize="35dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="#+id/editText1"
android:layout_width="120dp"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="38dp"
android:layout_weight="1"
android:background="#android:color/white"
android:fontFamily="sans-serif-condensed"
android:hint="Enter Date"
android:inputType="date"
android:maxLength="2"
android:textAlignment="gravity"
android:textColor="#android:color/black"
android:textColorHint="#android:color/black"
android:textSize="20dp">
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="120dp"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="40dp"
android:layout_weight="1"
android:background="#android:color/white"
android:fontFamily="sans-serif-condensed"
android:hint="Enter Month!"
android:inputType="date"
android:maxLength="2"
android:textAlignment="gravity"
android:textColor="#android:color/black"
android:textColorHint="#android:color/black"
android:textSize="20dp">
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText3"
android:layout_width="120dp"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="41dp"
android:layout_weight="1"
android:background="#android:color/white"
android:fontFamily="sans-serif-condensed"
android:hint="Enter Year!"
android:inputType="date"
android:maxLength="4"
android:textAlignment="gravity"
android:textColor="#android:color/black"
android:textColorHint="#android:color/black"
android:textSize="20dp">
<requestFocus />
</EditText>
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_bel="#android:color/white"
android:onClick="chow="#id/l1"
android:layout_centerHorizontal="true"
android:text="Find"
android:layout_margin="24dp"
android:backgroundeck"/>
<TextView
android:id="#+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true"
android:layout_margin="48dp"
android:fontFamily="sans-serif-condensed"
android:text="Answer"
android:textColor="#android:color/black"
android:textSize="40dp" />
</RelativeLayout>
MainActivity.java
package com.example.android.dayfinder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import source.dayfinder;
public class MainActivity extends AppCompatActivity {
int e1;
String s1,s2,s3;
private EditText medit1;
private EditText medit2;
private EditText medit3;
private TextView mText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button mbutton=(Button)findViewById(R.id.button);
public void check(View view) {
medit1=(EditText)findViewById(R.id.editText1);
medit2=(EditText)findViewById(R.id.editText2);
medit3=(EditText)findViewById(R.id.editText3);
mText = (TextView)findViewById(R.id.answer);
s1=medit1.getText().toString();
s2=medit2.getText().toString();
s3=medit3.getText().toString();
dayfinder f=new dayfinder();
f.finder(s1,s2,s3);
mText.setText(f.finder(s1,s2,s3));
}
}
package dayfinder.java (which i use in mainactivity.java)
package source;
public class dayfinder {
public String finder(String a1, String a2, String a3) {
int q, w, x;
String e1;
q = Integer.parseInt(a1);
if ((q < 1000) || (q > 4000)) {
e1 = "error";
return e1;
}
else
{
e1="null";
return e1;
}
}
}
Reformat your MainActivity.java like below and if you have any your error after this again please update your question
Follow naming conventions
Took dayfinder object creation to onCreate to avoid creating new objects every time your check method gets called
package com.example.android.dayfinder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import source.dayfinder;
public class MainActivity extends AppCompatActivity {
int e1;
String s1,s2,s3;
private EditText medit1;
private EditText medit2;
private EditText medit3;
private TextView mText ;
private Button mbutton;
private dayfinder f;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
f=new dayfinder();
}
public void initializeViews(){
mbutton=(Button)findViewById(R.id.button);
medit1=(EditText)findViewById(R.id.editText1);
medit2=(EditText)findViewById(R.id.editText2);
medit3=(EditText)findViewById(R.id.editText3);
mText = (TextView)findViewById(R.id.answer);
}
public void check(View view) {
s1=medit1.getText().toString();
s2=medit2.getText().toString();
s3=medit3.getText().toString();
f.finder(s1,s2,s3);
mText.setText(f.finder(s1,s2,s3));
}
}
I am trying to go back to previous activity after a linear layout or back button is touched however I am getting the nullpointer exception error even tough I have initialized the button and layout in onCreate method
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.LinearLayout.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
My Java file
package com.profile;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import com.R;
public class AboutActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
LinearLayout backlayout=(LinearLayout) findViewById(R.id.back_button_linear_layout);
ImageButton backButton=(ImageButton) findViewById(R.id.back_profile_btn);
backlayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
}
My XML file
<?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="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="com.profile.AboutActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#color/colorPrimaryDark">
<LinearLayout
android:id="#+id/back_profile_layout"
android:layout_width="55dp"
android:layout_height="match_parent"
android:gravity="start|center_vertical"
android:orientation="vertical">
<ImageButton
android:id="#+id/back_profile_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:adjustViewBounds="true"
android:background="#color/transparentBackgroundPrimaryDark"
android:maxHeight="20dp"
android:maxWidth="20dp"
android:scaleType="centerInside"
android:src="#drawable/back_100px" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="About"
android:textAllCaps="true"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
<android.support.v4.widget.Space
android:layout_width="10sp"
android:layout_height="20sp" />
<Button
android:id="#+id/Terms"
android:layout_width="345dp"
android:layout_height="wrap_content"
android:background="#drawable/rounded_button_primary"
android:drawable="#color/colorPrimary"
android:clickable="true"
android:textAllCaps="false"
android:layout_gravity="center"
android:text="Terms and Conditions"
android:textColor="#color/colorPrimaryDark"
android:textSize="18sp"
/>
<android.support.v4.widget.Space
android:layout_width="10sp"
android:layout_height="20sp" />
<Button
android:id="#+id/Data_policy"
android:layout_width="345dp"
android:layout_height="wrap_content"
android:background="#drawable/rounded_button_primary"
android:color="#color/colorPrimary"
android:clickable="true"
android:textAllCaps="false"
android:layout_gravity="center"
android:text="Data policy"
android:textColor="#color/colorPrimaryDark"
android:textSize="18sp"
/>
<android.support.v4.widget.Space
android:layout_width="10sp"
android:layout_height="20sp" />
<Button
android:id="#+id/Licences"
android:layout_width="345dp"
android:layout_height="wrap_content"
android:background="#drawable/rounded_button_primary"
android:drawable="#color/colorPrimary"
android:clickable="true"
android:textAllCaps="false"
android:layout_gravity="center"
android:text="Licences"
android:textColor="#color/colorPrimaryDark"
android:textSize="18sp"
/>
<android.support.v4.widget.Space
android:layout_width="10sp"
android:layout_height="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="209dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Version 1.1.1000"
android:gravity="center"
android:textColor="#color/colorAccent"
android:textSize="16sp"
android:textStyle="bold"
android:layout_weight="0.87" />
</LinearLayout>
Change this line
LinearLayout backlayout=(LinearLayout) findViewById(R.id.back_button_linear_layout);
to this
LinearLayout backlayout=(LinearLayout) findViewById(R.id.back_profile_layout);
because you are assign wrong id to LinearLayout that's why it give you NullPointerExecption.
and also Remove this import com.R; is not require.
I have problem with my application. I have an application with checkboxes. How can i save them?
My layout is:
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/lstvw_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:layout_toLeftOf="#+id/itemCheckBox"
android:padding="8dp"
android:text="Switzerland" />
<TextView
android:id="#+id/lstvw_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="35dp"
android:textSize="14dp"
android:textColor="#545454"
android:layout_toLeftOf="#+id/itemCheckBox"
android:padding="8dp"
android:text="Bern" />
<CheckBox
android:id="#+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_alignParentRight="true"
android:gravity="right" />
<TextView
android:id="#+id/lstvw_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="80dp"
android:textSize="20dp"
android:layout_toLeftOf="#+id/itemCheckBox"
android:padding="8dp"
android:text="Germany" />
<TextView
android:id="#+id/lstvw_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="105dp"
android:textSize="14dp"
android:textColor="#545454"
android:layout_toLeftOf="#+id/itemCheckBox"
android:padding="8dp"
android:text="Berlin" />
<CheckBox
android:id="#+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_alignParentRight="true"
android:gravity="right" />
<TextView
android:id="#+id/lstvw_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="150dp"
android:textSize="20dp"
android:layout_toLeftOf="#+id/itemCheckBox"
android:padding="8dp"
android:text="Great Britain" />
<TextView
android:id="#+id/lstvw_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="175dp"
android:textSize="14dp"
android:textColor="#545454"
android:layout_toLeftOf="#+id/itemCheckBox"
android:padding="8dp"
android:text="London" />
<CheckBox
android:id="#+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_alignParentRight="true"
android:gravity="right" />
<TextView
android:id="#+id/lstvw_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="220dp"
android:textSize="20dp"
android:layout_toLeftOf="#+id/itemCheckBox"
android:padding="8dp"
android:text="France" />
<TextView
android:id="#+id/lstvw_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="245dp"
android:textSize="14dp"
android:textColor="#545454"
android:layout_toLeftOf="#+id/itemCheckBox"
android:padding="8dp"
android:text="Paris" />
<CheckBox
android:id="#+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="230dp"
android:layout_alignParentRight="true"
android:gravity="right" />
</RelativeLayout>
And my main.class is:
package com.example.capitalcities;
import com.example.capitalcities.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.view.View.OnClickListener;
public class MainActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
return true;
}
}
Do you now how to do this?
I hope you understand me, and i hope you could help me.
Use SharedPreferences to store your checkboxes as boolean.
Something like :
SharedPreferences sharedPreferences = PreferencesManager.getDefaultSharedPreferences(this);
checkBoxDummy.setChecked(sharedPreferences.getBoolean("checkBoxDummy", false));
To store values, use a listener. Something like :
checkBoxDummy.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
sharedPreferences.putBoolean("checkBoxDummy", isChecked);
}
});