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."
Related
I have added the details inside the displayItems activity after user click the item on Listview. The question is how can I make the URL clickable so user can got to that specific website.(The details is called from csv file). Anyone can guide me?
My Listview.java
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(ShoppingMall.this, Displayitem.class);
if (adapter.getItem(position).length > 0) {
intent.putExtra(Displayitem.EXTRA_NAME, adapter.getItem(position)[1]);
intent.putExtra(Displayitem.EXTRA_STATE, adapter.getItem(position)[2]);
intent.putExtra(Displayitem.EXTRA_ADDRESS, adapter.getItem(position)[4]);
intent.putExtra(Displayitem.EXTRA_URL, adapter.getItem(position)[5]);
intent.putExtra(Displayitem.EXTRA_NUMBER, adapter.getItem(position)[6]);
intent.putExtra(Displayitem.EXTRA_MAIL, adapter.getItem(position)[7]);
}
startActivity(intent);
}
});
My displayItem.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displayitem);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
tvMallName = (TextView) findViewById(R.id.tv_mallname);
state = (TextView) findViewById(R.id.states);
mallAddress = (TextView) findViewById(R.id.mall_Address);
url = (TextView) findViewById(R.id.url);
phone_number = (TextView) findViewById(R.id.phone_number);
mail = (TextView) findViewById(R.id.mail);
extras = getIntent().getExtras();
String malladress = extras.getString(EXTRA_ADDRESS,"");
String mallName = extras.getString(EXTRA_NAME, "");
String mallurl = extras.getString(EXTRA_URL,"-");
String mallnumber = extras.getString(EXTRA_NUMBER,"-");
String mallstate = extras.getString(EXTRA_STATE,"");
String mallmail = extras.getString(EXTRA_MAIL,"-");
mallAddress.setText(malladress);
tvMallName.setText(mallName);
state.setText(mallstate);
url.setText(mallurl);
mail.setText(mallmail);
phone_number.setText(mallnumber);
}
Methode 1 :
TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));
Method 2:
use android:autoLink="web" in your TextView's xml. It should automatically convert urls click-able (if found in text)
Also include android:linksClickable="true"
Put this piece of code in onClick method of of textview. This will launch the browser with the specified URL
Intent intent= new Intent(Intent.ACTION_VIEW,Uri.parse(YOUR_URL));
startActivity(intent);
I have used TabActivity i have five tab, in every activity there is database connection
i want that if i move one tab to another tab the activity should not be finish, so if i come back so activities onCreate() method should not call again
my code is following
public class TabSample extends TabActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_main);
setTabs();
}
private void setTabs() {
addTab("", R.drawable.a, A.class);
addTab("", R.drawable.b, B.class);
addTab("", R.drawable.c, C.class);
addTab("", R.drawable.d, D.class);
addTab("", R.drawable.e, E.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);
}
}
You should your proccess running in Background Thread (Async Task) or Service. Only result views in Tab activity. İf tab activity is start, you should fill data with Background Thread..
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.
I have tabHost widget with dynamic created tabs.
In each of newly created tab, there is relative layout with some views (textView, listView etc).
My question is: how to access to these views and how to append new content there? I tried with setting ids or tags, but i didn't work.
public class Main extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("buttontab");
spec.setContent(R.id.form);
spec.setIndicator("Search-form");
tabs.addTab(spec);
Button btn=(Button)tabs.getCurrentView().findViewById(R.id.buttontab);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final TabHost.TabSpec spec=tabs.newTabSpec("tag1");
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag)
{
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(Main.this);
// Defining the RelativeLayout layout parameters.
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
TextView t = new TextView(Main.this);
// t.setId(12); //This dosen't work
t.setText("Searching in progress. Please wait...");
ListView resultsList = new ListView(Main.this);
//Here: array adapter etc
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(t);
relativeLayout.addView(resultsList);
return relativeLayout;
}
});
spec.setIndicator("Tab with results");
tabs.addTab(spec);
new GetData(Main.this).execute();
}
});
}
public void appendResultsToViews(String result)
{
//Here i need to access TextView from tab with specific tag and append results to TextView, which was created dynamically earlier
}
Thank you for your help.
set tag of child view and use findViewWithTag(tag)
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