Dynamically adding a whole row in GridView, or splitting the View - android

I have a GridView with 4 columns, when one is selected, I wanted to dynamically add a whole row underneath the selected cell, and inflate a layout in it, so that I could add some information about the selected cell. Is there anyway to do that? Or maybe, is there a way to split a view in half, then glue it back together when done? Sounds crazy.
Essentially, I'm looking for something you can find on iTunes 11 (see the picture below).

Yeah. You can do that. the xml file where you defined grid view, below that define one linear layout too and make it invisible by adding following code:
android:visibility="invisible"
On click the GridView, inflate that layout and set its visibility and use following java codes:
LinearLayout l2=(LinearLayout)findViewById(R.id.linearLayout);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View detailView = inflater.inflate(R.layout.yourInformationLayoutOfGridItem, l2, false);
for set visibility true/false by java code:
Visibile: l2.setVisibility(0);
Invisible: l2.setVisibility(4);

The Solution to your question is to simple use TabHost widget:
The XML file to that is as below:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
And The java code for that is as below:
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Photos");
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, PhotosActivity.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Songs");
// setting Title and Icon for the Tab
songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
Intent songsIntent = new Intent(this, SongsActivity.class);
songspec.setContent(songsIntent);
// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Videos");
videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab));
Intent videosIntent = new Intent(this, VideosActivity.class);
videospec.setContent(videosIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos tab
}
Hope this works fine for you.. And hope this is what you asked for.

Related

Tabs in Android not working correctly?

I have created 4 tabs in my application using tabHost ,
it working fine.
below i show my code for adding only 2 tabs.
public class Home_tab extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_main);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
// Android tab
Intent intentAndroid = new Intent().setClass(this, CoalActivity.class);
TabSpec tabSpecAndroid = tabHost
.newTabSpec("Android")
.setIndicator("", ressources.getDrawable(R.drawable.tab_dis))
.setContent(intentAndroid);
// Apple tab
Intent intentApple = new Intent().setClass(this, EnergyActivity.class);
TabSpec tabSpecApple = tabHost
.newTabSpec("Apple")
.setIndicator("", ressources.getDrawable(R.drawable.tab_foc))
.setContent(intentApple);
// add all tabs
tabHost.addTab(tabSpecAndroid);
tabHost.addTab(tabSpecApple);
}
}
This is my xml file
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:scrollbarAlwaysDrawHorizontalTrack="true"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
But my problem is when i add more tabs, it comes in the same window.
for example, currently i added 4 tabs, when i try to add 3 more tabs , all the tabs are coming in the same window? i want to add only 4 tabs in the same window, other tabs should appears only when i scroll the tab bar
How to solve this??
Check following links and let me know if they helps in solving your problem
Link1
Link2
link3

How to load all the tabs of a tabbar simultaneously in Android

I have four tabs in tabbar. Whenever I launch my application I need to run all the tabs (four) of a tabbar, because all the tabs contains webviews that should be loaded whenever the app is launched. But the first tab should gets highlighted. I have used
setCurrentTab(0);
to load the first tab,but iam unable to load remaining tabs without clicking.
To do this, first create a xml file. The sample file is shown as below.
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/background">
<TabWidget android:layout_width="fill_parent" android:id="#android:id/tabs"
android:layout_height="wrap_content" />
<FrameLayout android:layout_width="fill_parent" android:id="#android:id/tabcontent"
android:layout_gravity="bottom" android:paddingTop="70dp"
android:layout_height="match_parent">
<ScrollView android:layout_height="fill_parent" android:id="#+id/viewTab1"
android:layout_width="match_parent">
<!-- Your view you want -->
</ScrollView>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/viewTab2">
<!-- Your view you want -->
</LinearLayout>
<!-- Add as many view you want with unique id -->
</FrameLayout>
Each layout will refer to a unique layout.
In main activity use following
TabHost tabHost = getTabHost();
TabSpec spec1 = tabHost.newTabSpec("FirstTabView");
TabSpec spec2 = tabHost.newTabSpec("SecondTabView");
spec1.setContent(R.id.viewTab1);
spec1.setIndicator("First Tab");
spec2.setIndicator("Second Tab");
spec2.setContent(R.id.viewTab2);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.setup();
You can add as many tabs needed. And this all will be initialized at same time.
I hope this helps you :)

Android TabHost content not showing

I've created a TabHost and assigned 4 activity intents with tabs and these seems to work just fine. My only problem is that the activity content is not showing within the framelayout #tabcontent in my tabhost view.
I've followed the official reference and scanned the internet for a solution but I can't see what the problem is.
The Log.v("Activity", "Reports") is logged in ant, so it does execute the activity. Therefore I'm guessing its the setContentView() in my ReportsActivity that is causing the problem. But I'm new to this so I can't really tell. (There are no generated errors)
This is my tabhost
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/tabhost"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5sp"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
This is how I add tabs in my TabActivity
// Glob
Intent intent;
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Resources res = getResources();
// Reports tab
intent = new Intent().setClass(this, ReportsActivity.class);
spec = tabHost.newTabSpec("reports")
.setIndicator(
res.getText(R.string.reports),
res.getDrawable(R.drawable.reports))
.setContent(intent);
tabHost.addTab(spec);
And this is my content activity (R.layout.reports = linearlayout with a textview)
public class ReportsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reports);
Log.v("Activity", "Reports");
}
}
Try implementing your TabSpec like this:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
spec = tabHost.newTabSpec("reports")
.setIndicator(
res.getText(R.string.reports),
res.getDrawable(R.drawable.reports))
.setContent(intent);
This is because you have chosen a LinearLayout which defaults to Horizontal layout.
If you set android:orientation="vertical" inside the LinearLayout tag that should fix your problem

