Android : Exchange views between tabs - android

I actually have an activity called "Creation.java" which provides access to different tabs and that works fine.
My tabs are activities and contains different Views like EditText or Spinners. As I have a lot of datas to save (really a lot) I'd like to propose to the user to fill all the Views by switching between tabs and then, in a last Tab, to validate all the datas and write it in an xml file.
But it seems like I can't get access to the datas from, by example my first tab in order to validate it within the last tab with a validate button.
Example for a customer creation in creationclient.xml which is set in the activity CreationClient.java :
<EditText android:id="#+id/nom1"
android:layout_width="#dimen/margexlarge"
android:layout_height="wrap_content"
android:text="Nom"
/>
<EditText android:id="#+id/prenom1"
android:layout_width="#dimen/margexlarge"
android:layout_height="wrap_content"
android:text="Prénom"
/>
I can't get the content in validation.java, I get an error cause it can't get the String from the View :
nom1 = (EditText) findViewById(R.id.nom1);
prenom1 = (EditText) findViewById(R.id.prenom1);
validationDuDossier = (Button) findViewById(R.id.validationdudossier);
idDossier = ((nom1.getText().toString().substring(0, 2)) + (prenom1.getText().toString().substring(0,2)));
So my point is : Can I get access to the datas of a layout A in an activity B even if I've set a layout B in this activity ?
Or do I have an exclusive access to the datas from the layout I set to my activity ?

You will need to link the edit text to the the last tab.
By either doing for example.
info.getText().toString(lastTab);
or you could do as following
lastTab = info.getText().toString();
From there you will link the lastTab holding the information to the last Views TextView or how ever you are displaying the information.

Related

EditText value is getting replaced with another edit text on backpress

I am in a situation here, I've two edit text in a fragment, on back press the value of first edit text is getting replaced by second edit text. Now both the edit text are having same values.
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:hint="hint" />
Above is my EditText, this EditText is inside a layout and I've included the layout in my fragment two times to get the EditText, on click of submit I replace this fragment with another one but on backpress when I come back to this fragment, the value of first edit box is getting replaced by second one. Now both has same values.
This is how I am replacing fragment.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.layout_fragment, fragment, tag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commitAllowingStateLoss();
getSupportFragmentManager().executePendingTransactions();
Both of your EditTexts are using the same id. In your XML files you need to make sure that the id for both edit texts are different. Even if there are in separate XML files, they must have different ids.
For instance, you might change their declarations to...
<EditText
android:id="#+id/edit_text1"
<EditText
android:id="#+id/edit_text2"
You should use different id for the EditText.
Assume the EditText is defined in edit_text_layout.xml, when including the layout, specify different id.
In fragment_layout.xml:
<include
layout="#layout/edit_text_layout"
android:id="#+id/edit_text_1" />
<include
layout="#layout/edit_text_layout"
android:id="#+id/edit_text_2" />
Explain:
Android save a view's state in a SparseArray, the key is the view's id. When restoring the state, views have the same id will be get the same state value.
If the view doesn't have an id (NO_ID), its state won't be saved or restored.
android.view.View.java
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
......
if (state != null) {
......
container.put(mID, state);
}
}
}
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
if (mID != NO_ID) {
Parcelable state = container.get(mID);
if (state != null) {
......
onRestoreInstanceState(state);
......
}
}
}
I know this is an old question but I ran into a very similar issue and although the current answers are correct (its a duplicate ID issue), the actual answers given weren't working or quite in the right area.
I had a Fragment, and in that Fragment I had 2 custom controls, both with unique ID's, but internally in those controls they included the same common Layout, and so that Layout of course had an EditText with the same ID in both.
#wrkwrk is right in that its an ID issue, but changing the ID on the "include" isn't enough if the included layout contains the EditText, and if you want to include the same layout multiple times, you can't give it a different ID in the axml.
So for my work around, in the code for my custom control that loads the EditText in, after loading it in I simply changed the ID afterwards using the View.GenerateViewId() function.
So something like (this is in C#/Xamarin)
var editText = _root.FindViewById<EditText>(Resource.Id.entryEditText);
editText.Id = GenerateViewId();
That seemed to fix the issues for me. So I can have one common ID in the layout so my control can find it fine, but then it changes so multiple instances of that control in a fragment will all save off their EditText state fine when going back.
Alternatively, you could give it no ID in the layout but a Tag instead and do FindViewByTag and then assign it an ID after loading.

How to pass data from fragment (.java file) to my fragment_layout.xml

My Problem: I have a fragment which should display the time it gets from the database.
In my fragment.java I get the data from the database and want to pass it to my fragment_layout.xml to show it.
(Every time the fragment is displayed, the actuall time is taken from the database)
I don't want to save the data for a long time, just want to show it.
What's the best way to do this?
You can't put it to the *.xml file.
Your fragment.java file should have something like a reference to a TextView, which is defined i the fragment_layout.xml.
fragment_layout.xml:
<TextView
android:id="#+id/myTimeTextView"
android:... />
fragment.java:
TextView myTextView = view.FindViewById(Resource.layout.myTimeTextView);
You'll can change the Text like this:
myTextView.SetText(myTimeString);
"myTimeString" needs to be defined in a method, that is called even when the Fragment is shown (use an event).

How to have several tabs from the same layout, correctly handling edittext?

I've got three tabs, all from a layout xml file,say list.xml. In list.xml I've defined a EditText, something like this:
<EditText
android:id="#+id/filterText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/filterClear"
android:hint="#string/filterHint"
android:text="#string/filter">
</EditText>
However, it seems that if doing a orientation change and having typed something into one of the three EditTexts (all I guess with the same ID), the text doesn't restore to the same state. Sometimes (depending on which tab I've typed into) all fields are empty or all fields are filled.
What is the proper way to handle this?
I ended up having three separate layout files list1.xml, list2.xml, list3.xml, that only differed in the IDs. So list1.xml would have #+id/filterText1, list2.xml would have #+id/filterText2 and so on.
And then in the ListActivity.java file I used to have this as part of my onCreate:
setContentView(R.layout.list);
And several references to this throughout the activity:
EditText filter = (EditText)findViewById(R.id.filterText);
...
Instead I have now got this in my onCreate:
if(START_TAB1.equals(startMode)) { // Checks which tab is being started
setContentView(R.layout.list1); // Use appropriate layout
filterTextId = R.id.filterText1; // Save correct ID in a class variable
}
// And then the same "else if..." for all other tabs
And this when referencing the field itself throughout the activity:
EditText filter = (EditText)findViewById(filterTextId); // Use common ID

