Simple tab activity in android - java

What i am trying to do::
BreakfastLunchDinnerIndividualListOfItems.java
public class BreakfastLunchDinnerIndividualListOfItems extends TabActivity implements OnTabChangeListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.breakfast_lunch_dinner_individual_list_of_items);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
String REST = getTabHost().toString();
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, BLD_IndividualListOfItems_Starters.class);
//intent.putExtra("Starters", REST);
spec = tabHost.newTabSpec("Starters").setIndicator("Starters").setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, BLD_IndividualListOfItems_MainCourse.class);
//intent.putExtra("MainCourse", REST);
spec = tabHost.newTabSpec("MAIN_COURSE").setIndicator("Main Course").setContent(intent);
tabHost.addTab(spec);
}
public void onTabChanged(String arg0)
{
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(),arg0, Toast.LENGTH_LONG).show();
}
}
I tried with String REST = getTabHost().toString(); problem is that
on click of tab i am not able to send perticular tabtext to the
activity that the tab launches
I know tab activity is depricated, i am just learning
How to resolve this, hope i am clear

spec = tabHost.newTabSpec("Starters").setIndicator("Starters").setContent(intent);
spec = tabHost.newTabSpec("MAIN_COURSE").setIndicator("Main Course").setContent(intent);
here Starters and Main Course are the titles of tabs. The easiest way to send this strings to bounded activities is to send them throught intents which are bounded to corresponding
tabSpecs.
String TAB_TITLE="Starters";
Intent intent = new Intent().setClass(this, BLD_IndividualListOfItems_Starters.class);
Bundle bundle =new Bundle();
bundle.putString("key_title", TAB_TITLE);
intent.putExtras(bundle);
spec = tabHost.newTabSpec("Starters").setIndicator(TAB_TITLE).setContent(intent);
tabHost.addTab(spec);
And here is how you can get this string in BLD_IndividualListOfItems_Starters activity :
protected void onCreate(Bundle savedInstanceState) {
...
String title=getIntent().getExtras().getString("key_title");
}
But as Raghunandan says, it's better to use fragments instead of depracated TabActivity.
EDITED:
if you want to send text to corresponding activity dinamically, i mean exactly after tab is changed - you can:
create and broadcast custom intent with your string. (CUSTOM_INTENT_EXAMPLE)
in BLD_IndividualListOfItems_Starters activity register BroadcastReciever which will catch your custom intent and take text from it. (BROADCAST_RECIEVER_FROM_ACTIVITY_EXAMPLE)
You can't directly access tab text from BLD_IndividualListOfItems_Starters, because TabActivity and BLD_IndividualListOfItems_Starters are two different activities.
But you can send data between activities via bundles, static fields, singletons etc. Here is link to docs
http://developer.android.com/guide/faq/framework.html#3

Related

Multiple buttons in single page navigating each button to different page in android studio

Please solve this problem in android studio, I am new to app development.
I want to create 3 buttons in a single page and navigate each button to each different page.
I need code for java i.e "Mainactivity.java"
I have declared 3 button id's
I have set everything in app manifest.
I am able to navigate only single button at once but how can I arrange all three buttons for navigation?
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonWRGL = (Button)findViewById(R.id.buttonWRGL);
Button buttonHNK = (Button)findViewById(R.id.buttonHNK);
Button buttonKZP = (Button)findViewById(R.id.buttonKZP);
buttonWRGL.setOnClickListener(this);
buttonHNK.setOnClickListener(this);
buttonKZP.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.buttonWRGL:
break;
case R.id.buttonHNK:
break;
case R.id.buttonKZP:
break;
}
}
}
Your question seems unclear, but I suppose that you are asking how to load another layout/activity/fragment by clicking on a button.
Well, it depends on which of the three actions you want to do:
1) in order to load another layout, you need to inflate the new layout in your view; in order to do that you need to clear the actual layout and inflate the new one.
Here's some sample code:
//you may change it to whichever layout you used
LinearLayout ll = (LinearLayout) findViewById(R.id.mainLayout);
//remove previous view
ll.removeAllViews();
//set the new view
setContentView(R.layout.new_layout);
2) in case you want to start a new activity, you need to use a Intent and load it.
Sample code:
//create the new intent; it will refer to the new activity
Intent intent = new Intent(this, NewActivity.class);
//pass any data to the new activity; cancel this line if you don't need it
intent.putExtra(extra_title, extra)
//start the new activity
startActivity(intent);
3) in case you want to change fragment, you need to perform a transaction.
Sample code:
//create the new fragment
Fragment newFragment = new MyFragment();
//start a fragment transaction
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//replace the old fragment with the new
transaction.replace(R.id.frame, newFragment).commit();
Hope this helps; if not, try to edit your question in order to clarify what you mean.
EDIT:
You should add a new OnClickListener to each button, but I would do it differently than you are doing right now.
I would do something like this sample code:
buttonWRGL.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(this, NewActivity1.class);
startActivity(intent);
}
});
For each button. In this code, I directly attach a specific OnClickListener to the button; it will contain the intent that you need.
You can copy this in every button, even 10k buttons, you just need to change the activity name inside the intent declaration with the activity that you want to launch.