Tabview to follow all activities

Im new to android and i am trying to develop a simple application with tabhost. And i want the tabhost to be in all of my activites. I have 5 tabs and 6-7 activites, and i want them all to be shown in those five tabs.
Here i create my tabactivity:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#android:id/tabs" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</TabHost>
And here i add tabs:
TabSpec tabSpec = mTabHost.newTabSpec("tab_test1");
tabSpec.setIndicator("Map",res.getDrawable(R.drawable.world));
Context ctx = this.getApplicationContext();
Intent i = new Intent(ctx, DealFinderActivity.class);
tabSpec.setContent(i);
mTabHost.addTab(tabSpec);
mTabHost.getTabWidget().setBackgroundColor(Color.GRAY);
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator
("List Deals",res.getDrawable(R.drawable.database))
.setContent(new Intent(this,ListDeals.class)));
// TextView textView = (TextView) findViewById(R.id.textview3);
// textView.setText("hello");
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("Favorites",
res.getDrawable(R.drawable.heart)).setContent(
new Intent(this, ListDeals.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test4").setIndicator("Settings",
res.getDrawable(R.drawable.preferences)).setContent(
new Intent(this, DealDescription.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test5").setIndicator("About",
res.getDrawable(R.drawable.newspaper)).setContent(
new Intent(this, DealDescription.class)));
And in the ListDeals.class i use this to change to a new activity:
Intent myIntent = new Intent(getApplicationContext(),
DealMyAss.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(myIntent);
But here the tabview disappears??
Any ideas how i can keep the tabview??
Thanks in advance
ActivityGroup is what you are looking for .
as name suggest it ambeds multiple activities within a tab .
go through examples . one is here .
Create one ActivityGroup for every tab .

Changing view inside TabHost (one activity, multiple views)

right now I'm using in my application tabhost with single activity and separate views.
Tab Activity:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabs = (TabHost) findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec = tabs.newTabSpec(TAB_HOTELS);
spec.setContent(R.id.tab1);
spec.setIndicator("Hotels");
tabs.addTab(spec);
spec = tabs.newTabSpec(TAB_LAST_MINUTE);
spec.setContent(R.id.tab2);
spec.setIndicator("Last Minute");
tabs.addTab(spec);
Layout
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/tab1"
android:orientation="vertical">
<ListView android:id="#+id/listaLocalita"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/tab2"
android:orientation="vertical" android:paddingTop="60px">
<TextView android:layout_width="fill_parent"
android:layout_height="100px" android:text="This is tab 2"
android:id="#+id/txt2" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
And things are working quite well as long as i'm using one level depth. Unfortunately now I need to make one of the tab to work like following:
Tab loading ListView with the list of cities, User clicks on city name, tab's content is replaced with the list of the Hotels, user choose a hotel and always in the the same tab, hotel's info is loaded.
How can i achieve this scenario? LayoutInflater?
Thanks in advance.
you can use intent to set Activity as your tab content, and this activity can call other activities, either listView or any other.
/** tid1 is firstTabSpec Id. Its used to access outside. */
TabSpec MainTab = tabHost.newTabSpec("tag1");
TabSpec SearchTab = tabHost.newTabSpec("tag2");
TabSpec MessageTab = tabHost.newTabSpec("tag3");
TabSpec ServicesTab = tabHost.newTabSpec("tag4");
TabSpec SettingsTab = tabHost.newTabSpec("tag5");
MainTab.setIndicator("Main", res.getDrawable(R.drawable.anchor_36));
MainTab.setContent(new Intent(this, MainView.class));
SearchTab.setIndicator("Search", res.getDrawable(R.drawable.clock_36));
SearchTab.setContent(new Intent(this, SearchView.class));
MessageTab.setIndicator("Messages", res
.getDrawable(R.drawable.dialog_36));
MessageTab.setContent(new Intent(this, MessagesView.class));
ServicesTab.setIndicator("Services", res
.getDrawable(R.drawable.cloud_36));
ServicesTab.setContent(new Intent(this, ServicesView.class));
SettingsTab.setIndicator("Settings", res
.getDrawable(R.drawable.settings_36));
SettingsTab.setContent(new Intent(this, SettingsView.class));
/** Add tabSpec to the TabHost to display. */
tabHost.addTab(MainTab);
tabHost.addTab(SearchTab);
tabHost.addTab(MessageTab);
tabHost.addTab(ServicesTab);
tabHost.addTab(SettingsTab);
Now in the loaded activity you can use the code like
StartActivity(new intent(currentActivity.this, newActivity.class));
this will work for you.

Categories

Resources