AlertDialog vs Spinner vs ListView

I've got what I thought was a simple android UI design problem but I've been going around in circles for a couple of days. I have a REST service that I'm downloading XML from and displaying the XML in a form in an android app. I have a web page built and am mimicking this with android, same options, same URLs being sent to the REST service whether from android or the web pages. With HTML I can easily create checkbox groups and radiobutton/dropdowns for various id/display items, so for instance, I can display a planet option as:
<select name="planet"><option value="0">Mercury</option></select>
I wanted to do something similar in android where I had a pair of values, one an id and the other the user-friendly text to display. So I decided to create an adapter using android.util.Pair:
public class PairView extends Pair<String, String> {
public PairView(String first, String second) {
super(first, second);
}
public String toString() {
return second;
}
}
public class PairAdapter extends ArrayAdapter<PairView> {
}
So now I can put my id in pair.first and what to display to the user in pair.second.
My problem comes in that some of these options will be single-selects and some will be multi-selects. In html, that's not an issue, just use a checkbox group for multi, and radio buttons/dropdowns for single selects. In android however, it seems it's not so straight forward. I tried using Spinners for the adapters, but Spinner seems to only allow single selection. AlertDialog.Builder allows for single and multi-selections, but curiously I don't see an option for using an adapter for the multi-selection, just for single selections.
I guess what I really want is a consistent look for all my options, with radio buttons displayed for single selections and checkboxes displayed for multi selections, via an adapter so I can get the id's from the Pair for the items selected.
What approach should I use? A custom spinner with code added for multi-selections? AlertDialog.Builder and somehow make it use an adapter for multi-selections? Just create a plain Alert and wrap a ListView in it? Another option that is (hopefully) simpler?
I feel like I'm missing something very basic here.
I had a similar situation in an app I was making so would share what I opted for. I had different type of questions and depending on that I removed and added things in my activity. For radio buttons I used with elements in it. For multiple choice questions I wanted a checkbox based view so I added an empty within my layout and in code added CheckBox(s) to it.
As for the caption and value, for radio buttons and checkboxes you can set display text by setText and add any object/value as a tag. So what I used to do was something like this:
CheckBox option = new CheckBox(MyActivity.this);
option.setText("Option 1");
option.setTag(10);
Later on when you get the selected option, you can simply get its tag and use its value.
This is just one way of doing it which I found simple. Hope this helps

Populating data on gridview

In my android app, i have a particular scenario, for one of the screens.
I require 2 button,one on each side of the corner(left and right).
Below this i want to populate data in a control.
If left button is clicked, the control should be a gridview.
If right button is clicked , the control should be a listview.
And accordingly the data should be populated.
How should i approach this scenario.
Should i create controls dynamically, or use xml instead
Rgds
Create the view with two different layouts.
Assume that you have 2 xml layouts named gridLayout.xml and listLayout.xml
and that somehow mode is determined earlier in your code and set to one of two constants GRIDVIEW or LISTVIEW. Than you can use a code fragment like:
private Context m_Context = activity.getBaseContext();
private ViewHolder m_Inflater = LayoutInflater.from(m_Context);
...
if (mode == GRIDTYPE)
viewDisplay = m_Inflater.inflate(R.layout.gridLayout, null);
} else {
viewDisplay = m_Inflater.inflate(R.layout.listLayout, null);

Categories

Resources