In my application, I'm creating dynamic tabs.Inside framelayout I've created a edittext.For
each tab i create content of tab will be edittext. When i type in editext of one tab and later if i create a new tab and when I switch between these tabs, it shows the content of editext as empty.
Update:
I don't want to create a separate activity for each tab.I'l explain along with the code.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TabHost tabs = (TabHost)this.findViewById(R.id.tabs);
tabs.setup();
TabHost.TabSpec spec1=tabs.newTabSpec(f);
spec1.setContent(R.id.content1);
spec1.setIndicator(f);
tabs.addTab(spec1);
tabs.setCurrentTab(i);
list.add(spec1);
i++;
Addbtn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
final TabHost tabs = (TabHost)this.findViewById(R.id.tabs);
tabs.setup();
TabHost.TabSpec spec1=tabs.newTabSpec(f);
spec1.setContent(R.id.content1);
spec1.setIndicator(f);
tabs.addTab(spec1);
tabs.setCurrentTab(i);
list.add(spec1);
i++;
});
Default one tab will be there. here content of tab is edittext. When i click on button new tab will be created. problem is with the content of tab. In default tab if i type in edittext, and create a new tab and switch to the default tab the content of default would be lost.
You need to store the value of the EditText in some manner. A good way is to show tab content through an activity and use this method to store instance state.
You can use an EditorActionListener, which is called when someone presses the 'Enter' or IME_ACTION key to store the values of your EditText.
Instead of doing spec1.setContent(R.id.content1);, create an EditText dynamically (new EditText(context)) and put a number (Tab Number) as a Tag, which you can retrieve and store in a List.
mEditText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
int position = (int) v.getTag();
String s = ((EditText) v).getText()
// store in an object
return false;
}
});
Not sure how you will retrieve this without launching subActivities!
Related
Hi I have a program with several fragments in one activity. The menu fragment lets the user switch to one of the other fragment layouts. I have many buttons on the menu fragment layout that change text when pressed.
I want this text to remain the same when I navigate back from one of the other fragments but it goes back to its default text while the spinners hold their position. Since the fragment only stopped not destroyed I thought the buttons would hold there text? Sorry if it's something obvious, I'm new to programming.
here is some out my code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.options_menu, container, false);
Channel0 = (Button)rootView.findViewById(R.id.Channel0);
OpenChannel0 = (Button)rootView.findViewById(R.id.Channel0Open);
OpenChannel0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ( OpenChannel0.getText().equals("Open") )
{
Channel0Spinner.setEnabled(false);
OpenChannel0.setText("Close");
String text = Channel0Spinner.getSelectedItem().toString();
Channel0.setText("Channel 0 ("+ text +")");
else
{
Channel0Spinner.setEnabled(true);
OpenChannel0.setText("Open");
}
}
});
Channel0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
f = new Channel0frag();
FragmentTransaction frag = getFragmentManager().beginTransaction();
frag.replace(R.id.container, f);
frag.addToBackStack(null);
frag.commit();
}
}
});
So when I navigate back from Channel0frag, I want buttons Channel0 and OpenChannel0 to keep whatever text is set.
UPDATE:
When I enter the following code in the onResume method, it still outputs the original button text when I navigate back from another fragment even though the spinner does not lose its position. Why is Channel0 button text not getting assigned to the text displayed in the spinner?
String text = Channel0Spinner.getSelectedItem().toString();
Channel0.setText("Channel 0 ("+ text +")");
maintain global variable in your application class and override onResume on fragment class.
I Created a boolean variable and set it to true then OpenChannel0 button text was "Open" and checked the state of it and set the button text in onResume. For OpenChannel0 I used Shared Preferences to save the text state as there was several different text states for this button.
I have TabActivity which holds 4 tabs.
the tabs created in the tabhost activity like this
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
Intent send_names_intent1 = new Intent(this,tab1.class);
//here is some data I receive it from previous activity
//and want to send them to the tab
send_names_intent1.putExtra("names", names);
send_names_intent1.putExtra("check", test1);
firstTabSpec.setIndicator("Kingdom I").setContent(send_names_intent1);
tabHost.addTab(firstTabSpec);
in every tab the user do some work and the result will displayed in the tab,
the problem is that when switching to the 2nd tab and then go back to the 1st tab all the results will gone and the tab will be created again.
NOTICE : I tried to use the getsharedprefrences() but it will loads the saved data even if the application closed and opens again.
You can use a boolean value boolean test; and int value int last=0 then use this method
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
int currenttab = tabHost.getCurrentTab();
if (currenttab != last){
test[last] = false;
send_names_intent [last].putExtra("check", test[last]);
last = currenttab;
}
}
});
when you use this method you will have a boolean value in your tab1.class
Intent names_intent = getIntent();
prefcheck = names_intent.getBooleanExtra("check", false);
then check the prefcheck value if (prefcheck == false) save the SharedPreferences.
Have you tried onSaveInstanceState?
protected void onSaveInstanceState(Bundle icicle) {
super.onSaveInstanceState(outState);
icicle.putString("names", names);
}
You say you used shared preferences right? Why not have it so that when a tab is opened, it increments a value of a shared preference by one, and om destroy decrements it, and then checks to see if the value is 0, if so it deletes all the save preferences you want by setting them to the default value, or the value you want.
In my project, I have two tabs and a button. For two tabs,I have two activities and button calling different activity. the thing is I am showing result of button on first tab. i.e tab0 is active on tab0Event and on button click event too. And am able to change the tab events using tabHost.setOnTabChangedListener, but now what i further want is, say suppose i click on button so now button view is displaying(tab0 is active) but again if i click on tab0, tab0 activity should be displayed.
I tried many solutions for clicking on tab, one is
getTabWidget().getChildAt(getTabHost().getCurrentTab()).setOnClickListener
(new View.OnClickListener() {
#Override public void onClick(View v) {
System.out.println(getTabHost().getCurrentTab());
} });
But when i used this code with tabChnageListner, tab change not working and i got very unexpected results.
Would you please suggest solution for my problem.
Thanks.
code that is working for tab changed is as: (working fine for tab change, need to add Tab Onclick in it)
public class TabLayoutUsingTabChangeEventActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
final TabHost.TabSpec sp1 = tabHost.newTabSpec("TAB1");
TabHost.TabSpec sp2 = tabHost.newTabSpec("TAB2");
//Creating First Tab
Intent intent1 = new Intent(this, Tab1Activity.class);
sp1.setIndicator("TAB1").setContent(intent1);
tabHost.addTab(sp1);
//Creating Second Tab
Intent intent2 = new Intent(this, Tab2Activity.class);
sp2.setIndicator("TAB2").setContent(intent2);
tabHost.addTab(sp2);
//Tab Changed Event
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
Log.i("TabId :", tabId);
if(tabId.equals("TAB2")){
Log.i("TAB1", "TAB1 Changed");
Intent intent1 = new Intent().setClass(getApplicationContext(), Tab1Activity.class);
sp1.setIndicator("TAB1").setContent(intent1);
tabHost.setCurrentTab(0);
}
}
});
Button addNewButton = (Button)findViewById(R.id.add_new_ticket_btn);
addNewButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent in = new Intent().setClass(getApplicationContext(), AddNewTicketActivity.class);
sp1.setContent(in);
tabHost.setCurrentTab(0);
//startActivity(in);
}
});
}
}
You can implement this listener:
host.setOnTabChangedListener(new OnTabChangeListener()
{
#Override
public void onTabChanged(String tabId)
{
if (getTabHost().getCurrentTabTag().equals("tab0"))
{
host.getCurrentTabView().setOnTouchListener(
new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
if (getTabHost().getCurrentTabTag().equals("tab0")
{
getTabHost().setCurrentTabByTag("tab1");
getTabHost().setCurrentTabByTag("tab0");
}
return false;
}
return false;
}
});
}
}
});
Also when you add tabs set tag for every tab:
host.addTab(host.newTabSpec("tab0"));
host.addTab(host.newTabSpec("tab1"));
You can used Fragment to layout
You cant. Views in TabWidget already have onClickListener and it is required fo normal workflow. If you remove it you will break TabWidget's logic.
When you set your onClickListener you remove previuos onClickListener and you break TabWidget logic.
Usually in such cases compound click listener is created (a click listener which handles click event and calls another click listener). But not in your case because there is no way to get old click listener because View doesn't have getOnClickListener method.
This is TabWidget source code. All related values are private... so we can't fix anithing from this side.
The only way to achieve your goal is a hack with Reflections because they allows to read private vars. So before set View's new onlick listener you should get old one (using Reflections), then create compound onClickListener which will do your event handling code and then call old onClickListener. Set compound click listener to the View.
hi can you tell me how to disable a tab in the UI of android code.. (eclair code)
If you mean to disable one tab button on TabWidget, then try this code:
// tabHost = ... (get TabHost)
tabHost.getTabWidget().getChildTabViewAt(your_index).setEnabled(false);
If you want to disable tab widget in overall, then:
// tabWidget = ... (get TabWidget)
tabWidget.setEnabled(false);
Read SDK Help for references:
TabHost
TabWidget
Extend TabHost and override methods:
#Override
public void setCurrentTab(int currentTab) {
if (currentTab != 2) // position of the tab that should not get selected
super.setCurrentTab(currentTab);
else
// in my case I want to trigger something here but I don't want the button to get selected
}
#Override
public void setCurrentTabByTag(String tag) {
if (!"\"plus_tab\"".equals(tag)) // tag of the tab that should not get selected
super.setCurrentTabByTag(tag);
else
// in my case I want to trigger something here but I don't want the button to get selected
}
Greetings,
I am trying to get the Click - event when clicking on the currently selected tab of my TabActivity. The onTabChangedHandler is only called whenever the tab is changed, not if the currently active Tab is clicked. The debugger tells me i have the onClickListener Registered for the TabWidget within my TabHost.
Am i registering for the wrong View?
Also, I am unable to create a Context Menu for the Tabs, only for its content, is this problem related?
public class TestDroidViewTab extends TabActivity
implements TabContentFactory
, OnTabChangeListener, OnClickListener {
private static final String LOG_KEY = "TEST";
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
TabHost.TabSpec ts = tabHost.newTabSpec("ID_1");
ts.setIndicator("1");
ts.setContent(this);
tabHost.addTab(ts);
ts = tabHost.newTabSpec("ID_2");
ts.setIndicator("2");
ts.setContent(this);
tabHost.addTab(ts);
ts = tabHost.newTabSpec("ID_3");
ts.setIndicator("3");
ts.setContent(this);
tabHost.addTab(ts);
tabHost.setOnClickListener(this);
tabHost.setOnTabChangedListener(this);
}
public void onClick(View v) {
Log.d(LOG_KEY, "OnClick");
}
public void onTabChanged(String tabId) {
Log.d(LOG_KEY, "OnTabChanged");
}
If you want to see that a particular tab is clicked, you need to add your listener to the tab itself, not the TabHost.
The hierarchy of views in a tab implementation is:
TabHost
TabWidget
(tab)
(tab)
FrameLayout
The tabs are added at runtime by calling: tabHost.addTab(tabHost.newTabSpec(""));
You can then get a handle to the individual tabs by calling: getTabWidget().getChildAt(4);
In essence, you are adding your OnClickListener to a child of the TabWidget. You can now pick up the clicks on your individual tab. However, this will override the default behavior which changes the content when a tab is clicked. So, to get your content to change, your OnClickListener will need to do that for you.
Here is a full example, which lets you intercept the click event, and change the content below the tab:
final String myTabTag = "My Tab";
final int myTabIndex = 3;
getTabHost().addTab( getTabHost().newTabSpec(myTabTag) );
getTabWidget().getChildAt(myTabIndex).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (getTabHost().getCurrentTabTag().equals(myTabTag)) {
getTabHost().setCurrentTab(myTabIndex );
}
}
});
use setOnTabChangedListener instead of OnClickListener ;)
static TabHost tabHost;
tabHost = getTabHost();
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
Log.i("******Clickin Tab number ... ", "" + tabHost.getCurrentTab());
}
});
Your clause is wrong, use:
...
if (getTabHost().getCurrentTabTag().equals(myTabTag) == false) {
getTabHost().setCurrentTab(myTabIndex );
}
...
into my code, it shows some errors and ask me to create new methods in
those names like getTabWidget(), getTabHost(), etc. Waiting for your
response.
Try this
tabHost.getTabHost().setCurrentTab(myTabIndex);