I am very new to android and I am trying to make CardView by programmatically, I am using Fragment. By setting an OnClick on the button I am able to create cards in my layout. The issue is after closing the activity cards are not showing.
How can I save the cards, so after closing the activity cards should be there?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle("Okhlee");
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hawker_jobs_fragment_layout, container, false);
context = getContext();
button = (Button) view.findViewById(R.id.hawker_job_add_card_view_button);
button.setOnClickListener(this);
relativeLayout = (RelativeLayout) view.findViewById(R.id.hawker_job_relative_layout);
return view;
}
#Override
public void onClick(View v) {
createCardView();
}
/*Adding cardview programmatically function */
private void createCardView() {
CardView addedCardView = new CardView(context);
layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, height);
addedCardView.setLayoutParams(layoutParams);
addedCardView.setPadding(10 ,10, 10, 10);
addedCardView.setRadius(15);
addedCardView.setCardBackgroundColor(Color.BLUE);
addedCardView.setMaxCardElevation(30);
addedCardView.setMaxCardElevation(6);
relativeLayout.addView(addedCardView);
}
Sorry for my bad English.
You can use SharedPreferences in order to know whether you should display the card.
Here's a sample code using your original one -
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hawker_jobs_fragment_layout, container, false);
context = getContext();
button = (Button) view.findViewById(R.id.hawker_job_add_card_view_button);
button.setOnClickListener(this);
relativeLayout = (RelativeLayout) view.findViewById(R.id.hawker_job_relative_layout);
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
boolean shouldDisplayCard = sharedPref.getBoolean("should_display_card", false);
if(shouldDisplayCard)
createCardView();
return view;
}
#Override
public void onClick(View v) {
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("should_display_card", true);
editor.apply();
createCardView();
}
Related
I had a fragment, in which first I check for availability for internet connection. If its available its fine, otherwise, I want to refresh the page by clicking on retry button. The problem which comes is, I am not able to refresh the fragment
This is my fragment code.
public View onCreateView(LayoutInflater inflater, #Nullable final ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.homework, container, false);
listView = rootView.findViewById(R.id.home_list);
adapter = new HomeWorkAdapter(getContext(), R.layout.home_single, arrayList);
listView.setAdapter(adapter);
cal = rootView.findViewById(R.id.date_select);
boolean x = handleNetworkConnection();
if (x == true) {
methodListener();
SharedPreferences preferences = getActivity().getSharedPreferences("user_login", Context.MODE_PRIVATE);
HashMap<String, String> map = new HashMap<>();
map.put("school_id", preferences.getString("school_id", ""));
map.put("class", preferences.getString("classes", ""));
map.put("sec", "homeera");
defaultfetch(map);
}
else
{
final LinearLayout ll = new LinearLayout(getActivity());
TextView textView = new TextView(getActivity());
Button button = new Button(getActivity());
ImageView image = new ImageView(getActivity());
ll.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
ll.setBackgroundColor(Color.parseColor("#ecf0f1"));
image.setImageResource(R.drawable.errorsq);
button.setBackgroundColor(Color.parseColor("#02b48a"));
button.setText("Try Again");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
/*
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(getActivity().).attach(this).commit();
*/
}
});
ll.addView(textView);
ll.addView(image);
ll.addView(button);
ll.setOrientation(LinearLayout.VERTICAL);
View rootview = ll;
return rootview;
}
return rootView;
}
In this code, I had applied a way to refresh the fragment, but the problem is that after refreshing UI comes blank. Nothing is there on the screen.
You should not refresh the fragment but refresh the list when you get an internet connection.
Since your method handleNetworkConnection() returns synchronously, you could organize the code as follows:
public View onCreateView(LayoutInflater inflater, #Nullable final ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.homework, container, false);
listView = rootView.findViewById(R.id.home_list);
adapter = new HomeWorkAdapter(getContext(), R.layout.home_single, arrayList);
listView.setAdapter(adapter);
cal = rootView.findViewById(R.id.date_select);
prepareList();
}
void prepareList() {
boolean x = handleNetworkConnection();
if (x == true) {
// same code as now, load the list content and display it
} else {
// same code as now except the click listener:
public void onClick(View view) {
prepareList();
}
//
}
}
I am new to Java/android develop and trying to follow instructions from android site but cannot find clear answer to implementing onclicklistener in fragment. Please help me why its keep crashing in runtime when I click.
public class TestFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDBhelper = new UserBaseHelper(getActivity());
// Capture our button from layout
TextView TextviewShow = (TextView)getActivity().findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
}
.....
}
try this
use this
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
instead of this
TextView TextviewShow = (TextView)getActivity().findViewById(R.id.text_show);
change your code as below
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
// Capture our button from layout
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
return v;
}
Try this
public class TestFragment extends Fragment {
View v;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDBhelper = new UserBaseHelper(getActivity());
// Capture our button from layout
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
}
.....
}
Move your code from onCreate() method to onCreateView() method and change the following code
From
(TextView)getActivity().findViewById(R.id.text_show);
to
(TextView)v.findViewById(R.id.text_show);
I am trying to implement a button into a fragment in order to use the soundPool in order to play a sound with a button. At the moment the playSound1 is coming up as never used and I have tried to implement the onClick Method but it is now coming up saying it cannot resolve the method. How do I link the soundPool to a button in a fragment? this is the .java file
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb1Id = Clubb1.load(getActivity(), R.raw.clubb1, 1);
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_one_layout, container, false);
Button buttonA = (Button) findViewById(R.id.buttonA);
buttonA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
public void playSound1()
{Clubb1.play(clubb1Id,1,1,1,0,1);}
});
Change your method to :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb1Id = Clubb1.load(getActivity(), R.raw.clubb1, 1);
View rootView = inflater.inflate(R.layout.fragment_one_layout, container, false);
Button buttonA = (Button) rootView.findViewById(R.id.buttonA);
buttonA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Clubb1.play(clubb1Id, 1, 1, 1, 0, 1);
}
});
return veiw;
}
You did a lot of mistakes here. So im not sure that my approach help you enough ) Write your program result into comment and we`ll try more.
There are multiple mistakes in your code.
You return a value way to early, the code beyond your return statement isn't executed
You're inflating the View, but all your inner elements eg your button is in that view so you have to find that view by id in that view.
I corrected it:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_one_layout, container, false);
Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
clubb1Id = Clubb1.load(getActivity(), R.raw.clubb1, 1);
Button buttonA = (Button) root.findViewById(R.id.buttonA);
buttonA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
public void playSound1()
{Clubb1.play(clubb1Id,1,1,1,0,1);}
});
return root;
}
Is is possible to LOCKED fragments on button click?
For example.. Im making a quiz app but it is in Fragment form..
after clicking the SUBMIT BUTTON. you must swipe to the next page or the QUESTION 2.java.
now the thing is..
I want to disable the PREVIOUS fragment so you cannot go back to QUESTION 1.
Thank you guys!
my code..
QUESTION 1.JAVA
public class Question1 extends Fragment{
RadioButton q1a2;
Button btn1;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.question1, null);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
q1a2 = (RadioButton)getView().findViewById(R.id.q1a2);
btn1 = (Button)getView().findViewById(R.id.btnq1);
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
SharedPreferences.Editor editor = app_preferences.edit();
Toast.makeText(getActivity(), "Submitted", Toast.LENGTH_SHORT).show();
if (q1a2.isChecked()){
editor.putInt("answer_value", 1);
} else {
editor.putInt("answer_value", 0);
}
editor.commit();
}
});
}
}
QUESTION 2.JAVA
public class Question2 extends Fragment{
RadioButton q2a1;
Button btn2;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.question2, null);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
q2a1 = (RadioButton)getView().findViewById(R.id.q2a1);
btn2 = (Button)getView().findViewById(R.id.q2_button);
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
btn2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
SharedPreferences.Editor editor = app_preferences.edit();
if (q2a1.isChecked()){
editor.putInt("answer_value2", 1);
} else {
editor.putInt("answer_value2", 0);
}
editor.commit();
}
});
}
}
You can remove the old fragments from the viewpager dynamically, as the user moves forward:
dynamically add and remove view to viewpager
I'd like to create editable textfield in fragment, which after close or stop of app would be saved. But there's something wrong in line with return notatki; I already have this:
public class DetailFragment2 extends Fragment {
private EditText notatki;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#SuppressLint("SimpleDateFormat")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
notatki = (EditText) view.findViewById(R.id.editText1);
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
notatki.setText(settings.getString("value", ""));
return view;
}
#Override
public void onStop( ){
super.onStop();
if(notatki.getText() != null) {
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("value", notatki.getText().toString());
editor.commit();
}
}
}
When I change return notatki; to return view; it works until the stop of app, when I wanted to save content of editText but it isn't saving anything.
Change to
private EditText notatki;
#SuppressLint("SimpleDateFormat")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
notatki = (EditText) view.findViewById(R.id.editText1);
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
notatki.setText(settings.getString("value", ""));
return view; // return view here instead of notaki
}
You have already declared EditText as a class member
private EditText notatki;
So just initialize it in onCreateView.
Change
EditText notatki = (EditText) view.findViewById(R.id.editText1);
to
notatki = (EditText) view.findViewById(R.id.editText1);
because if you declare it again in your onCreateView it will shadow the global one and you will get it null in onStop
Try This....
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
EditText notatki = (EditText) view.findViewById(R.id.editText1);
SharedPreferences settings = this.getActivity().getSharedPreferences("PREFS", 0);
notatki.setText(settings.getString("value", ""));
return view;
}
You are doing it right but the problem is that you should return the parent view from onCreateView and not the EditText instance notatki
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details2, parent, false);
....
return view;
}
private EditText etext;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.secondfragment,container,false);
etext = (EditText) getView().findViewById(R.id.editText4);
return v;