I make an timetable application for android and I'm using ViewPagerIndicator as fragment. But now I've a problem, I want to get the TabPageIndicator to the center. Like this:
And I would to like to set the individual tab width and height. And set that tab to a drawable that support selected/pressed states.
I've not found it yet to solve these problems. I've try something with onPageSelected and ContextThemeWrapper but that doesn't work. (Maybe I'm not using it correctly).
Is this all possible to make or should I look to something else than ViewPagerIndicator? (An other option without ViewPagerIndicator is also welcome).
The app on the image is Untis Mobile and can be found on the PlayStore.
I've already ask the creators how they do it but they said the app isn't open source. (But I have found in the source that they do it with TwoDScrollView, but I'm not sure)
Edit:
The styling is fixed with vpiTabPageIndicatorStyle. (With help of XDA Developers!)
When I add 20 items it stays on the middle but only from item 4 to item 17, item 1, 2, 3, 18, 19, 20 are not in the middle. See:
Can I add a 3 empty items (or empty space) to ensure that the first and last items are in the middle? Something like this (from the Untis Mobile app):
(I've already tried android:paddingLeft in vpiTabPageIndicatorStyle but that doesn't work)
I know that viewPager.setCurrentItem(total/2) will set the TabPageIndicator in the middle but then it stays on the middle and I'm not able to select a other day (I call it in onPageScrolled). So I want as you scroll the selected one needs to be in the middle. Is this possible?
The widget you are looking for is similar to the android horizontal wheel. There is an implementation at android-spinnerwheel which can be what you need.
Then you don't need an viewpagerindicator. Use a horizontal scroll view as Amulya Khare suggested in your activity and replace fragments when scrolling occurs.
Your activity layout could be like :
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SomeWheelView
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<SomeStaticIndicatorView
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</LinearLayout>
Set a listener to wheelview and when change occurs you can put your fragments in framelayout in your activity.
Use it like this :
TabPagerIndicator tbi;
ViewPager pager;
in onCreate() do like this
tbi=(TabPagerIndicator)findViewById(R.id.something);
tbi.setViewPager(pager);
I hope this will work out for you. :D
Related
I have this GridAdapter that shows my working GridView of at least 100+ buttons, but all of these will need to navigate to a page once clicked. I'm unsure of how to begin or if what I have is a substantial place to start. I know where I'd put an OnClick method and listener but I am unsure of how to ensure every button in the grid is heard separately since they don't have Ids. Guides online have been helpful but I'm still lost. If anyone has some advice as to how to proceed I'd be grateful. Both java files are attached but the array of images is not included in the Adapter screenshot.
Grid_Adapter
Grid_Fragment
You could add android:onClick="[method name here]" within the ImageButtons in your XML layout code, to call the code when each button is clicked. Note that this method has to be public and exist within the current activity.
As an example:
<?xml version="1.0" encoding="utf-8"?>
<!-- truncated -->
<Button android:id="#+id/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:onClick="buttonClickMethod1" />
<!-- truncated -->
Also note that this isn't the preferred method, and that using onClick listeners or data bindings is a better approach.
Finally, I would question your activity layout of having 100+ buttons from a user perspective - that seems very high.
I'm new to app development, so maybe I just made an obvious mistake.
However my problem is, like I mentioned in the title, my preview is not showing the ListView examples (Picture 1 show what I want the preview to show), but when I add an ArrayList and start it on the emulator every thing is displayed correctly.
1 https://i.stack.imgur.com/w4uON.png
2 https://i.stack.imgur.com/6Hneo.png
3 https://imgur.com/sQtBpAo
4 https://imgur.com/a/moT6M4K
In your layout XML (activity_main.xml), you can add the tools:listitem attribute to the ListView to specify a layout to show in the preview. Like this...
<ListView
android:id="#+id/myListView"
android:layout_width="0dp"
android:layout_height="0dp"
tools:listitem="#layout/item_something"
... />
Just replace 'item_something' with the name of the layout you want to use for each item.
PS. When you're in 'Design' mode rather than editing the XML, look for the attributes with a small spanner icon to the left. There are other 'tools' attributes you can use for similar things.
My first question was to click through layout. I solved it out.
Now my question is how to set button's position at desired location?
My application's minimum api level is 8 (I can't set getX() or getLeft())
Test XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<Button android:id="#+id/buttonx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="X" />
</LinearLayout>
Acitivty code:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class Test extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL); requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.test);
Button xclose = (Button) findViewById(R.id.buttonx);
xclose.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
}
}
any way to change button's position dynamically or programatically?
If you need to align the views in Linear Layout Please read about gravity and layout_gravity properties.
You have another layout like Relative layout. It will give you more properties for view alignment.ex:layout_alignLeft,layout_alignParentLeft etc...
Study about it and use it however you want....Thanks...
You can always use NineOldAndroids by Jake Wharton to use setX() and setY() (and many other properties).
This library ports back the Honeycomb-Animation Framework to api level 1!
If that doesn't work, you could create your own ViewGroup and measure/layout the children yourself. More information here.
EDIT: Also, View.getLeft()/getTop()/... is available since api level 1. See here
Hope this helps!
You can use padding or margin in code for example
xclose.setPadding(left, top, right, bottom);
At the time of onclick u try this
I don't know where do you want to locate your button, but you should look into FrameLayout (which will let you put the button anywhere in your layout).
Using Android, you usually have three layout options as detailed in the Layouts | Android Developers notes. The Linear Layout gives you the option to declare elements and chain them either horizontally or vertically. The Relative Layout, which is what I believe you need, allows you to freely define the position of an element relative to another element, and the Web View is usually used to show web related content.
As I said, I believe the Relative Layout is what you are looking for. Have a look at this example which neatly outlines how to use the different alignment options to place elements. You will not there are two major categories. The first lets you align your element with a parent element which is handy if you want to move elements left, right, top, bottom, or centre. The second lets you define the alignment in regard to elements on the same level by specifying that your element be, for example, android:layout_toLeftOf or android:layout_below. You can combine all of these to get the desired result.
I am well aware that there are many options to achieve the result but I found this to be quite simple and usually sufficient to achieve visually attractive layouts.
<RelativeLayout 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:background="#drawable/blur2"
android:orientation="horizontal"
tools:context=".MainActivity" >
<Button android:id="#+id/buttonx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="X" />
</RelativeLayout>
Make sure it's relative, not Linearlayout, because on relative it would allow you to move buttons easily.
I have an android project that has several small views which I need to instantiate at runtime. I haven't been able to figure out a way to store all of these related views in a single xml file and I now there are going to be many of these xml files. I was just wondering if there is any way to have them all in a single file, but not belonging to some parent ViewGroup.
The layout folder in android kinda sucks since there's no way to make subfolders, everything is just piled into the same place, ugh.
I hope someone can tell me of a better way of organizing these things.
If I understand you correctly you want several views meged onto one screen or merged into one xml file. You can include other xml's into one.
The articles showed you how to use the tag in XML layouts,
to reuse and share your layout code. This article explains the tag and how it complements the tag.
http://developer.android.com/resources/articles/layout-tricks-merge.html
Also, this video might help (about 19 minutes in). Shows you how to extract a current layout and be able to include it in others.
a couple things:
Yes, the layout folder is a pain. I use strict naming conventions to make it bearable, and in eclipse use the shortcut ctrl + shift + r to quickly find the layout I am looking for. Try naming your layouts after your activity: activity1_menu_overlay and activity1_main. With the above shortcut, just type out Activity1 and it will only show you the relevant layouts.
And if that doesn't work, you can try wrapping all your views in LinearLayouts and using view.setVisibility(View.Gone); or view.setVisibility(View.Visible); to show/hide the appropriate views.
Here is an example of that second one, because it's tough to explain.
one XML file:
<LinearLayout>
<LinearLayout ... android:visibility="visible">
<copy/paste of view 1>
</Linearlayout>
<Linearlayout ... android:visibility="gone">
<copy/paste of view 2>
</Linearlayout>
<Linearlayout ... android:visibility="gone">
<copy/paste of view 3>
</Linearlayout>
<Linearlayout ... android:visibility="gone">
<copy/paste of view etc.>
</Linearlayout>
</Linearlayout>
keep in mind this approach will require you to define a reference to each "child" LinearLayout view in your activity, so you can call setVisiblity appropriately.
This approach works well for animations, and I would only use it for 2 or 3 possible views in one xml file.
I have a custom spinner dropdown xml file in /res/layout/:
spinner_view_dropdown.xml:
<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_item_dropdown"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
style="#style/spinner_item_dropdown" />
I'm setting the spinner dropdown via java:
// "Spinner", aka breadcrumbs
Spinner spin = (Spinner) findViewById(R.id.breadcrumb_dropdown);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.breadcrumb, R.layout.spinner_view);
adapter.setDropDownViewResource(R.layout.spinner_view_dropdown);
spin.setAdapter(adapter);
// /"Spinner"
Unfortunately, a white background still exists on the spinner popup regardless if I set the background to transparent.
How do I fix this?
You can override the style for the dropdown, and the dropdown item by using a Theme in your app that inherits from one of the Android themes, then override the
android:dropDownSpinnerStyle, or android:spinnerDropDownItemStyle, and even the android:dropDownListViewStyle attribute of the theme, pointing to your own custom style instead of the Android style that is defined in their theme. I created a fully customized spinner this way, with a transparent button AND dropdown. I even got rid of the dropdown list dividers, and set my own spacing for the dropdown items when I built the tablet app for Fandango (take a look at the sort movies spinner on the main page of the app).
Everything in Android is customizable, you just have to know where to look. ;-)
Try setting on the spinner this:
android:popupBackground="#android:color/transparent"
The layout you're defining is only used for an entry of your drop-down, not the drop-down itself. So setting the background to transparent won't have any effect on its background. But even if it would, setting the background to transparent would still have no effect, because a TextView (actually I believe any view) has a transparent background by default.
That being said, the right question would be: can you provide a custom layout for an entry's parent view (which is probably a List)? As far as I know, the answer is no, unfortunately.
Try
android:cacheColorHint="#00000000"
to get transparency.
I dont know if it works for you but there is a post
http://android-developers.blogspot.com/2009/01/why-is-my-list-black-android.html
that explains why the moving parts of a list appear in the background color.
Maybe its the same issue with your spinner.
It's a bug in 1.5 I think, see here
http://www.symsource.com/index.php?view=article&id=418&option=com_content&format=pdf
Run it in a 1.6 emulator or device, does it still stay white?
I actually came here looking for an approach to this, I suspect this may involve manually writing to the canvas or something like that.
Any ideas.
P.S. Accidentally posted when I thought I was logged in, anyone know how to get rid of the anonymous comment? Maybe an admin could fix this?