Does android provides any opportunity to customize standard PageTitleStrip or PageTabStrip?
I want to create some title like this? What view should i extend, maybe there are some best practice in this sphere?
Just create your own custom view, connect it to your view pager via call to ViewPager.setOnPageChangeListener and voila - you have your own page strip.
You can look at sources of ViewPagerIndicator and PageTitleStrip itself (Android source code is freely available) to get an idea how to implement this in details
This is maybe a little late but I finally found a solution to my problem of how to customize the titlestrip. I realize this is not a very clean way of customizing the titlestrip but I couldn't find any better solution anywhere. I hope it will save some time for you guys.
First create a new class extending the PagerTitleStrip and override the constructor taking two arguments:
public class MyPagerTitleStrip extends PagerTitleStrip {
public MyPagerTitleStrip(Context context, AttributeSet attrs) {
super(context, attrs);
((ViewManager) (this.getChildAt(2)).getParent()).removeView((TextView)this.getChildAt(2));
((ViewManager) (this.getChildAt(0)).getParent()).removeView((TextView)this.getChildAt(0));
}
}
As you can see, I removed two textviews from the titlestrip in a hacky kind of way so be weary of this. These are the two textviews displaying the titles of the previous and next item respectively.
You may want to do something different than me so have a close look at he android PagerTitleStrip class, especially the constructor, before you change/hack anything. The constructor is probably the place where you want to add drawables etc.
Changing Views in the protected void onLayout() method will probably cause the view to be drawn twice which is not very good for the performance of your app (you'll see a lot of warning in your log).
In xml everything is still the same:
<android.support.v4.view.ViewPager
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/main_fragment_pager"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.package.MyPagerTitleStrip
android:id="#+id/fragment_pager_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:background="#111111"
style="#style/viewPagerTitleStrip">
</com.package.MyPagerTitleStrip>
</android.support.v4.view.ViewPager>
Related
I am currently using an architecture where I have multiple fragments that I swap in the same activity.
I need to be able to use a different background color for one of these fragments BUT I don't want to use the lazy solution that adds one layer of overdraw.
For clarity : I have a window background setted by my theme and on top of it I draw cards & lists. In one of my fragments I need a slightly different window background color.
Is there any way to do that ? I already tried to use a ContextThemeWrapper but it does not seem to work (maybe because the background has already been drawed ?)
Thanks in advance for your help.
Just found it :
It is doable by calling
ColorDrawable cd = new ColorDrawable(getActivity().getResources().getColor(
R.color.your_color));
getActivity().getWindow().setBackgroundDrawable(cd);
during the initialization of your fragment, when it is attached to the activity.
Unfortunately changing a fragment's background at runtime isn't supported. To get this functionality you can wrap your fragment inside of a RelativeLayout or something similar and set the background programmatically on that.
<RelativeLayout
android:id="#+id/fragmentContainer1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ccc">
<fragment android:name="whatever.fragment.name"
android:id="#+id/frag_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Then in your code get by id fragmentContainer1 and do view.setBackgroundColor(YourColorVariable) on it.
Another way may be to get your fragment, find the root view, then set the background on that. fragment.getRootView().setBackgroundColor(Color.WHITE) This depends on your fragment's layout though, so it may or may not work.
Hi all and thank you in advance and sorry for my english. I have two big doubts
1 - I haven't been too much time programming in android, and i pretty sure there is a lot of things i have made in wrong ways. For example, I have made several apps where in xml definition i include another xml.
For example, imagine 2 activities with a header_section.xml being include in both activities xml definition. That header_section has 5 buttons and more views etc. Ok, in the xml is just make an include and it works......but to implement the buttons.....do i have to REPEAT the code in both activities?? it sounds really bad practice to duplicate code in both activities.....but how can i do, for example this in activities A and B? Do i have to put this code exactly the same in both activities classes????
private View header_section;
private Button bExample;
header_section=findViewById(R.id.header_section);
bExample=(Button)header_section.findViewById(R.id.bExample);
bExample.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Whatwever...call a number, for example
}
});
In main xml something like:
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="0dp" >
<include android:id="#+id/header_section" android:layout_gravity="center" android:gravity="center" layout="#layout/header_section" />
</LinearLayout>
and in header_section.xml something like:
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="0dp" >
<Button android:id="#+id/bExample" />
</LinearLayout>
2 - Imagine you have like 10 activities in your app. If in all of them there are a header section and bottom section with the same functionality, changing just the central area (showing different lists, views etc)......is better have just one activitie in ALL the app, with a viewflipper in the central area? or have 10 activities having, i do not know if can be avoided, asked in point 1, code repeated in all 10 activities for the implementation of the headers and bottoms views, handlers etc?
Thanks and best regards
1) Yes, usually, you should have to use them BUT you can make it simplier...
1.a) bExample=(Button)findViewById(R.id.bExample); //don't need to load the View
1.b) You can shorten a bit how you call the onclick, in your button/clicable element inside the LAYOUT, here's an example:
<!--inside layout -->
<Button android:id="#+id/bExample" android:onClick="aceptar" />
and
//inside the Activity
public void aceptar(View v){
//here the code of the button
}
For the question about implementing the same methods inside all Activities, check this post: Adding the same context menu to multiple activities
2) Depending the application
If you don't do much, you can load all in the same activity and HIDE/SHOW the layout elements you don't want.
But it's better to use different activities, anyway, if the layout is not "heavy" (too many elements/includes inside) you can load the SAME layout for all your activites, and you only need to change the different contents (strings) and/or HIDE/SHOW the different elements.
As I am actually not very confident with programatically changing Views, I have following problem:
At the first start of my app, I want to have an overlay for the main screen, that tells the user to have a look at the settings, as there are two critical options the user has to configure.
I don't want to use an AlertDialog and rather not use a wizard. So, I decided to take an approach similar to Go SMS and create an overlay at the first start. The mockup I created looks like this:
Normal menu:
First start:
So these are the problems I have:
Like I said, I don't want to use a screenshot overlaying on first start, as this would take too much space and would not be language and screen independent.
I would have the circle as an png, but I don't know how exactly put it over the image
The same problem with the text
And finally I want to put a semi-transparent white over the app. It does not necessarily need the hole for the icon, though it would be nice.
In case you need the Layout Source, you can get it at pastebin
So, I just need to get a start here, if it is better to use LayoutInflater or ViewStub and how to realize it, as I have absolutely no experience with it...
Thanks!
/edit: I uploaded a new, more well-arranged layout.
I have faced a similar problem, I client wanted a walkthrough of the application, where the entire screen had to become whiter (as they said: "transparent"), except for the button being explained by an overlay speech-bubble.
Fortunately for you, your layout is not nearly as complicated as the one I had to work with :)
Now, you can get the transparency-effect in two ways, either have a white background and call all the views setAlpha() methods, or you can create a half-transparent white overlay.
If you go with the overlay, you'll have to find a way to display the opaque buttons through the overlay. This can get a bit complicated.
If you go with the first option, you can just setAlpha(1) on the opaque view to get it to show up.
The setAlpha() method is only available from api version 11+, so if you target an earlier version, you might have to do it in a slightly more complicated way.
Example of setting alpha on views pre-honeycomb:
Layout for your buttons (make them however you want, just make them similar so you can loop through them):
<LinearLayout
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:tag="image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/tile"/>
<TextView
android:tag="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:text="button1"/>
</LinearLayout>
In your program, when you are want to make the buttons transparent:
LinearLayout l = (LinearLayout) findViewById(R.id.button1);
((ImageView)l.findViewWithTag("image")).setAlpha(0x7F);
((TextView)l.findViewWithTag("text")).setTextColor(0x7F000000);
When you have decided on how you want to create the transparency effect, you will have to decide on how to display the overlay-text/bubble. You'll most likely want to put this in a separate layer on top of your entire layout, to make sure that it is not affected by your new view.
One way to achieve this is by changing your root layout element to a FrameLayout, and then creating/displaying in this. e.g:
<FrameLayout background="#FFFF"> <!-- white background, just in case -->
<LinearLayout>
<!-- the rest of your layout -->
</LinearLayout>
<LinearLayout visibility="gone"> <!-- this will be your overlay view -->
<ImageView /> <!-- the arrow/ring -->
<TextView /> <!-- the description -->
</LinearLayout>
</FrameLayout>
When the introduction is displayed, you set the position of the hidden overlay-view to the position of the table item to be explained, change the text to an appropriate string/resource and display the view.
When the introduction is over, you reset the alpha values of all buttons, and set the visibility of the overlay to gone again.
Since I don't have much experience with ViewStub, I would do it with LayoutInflater.
First of all, you need to have a second layout loaded on top of your current layout. The easiest is to have a FrameLayout, which has as one child your current view, and the dynamically you load the second child on the first start. When you load a content view in an Activity, it will be attached to some already created views (some DecorView, a FrameLayout, etc). So you can either re-use the existing FrameLayout, or you can create a new one.
I would vote for the second solution, since it's more stable (I just mentioned the other possibility in case you want to minimize the number of layers).
So, as a first step, wrap your current layout inside a FrameLayout, and give it an id, let's say "#id/root".
Then, in the onCreate method, you can have something like this:
setContentView(R.layout.main);
if (isFirstRun()) {
ViewGroup parent = (ViewGroup)findViewById(R.id.root); // locate the FrameLayout
LayoutInflater li = LayoutInflater.from(this); // get an instance of LayoutInflater
li.inflate(R.layout.overlay, parent);
}
So far you will have the overlay loaded. Now it's up to you to define the overlay.
To make the whitening effect, just set the following attribute on the root view in your overlay.xml layout:
android:background="#40ffffff"
To position the circle, first you need to find it's location. You can use the View.getLocationOnScreen to get the absolute coordinate of the icon (below the circle) on the screen. Then you can have two options:
either create a custom view (for the overlay) and manually draw the circle at the given location
or add the circle using an ImageView and adjust the left and top margins based on the coordinates
I'm a beginner at android programming, so excuse me if my wording is slightly incorrect.
I have a custom canvas view along with a TextView inside a linear layout, defined in the layout file as
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="#+id/blah"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:gravity="top|center_horizontal"
/>
<com.*.Overlay android:id="#+id/combined"
android:layout_width="fill_parent"
android:layout_height="100dp"
/>
</LinearLayout>
I need to be able to read the text contained in the TextView from within the Overlay custom class that I created.
(The overlay class takes in 2 bitmaps and puts one on top of the other. The bitmaps used will depend on the text in the TextView.)
I considered using intents, but the Overlay class doesn't have an onCreate method.. All my code is within the onDraw method. I also added the necessary constructors.
I'm not sure what to try next, perhaps try accessing the parent linearlayout and then its child textview?
Hope I managed to explain everything in a non-confusing manner
Ok, managed to fix the issue... sort of
I found out that Views need to be contained in Activities.. so I created a new Activity with my custom view as an inner class, passed an intent with the necessary data to the activity and was able to use it successfully in my custom canvas view.
I was a bit surprised I didn't get any responses, but I guess that's because I'm new here
I am using the method setKeepScreenOn(true) and haven't been able to figure out how to call this in relation to the current Activity (which has a content view set). I've been able to get it to work by calling it on one of my buttons which is always present in the view, but this feels wrong - and I'm sure there must be a way to get around this. I tried referencing the current focus like this:
getCurrentFocus().setKeepScreenOn(true);
but that threw a NullPointerException. Maybe there was no current focus.
So, can anyone tell me how I can reference the view class which I am working inside? Thanks :)
Try this answer:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow is a method defined for activities, and won't require you to find a View first.
As Hawk said but poorly explained.
You can also use FLAG_KEEP_SCREEN_ON in your XML layout file.
Note the android:keepScreenOn="true"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true"
android:orientation="vertical" >
<!-- whatever is in your layout -->
</LinearLayout>
I've now written all the choices for keeping the screen on up into a blog post:
http://blog.blundellapps.com/tut-keep-screen-onawake-3-possible-ways/
Set android:keepScreenOn in XML
If you are doing it on a class extends View. You can simple:
this.setKeepScreenOn(true);
According to Google Docs for android Developers you've two ways to do this :
First way :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
Second way is to add in your xml file layout this attribute:
android:keepScreenOn="true"