my textview in activity configure disappears after setting it
the small text 192.168.0, is the text view appears only when i hit the save button
the textview is disappearing completely when i come back to this activity
edit: added my java code and my xml
package com.example.smartcharge;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Objects;
public class configure extends AppCompatActivity {
EditText ip, port;
public TextView tv;
Button SAVEBTN;
SharedPreferences sharedPreferences;
public static final String MyPREFERENCES = "MyPrefs";
public static final String IPSTR = "ipKey";
public static final String PORTSTR = "portKey";
public static final String TEXT1 = "textkey";
private String text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_configure);
Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(false);
ip = findViewById(R.id.et1);
port = findViewById(R.id.et2);
SAVEBTN = findViewById(R.id.savebtn);
tv = findViewById(R.id.tv2);
SAVEBTN.setOnClickListener(v -> {
senddata();
});
loaddata();
updateviews();
}
public void senddata(){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(configure.this);
SharedPreferences.Editor editor = sharedPreferences.edit();
String ipstr = ip.getText().toString();
String portstr = port.getText().toString();
tv.setText(ipstr + "," + portstr);
editor.putString(IPSTR, ipstr);
editor.putString(PORTSTR, portstr);
editor.putString(TEXT1, tv.getText().toString());
editor.apply();
Toast.makeText(configure.this,"saved",Toast.LENGTH_LONG).show();
}
public void loaddata(){
sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
text1 = sharedPreferences.getString(TEXT1 , "");
}
public void updateviews(){
tv.setText(text1);
}
}
Below is xml code of my configure activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".configure">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Broker Settings"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.361"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.038" />
<TextView
android:id="#+id/tv2"
android:layout_width="327dp"
android:layout_height="49dp"
android:text="TextView"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.309"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.247" />
<Button
android:id="#+id/savebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btns"
android:text="save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.572" />
<EditText
android:id="#+id/et2"
android:layout_width="141dp"
android:layout_height="wrap_content"
android:backgroundTint="#D1D1D1"
android:hint="port "
android:inputType="number"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.096"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.444">
</EditText>
<EditText
android:id="#+id/et1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:backgroundTint="#D1D1D1"
android:hint="ip Address"
android:inputType="number"
android:text="192.168.0."
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.163"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.339">
</EditText>
</androidx.constraintlayout.widget.ConstraintLayout>
any one have any clue of this behavior?, please help me if you can :)
The text disappears since the updating of your TextView takes place in onCreate.
#Override
protected void onResume() {
SAVEBTN.setOnClickListener(v -> {
senddata();
});
loaddata();
updateviews();
}
This should do the trick.
Go through the Activity lifecycle to see how this works. But basically moving methods to onResume should work.
Related
Hi I've been trying to make a pop up window which presents a trackable seekbar as a part of a school project, both parts of the code work alone but when put together the seekbar progress is not tracked (you can move the seekbar but the number won't change), Help would be very appreciated!
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.activity_pop_up, null);
// create the popup window
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = 500;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window token
popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonShowPopupWindowClick"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.368" />
</androidx.constraintlayout.widget.ConstraintLayout>
popUp.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
public class popUp extends AppCompatActivity {
private TextView textView;
private SeekBar seekBar;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
seekBar = (SeekBar) findViewById(R.id.seekBar);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressBar.setProgress(progress);
textView.setText("" + progress + "$");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
activity_pop_up.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right">
<Button
android:id="#+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="confirm"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.06"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.01999998" />
<SeekBar
android:id="#+id/seekBar"
android:layout_width="250dp"
android:layout_height="55dp"
android:progress="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.78"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.01999998" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView3"
android:layout_width="154dp"
android:layout_height="45dp"
android:gravity="center"
android:text="amount:"
android:textSize="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.110000014" />
<TextView
android:id="#+id/textView"
android:layout_width="214dp"
android:layout_height="44dp"
android:text="0"
android:textSize="30dp"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.75"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.110000014" />
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/seekBar"
android:layout_alignParentStart="true"
android:layout_marginBottom="21dp" />
</LinearLayout>
On the main screen I have 7 buttons (days) and a fragment.
For every day of the week I wanted to generate a fragment with some EditTexts and a ListView, but creating 7 fragments with the same layout and almost the same class content seems too repetitive.
I only did it for Monday and Tuesday.
The only difference between MondayFragment and TuesdayFragment is that in TuesdayFragment I renamed the variable mondayTV to tuesdayTV and in layout I changed the ids of the submit button and the ListView. Also the key for Bundle is different. I don't think it's worth posting it since it's so similar to MondayFragment.
I want to know if it's possible to create a fragment template and use it to generate a fragment for every button of the activity given this code I wrote. There is still a lot to work on when it comes to functionality but I can't get this idea out of my head.
MainActivity.java
package com.example.dietmanagement;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public Button monday, tuesday, wednesday, thursday, friday, saturday, sunday;
final MondayFragment mondayFragment = new MondayFragment();
final TuesdayFragment tuesdayFragment = new TuesdayFragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*Intent i = getIntent();
String current_user = i.getStringExtra("current_user");
TextView current_user_txtview = findViewById(R.id.current_user);
current_user_txtview.setText("Welcome, " + current_user);*/
monday = (Button)findViewById(R.id.monday_btn);
tuesday = (Button)findViewById(R.id.tuesday_btn);
wednesday = (Button)findViewById(R.id.wednesday_btn);
thursday = (Button)findViewById(R.id.thursday_btn);
friday = (Button)findViewById(R.id.friday_btn);
saturday = (Button)findViewById(R.id.saturday_btn);
sunday = (Button)findViewById(R.id.sunday_btn);
monday.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openFragment(mondayFragment);
getDay(mondayFragment, "monday", (String) monday.getContentDescription());
}
});
tuesday.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openFragment(tuesdayFragment);
getDay(tuesdayFragment, "tuesday", (String) tuesday.getContentDescription());
}
});
}
private void openFragment(final Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.daysfragment, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
public void getDay(final Fragment fragment, String key, String value)
{
Bundle bnd = new Bundle();
bnd.putString(key, value);
fragment.setArguments(bnd);
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="#color/background_green"
tools:context=".MainActivity">
<Button
android:id="#+id/tuesday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/button_states"
android:contentDescription="#string/tuesday_context"
android:text="#string/tuesday"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.194"
app:layout_constraintStart_toEndOf="#+id/monday_btn"
app:layout_constraintTop_toBottomOf="#+id/main_title"
app:layout_constraintVertical_bias="0.017" />
<TextView
android:id="#+id/main_title"
android:layout_width="147dp"
android:layout_height="93dp"
android:fontFamily="sans-serif-medium"
android:text="#string/welcome_label"
android:textColor="#FFFFFF"
android:textSize="70sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.414"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.049" />
<Button
android:id="#+id/monday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/button_states"
android:contentDescription="#string/monday_context"
android:text="#string/monday"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.16"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/main_title"
app:layout_constraintVertical_bias="0.017" />
<Button
android:id="#+id/thursday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/button_states"
android:text="#string/thursday"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.018"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/main_title"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="#+id/sunday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/button_states"
android:text="#string/sunday"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.786"
app:layout_constraintStart_toEndOf="#+id/saturday_btn"
app:layout_constraintTop_toBottomOf="#+id/main_title"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="#+id/saturday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/button_states"
android:text="#string/saturday"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.225"
app:layout_constraintStart_toEndOf="#+id/friday_btn"
app:layout_constraintTop_toBottomOf="#+id/main_title"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="#+id/friday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/button_states"
android:text="#string/friday"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.137"
app:layout_constraintStart_toEndOf="#+id/thursday_btn"
app:layout_constraintTop_toBottomOf="#+id/main_title"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="#+id/wednesday_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/button_states"
android:text="#string/wednesday"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toEndOf="#+id/tuesday_btn"
app:layout_constraintTop_toBottomOf="#+id/main_title"
app:layout_constraintVertical_bias="0.017" />
<ImageView
android:id="#+id/logo"
android:layout_width="52dp"
android:layout_height="58dp"
android:layout_marginStart="8dp"
android:layout_marginTop="52dp"
android:contentDescription="#string/app_name"
app:layout_constraintStart_toEndOf="#+id/main_title"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_logo" />
<TextView
android:id="#+id/current_user"
android:layout_width="362dp"
android:layout_height="27dp"
android:text="#string/current_user"
android:textAlignment="viewEnd"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/main_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.938"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.318" />
<FrameLayout
android:id="#+id/daysfragment"
android:layout_width="match_parent"
android:layout_height="460dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MondayFragment.java
package com.example.dietmanagement;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MondayFragment extends Fragment {
public TextView mondayTV;
public ArrayList<String> hour_food;
public ArrayAdapter<String> listViewAdapter;
public ListView listView;
public EditText input_meal;
public EditText input_time;
public Button submit;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_monday, container, false);
mondayTV = (TextView) v.findViewById(R.id.day);
Bundle bndMon = getArguments();
String day = bndMon.getString("monday");
mondayTV.setText(day);
hour_food = new ArrayList<String>();
listViewAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, hour_food);
listView = (ListView)v.findViewById(R.id.monday_list_item);
listView.setAdapter(listViewAdapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
hour_food.remove(position);
Toast.makeText(getActivity(), "Meal Removed", Toast.LENGTH_SHORT).show();
listViewAdapter.notifyDataSetChanged();
return true;
}
});
input_meal = v.findViewById(R.id.input_meal);
input_time = v.findViewById(R.id.input_time);
submit = (Button) v.findViewById(R.id.submit_food_btn);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(TextUtils.isEmpty(input_time.getText())) {
Toast.makeText(getActivity(), "Empty time input", Toast.LENGTH_SHORT).show();
} else if(TextUtils.isEmpty(input_meal.getText())){
Toast.makeText(getActivity(), "Empty meal input", Toast.LENGTH_SHORT).show();
}
else
{
hour_food.add(String.format("%s - %s", input_meal.getText().toString(), input_time.getText().toString()));
listViewAdapter.notifyDataSetChanged();
input_meal.setText("");
input_time.setText("");
}
}
});
return v;
}
}
fragment_monday.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background_green"
tools:context=".MondayFragment">
<TextView
android:id="#+id/day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="#string/day" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/input_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints="#string/time"
android:hint="#string/time"
android:inputType="time" />
<EditText
android:id="#+id/input_meal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="#string/meal"
android:hint="#string/meal"
android:inputType="textAutoCorrect|textCapSentences" />
</LinearLayout>
<Button
android:id="#+id/submit_food_btn_monday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit"
android:background="#color/white"
android:textColor="#color/background_green"
android:layout_gravity="end"
android:layout_marginTop="10dp"/>
<ListView
android:id="#+id/monday_list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
strings.xml
Create DayFragment and layout only for one day. Pass extra information for Fragment and handle situations for different days with that extra information (in your case it looks like only String from bundle is different).
fragment_day.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background_green">
<TextView
android:id="#+id/day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="#string/day" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/input_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints="#string/time"
android:hint="#string/time"
android:inputType="time" />
<EditText
android:id="#+id/input_meal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="#string/meal"
android:hint="#string/meal"
android:inputType="textAutoCorrect|textCapSentences" />
</LinearLayout>
<Button
android:id="#+id/submit_food_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit"
android:background="#color/white"
android:textColor="#color/background_green"
android:layout_gravity="end"
android:layout_marginTop="10dp"/>
<ListView
android:id="#+id/list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
DayFragment.java
public class DayFragment extends Fragment {
private TextView dayTV;
private ArrayList<String> hour_food;
private ArrayAdapter<String> listViewAdapter;
private ListView listView;
private EditText input_meal;
private EditText input_time;
private Button submit;
private String text;
public DayFragment(String text) {
this.text = text;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_day, container, false);
dayTV = v.findViewById(R.id.day);
dayTV.setText(text);
hour_food = new ArrayList<>();
listViewAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, hour_food);
listView = v.findViewById(R.id.list_item);
listView.setAdapter(listViewAdapter);
listView.setOnItemLongClickListener((parent, view, position, id) -> {
hour_food.remove(position);
Toast.makeText(getActivity(), "Meal Removed", Toast.LENGTH_SHORT).show();
listViewAdapter.notifyDataSetChanged();
return true;
});
input_meal = v.findViewById(R.id.input_meal);
input_time = v.findViewById(R.id.input_time);
submit = v.findViewById(R.id.submit_food_btn);
submit.setOnClickListener(v1 -> {
if (TextUtils.isEmpty(input_time.getText())) {
Toast.makeText(getActivity(), "Empty time input", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(input_meal.getText())) {
Toast.makeText(getActivity(), "Empty meal input", Toast.LENGTH_SHORT).show();
} else {
hour_food.add(String.format("%s - %s", input_meal.getText().toString(), input_time.getText().toString()));
listViewAdapter.notifyDataSetChanged();
input_meal.setText("");
input_time.setText("");
}
});
return v;
}
}
Use constructor for passing information.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button monday = findViewById(R.id.monday_btn),
tuesday = findViewById(R.id.tuesday_btn),
wednesday = findViewById(R.id.wednesday_btn),
thursday = findViewById(R.id.thursday_btn),
friday = findViewById(R.id.friday_btn),
saturday = findViewById(R.id.saturday_btn),
sunday = findViewById(R.id.sunday_btn);
openFragment(monday);
openFragment(tuesday);
openFragment(wednesday);
openFragment(thursday);
openFragment(friday);
openFragment(saturday);
openFragment(sunday);
}
private void openFragment(Button btn) {
btn.setOnClickListener(v -> {
String contentDescription = btn.getContentDescription().toString();
getSupportFragmentManager().beginTransaction()
.replace(R.id.daysfragment, new DayFragment(contentDescription))
.addToBackStack(null)
.commit();
});
}
}
In MainActivity openFragment() method takes a Button parameter and with that Button and sets onClickListener to that Button. When you click any Button it gets content description from that Button and passing it to Fragment and opens that Fragment with that content description.
I've read many answers and I cannot find where I'm going wrong with my code. I want to have 2 buttons on my first activity. When I click button1 or button2 I want text held within that specific string resource to appear on the textview of activity two. I am able to populate one of the strings, but it will not separate them depending on which button I click.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<Button
android:id="#+id/button_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginTop="150dp"
android:layout_marginEnd="161dp"
android:onClick="launchSecondActivity"
android:text="Passage 1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginTop="244dp"
android:layout_marginEnd="161dp"
android:onClick="launchSecondActivity"
android:text="Passage 2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginTop="368dp"
android:layout_marginEnd="161dp"
android:onClick="launchSecondActivity"
android:text="Passage 3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/button_main_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginTop="114dp"
android:layout_marginEnd="172dp"
android:layout_marginBottom="400dp"
android:text="#string/button_main_passage"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_main" />
<TextView
android:id="#+id/button_second_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginTop="114dp"
android:layout_marginEnd="172dp"
android:layout_marginBottom="400dp"
android:text="#string/button_second_passage"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_main" />
<TextView
android:id="#+id/button_third_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginTop="114dp"
android:layout_marginEnd="172dp"
android:layout_marginBottom="400dp"
android:text="#string/button_third_passage"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_main" />
</androidx.constraintlayout.widget.ConstraintLayout>
Activity_passage.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".PassageActivity">
<ScrollView
android:id="#+id/scroll_passage"
android:layout_width="409dp"
android:layout_height="729dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/text_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="#+id/text_message_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
strings.xml
<resources>
<string name="app_name">ThreeButtons</string>
<string name="activity2_name">Passage</string>
<string name="button_main_passage">First Passage First Passage First Passage First
Passage</string>
<string name="button_second_passage">Second Passage Second Passage Second Passage Second
Passage</string>
</resources>
MainActivity.java
package com.dev20.threebuttons;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mButtonMainText;
private TextView mButtonSecondText;
private static final String LOG_TAG = MainActivity.class.getSimpleName();
public static final String EXTRA_MESSAGE = "com.example.android.threebuttons.extra.MESSAGE";
public static final String EXTRA_MESSAGE2 = "com.example.android.threebuttons.extra.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonMainText = findViewById(R.id.button_main_text);
mButtonSecondText = findViewById(R.id.button_second_text);
}
public void launchSecondActivity(View view) {
Log.d(LOG_TAG, "Button Clicked");
Intent intent = new Intent(this, PassageActivity.class);
String message = mButtonMainText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
Intent intent2 = new Intent(this, PassageActivity.class);
String message2 = mButtonSecondText.getText().toString();
intent2.putExtra(EXTRA_MESSAGE2, message2);
startActivity(intent);
}
}
PassageActivity.java
package com.dev20.threebuttons;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class PassageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_passage);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = findViewById(R.id.text_message);
textView.setText(message);
Intent intent2 = getIntent();
String message2 = intent2.getStringExtra(MainActivity.EXTRA_MESSAGE2);
TextView textView2 = findViewById(R.id.text_message);
textView2.setText(message2);
}
}
I think You have to make two separate functions. Why You add 2 intents in one function? You only want to make one intent and pass one string. Also, you can add tag to button 1 = But1 and button 2 = But2. Just add to XML layout android:tag="But1" to both buttons.
'
public void launchSecondActivity(View view) {
String arg = null;
if (view.getTag().equals("But1"))
{
arg = mButtonMainText.getText().toString();
}
else if (view.getTag().equals("But2"))
{
arg = mButtonSecondText.getText().toString();
}
Intent intent = new Intent(this, PassageActivity.class);
intent.putExtra(EXTRA_MESSAGE, arg);
startActivity(intent);
}
I want to use "Shared preference" for saving my username and password and it saved but do not work on second time i open the app.
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.icu.text.IDNA;
import android.support.v4.media.RatingCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Login_page extends AppCompatActivity {
TextView tv_user, tv_pass;
EditText tx_user, tx_pass;
Button btn_login;
ImageView img_ghorme;
SharedPreferences save;
public void findall()
{
tv_user = (TextView)findViewById(R.id.tv_user);
tv_pass = (TextView)findViewById(R.id.tv_pass);
tx_user = (EditText)findViewById(R.id.tx_user);
tx_pass = (EditText)findViewById(R.id.tx_pass);
btn_login = (Button)findViewById(R.id.btn_login);
img_ghorme = (ImageView)findViewById(R.id.img_ghorme);
}
public String save_user = "";
public String save_pass = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page2);
findall();
if(save_user.equals("admin") && save_pass.equals("admin"))
{
Intent skip = new Intent(Login_page.this, food_page.class);
startActivity(skip);
}
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String user = tx_user.getText()+"";
String pass = tx_pass.getText()+"";
save_user = save.getString("username", "");
save_pass = save.getString("password", "");
SharedPreferences.Editor e = save.edit();
e.putString("username", user);
e.putString("password", pass);
e.apply();
if(user.equals("admin") && pass.equals("admin"))
{
Intent food = new Intent(Login_page.this, food_page.class);
startActivity(food);
}
if (!user.equals("admin") || !pass.equals("admin"))
{
e.putString("username", "");
e.putString("password", "");
e.apply();
tx_user.setText("");
tx_pass.setText("");
}
}
});
save = getSharedPreferences("user", MODE_PRIVATE);
save = getSharedPreferences("pass", MODE_PRIVATE);
Typeface font_shabnam = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
Typeface font_shabnam_light = Typeface.createFromAsset(getAssets(),"fonts/Shabnam_Light.ttf");
Typeface font_shabnam_bold = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
tv_user.setTypeface(font_shabnam_bold);
tv_pass.setTypeface(font_shabnam_bold);
tx_user.setTypeface(font_shabnam_light);
tx_pass.setTypeface(font_shabnam_light);
btn_login.setTypeface(font_shabnam_bold);
Animation ani_rtl = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_ltr);
Animation ani_ltr = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_rtl);
Animation ani_fade = AnimationUtils.loadAnimation(Login_page.this, R.anim.fade);
Animation ani_dtu = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_dtu);
tv_user.setVisibility(View.VISIBLE);
tv_user.startAnimation(ani_rtl);
tv_pass.setVisibility(View.VISIBLE);
tv_pass.startAnimation(ani_rtl);
tx_user.setVisibility(View.VISIBLE);
tx_user.startAnimation(ani_ltr);
tx_pass.setVisibility(View.VISIBLE);
tx_pass.startAnimation(ani_ltr);
btn_login.setVisibility(View.VISIBLE);
btn_login.startAnimation(ani_fade);
img_ghorme.startAnimation(ani_dtu);
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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.home.seesion10.Login_page"
android:layoutDirection="rtl"
android:textDirection="rtl"
android:background="#color/textShiri">
<ImageView
android:id="#+id/img_ghorme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/cirlcleghormesabzi"
android:layout_marginTop="1500dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="100dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.902"
android:layout_marginStart="100dp" />
<TextView
android:id="#+id/tv_user"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:text="نام کاربری"
android:textSize="15sp"
android:visibility="invisible"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/tx_user"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:background="#drawable/tx_bg"
android:drawableLeft="#drawable/ic_user"
android:ems="10"
android:hint="نام کاربری"
android:inputType="textPersonName"
android:padding="10dp"
android:text=""
android:textColor="#color/textShiri"
android:textColorHint="#color/textShiri"
android:visibility="invisible"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_user" />
<TextView
android:id="#+id/tv_pass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:text="رمز عبور"
android:textSize="15sp"
android:visibility="invisible"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tx_user" />
<EditText
android:id="#+id/tx_pass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:background="#drawable/tx_bg"
android:drawableLeft="#drawable/ic_pass"
android:ems="15"
android:hint="........."
android:inputType="textPassword"
android:padding="10dp"
android:text=""
android:textColor="#color/textShiri"
android:textColorHint="#color/textShiri"
android:visibility="invisible"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_pass" />
<Button
android:id="#+id/btn_login"
android:layout_width="122dp"
android:layout_height="50dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="20dp"
android:background="#drawable/btn_bg"
android:text="ورود"
android:textColor="#color/textShiri"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/img_ghorme"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tx_pass" />
</android.support.constraint.ConstraintLayout>
i expected that when i write admin admin for username and password, it saved it and for the second time i open the app it skip that login page but its not work and show the login page again
You are not initialising the sharedPreference before using it and defining two sharedPreferences for each key-pair. Change your code as below.
public class Login_page extends AppCompatActivity {
TextView tv_user, tv_pass;
EditText tx_user, tx_pass;
Button btn_login;
ImageView img_ghorme;
SharedPreferences save;
public void findall()
{
tv_user = findViewById(R.id.tv_user);
tv_pass = findViewById(R.id.tv_pass);
tx_user = findViewById(R.id.tx_user);
tx_pass = findViewById(R.id.tx_pass);
btn_login = findViewById(R.id.btn_login);
img_ghorme = findViewById(R.id.img_ghorme);
}
public String save_user = "";
public String save_pass = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
findall();
save = getSharedPreferences("userInfo", MODE_PRIVATE);
save_user = save.getString("username", "");
save_pass = save.getString("password", "");
if(save_user.equals("admin") && save_pass.equals("admin"))
{
Intent skip = new Intent(Login_page.this, food_page.class);
startActivity(skip);
}
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
String user = tx_user.getText().toString();
String pass = tx_pass.getText().toString();
SharedPreferences.Editor e = save.edit();
e.putString("username", user);
e.putString("password", pass);
e.apply();
if(user.equals("admin") && pass.equals("admin"))
{
Intent food = new Intent(Login_page.this, food_page.class);
startActivity(food);
}
if (!user.equals("admin") || !pass.equals("admin"))
{
e.putString("username", "");
e.putString("password", "");
e.apply();
tx_user.setText("");
tx_pass.setText("");
}
}
});
Typeface font_shabnam = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
Typeface font_shabnam_light = Typeface.createFromAsset(getAssets(),"fonts/Shabnam_Light.ttf");
Typeface font_shabnam_bold = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
tv_user.setTypeface(font_shabnam_bold);
tv_pass.setTypeface(font_shabnam_bold);
tx_user.setTypeface(font_shabnam_light);
tx_pass.setTypeface(font_shabnam_light);
btn_login.setTypeface(font_shabnam_bold);
Animation ani_rtl = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_ltr);
Animation ani_ltr = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_rtl);
Animation ani_fade = AnimationUtils.loadAnimation(Login_page.this, R.anim.fade);
Animation ani_dtu = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_dtu);
tv_user.setVisibility(View.VISIBLE);
tv_user.startAnimation(ani_rtl);
tv_pass.setVisibility(View.VISIBLE);
tv_pass.startAnimation(ani_rtl);
tx_user.setVisibility(View.VISIBLE);
tx_user.startAnimation(ani_ltr);
tx_pass.setVisibility(View.VISIBLE);
tx_pass.startAnimation(ani_ltr);
btn_login.setVisibility(View.VISIBLE);
btn_login.startAnimation(ani_fade);
img_ghorme.startAnimation(ani_dtu);
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
My android app links to a new activity to allow a user to create their profile and save the strings they use to a firebase database. However, the activity loads but is blank as shown below. This issue only seemed to arise after I added functionality to save user data to firebase.
blank activity
package com.example.eventfeed;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap;
import java.util.Map;
public class CreateProfileActivity extends AppCompatActivity {
public static final String INTEREST_KEY = "interest";
public static final String NAME_KEY = "name";
private DocumentReference mDocRef = RegisterActivity.getmDocRef();
private Button createBtn;
#Override
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
this.setContentView(R.layout.create_profile_layout);
// Toast.makeText(this, "Create Activity", Toast.LENGTH_SHORT).show();
EditText interestsView = (EditText) findViewById(R.id.interests);
EditText nameView = (EditText) findViewById(R.id.profile_name);
String interests = interestsView.getText().toString();
String name= nameView.getText().toString();
Map<String, Object> dataToSave = new HashMap<>();
mDocRef = FirebaseFirestore.getInstance().document("users/" + ActivityLoginEmail.getEmailStr() + "/profileInfo");
dataToSave.put(INTEREST_KEY, interests);
dataToSave.put(NAME_KEY, name);
mDocRef.set(dataToSave);
createBtn = (Button) findViewById(R.id.createProfileBtn);
createBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CreateProfileActivity.this, Profile.class);
startActivity(intent);
}
});
}
}
The xml files that format the page
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="#+id/create_profile_layout"
tools:context=".CreateProfileActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include
android:id="#+id/main_page_toolbar"
layout="#layout/app_bar_layout">
</include>
<include layout="#layout/create_profile_content"></include>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_view">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Create profile activity xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".CreateProfileActivity"
android:id="#+id/create_profile_content"
tools:showIn="#layout/create_profile_layout">
<ImageView
android:id="#+id/imageView"
android:layout_width="0dp"
android:layout_height="200dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/vibe_concert" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="56dp"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_goneMarginTop="100dp"
app:srcCompat="#drawable/vibe_image" />
<EditText
android:id="#+id/profile_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="158dp"
android:layout_marginLeft="158dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="156dp"
android:layout_marginRight="156dp"
android:layout_marginBottom="228dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2"
app:layout_constraintVertical_bias="0.553"
/>
<TextView
android:id="#+id/interest_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="224dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/profile_interest"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.025"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2"
tools:text="Interests" />
<EditText
android:id="#+id/interests"
android:layout_width="200dp"
android:layout_height="87dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="140dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4"
app:layout_constraintVertical_bias="0.091" />
<TextView
android:id="#+id/profile_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.679"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.544" />
<Button
android:id="#+id/createProfileBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.835"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/interests"
app:layout_constraintVertical_bias="0.473"
tools:text="Create Profile" />
</android.support.constraint.ConstraintLayout>
The method that is called to send the user to the create profile activity
private void LoginUser(){
setEmailStr(email.getText().toString().trim());
String Password = password.getText().toString().trim();
mAuth.signInWithEmailAndPassword(emailStr, Password)
.addOnCompleteListener(this, new
OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
currentUser = mAuth.getCurrentUser();
finish();
Toast.makeText(ActivityLoginEmail.this, "Create
Activity", Toast.LENGTH_SHORT).show();
setCurrent_user_db(false);
startActivity(new Intent(ActivityLoginEmail.this,
CreateProfileActivity.class));
}else {
setCurrent_user_db(true);
Toast.makeText(ActivityLoginEmail.this, "couldn't login",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
EDIT:After changing the xml file to a simple one with just a single button and adding a toast command to print if the activity loads nothing happens. Therefore the problem is most likely not within the xml files.
EDIT2:I found the solution by removing the PersistableBundle parameter from my oncreate method. Not exactly sure what this does to begin with if anyone would like to elaborate.
public class CreateProfileActivity extends AppCompatActivity {
public static final String INTEREST_KEY = "interest";
public static final String NAME_KEY = "name";
private DocumentReference mDocRef = RegisterActivity.getmDocRef();
private Button createBtn;
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.create_profile_layout);
Toast.makeText(this, "Create Activity", Toast.LENGTH_SHORT).show();
EditText interestsView = (EditText) findViewById(R.id.interests);
EditText nameView = (EditText) findViewById(R.id.profile_name);
String interests = interestsView.getText().toString();
String name= nameView.getText().toString();
Map<String, Object> dataToSave = new HashMap<>();
mDocRef = FirebaseFirestore.getInstance().document("users/" +
ActivityLoginEmail.getEmailStr() + "/profileInfo");
dataToSave.put(INTEREST_KEY, interests);
dataToSave.put(NAME_KEY, name);
mDocRef.set(dataToSave);
createBtn = (Button) findViewById(R.id.createProfileBtn);
createBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CreateProfileActivity.this, Profile.class);
startActivity(intent);
}
});
}
}
try by moveing finish() to the bottom of this condition
if (task.isSuccessful()){
currentUser = mAuth.getCurrentUser();
Toast.makeText(ActivityLoginEmail.this, "Create
Activity", Toast.LENGTH_SHORT).show();
setCurrent_user_db(false);
startActivity(new Intent(ActivityLoginEmail.this,
CreateProfileActivity.class));
finish();
}