my custom View control not Redrawing in Fragment - android

I have created my custom view control which extends from the View control, only drawing some information in the onDraw method.
Everything works if I create the control on a regular activity layout. But if I create this control in onCreateView() of Fragment, it paints while creating and then my View "freezes". It works and resolves but is not calling onDraw(). I'm trying to call invalidate() but nothing.
Any ideas?
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.button, container, false);
mAgatButton = (agatButton) v.findViewById(R.id.agatButton1);
mAgatButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// This not Repainted
final GraphViewSeries TahoSer = mGraphTaho.getSeries((byte)0);
mGraphTahoPos++;
TahoSer.appendData(new GraphViewData(mGraphTahoPos, 50d), true);
//This Work correct
mTahoLabel.setText(String.format(getString(R.string.button_rpm), (int)(Math.random()*1000)));
Log.d(agatButton.class.toString(),"Click Detected: "+TahoSer.size());
}
});
final Context context = getActivity().getApplicationContext();
LinearLayout graphView = (LinearLayout) v.findViewById(R.id.graph_lay_taho);
graphView.addView(mGraphTaho = getGraphView(context,"Taho"));
mTahoLabel = (TextView) v.findViewById(R.id.tahoText);
mTahoLabel.setText(String.format(getString(R.string.button_rpm), 0));
return v;
}
private GraphView getGraphView(Context context, String Name)
{
final GraphView tmpView = new LineGraphView(context,Name);
tmpView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
List<GraphViewData> initalSeriesData = new ArrayList<GraphViewData>();
for (byte i=0;i<20;i++)
initalSeriesData.add(new GraphViewData(i, 0d));
final GraphViewSeries tahoSeries = new GraphViewSeries(initalSeriesData);
((LineGraphView) tmpView).setDrawBackground(true);
tmpView.addSeries(tahoSeries);
tmpView.setViewPort(1, 20);
tmpView.setScalable(false);
tmpView.setManualYAxis(true);
tmpView.setManualYAxisBounds(100, 0);
return tmpView;
}
If I don't use Fragment everything works. What is wrong?

Related

Fragement class inside a recycler view

i know that i can use a recycler view to show a list of item but what if I wanna show a list of existing Fragments?
I'll try to explain better with an example:
In my app I have a fragment called review that's look like this:
public class ReviewView extends Fragment implements ReviewViewContract.View {
private TextView tvContent, tvName,tvDate,tvReactionCounter, tvCommentCountCounter;
private ImageView userPhoto;
private ImageButton btnMore;
private Button btnComment, btnLike;
private PopupMenu popup;
private ReviewViewContract.Presenter presenter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.component_review, container, false);
presenter = new ReviewViewPresenter(this,getContext(),getArguments());
initComponent(v);
presenter.getDetail();
return v;
}
//Some view methods...
#Override
public void initComponent(View view) {
userPhoto = view.findViewById(R.id.img_userphoto_profile);
tvName = view.findViewById(R.id.tv_name_profile);
tvDate = view.findViewById(R.id.tvdate_profile);
tvContent = view.findViewById(R.id.content_review_component);
tvReactionCounter = view.findViewById(R.id.tv_reactionCounter);
tvCommentCountCounter = view.findViewById(R.id.tv_commentsCounter);
btnComment = view.findViewById(R.id.btn_comment);
btnLike=view.findViewById(R.id.btn_like);
popup= new PopupMenu(view.getContext(), btnMore);
popup.getMenuInflater()
.inflate(R.menu.menu_review, popup.getMenu());
btnMore = view.findViewById(R.id.more_review);
popup = new PopupMenu(view.getContext(), btnMore);
popup.getMenuInflater()
.inflate(R.menu.menu_review, popup.getMenu());
btnMore.setOnClickListener(v->popup.show());
}
#Override
public void setDetail(String title, String date, String authorImageURL, String overView) {
tvName.setText("ciao");
}
private void likeBtnListener(){...};
private void commentsBtnListener(){...};
}
Now I need to show an indefinite number of reviewView in my activity, I thought to use a recycler view to do that but I do not want to rewrite all methods of ReviewView in the viewHolder class,(it doesn't seem a smart thing to do) so what should I do?
PS: I know that I can archive this with something like
Review fragment = new ReviewView();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
But this way doesn't seem the best way to do this.

Android - Getting Objects in Multiple RootView

public class OccuMechanical extends android.support.v4.app.Fragment {
private View v_schedule;
private LinearLayout layout_schedule;
private ImageButton iv_add_schedule;
private int i = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_occu_mechanical, container, false);
layout_schedule = (LinearLayout) v.findViewById(R.id.layout_mech_schedule);
iv_add_schedule = (ImageButton) v.findViewById(R.id.iv_add_schedule);
iv_add_schedule.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i++;
v_schedule = LayoutInflater.from(getActivity()).inflate(R.layout.layout_mech_schedule, null);
layout_schedule.addView(v_schedule);
EditText et = (EditText) layout_schedule.getRootView().findViewById(R.id.layout_description);
et.setText("Test: " + String.valueOf(i));
}
});
return v;
}
}
idk if my term is right in the title but here's what im doing...
when I click the 'Add more equipment' it adds layout to my app so
I want to get all the EditText inside the layout i added in the RootView,
I tried setting the text to test it but the first editText of the first rootView is the only one updating. All the codes in my fragment listed above. attached image is the result of this code.
image result
I've done this task by using *RecyclerView as #hjchin suggested. Thank You!
RecyclerView Tutorial # Developer.Android

