Why setText won't output to string inside fragment? - android

After I c/p code to fragment, my function won't set the string inside the app. But it worked at first without fragment, and I can't find the problem. App runs normally, but it won't output setText string on screen, while string is visible with it default string
public class MainFragment extends FragmentActivity{
EditText editNumber;
Button calculate;
TextView result;
int two = 2;
int seven = 7;
int inputNumber;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//find elements
editNumber = (EditText) findViewById(R.id.editNumber);
calculate = (Button) findViewById(R.id.calculateButton);
result = (TextView) findViewById(R.id.resultText);
//listener
editNumber.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inputNumber = Integer.parseInt(editNumber.getText().toString());
}
});//onClick
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText("Output: " + (inputNumber / two) + seven);
}
});//onclick
}//onCreate
}//main
[code]

In FragmentActivity you should define a Fragment object, insert it into the Activity's frame and display things in it. Please refer to:
http://developer.android.com/guide/components/fragments.html

Are you sure your MainFragment is a fragment? It extends activity: FragmentActivity. If it is just a typo issue then you have to provide your layout in the onCreateView() method of the fragment. Something like this:
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.activity_main, container, false);
result = (TextView) v.findViewById(R.id.resultText);
return v;
}
Finding a view in a fragment in other method than onCreateView() you can call:
getView().findViewById(R.id.resultText);

Related

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

TextView SetTextSize not working inside fragment

I have a Activity with TabLayout and ViewPager. I have two fixed buttons floating on top of the activity at bottom right corner. These two buttons are Plus and Minus buttons for increasing/decreasing the font sizes of the text inside the fragment. I created an Interface inside Activity and implemented it inside the fragment where I am changing the textsize of TextView.
Problem is that it does not work at the time when I click the button. When I swipe the fragment then it shows with updated textsize. Am I missing something? Is this the correct way?
Inside Activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post_single);
//....
onCoachMark();
//....
}
public void onCoachMark() {
final FrameLayout overlayFramelayout = new FrameLayout(getApplicationContext());
setContentView(overlayFramelayout);
View view = getLayoutInflater().inflate(R.layout.post_single, overlayFramelayout, false);
final View overlay_view = getLayoutInflater().inflate(R.layout.zoom_overlay, overlayFramelayout, false);
final ImageView ivPlus = (ImageView) overlay_view.findViewById(R.id.ivPlus);
final ImageView ivMinus = (ImageView) overlay_view.findViewById(R.id.ivMinus);
overlayFramelayout.addView(view);
overlayFramelayout.addView(overlay_view);
ivPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CommonFunctions.FONT_SIZE += 3;
CommonFunctions.putMySharedPreferences(getApplicationContext(), "fontSize", String.valueOf(CommonFunctions.FONT_SIZE));
iCoachMark.onTextSizeChange();
}
});
ivMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CommonFunctions.FONT_SIZE -= 3;
CommonFunctions.putMySharedPreferences(getApplicationContext(), "fontSize", String.valueOf(CommonFunctions.FONT_SIZE));
iCoachMark.onTextSizeChange();
}
});
}
public interface ICoachMark {
void onTextSizeChange();
}
public void setOnCoachMark(ICoachMark iCoachMark) {
this.iCoachMark = iCoachMark;
}
Inside Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.post_content, container, false);
tvTitle = (TextView) rootView.findViewById(R.id.tvTitle);
tvDescription = (TextView) rootView.findViewById(R.id.tvDescription);
svPost = (NestedScrollView) rootView.findViewById(R.id.svPost);
String fs = CommonFunctions.getMySharedPreferences(getActivity().getApplicationContext(), "fontSize");
if (fs.matches("")) {
CommonFunctions.putMySharedPreferences(getActivity().getApplicationContext(), "fontSize", String.valueOf(CommonFunctions.FONT_SIZE));
} else {
CommonFunctions.FONT_SIZE = Integer.valueOf(fs);
tvTitle.setTextSize(CommonFunctions.FONT_SIZE);
tvDescription.setTextSize(CommonFunctions.FONT_SIZE);
}
((PostActivity) getActivity()).onTextSizeChange(new PostActivity.ICoachMark() {
#Override
public void onTextSizeChange() {
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, CommonFunctions.FONT_SIZE);
tvDescription.setTextSize(TypedValue.COMPLEX_UNIT_SP, CommonFunctions.FONT_SIZE);
}
});
}

Add an item to ListView (using Tabs)