Using Intent in Android

I am currently learning Android app development and I am a bit confused on how to use Intent. I am trying to make a "To Do list" app. My problem right now is that I want to be able to tap the item in my to do list to go to a Edit Item page.
Here is what I have so far.
ToDoActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do);
etNewItem = (EditText) findViewById(R.id.etNewItem);
lvItems = (ListView) findViewById(R.id.lvItems); // now we have access to ListView
readItems(); // read items from file
todoAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems); //create adapter
lvItems.setAdapter(todoAdapter); // populate listview using the adapter
setupListViewListener();
setupEditItemListener();
}
The activity I want to launch is called EditItemListener. These are the two functions that I am playing Intent with. Right now I am just testing how to display the EditItemActivity.
private void launchEditItem() {
Intent i = new Intent(this, EditItemActivity);
startActivity(i);
}
private void setupEditItemListener() { // on click, run this function to display edit page
lvItems.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
launchEditItem();
}
});
}
You should specify it as a class
Intent i = new Intent(this, EditItemActivity.class);
Also if you want to kill the current activity
use finish() after StartActivity()
on a listview set the OnItemClickListener and on the listener do this
Intent i = new Intent(this, YourActivity.class); //where you are (this) and where you go (YourAcitivity.class)
startActivity(i); //Now GO!
The intent will start another activity, this activity must be declared on AndroidMainfest.xml

getTabHost() method in undefined

I am making tabs in android. I am using TabHost to achieve this. I am getting red line under getTabHost() method on this line TabHost tabHost = getTabHost();. I am not sure why am I getting this. How can I make this example work?
Here is my complete code
public class TabActivity extends Activity {
//TabHost mTabHost;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
//Artist Tab
intent = new Intent(this, Artist.class);
spec = tabHost.newTabSpec("artists").setIndicator("Artist", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
tabHost.addTab(spec);
//Songs
intent = new Intent(this, Songs.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
tabHost.addTab(spec);
//Albums
intent = new Intent(this, Album.class);
spec = tabHost.newTabSpec("artists").setIndicator("Artist", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
}
}
you need to extend MyTabActivity not simple activity
try
public class MyTabActivity extends TabActivity
intead of
public class MyTabActivity extends Activity

Reload tab in android

I have tabhost in my application(2 tabs),when i select tab2 the content has been dislayed has a list view ,again i have list click event here to move further another list view,here the 2tab already in selected mode and it's focus not been changed,when i click the 2tab again i need it to reload its content has loaded initially with first list view content.how can i get it.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("OPT")
.setContent(new Intent(this, TabGroup1Activity.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("EDIT")
.setContent(new Intent(this, TabGroup2Activity.class)));
tabHost.setCurrentTab(1);
}
You could also re-issue another intent to refresh the tab
try public void onResume() {} method

How to initialize tab content when app is initialized

I have an app that has 2 tabs. Each tab loads an xml file (fairly large, maybe 400 item rss file).
By default the tab doesn't get the xml until it's clicked on. I simply wanted a way to load it all when the app is first opened.
Here is the main view:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Audio Feed
intent = new Intent().setClass(this, AudioFeed.class);
spec = tabHost.newTabSpec("audio").setIndicator("",
res.getDrawable(R.drawable.ic_tab_audio))
.setContent(intent);
tabHost.addTab(spec);
// Video Feed
intent = new Intent().setClass(this, VideoFeed.class);
spec = tabHost.newTabSpec("video").setIndicator("",
res.getDrawable(R.drawable.ic_tab_video))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0); //todo: remember what tab user was last on
}
I use TabHost.setCurrentTab(x) or TabHost.setCurrentTabByTag(x).
mTabHost.setCurrentTab(1);
mTabHost.setCurrentTab(0);
For Initialization I use TabHost.OnTabChangeListener.
private OnTabChangeListener mOnTabChangeListener = new OnTabChangeListener() {
#Override
public void onTabChanged(String tag) {
if (FBIntent.EXTRA_XX.equals(tag)) {
// Current tab is xx.
... if xx not init -> ...
} else if (FBIntent.EXTRA_YY.equals(tag)) {
// Current tab is yy.
...
}
}
};
In this same method do whats the other tabs do. I mean what the other do when you click them. So you have the same thing for all tabs. Don't forget to include this same tab in the OnTabChanged() method and do the same operations again, otherwise you'll get nothing when this tab is clicked again.

Categories