Add editText to fragment - android

I'm trying to add editText and textView to a fragment. The code to generate the editText and TExtView is correct because I've used it before, but in this case nothing appears. Any ideas?
Fragment code:
public class MarcadoresFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.fragment_marcadores, container, false);
return myFragmentView;
}
}
Add the editText:
public void createEditText(int size){
Log.d("det", String.valueOf(size));
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.fragment_marcadores, null);
LinearLayout layout = (LinearLayout) dialogView.findViewById(R.id.marcadoresFrag); // this is whatever view you want to add them to
EditText editText;
TextView textView;
if(layout!=null)
for(int i=0;i<size;i++){
textView = new TextView(dialogView.getContext());
textView.setText("Descrição do marcador " + String.valueOf(i + 1));
textView.layout(0, 5, 0, 5);
// add to the view
layout.addView(textView);
editText = new EditText(dialogView.getContext());
editText.setId(i);
editText.setHint("Ex: Bebedouro ndsfhjdsjvdsbjvjhvjdfjbvfjfvjklbjklfv" + i + 1);
Log.d("det", "jsdgbiusdbgksjd");
allEds.add(editText);
// add to the view
layout.addView(editText);
}
}

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

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

How to load layout in code from xml file

I'm using ViewPageIndicator and I have a problem with using XML layout in code. Usually I'm using setContentView(R.layout.activity_main).
Of course I can create layout in code but I prefer to use XML files.
public final class FragmentOne extends Fragment {
private static final String KEY_CONTENT = "FragmentOne";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView text = new TextView(getActivity());
text.setGravity(Gravity.CENTER);
text.setText("some text");
text.setTextSize(20 * getResources().getDisplayMetrics().density);
text.setPadding(20, 20, 20, 20);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.setGravity(Gravity.CENTER);
layout.addView(text);
return layout;
}
}
In onCreateView() you can use the LayoutInflater passed in as the first argument to inflate the layout.
Your code would look something like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.my_fragment_layout, container, false);
}

Fragmentation Layout inflater issue

hello guys..
i want to add two layout in my fragmentation .one layout already inflated after that second layout inflate on first layout button click...well i tried to remove first layout on button click of first layout button and inflate second layout but my button click does not response any type of reaction..please help me
public static LinearLayout mLL_MyNotes,mLin_myNotes,mLL_UploadNotes;
public static ViewFlipper mVF_MyNotes,mViewUploadMyNotes;
public static View mViewMyNotes;
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
TextView text = new TextView(getActivity());
text.setGravity(Gravity.CENTER);
text.setTextSize(20 * getResources().getDisplayMetrics().density);
text.setPadding(20, 20, 20, 20);
if(mContent.equalsIgnoreCase("My Notes")){
mViewMyNotes = inflater.inflate(R.layout.activity_myspace_mynotes_list, container, false);
mLin_myNotes=(LinearLayout)mViewMyNotes.findViewById(R.id.mainLin_MyNotes);
mVF_MyNotes = (ViewFlipper) mViewMyNotes.findViewById(R.id.vf_MySpace_MyNotes);
mbtn_Upload_MyNotes = (Button) mViewMyNotes.findViewById(R.id.btnUpload_MySpace_MyNotes);
mWV_MyNotes = (WebView) mViewMyNotes.findViewById(R.id.wv_MySpace_MyNotes);
mLV_MyNotes = (ListView) mViewMyNotes.findViewById(R.id.lv_MySpace_MyNotes);
mLV_MyNotes.setAdapter(new MySpace_MyNotesAdapter(getActivity(),R.layout.activity_myspace_mynotes_listitem));
mbtn_Upload_MyNotes.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater Newinflater = (LayoutInflater) getActivity().
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewUploadMyNotes==Newinflater.inflate(R.layout.activity_myspace_mynotes_uploades,null);
mLL_UploadNotes = (LinearLayout) mViewUploadMyNotes.findViewById(R.id.llUpload_MyNotes);
((ViewGroup) mLin_myNotes.getParent()).removeView(mViewUploadMyNotes);
mLin_myNotes.addView(mViewUploadMyNotes);
}
});
return mViewMyNotes;

my custom View control not Redrawing in Fragment

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?

Categories

Resources