How to Refresh the fragment, when Internet Connection is Resume

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

setting the background colour from program rather than from xml for fragments

I need some help in setting the background colour using code for my current fragment. The fragment is created on selecting a particular tab.
Below is my main activity code
public class TabActionBarActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the text and background colour
setTheme(R.style.MyTheme);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
String label6 = getResources().getString(R.string.label6);
tab = actionBar.newTab();
tab.setText(label6);
TabListener<Tab6Fragment> tl6 = new TabListener<Tab6Fragment>(this,
label6, Tab6Fragment.class);
tab.setTabListener(tl6);
actionBar.addTab(tab);
String label7 = getResources().getString(R.string.label7);
tab = actionBar.newTab();
tab.setText(label7);
TabListener<Tab7Fragment> tl7 = new TabListener<Tab7Fragment>(this,
label7, Tab7Fragment.class);
tab.setTabListener(tl7);
actionBar.addTab(tab);
}
Now the code for the Tab7Fragment class looks like this
public class Tab7Fragment extends Fragment {
TextView tv1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ScrollView sv = new ScrollView(this.getActivity());
LinearLayout ll = new LinearLayout(this.getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this.getActivity());
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
EditText et = new EditText(this.getActivity());
et.setText("weeeeeeeeeee~!");
ll.addView(et);
Button b = new Button(this.getActivity());
b.setText("I don't do anything, but I was added dynamically. :)");
ll.addView(b);
for(int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(this.getActivity());
cb.setText("I'm dynamic!");
ll.addView(cb);
}
return this.getView();
}
}
Now how can i set the view for this fragment?
this.getActivity().setContentView(sv);
I know the above method is incorrect. But i need to set the content view with the scrollview layout. and another question is how do i set the background colour for this view?(using setBackgroundColor()?
Try this code in your onCreateView():
View view = inflater.inflate(R.layout.your_fragment_layout, container, false);
Then dont forget to return the view that you just inflated to the android runtime.
Remove return this.getView(); and add return view;
To set background color :
sv.setBackgroundColor(getRessources().getColors(R.color.yourcolor));
To inflate layout into a fragment, you need to return a View from onCreateView :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.your layout, container, false);
}

onClickListener in ListFragment not responding

Confused as to why the onClickListener is not responding. Is it because but buttons are set in one layout, but bound to another one? Any help would be appreciated.
public class ActivityList extends ListFragment
{
private ActivityDbAdapter mDbHelper;
private Long mRowId=Long.valueOf(1);
private Activity mContext; //changed to private
AlertDialog activity_relationship;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mContext=this.getActivity();
mDbHelper=new ActivityDbAdapter(mContext);
mDbHelper.open();
Cursor activity = mDbHelper.fetchAll(mRowId);
View view = (View) inflater.inflate(R.layout.activity_activity_row, container,false);
Button relationship = (Button) view.findViewById(R.id.show_relationship_activities);
String[] from = new String[]{ActivityDbAdapter.COLUMN_NAME_VALUE1_RELATIONSHIP , //from and too.
ActivityDbAdapter.COLUMN_NAME_VALUE1_EDUCATION,
ActivityDbAdapter.COLUMN_NAME_VALUE1_RECREATION,
ActivityDbAdapter.COLUMN_NAME_VALUE1_MIND, ActivityDbAdapter.COLUMN_NAME_VALUE1_DAILY};
int[] to = new int[]{R.id.relationship_value,R.id.education_value, R.id.recreation_value,
R.id.mind_value, R.id.daily_value};
SimpleCursorAdapter contacts =
new SimpleCursorAdapter(mContext, R.layout.activity_activity_row, activity, from, to); //my cursor adapter
relationship.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg) {
Log.e("Why","Are you not working?");
Cursor activity = mDbHelper.fetchAll(mRowId);
String mactivity_relationship=activity.getString( //define all of the Strings. Gahh!!
activity.getColumnIndexOrThrow(ActivityDbAdapter.COLUMN_NAME_ACTIVITY1_RELATIONSHIP));
AlertDialog activity_relationship=new AlertDialog.Builder(mContext).create();
activity_relationship.setTitle("Relationship Activities");
String activities=(mactivity_relationship+"\n"+mactivity_relationship2+"\n"+mactivity_relationship3+
"\n"+mactivity_relationship4+"\n"+mactivity_relationship5);
activity_relationship.setMessage(activities);
activity_relationship.show();
}
});
setListAdapter(contacts);
return inflater.inflate(R.layout.activity_activity_list, container, false);
}
}
As I have it right now, the log is not even showing up.
You may want to return the view that you actually create(and use for getting the Button) in the onCreateView:
//...
return view;
instead of returning a newly inflated layout(and losing the previous Button):
return inflater.inflate(R.layout.activity_activity_list, container, false);
Or add the view to a view from the R.layout.activity_activity_list file that you inflate and return.

Categories

Resources