Tabs doesn't take full width of the screen - java

I am creating tabs inside the fragment class. Right now i am showing two tabs in the fragment class. Everything works fine and tabs are shown properly. Only cache is that, the tabs shown only take half of the screen width, It doesn't take the full screen width.
So anyone tell me what i need to change in my code to achieve that
My Code
tabsinfo_fragment.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="#EFEFEF" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</HorizontalScrollView>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="#+id/tab_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="#+id/tab_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</LinearLayout>
</TabHost>
tab.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:background="#drawable/tab_selector">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#drawable/tab_text_selector"
android:textIsSelectable="false" />
</LinearLayout>
code inside fragment class
public class TabsInfoFragment extends Fragment implements OnTabChangeListener
{
private View m_Root;
private TabHost m_TabHost;
private int m_CurrentTab;
public String m_VisitorTabText;
public String m_FeedTabText;
public TabsInfoFragment()
{
}
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
m_VisitorTabText = Integer.toString(R.string.tab_visitor_text);
m_FeedTabText = Integer.toString(R.string.tab_feed_text);
m_Root = inflater.inflate(R.layout.tabsinfo_fragment, null);
m_TabHost = (TabHost) m_Root.findViewById(android.R.id.tabhost);
setupTabs();
return m_Root;
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
m_TabHost.setOnTabChangedListener(this);
m_TabHost.setCurrentTab(m_CurrentTab);
// Manually start loading stuff in the first tab
updateTab(m_VisitorTabText, R.id.tab_1, new ProfileInfoFragment());
}
private void setupTabs()
{
m_TabHost.setup();
m_TabHost.addTab(newTab(m_VisitorTabText, R.string.tab_visitor_text, R.id.tab_1));
m_TabHost.addTab(newTab(m_FeedTabText, R.string.tab_feed_text, R.id.tab_2));
}
private TabSpec newTab(String tag, int labelId, int tabContentId)
{
View indicator = LayoutInflater.from(getActivity()).inflate(R.layout.tab, (ViewGroup) m_Root.findViewById(android.R.id.tabs), false);
((TextView) indicator.findViewById(R.id.text)).setText(labelId);
TabSpec tabSpec = m_TabHost.newTabSpec(tag);
tabSpec.setIndicator(indicator);
tabSpec.setContent(tabContentId);
return tabSpec;
}
#Override
public void onTabChanged(String tabId)
{
if (m_VisitorTabText.equals(tabId))
{
updateTab(tabId, R.id.tab_1, new ProfileInfoFragment());
m_CurrentTab = 0;
return;
}
if (m_FeedTabText.equals(tabId))
{
updateTab(tabId, R.id.tab_2, new ProfileInfoFragment());
m_CurrentTab = 1;
return;
}
}
private void updateTab(String tabId, int placeholder, Fragment fragmentClass)
{
FragmentManager fm = getFragmentManager();
if (fm.findFragmentByTag(tabId) == null)
{
fm.beginTransaction().replace(placeholder, fragmentClass, tabId).commit();
}
}
}
ScreenShot

The TabWidget class is a subclass of LinearLayout, so in the tab.xml file for the root xml element, in your case a LinearLayout, set the width to 0dp and the weight to 1, then all the tabs will be equal width. Like so:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:gravity="center">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="This is a tab" />
</LinearLayout>

Try changing your TabWidget to this instead;
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

try this
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+android:id/tab_1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<FrameLayout
android:id="#+android:id/tab_2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</TabHost>

Try removing HorizontalScrollView and adding android:layout_width="fill_parent" to TabWidget

Need to add one single line
tabs.setDistributeEvenly(true);

Related

Center App Bar title on Android