I'm trying to add an item to a ListView which is in one of my 3 tabs, the object will be created on another activity and after creating I want to return to the Tab and there's already shown the object in the ListView, I wanted to do FindViewById in the java class of the Tab but this doesn't work, does anyone know how i could do this?
Code:
Create Idea Activity
public class CreateIdeaActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_idea);
final EditText title = (EditText) findViewById(R.id.ideaTitle);
final EditText text = (EditText) findViewById(R.id.ideaText);
final Button create = (Button) findViewById(R.id.btn_create);
create.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view)
{
Idea idea = new Idea(title.getText().toString(),text.getText().toString(),"");
Manager.AddObjectToIdeaList(idea); // Manager Class: List.add(idea)
}
});
}
One of my tabs (xml contains ListView) where I want to add items from the Manager-List to the ListView (findViewById does not work):
public class ideastab_main extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_ideastab_main, container, false);
return rootView;
}
}

Referencing EditText in fragment class of swipe tab caused the Activity to crashed

I am creating a simple student grade calculator with four EditText in a fragment activity of a swipe tab, but referencing the edittext view caused the activity. I have commented the reference code section and the app is no more crashing. below is the class
public static class Course1 extends Fragment implements
OnClickListener {
EditText courseOne, courseTwo, courseThree, courseFour;
Button compute;
double sumPoint, proPoint1, proPoint2, cgpa;
double edit1, edit2, edit3, edit4, sumUnits;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.cgpa_one, container,
false);
/**
courseOne = (EditText)rootView.findViewById(R.id.editText2);
courseTwo = (EditText)rootView.findViewById(R.id.editText3);
courseThree = (EditText)rootView.findViewById(R.id.editText5);
courseFour = (EditText)rootView.findViewById(R.id.editText6);
compute.setOnClickListener(this);
*/
return rootView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
edit1 = Double.valueOf(courseOne.getText().toString());
edit2 = Double.valueOf(courseTwo.getText().toString());
edit3 = Double.valueOf(courseThree.getText().toString());
edit4 = Double.valueOf(courseFour.getText().toString());
proPoint1 = edit1 * edit2;
proPoint2 = edit3 * edit4;
sumPoint = proPoint1 + proPoint2;
sumUnits = edit2 + edit4;
cgpa = (float) sumPoint/sumUnits;
Intent i = new
Intent("com.thenextgeneration.cscumyu.CGPARESULT");
Bundle b = new Bundle();
b.putDouble("key", cgpa);
i.putExtras(b);
startActivity(i);
}
}
I have imported editText in the parent class
Please what way should I follow??
I forgot to reference the compute button
by
compute = (Button)rootView.findVieById(R.id.compute);
It now work

How to rewrite to an EditText after screen rotation using fragment in android

I know this question have been asked many times here but I tried all the answers with no success.
My EditText looses everything I typed after I rotate the screen even though I get the data logged after recreation
I have this code in my fragment
#Override
public void onSaveInstanceState(Bundle saveState){
super.onSaveInstanceState(saveState);
saveState.putString("A1", A1.getText().toString());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
if(savedInstanceState != null) {
Log.d("Novinyo2", savedInstanceState.getString("A1"));
String A1_value = savedInstanceState.getString("A1");
EditText A1_ = (EditText) rootView.findViewById(R.id.txta1);
A1_.setText(A1_value);
}
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
text1 = (TextView) getActivity().findViewById(R.id.accord1);
text2 = (TextView) getActivity().findViewById(R.id.accord2);
text3 = (TextView) getActivity().findViewById(R.id.accord3);
Code = (EditText) getActivity().findViewById(R.id.stationcode);
Name = (EditText) getActivity().findViewById(R.id.stationname);
A1 = (EditText)getActivity().findViewById(R.id.txta1);
A2 = (EditText)getActivity().findViewById(R.id.txta2);
text1.setOnClickListener(this);
text2.setOnClickListener(this);
text3.setOnClickListener(this);
}
here
#Override
public void onSaveInstanceState(Bundle saveState){
super.onSaveInstanceState(saveState);
saveState.putString("A1", A1.getText().toString());
}
you are saving an empty Bundle and then you are putting a value to bundle,
so when you are trying to retrieve those data the bundle is empty.
try that
#Override
public void onSaveInstanceState(Bundle saveState){
saveState.putString("A1", A1.getText().toString());
super.onSaveInstanceState(saveState);
}
UPDATE
on your Manifest file do you have that attribute android:configChanges="orientation|screenSize" on your activity tag?
You need to call super.onSaveInstanceState(saveState) after saving your data.
The resulting code:
saveState.putString("A1", A1.getText().toString());
super.onSaveInstanceState(saveState);

Categories

Resources