I am currently working on a Connect four game
I have managed to develop the app to point to where the game knows what column the user has selected. I am currently trying to change the colour of the gap to the respective colour of the user (token has been placed).
The problem I am having is when connectfourView.invalidate() is called I get a java.lang.NullPointerexception
Code below
public void tokenPlacement (int index, float xpos) {
int x = 0;
int lowerxpos = (int) (xpos - 10);
int higherxpos = (int) (xpos + 10);
while (x <= 6)
{
if ( lowerxpos <= ((float) (columnselects.get(x).getWidthPos())) && higherxpos >= ((float) (columnselects.get(x).getWidthPos())))
{
Log.d("In IF", Float.toString(x));
Log.d("Looking at the colour", Float.toString(gaps.get(x).getColor()));
gaps.get(x).setColor(-1);
Log.d("After change what is the colour now", Float.toString(gaps.get(x).getColor()));
connectfourView.invalidate();
break;
}
x++;
}
}
About the code. Firstly I know this will only affect the bottom layer of each column's gaps. I will fix this when the colour changing is working. This code is from Gaps.java. This view is ConnectFourView.java (instance connectfourView) where the gaps are drawn to screen. All gaps are stored in a list (gaps) and are defined in the Gap.java (x-postion, colour etc.)
Section from Gap.java
public Gap (int j, int i, int color, double diameter, double widthpos, double heightpos){
this.j = j;
this.i = i;
this.color = color;
this.diameter = diameter;
this.widthpos = widthpos;
this.heightpos = heightpos;
}
public int getJ() {return j;}
public int getI() {return i;}
public int getColor() {return color;}
public void setColor(int newColor) {this.color = newColor;}
public double getDiameter() {return diameter;}
public double getWidthPos() {return widthpos;}
public double getHeightPos() {return heightpos;}
}
This is a question I asked to get to this point
Note that the gaps are added with a button press 'New Game'
If any other information or code is need please leave a comment
Added for dmon:
This is where connectfourView is defined, it has also been imported
public class Gaps {
ConnectFourView connectfourView;
/* Other code, not related to question */
public void tokenplacement()//at bottom of class Gaps
I have noticed that when a click the exit button on the screen all the gaps do change color just before the main menu is loaded, so the problem is just trying to refresh the screen
From ConnectFourView.java
public ConnectFourView(Context context, Gaps gaps) {
super(context);
this.gaps = gaps;
setFocusable(true);
}
//used to mange the attributes of the board (different colour to background for board)
public ConnectFourView(Context context, AttributeSet attrs, Gaps gaps) {
super(context, attrs);
this.gaps = gaps;
setFocusable(true);
setMinimumWidth(getWidth());
setMinimumHeight(getHeight());
}
link to dropbox
As others have said (I gave Henry the bump), connectfourView has to be null there. You need to initialize it:
ConnectFourView connectfourView = new ConnectFourView();
I don't see where that is ever done so it's likely set to null by default.
Related
I a using MpAndroidChart library. I need to implement a design where I need to color the area between two limit lines. I have attached an image for reference. I have tried multiple ways but I have failed to achieve it. I am using this library for the first time. Can anyone help me about how this could be achieved.
As you can see the green shade behind the line graph. Which is the limit. I need to get that green shade
Thanks in advance,
Anudeep Reddy.
I don't think that there is a direct way to achieve this, but this workaround should help you:
LimitLine ll = new LimitLine(lowerLimit, "Systolic range");
ll.setLineColor(Color.GREEN);
ll.setLineWidth(upperLimit - lowerLimit);
ll.setTextColor(Color.WHITE);
ll.setTextSize(12f);
chart.getAxisLeft().setDrawLimitLinesBehindData(true);
The important thing here is the method setDrawLimitLinesBehindData(true).
As always, all the information is available in the documentation.
I had the same problem but reached a different workaround without having to subclass the LineChart. Using canvas to draw the rectangle works, but you have to translate your charts coordinates to the canvas coordinates. You cannot use a single limit line as there is a limit to the width of the line. The workaround I used was to simply loop through limit lines to create a rectangle within my range.
float increment = (rangeHigh - rangeLow) / 20;
float metricLine = rangeLow;
for(int i = 0; i < 20; i++) {
LimitLine llRange = new LimitLine(metricLine, "");
llRange.setLineColor(Color.parseColor("#b5eb45"));
llRange.setLineWidth(10f);
leftAxis.addLimitLine(llRange);
metricLine = metricLine + increment;
}
As this is still an issue I throw in my two cents.
I tried the solution of #HouseOfHufflepuff but I got the error message that I use too much limit lines in the plot. It seems to work anyway but I guess the performance is not optimal.
So I implemented a subclass for drawing zones in the background. Maybe it's helpful for someone:
public class TargetZoneCombinedChart extends CombinedChart {
protected Paint mYAxisSafeZonePaint;
private List<TargetZone> mTargetZones;
public TargetZoneCombinedChart(Context context) {
super(context);
}
public TargetZoneCombinedChart(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TargetZoneCombinedChart(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void init() {
super.init();
mYAxisSafeZonePaint = new Paint();
mYAxisSafeZonePaint.setStyle(Paint.Style.FILL);
// mGridBackgroundPaint.setColor(Color.rgb(240, 240, 240));
mTargetZones = new ArrayList<>();
}
#Override
protected void onDraw(Canvas canvas) {
for (TargetZone targetZone : mTargetZones) {
// prepare coordinates
float[] pts = new float[4];
pts[1] = targetZone.lowerLimit;
pts[3] = targetZone.upperLimit;
mLeftAxisTransformer.pointValuesToPixel(pts);
// draw
mYAxisSafeZonePaint.setColor(targetZone.color);
canvas.drawRect(mViewPortHandler.contentLeft(), pts[1], mViewPortHandler.contentRight(),
pts[3], mYAxisSafeZonePaint);
}
super.onDraw(canvas);
}
public void addTargetZone(TargetZone targetZone){
mTargetZones.add(targetZone);
}
public List<TargetZone> getTargetZones(){
return mTargetZones;
}
public void clearTargetZones(){
mTargetZones = new ArrayList<>();
}
public static class TargetZone {
public final int color;
public final float lowerLimit;
public final float upperLimit;
public TargetZone(int color, float lowerLimit, float upperLimit) {
this.color = color;
this.lowerLimit = lowerLimit;
this.upperLimit = upperLimit;
}
}
}
To add a zone you just need to add a target zone object:
float rangeHigh = 180f;
float rangeLow = 80f;
chart.addTargetZone(new TargetZoneCombinedChart.TargetZone( Color.parseColor("#33b5eb45"),rangeLow,rangeHigh));
whereby the ranges are y values of the left axis.
This can be done by sub-classing the chart class (e.g. LineChart) and then overriding the onDraw() method. In the overridden onDraw() you can draw the rectangle(s) you need directly onto the canvas and then call super.onDraw() to complete the rendering of the chart.
There is an example of how to do this on the MP Android Github (see below). I followed the code in the example and it worked well for me.
https://github.com/PhilJay/MPAndroidChart/issues/485
I use a basic and very common implementation of a LeadingMarginSpan2 to wrap text around an image (since it seems to be the easiest way and please don't suggest me to use WebViews at this point):
public class MyLeadingMarginSpan2 implements LeadingMarginSpan.LeadingMarginSpan2 {
private int margin;
private int lines;
public MyLeadingMarginSpan2(int lines, int margin) {
this.margin = margin;
this.lines = lines;
}
#Override
public int getLeadingMargin(boolean first) {
return first ? margin : 0; // <--- the issue is here
}
#Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
int top, int baseline, int bottom, CharSequence text,
int start, int end, boolean first, Layout layout) {
}
#Override
public int getLeadingMarginLineCount() {
return lines;
}
}
The problem is that as soon as a paragraph occurs in the text, an unwanted margin is given to this line. I want to limit the number of times getLeadingMargin() returns the actual margin to the number of lines passed inside the constructor.
I've tried to count how many times this margin was returned and compare it against the number of lines, this didn't work however (in most cases no margin was applied, in some cases it was applied to a wrong number of lines).
Does anyone have a workaround for this issue?
Im new in Android world. I want to put some parallax background effects in my app.
How can I do it? How to approach to this in Android?
Is there any productive way to create 2-3 layer parallax background? Is there some tool, or class in android API?
Or maybe I have to modify background image location or margins "manually" in code?
Im using API level 19.
I have tried to understand Paralloid library, but this is too big to understand without any explanation. Im new to Android and Java, im not familiar with all Layouts and other UI objects, however I'm familiar with MVC.
I started bounty, maybe someone can explain step by step how that library works.
This is what you can do:
In your activity/fragment layout file specify 2 ScrollView's (say background_sv and content_sv).
<?xml version="1.0" encoding="utf-8"?>
<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" >
<com.example.parallax.MyScrollView
android:id="#+id/background_sv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/parallax_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="..." />
</com.example.parallax.MyScrollView>
<com.example.parallax.MyScrollView
android:id="#+id/content_sv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</com.example.parallax.MyScrollView>
</RelativeLayout>
Add a dummy view in the content scrollview of the height of the background and make it transparent. Now, attach a scroll listener to the content_sv. When the content scrollview is scrolled, call
mBgScrollView.scrollTo(0, (int)(y /*scroll Of content_sv*/ / 2f));
The existing API's doesn't have the support to get the scroll events.
Hence, we need to create a Custom ScrollView, to provide the ScrollViewListener.
package com.example.parallax;
// imports;
public class MyScrollView extends ScrollView {
public interface ScrollViewListener {
void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy);
}
private ScrollViewListener scrollViewListener = null;
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
#Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if(scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
}
Here is the activity which hosts both the content ScrollView and background ScrollView
package com.example.parallax;
// imports;
public class ParallaxActivity extends Activity implements ScrollViewListener {
private MyScrollView mBgScrollView;
private MyScrollView mContentScrollView;
#Override
public void onCreate(Bundle savedInstanceState) {
mBgScrollView = findViewById(R.id.background_sv);
mContentScrollView = findViewById(R.id.content_sv);
mContentScrollView.setOnScrollListener(this);
}
// this is method for onScrollListener put values according to your need
#Override
public void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy) {
super.onScrollChanged(scrollView, x, y, oldx, oldy);
// when the content scrollview will scroll by say 100px,
// the background scrollview will scroll by 50px. It will
// look like a parallax effect where the background is
// scrolling with a different speed then the content scrollview.
mBgScrollView.scrollTo(0, (int)(y / 2f));
}
}
I think the question is unclear, so this is not really an answer so much as an attempt to clarify with more detail than I could include in a comment.
My question is about what kind of parallax effect you want to achieve. Given these three examples (they are demo apps you can install from the Play Store), which if any has the type of parallax effect you want? Please answer in a comment.
Paralloid Demo
Parallax Scroll Demo
Google IO App
Given an answer, we all will find it easier to help out. If you edit your question to include this information, it will be improved.
The following contains an example application published by the author of Paralloid:
https://github.com/chrisjenx/Paralloid/tree/master/paralloidexample
From the GitHub page under the 'Getting Started' section:
Layout
ScrollView
This is an example, please refer to the paralloidexample App for full
code.
<FrameLayout ..>
<FrameLayout
android:id="#+id/top_content"
android:layout_width="match_parent"
android:layout_height="192dp"/>
<uk.co.chrisjenx.paralloid.views.ParallaxScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="#+id/scroll_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="192dp"/>
</uk.co.chrisjenx.paralloid.views.ParallaxScrollView>
</FrameLayout>
Fragment
Inside your onViewCreated() or onCreateView().
//...
FrameLayout topContent = (FrameLayout) rootView.findViewById(R.id.top_content);
ScrollView scrollView = (ScrollView) rootView.findViewById(R.id.scroll_view);
if (scrollView instanceof Parallaxor) {
((Parallaxor) scrollView).parallaxViewBy(topContent, 0.5f);
}
// TODO: add content to top/scroll content
Thats it!
Have a look at the Parallaxor interface for applicable Parallax
methods.
Hope this helps!
Also, here is a link to Google's 'getting started' page for android.
Also, here is a link to a 'java tutorial for complete beginners'.
As well as link to some documentation about layouts, which 'define the visual structure for a user interface'.
That being said, you would use the layout to define what the interface looks like and use the subsequent example code to define what happens when you interact with it.
P.S. You can see the application in action here
I use the ParallaxScroll library. Very easy to use, good samples and well documented.
Here is how it can be done using ScrollView and it's background image. I've committed the code in github.
You need to extend the ScrollView and Drawable classes.
By default the ScrollView background height will be same as viewport height. To achieve the parallax effect, the background height should be larger and should be based on the ScrollView child height and the background scrolling factor we want to impose.
Background scroll factor of 1 indicates, background height is same as ScrollView child height and hence background will scroll with same offset as the child scrolls.
0.5 indicates, background height is 0.5 times ScrollView child extended height and will scroll 50% slower compared to the child contents. This effectively brings the parallax scrolling effect.
Call following method from ScrollView constructor:
void init() {
// Calculate background drawable size before first draw of scrollview
getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
// Remove the listener
getViewTreeObserver().removeOnPreDrawListener(this);
mDrawable = (ParallaxDrawable) getBackground();
if(mDrawable != null && mDrawable instanceof ParallaxDrawable) {
// Get the only child of scrollview
View child = getChildAt(0);
int width = child.getWidth();
// calculate height of background based on child height and scroll factor
int height = (int) (getHeight() + (child.getHeight() - getHeight()) * mScrollFactor);
mDrawable.setSize(width, height);
}
return true;
}
});
}
When ScrollView is scrolled, take into consideration the scroll offset while drawing the background. This basically achieves the parallax effect.
ParallaxScrollView:
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
if(mDrawable != null && mDrawable instanceof ParallaxDrawable) {
// set the scroll offset for the background drawable.
mDrawable.setScrollOffset(x*mScrollFactor, y*mScrollFactor);
}
}
ParallaxDrawable:
#Override
public void draw(Canvas canvas) {
// To move the background up, translate canvas by negative offset
canvas.translate(-mScrollXOffset, -mScrollYOffset);
mDrawable.draw(canvas);
canvas.translate(mScrollXOffset, mScrollYOffset);
}
protected void onBoundsChange(Rect bounds) {
// This sets the size of background drawable.
mDrawable.setBounds(new Rect(bounds.top, bounds.left, bounds.left + mWidth, bounds.top + mHeight));
}
Usage of ParallaxScrollView and ParallaxDrawable:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parallax_layout);
final ParallaxScrollView scrollView = (ParallaxScrollView) findViewById(R.id.sv);
ParallaxDrawable drawable = new ParallaxDrawable(getResources().getDrawable(R.drawable.bg));
scrollView.setBackground( drawable, 0.2f );
}
}
parallax_layout.xml:
<manish.com.parallax.ParallaxScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:text="#string/text" />
<View
android:layout_width="match_parent"
android:layout_height="5dp" />
...
</LinearLayout>
</manish.com.parallax.ParallaxScrollView>
The Android API does not support much concrete tools for it as you probably noticed. In API 20 they added elevation which is an attribute for depth. This does not support parallax layouts itself but I would say it's a step by Google to make this kind of work easier. If you want a wild guess on if and when, I would say that parallax utilities could be added before API 25 is released, based on the latest update and the progress in battery efficiency.
For now all you need is to listen for some kind of movement and change the views positions based on a value representing elevation.
Your question made me upgrade my own project and this is how I did it using ViewDragHelper inside a Fragment.
public class MainFragment extends Fragment implements View.OnTouchListener {
private ImageView mDecor, mBamboo, mBackgroundBamboo;
private RelativeLayout mRootLayout;
private ViewDragHelper mDragHelper;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRootLayout = (RelativeLayout) inflater.inflate(R.layout.fragment_main, container, false);
mRootLayout.setOnTouchListener(this);
mDecor = (ImageView) mRootLayout.findViewById(R.id.decor);
mBamboo = (ImageView) mRootLayout.findViewById(R.id.bamboo);
mBackgroundBamboo = (ImageView) mRootLayout.findViewById(R.id.backround_bamboo);
mDragHelper = ViewDragHelper.create(mRootLayout, 1.0f, new ViewDragHelper.Callback() {
private final float MAX_LEFT = -0;
private final float MAX_TOP = -20;
private final float MAX_RIGHT = 50;
private final float MAX_BOTTOM = 10;
private final float MULTIPLIER = 0.1f;
private final int DECOR_ELEVATION = 3;
private final int FRONT_BAMBOO_ELEVATION = 6;
private final int BACKGROUND_BAMBOO_ELEVATION = 1;
private float mLeft = 0;
private float mTop = 0;
#Override
public boolean tryCaptureView(View view, int i) {
return true;
}
#Override
public int clampViewPositionVertical(View child, int top, int dy) {
mTop += dy * MULTIPLIER;
mTop = mTop > MAX_BOTTOM ? MAX_BOTTOM : mTop < MAX_TOP ? MAX_TOP : mTop;
mDecor.setTranslationY(mTop * DECOR_ELEVATION);
mBamboo.setTranslationY(mTop * FRONT_BAMBOO_ELEVATION);
mBackgroundBamboo.setTranslationY(mTop * BACKGROUND_BAMBOO_ELEVATION);
return 0;
}
#Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
mLeft += dx * MULTIPLIER;
mLeft = mLeft < MAX_LEFT ? MAX_LEFT : mLeft > MAX_RIGHT ? MAX_RIGHT : mLeft;
mDecor.setTranslationX(mLeft * DECOR_ELEVATION);
mBamboo.setTranslationX(mLeft * FRONT_BAMBOO_ELEVATION);
mBackgroundBamboo.setTranslationX(mLeft * BACKGROUND_BAMBOO_ELEVATION);
return 0;
}
#Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy){
mRootLayout.requestLayout();
}
});
return mRootLayout;
}
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mDragHelper.processTouchEvent(motionEvent);
// you can still use this touch listener for buttons etc.
return true;
}
}
Hi You can go with the below-given code for ParallaxView class
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.ArrayList;
public class ParallaxView extends SurfaceView implements Runnable {
private volatile boolean running;
private Thread gameThread = null;
// For drawing
private Paint paint;
private Canvas canvas;
private SurfaceHolder ourHolder;
// Holds a reference to the Activity
Context context;
// Control the fps
long fps =60;
// Screen resolution
int screenWidth;
int screenHeight;
ParallaxView(Context context, int screenWidth, int screenHeight) {
super(context);
this.context = context;
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
// Initialize our drawing objects
ourHolder = getHolder();
paint = new Paint();
}
#Override
public void run() {
while (running) {
long startFrameTime = System.currentTimeMillis();
update();
draw();
// Calculate the fps this frame
long timeThisFrame = System.currentTimeMillis() - startFrameTime;
if (timeThisFrame >= 1) {
fps = 1000 / timeThisFrame;
}
}
}
private void update() {
// Update all the background positions
}
private void draw() {
if (ourHolder.getSurface().isValid()) {
//First we lock the area of memory we will be drawing to
canvas = ourHolder.lockCanvas();
//draw a background color
canvas.drawColor(Color.argb(255, 0, 3, 70));
// Draw the background parallax
// Draw the rest of the game
paint.setTextSize(60);
paint.setColor(Color.argb(255, 255, 255, 255));
canvas.drawText("I am a plane", 350, screenHeight / 100 * 5, paint);
paint.setTextSize(220);
canvas.drawText("I'm a train", 50, screenHeight / 100*80, paint);
// Draw the foreground parallax
// Unlock and draw the scene
ourHolder.unlockCanvasAndPost(canvas);
}
}
// Clean up our thread if the game is stopped
public void pause() {
running = false;
try {
gameThread.join();
} catch (InterruptedException e) {
// Error
}
}
// Make a new thread and start it
// Execution moves to our run method
public void resume() {
running = true;
gameThread = new Thread(this);
gameThread.start();
}
}// End of ParallaxView
To know more you can go **
here
**: http://gamecodeschool.com/android/coding-a-parallax-scrolling-background-for-android/
Background
I'm trying to switch my alternative "App Manager" app from ActionBarSherlock library to the support library Google has created, since it gets more updates (ActionBarSherlock is no longer being developed, link here ) and I think it should cover a lot of functionality.
The problem
All went well (or so it seems), except for a class named ICSLinearLayout on ActionBarSherlock I've used to show dividers on, that is now called LinearLayoutICS .
It just doesn't show the dividers:
Note: before you ask "why don't you just use a GridView?", here's the reason, and also this, in case I'd ever want to add headers.
The code
The code is about the same as I've used for ActionBarSherlock:
rowLayout=new LinearLayoutICS(_context,null);
rowLayout.setMeasureWithLargestChildEnabled(true);
rowLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
rowLayout.setDividerDrawable(_context.getResources().getDrawable(R.drawable.list_divider_holo_dark));
rowLayout.setOrientation(LinearLayout.HORIZONTAL);
... // add views, layout params, etc...
The question
How can I use this class in order to support showing dividers on all supported OS versions of the support library?
What is wrong with the code I've written?
OK, it seems setShowDividers and setDividerDrawable cannot be used because LinearLayoutICS doesn't have them .
Not only that, but Lint didn't warn me about it being used.
So, what I ended up with is copying LinearLayoutICS code (from here, hope it's the latest version) and some of the original LinearLayout code, to make something that does work. I hope it doesn't have any bugs.
Long live open source ... :)
Sadly setMeasureWithLargestChildEnabled isn't available for old APIs, so I think the ActionBarSherlock way is still better in case that's something you wish to use.
EDIT: the setMeasureWithLargestChildEnabled method doesn't work on ActionBarSherlock.
Here's the code, for those who wish to use. I hope next time the library gets updated, I will remember to check this issue again.
public class LinearLayoutICS extends LinearLayout
{
private Drawable mDivider;
private int mDividerWidth,mDividerHeight;
private int mShowDividers;
private int mDividerPadding;
public LinearLayoutICS(final Context context,final AttributeSet attrs)
{
super(context,attrs);
// the R is from "android.support.v7.appcompat.R" .
final TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.LinearLayoutICS);
mDivider=a.getDrawable(R.styleable.LinearLayoutICS_divider);
if(mDivider!=null)
{
mDividerWidth=mDivider.getIntrinsicWidth();
mDividerHeight=mDivider.getIntrinsicHeight();
}
else mDividerHeight=mDividerWidth=0;
mShowDividers=a.getInt(R.styleable.LinearLayoutICS_showDividers,SHOW_DIVIDER_NONE);
mDividerPadding=a.getDimensionPixelSize(R.styleable.LinearLayoutICS_dividerPadding,0);
a.recycle();
setWillNotDraw(mDivider==null);
}
#Override
protected void onDraw(final Canvas canvas)
{
if(getOrientation()==VERTICAL)
drawDividersVertical(canvas);
else drawDividersHorizontal(canvas);
}
#Override
protected void measureChildWithMargins(final View child,final int parentWidthMeasureSpec,final int widthUsed,final int parentHeightMeasureSpec,final int heightUsed)
{
if(mDivider!=null)
{
final int childIndex=indexOfChild(child);
final int count=getChildCount();
final LayoutParams params=(LayoutParams)child.getLayoutParams();
// To display the dividers in-between the child views, we modify their margins
// to create space.
if(getOrientation()==VERTICAL)
{
if(hasDividerBeforeChildAt(childIndex))
params.topMargin=mDividerHeight;
else if(childIndex==count-1&&hasDividerBeforeChildAt(count))
params.bottomMargin=mDividerHeight;
}
else if(hasDividerBeforeChildAt(childIndex))
params.leftMargin=mDividerWidth;
else if(childIndex==count-1&&hasDividerBeforeChildAt(count))
params.rightMargin=mDividerWidth;
}
super.measureChildWithMargins(child,parentWidthMeasureSpec,widthUsed,parentHeightMeasureSpec,heightUsed);
}
void drawDividersVertical(final Canvas canvas)
{
final int count=getChildCount();
for(int i=0;i<count;i++)
{
final View child=getChildAt(i);
if(child!=null&&child.getVisibility()!=GONE&&hasDividerBeforeChildAt(i))
{
final LayoutParams lp=(LayoutParams)child.getLayoutParams();
drawHorizontalDivider(canvas,child.getTop()-lp.topMargin);
}
}
if(hasDividerBeforeChildAt(count))
{
final View child=getChildAt(count-1);
int bottom=0;
if(child==null)
bottom=getHeight()-getPaddingBottom()-mDividerHeight;
else bottom=child.getBottom();
drawHorizontalDivider(canvas,bottom);
}
}
void drawDividersHorizontal(final Canvas canvas)
{
final int count=getChildCount();
for(int i=0;i<count;i++)
{
final View child=getChildAt(i);
if(child!=null&&child.getVisibility()!=GONE&&hasDividerBeforeChildAt(i))
{
final LayoutParams lp=(LayoutParams)child.getLayoutParams();
drawVerticalDivider(canvas,child.getLeft()-lp.leftMargin);
}
}
if(hasDividerBeforeChildAt(count))
{
final View child=getChildAt(count-1);
int right=0;
if(child==null)
right=getWidth()-getPaddingRight()-mDividerWidth;
else right=child.getRight();
drawVerticalDivider(canvas,right);
}
}
void drawHorizontalDivider(final Canvas canvas,final int top)
{
mDivider.setBounds(getPaddingLeft()+mDividerPadding,top,getWidth()-getPaddingRight()-mDividerPadding,top+mDividerHeight);
mDivider.draw(canvas);
}
void drawVerticalDivider(final Canvas canvas,final int left)
{
mDivider.setBounds(left,getPaddingTop()+mDividerPadding,left+mDividerWidth,getHeight()-getPaddingBottom()-mDividerPadding);
mDivider.draw(canvas);
}
/**
* Determines where to position dividers between children.
*
* #param childIndex Index of child to check for preceding divider
* #return true if there should be a divider before the child at childIndex
* #hide Pending API consideration. Currently only used internally by the system.
*/
protected boolean hasDividerBeforeChildAt(final int childIndex)
{
if(childIndex==0)
return (mShowDividers&SHOW_DIVIDER_BEGINNING)!=0;
else if(childIndex==getChildCount())
return (mShowDividers&SHOW_DIVIDER_END)!=0;
else if((mShowDividers&SHOW_DIVIDER_MIDDLE)!=0)
{
boolean hasVisibleViewBefore=false;
for(int i=childIndex-1;i>=0;i--)
if(getChildAt(i).getVisibility()!=GONE)
{
hasVisibleViewBefore=true;
break;
}
return hasVisibleViewBefore;
}
return false;
}
#Override
public int getDividerPadding()
{
return mDividerPadding;
}
#Override
public void setDividerPadding(final int dividerPadding)
{
mDividerPadding=dividerPadding;
}
#Override
public void setShowDividers(final int showDividers)
{
if(mShowDividers!=showDividers)
requestLayout();
mShowDividers=showDividers;
}
#Override
public void setDividerDrawable(final Drawable divider)
{
if(divider==mDivider)
return;
mDivider=divider;
if(divider!=null)
{
mDividerWidth=divider.getIntrinsicWidth();
mDividerHeight=divider.getIntrinsicHeight();
}
else
{
mDividerWidth=0;
mDividerHeight=0;
}
setWillNotDraw(divider==null);
requestLayout();
}
#Override
public Drawable getDividerDrawable()
{
return mDivider;
}
}
I have a preferences.xml file with some checkbox preferences, a custom time picker preference, and a custom SeekBar preference. Everything works fine in my 2.2 emulator. I've tried running it in a 1.6 emulator and every time I adjust one of the sliders(SeekBar preferences), or change another preference, the SeekBar preferences change positions. Its like they just shuffle around. Anybody know how I can stop this?
This keeps me from having groupings because they are always changing positions.
I have this for my preference activity:
public class MySettings extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
This for my slider:
public class PreferenceSlider extends Preference implements OnSeekBarChangeListener
{
public static int maximum = 100;
public static int interval = 1;
private float oldValue = 50;
private TextView monitorBox;
private float beforeTouch = 50;
public PreferenceSlider(Context context)
{
super(context);
}
public PreferenceSlider(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public PreferenceSlider(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
#Override
protected View onCreateView(ViewGroup parent)
{
LinearLayout layout = new LinearLayout(getContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params1.gravity = Gravity.LEFT;
params1.weight = 1.0f;
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params2.gravity = Gravity.LEFT;
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(30, LinearLayout.LayoutParams.WRAP_CONTENT);
params3.gravity = Gravity.LEFT;
layout.setPadding(15, 5, 10, 5);
layout.setOrientation(LinearLayout.VERTICAL);
TextView view = new TextView(getContext());
view.setText(getTitle());
view.setTextSize(18);
view.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
view.setGravity(Gravity.LEFT);
view.setLayoutParams(params1);
SeekBar bar = new SeekBar(getContext());
bar.setMax(maximum);
bar.setProgress((int)this.oldValue);
//bar.
//bar.setLayoutParams(params2);
bar.setOnSeekBarChangeListener(this);
this.monitorBox = new TextView(getContext());
this.monitorBox.setTextSize(12);
this.monitorBox.setTypeface(Typeface.MONOSPACE, Typeface.ITALIC);
this.monitorBox.setLayoutParams(params3);
this.monitorBox.setPadding(2, 5, 0, 0);
this.monitorBox.setText(bar.getProgress()+"");
layout.addView(view);
layout.addView(bar);
layout.addView(this.monitorBox);
layout.setId(android.R.id.widget_frame);
return layout;
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromTouch)
{
progress = Math.round(((float)progress)/interval)*interval;
if(!callChangeListener(progress)){
seekBar.setProgress((int)this.oldValue);
return; }
seekBar.setProgress(progress);
this.oldValue = progress;
this.monitorBox.setText(progress+"");
updatePreference(progress);
//notifyChanged();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
beforeTouch = seekBar.getProgress();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
if(beforeTouch != seekBar.getProgress())
{
notifyChanged();
}
}
#Override
protected Object onGetDefaultValue(TypedArray ta,int index)
{
int dValue = (int)ta.getInt(index,50);
return validateValue(dValue);
}
#Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue)
{
int temp = restoreValue ? getPersistedInt(50) : (Integer)defaultValue;
if(!restoreValue)
persistInt(temp);
this.oldValue = temp;
}
private int validateValue(int value)
{
if(value > maximum)
value = maximum;
else if(value < 0)
value = 0;
else if(value % interval != 0)
value = Math.round(((float)value)/interval)*interval;
return value;
}
private void updatePreference(int newValue)
{
SharedPreferences.Editor editor = getEditor();
editor.putInt(getKey(), newValue);
editor.commit(); }
}
Then the preferences.xml is like this:
<com.myprogram.PreferenceSlider
android:key="ringerVolume"
android:defaultValue="50"
android:title="Ringer Volume"/>
<com.myprogram.PreferenceSlider
android:key="notificationVolume"
android:defaultValue="50"
android:title="Notification Volume"/>
You shouldnt worry about android 1.6, at least that this means that you have some dirt in your code, that is not visible for any reason in 2.2 but could be in some other versions, even newer one.
Anyways, most important here, i see that this is a question from 2011 but seems to be reopened now, and at this point, i wouldn't go on with an activity based in this code, that is quite obsolete, so having that besides it is not working perfectly, now it is the best moment, for some re-factoring.
As you can see here addPreferencesFromResource is deprecated since API level 11, that means that 75% of devices look at it like as some old stuff, and some newest devices could one day even raise an error in a sort of "I told you" basis.
There is not alternative method, since what android team wants here, a couple years ago, is not a change in the function, but a fully different approach, using Fragments.
Specifically, you are expected to create some PreferenceFragment objects to load your preferences from a resource file. I would recommend to do it, and don't waste time trying to fix the possible current errors on the activity. Migrating to the new approach will be easy, and you will get a much better result.
To get started, you can read some code here:
Android Developer - Reference - PreferenceFragment
Or read a complete guide here:
Android Developer - Api Guides - UI - Settings
If you go on with this, and have any further problem, don't hesitate to come back with any question and i will be glad to help you out!