I'm trying to create my own custom app bar with a center-aligned title. Android Studio's editor shows my title is properly centered, but it is not on my actual device.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.encounter_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.encounter_menu_bar_layout);
}
encounter_menu_bar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="#+id/encounter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/untitled"
android:textSize="20sp"
android:textColor="#color/titleTextColor"
android:textStyle="bold"
android:layout_gravity="center" />
</LinearLayout>
encounter_menu_bar.xml doesn't have anything in it:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
</menu>
EDIT
A lot of you are recommending that I change android:layout_gravity to android:gravity. I've tried that as well and I'm still not getting it to line up properly.
I'm enabling the app bar to show up on a specific fragment, as shown here:
EncounterFragment.java
public class EncounterFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_encounter, container, false);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.encounter_menu_bar, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO: This
return super.onOptionsItemSelected(item);
}
}
I also forgot to mention that I'm attaching the menu in a fragment. This is its layout:
fragment_encounter.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/encounter_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</LinearLayout>
Try adding android:gravity="center".
This happens because you have to center the whole text and not just the layout
Use android:layout_gravity="center"
Example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
NOTE: layout_gravity, not gravity
In your case:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="#+id/encounter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/untitled"
android:textSize="20sp"
android:textColor="#color/titleTextColor"
android:textStyle="bold"
android:layout_gravity="center" />
</LinearLayout>
Change the textview to this
make it match parent for width, And make gravity center to center the text
<TextView
android:id="#+id/encounter_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/untitled"
android:textSize="20sp"
android:textColor="#color/titleTextColor"
android:textStyle="bold"
android:gravity="center" />
You want to change your xml view to this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
#this should be horizontal, not vertical.
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="#+id/encounter_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/untitled"
android:textSize="20sp"
android:textColor="#color/titleTextColor"
android:textStyle="bold"
/>
To start, you need to make your linear layout horizontal, not vertical. By making it vertical, you are aligning it to the left.
Then, you need to make sure that your textview width and height match the parent, rather than just wrap the content. Then you can use the gravity attribute to make sure that the text is centered. When you used gravity before, the textview simply wrapped the content, and thus it didn't center in the textview. By matching the parent view bounds, you will be able to then center the text as you need to.
This is how your Toolbar XML should be
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:textColor="#android:color/white"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_gravity="center"
/>
</android.support.v7.widget.Toolbar>
Then in your Activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);

Java Android : onItemLongClick for listView doesn't respond

I'm developing an Android application.
I want to display a list of contacts (using listview) and I want to add 2 alerts dialogs: one by onItemClickListener and the other by onItemLongClickListener.
But the onItemLongClickListener is never called, just the onItemClickListener.
I already did it for another application, but here it doesn't work and I don't find why :/
My code:
public class ContactActivity extends AppCompatActivity {
private ListView listView;
private ContactAdapter adapter; //my adapter
private ArrayList<Contact> contacts; //my list of contacts
//...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
//here i init my "listView" and "contacts"
adapter = new ContactAdapter(this, contacts);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("longClick", "ok");
return true;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> a, View v, final int position, long id) {
//here i display a Dialog
}
});
//some code
So, when I "simple click" on an item of the list, the dialog appears (ok).
But when I "long click" on an item of the list, the dialog appears and i don't see the Log...
(The item that i use for my ListView got just 2 EditText)
PS: the XML where my listView is shown
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#color/white">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:id="#+id/screen">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:background="#drawable/w_aff2" />
</FrameLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="20"
android:weightSum="100">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="80">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="14dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_back"
android:layout_gravity="center"
android:background="#drawable/grey_button"
android:src="#drawable/i_back_blue" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="14dp">
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="14dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_add"
android:src="#drawable/i_add_blue"
android:background="#drawable/grey_button"
android:layout_gravity="center" />
</FrameLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
PS2: Don't look at the Log, I put that for example, but if i put anything else nothing happens, just the dialog appears...
PS3: The XML i use for each item of the listView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/item_screen"
android:background="#color/white">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
android:id="#+id/item_layout"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:padding="7dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/layout_label">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="#+id/item_label"
android:layout_weight="1"
android:textColor="#color/grey_dark"
android:gravity="center_vertical"
android:textStyle="bold"
android:textSize="16dp" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/empty_layout">
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/layout_value"
android:paddingTop="8dp"
android:paddingBottom="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="#+id/item_value"
android:textColor="#color/grey_dark"
android:textStyle="bold"
android:gravity="center_vertical|center_horizontal"
android:textSize="16dp" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
SOLUTION
I had a onSwipeRight() on my listener, it blocked the onItemLongClickListener
here is the code that was blocking the listener
listView.setOnTouchListener(new OnSwipeTouchListener(getApplicationContext()) {
public void onSwipeRight(){
//some code
}
});
Big thanks to ddb for his time :)
why are you passing a new AdapterView.OnItemLongClickListener?
just do like this below instead
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("longClick", "ok");
return true;
}
});
Do not set listView.setLongClickable(true);, listView.setClickable(true); in your java.
Neither
android:longClickable="true"
android:clickable="true"
Inside your xml

No animations/selections in Tabhost

I am trying to implement a tabview with the nice gradual change from one view to another animation and have the current tab highlighted. At current all I can get is the basic tabview that functions, but does instant switching of tabs and no highlighting of selections. Here is my current code:
package com.example.ex.test;
public class MainActivity extends navBar {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),
smithingTable.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"),
smithingTable2.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"),
Map.class, null);
}
}
meanwhile in my activity main I have:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
and in tab1 for example (tab2 is the same but with slight variation)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DeviceFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:layout_gravity="center_horizontal"
android:src="#drawable/newcomer_map" />
I would really recommend you use Material Design Tabs, it alredy has nice transitions and its much more customizable.
Here's a very good post on how to implement it
Found out that I needed to add pageviews/listeners

