TabActivity and navigation inside Android - java

I have done TabActivity with this code:
addTab(publication, "First", My_Files.class);
addTab(shop, "Second", Others.class);
private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost(); // The activity TabHost
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
In these first and second tabs, I have ListViews with some context where user press it and it goes to other Activity with this code:
Intent intent = new Intent("android.app.reader.FILES");
intent.putExtra("publicatonName", "fileName");
startActivity(intent);
But there disappear my TabActivity, but I still want to use it like iPhone
UITabBar with UINavigationController
So now I am thinking that when I go to other Activity I need to change current TabActivity button class target to current and somehow my Tab Bar Activity should be visible.
Maybe someone could help me.
Thanks.

ActivityGroup, which will be the container of your other Activities. When the user clicks one of the buttons, you'd get a reference to the LocalActivityManager, and use it to start, and embed the inner activity.
get some experience from here and also

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.

Refresh an Android tab in tabHost

i have a tabHost with 3 tabs and each of them has a youtube video embeded in it. Each time i click a tab it gives another video in the tab view as such:
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
TabSpec tab1 = tabHost.newTabSpec("Chilling");
TabSpec tab2 = tabHost.newTabSpec("Shit");
TabSpec tab3 = tabHost.newTabSpec("Gaming");
tab1.setIndicator("Chilling");
tab1.setContent(new Intent(this, ChillScreen.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tab2.setIndicator("Shit");
tab2.setContent(new Intent(this, SadScreen.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tab3.setIndicator("Gaming");
tab3.setContent(new Intent(this, GamingScreen.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
Now in the activity that creats these tabs i have a button and i want it to refresh the selected tab and i do this by:
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String current_tab = tabHost.getCurrentTabTag();
if (current_tab.contains("Chilling")) {
;}
The button checks the tab selected and i want it to refresh said selected tabView. How can i do that?
I know tabHost is deprecated but my app is almost ready and i wish to finish it like this:)
thank you very much in advance

how to set custom tabhost style without TabActivity

I set tab without TabActivity,
so my tabHost always has an error.
please tell me how to do.
thanks.
private void setupTab(Class<?> ccls, String name, String label,
Integer iconId) {
Intent intent = new Intent().setClass(this, ccls);
View tab = LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
ImageView image = (ImageView) tab.findViewById(R.id.icon);
TextView text = (TextView) tab.findViewById(R.id.text);
if (iconId != null) {
image.setImageResource(iconId);
}
text.setText(label);
TabSpec spec = tabHost.newTabSpec(name).setIndicator(tab)
.setContent(intent);
tabHost.addTab(spec);
}
i read Android Tab-Host
to earn
It can't build.
i'm run error by tabHost cannot be resolved.

Start Android App in a specific tab

The question is: I have a TabHost with 4 tabs (see code below) and I got a Button in MainMenuActivity class. The Button is set up with a OnClickListener and if it is clicked I want it to go to the second tab. I have tried with setCurrentTab(1) but that just messed the project up. What can I do?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTabs() ;
}
private void setTabs()
{
addTab("Home", R.drawable.tab_home, MainMenuActivity.class);
addTab("Calculate", R.drawable.tab_search, SpinnerClass.class);
addTab("Search", R.drawable.tab_home, ScrollView1.class);
addTab("Premium", R.drawable.tab_search, ScrollView2.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
tabHost.setCurrentTab(index) is the right way to go. What's the problem when you use it?
"setCurrentTab(int) opens the tab to be displayed by default, specified by the index position of the tab."

How to determine if a user clicked on a tab inside of a TabHost?

I have a TabHost with two tabs. I want to know when the user clicks on either tab. How can I achieve this?
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
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Main.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("watchlist").setIndicator("Watchlist",res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, ARActivity.class);
spec = tabHost.newTabSpec("trending").setIndicator("Trending",res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
I think you want to use an onTabChangeListener (http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html) and pass that to the TabHost. See http://developer.android.com/reference/android/widget/TabHost.html#setOnTabChangedListener%28android.widget.TabHost.OnTabChangeListener%29
You can effectively get notified every time someone switches a tab.

Categories