The application may be doing too much work on its main thread. Even using AsyncTask

I am creating a android application.
In my app I have one listView and one gridView and when I set the addapter of those view I get the The application may be doing too much work on its main thread
I have try to put those setadapter on a AsyncTask but nothing change.. I do not understand.
My Activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showToolbarTitle(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
toolbarTitle.setText("PARALLAX + FADING IMAGEVIEW");
toolbarTitle.setVisibility(View.INVISIBLE);
scrollview.setScrollViewCallbacks(this);
listView = (ListView) findViewById(R.id.listview);
gridView = (GridView) findViewById(R.id.gridView);
new LongOperation().execute(); //I call the AsyncTask
gridView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true;
}
return false;
}
});
}
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
CustomList adapter = new
CustomList(ParallaxLikeIoshedActivity.this, test, imageId, false);
// listView.setAdapter(adapter); // If I do this I will get the error
CustomList adapterGrid = new
CustomList(ParallaxLikeIoshedActivity.this, test, imageId, true);
// gridView.setAdapter(adapterGrid); // If I do this I will get the error
return "Executed";
}
#Override
protected void onPostExecute(String result) {
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... values) {}
}
This is the layout of the Activity :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
android:id="#+id/scrollview"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/image_container"
android:layout_width="match_parent"
android:layout_height="250dp">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#000000"/>
<!--<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/moviethumbnail"
tools:ignore="ContentDescription" />-->
</FrameLayout>
<LinearLayout
android:id="#+id/header"
android:layout_below="#id/image_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:background="#222"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/linearLayout">
<TextView
android:textAppearance="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MHeader 1 !!!"
android:textSize="40sp"
android:textColor="#color/white"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header"
android:background="#ff7f7f7f"
android:showDividers="middle">
<com.example.edouard.NonScrollListView
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingTop="20dp"
android:divider="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listview"
android:visibility="visible"/>
<GridView
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingTop="20dp"
android:divider="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/gridView"
android:visibility="invisible"/>
</FrameLayout>
</RelativeLayout>
</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>
</FrameLayout>

TabHost with Horizontal Scroll Issue

I want to have different number of tabs and I am using TabHost.
these could be between 1 to 8 in number depending on data.
I want to have horizontal scroll added so that when all 8 are there it doesn't look cramped up.
Issue is when 5 or more are there it looks fine and scroll works !
But when number of tabs are less I see blank space. tabs are not getting stretched to fill in the extra space.
How can I resolve this ?
Can this be done via Java code ?
here is my layout xml...
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!---Other Views--->
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</HorizontalScrollView>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
This is working for me. Please try.
<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">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tabStripEnabled="true"
android:orientation="horizontal" />
</HorizontalScrollView>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</TabHost>
BTW, Where does it say that TabHost is deprecated? I am getting no warnings...
Also, relying on this post, I believe that TabHost is alive and kicking.
I have added three tabs and have no problem using it and if you want to make it scrollable just Add <HorizontalScrollView> as a parent to <TabWidget>
mainActivity.xml
<?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="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#android:id/tabs" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</TabHost>
MainActivity
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
#SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements OnTabChangeListener
{
TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
TabSpec photospec = tabHost.newTabSpec("Home");
photospec.setIndicator("");
Intent photosIntent = new Intent(this, Download.class);
photospec.setContent(photosIntent);
TabSpec songspec = tabHost.newTabSpec("Songs");
songspec.setIndicator("");
Intent songsIntent = new Intent(this, Home.class);
songspec.setContent(songsIntent);
TabSpec videospec = tabHost.newTabSpec("Videos");
videospec.setIndicator("");
Intent videosIntent = new Intent(this, Album.class);
videospec.setContent(videosIntent);
tabHost.addTab(photospec);
tabHost.addTab(songspec);
tabHost.addTab(videospec);
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.download_unselect);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.main_selected);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.albums_unselect);
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 50;
tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 70;
tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = 50;
tabHost.setCurrentTab(1);
tabHost.setOnTabChangedListener(this);
}
#Override
public void onTabChanged(String tab)
{
int index = tabHost.getCurrentTab();
if(index == 0)
{
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.download_selected);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.main_unselect);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.albums_unselect);
}
else if(index == 1)
{
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.download_unselect);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.main_selected);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.albums_unselect);
}
else if(index == 2)
{
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.download_unselect);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.main_unselect);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.albums_selected);
}
}
}

Categories