So I've got this problem in my custom view. I'm trying to create a custom RelativeLayout with an infinite scrolling animation. To achieve this, I've created a layout backgroundscrollrelativelayout.xml like so:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainTile"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/topTile" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/leftTile" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/diagonalTile" />
</RelativeLayout>
The idea is that the ImageViews will translate their position on an animation update callback.
I've created the BackgroundScrollRelativeLayout.java like so:
public class BackgroundScrollRelativeLayout extends RelativeLayout {
final int layoutToUse = R.layout.backgroundscrollrelativelayout;
final int mainTileId = R.id.mainTile;
final int leftTileId = R.id.leftTile;
final int topTileId = R.id.topTile;
final int diagonalTileId = R.id.diagonalTile;
final float valueStart = 0.0f;
final float valueEnd = 1.0f;
final long animationDuration = 50000L;
private Context mContext;
private ValueAnimator scrollAnimator;
private ImageView mainTile;
private ImageView leftTile;
private ImageView topTile;
private ImageView diagonalTile;
public BackgroundScrollRelativeLayout(Context context) {
super(context);
mContext = context;
acquireViewsInLayout();
initializeAnimator();
scrollAnimator.start();
}
public BackgroundScrollRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
acquireViewsInLayout();
initializeAnimator();
scrollAnimator.start();
}
public BackgroundScrollRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
acquireViewsInLayout();
initializeAnimator();
scrollAnimator.start();
}
#Override
public void setBackgroundColor(int color) {
// Not supported
}
#Override
public void setBackgroundResource(int resid) {
// Not supported
}
#Override
public void setBackground(Drawable background) {
mainTile.setBackground(background);
leftTile.setBackground(background);
}
/*
#Override
public void setBackgroundDrawable(Drawable background) {
//super.setBackgroundDrawable(background);
//mainTile.setBackground(background);
//leftTile.setBackground(background);
}*/
// Intent: Inflate the layout associated with this View
private void inflateLayout(){
// TO inflateLayout, we connect the inflater to context, then we call inflate the layout associated
// with this view
//LayoutInflater inflater = LayoutInflater.from(mContext);
//inflater.inflate(layoutToUse, this);
inflate(getContext(), layoutToUse, this);
}
// Intent: Find all Views in Layout
private void findAllViewsById(){
mainTile = (ImageView)this.findViewById(mainTileId);
leftTile = (ImageView)this.findViewById(leftTileId);
topTile = (ImageView)this.findViewById(topTileId);
diagonalTile = (ImageView)this.findViewById(diagonalTileId);
}
// Intent: Concretely acquire all Views in Layout
private void acquireViewsInLayout(){
// TO acquireViewsInLayout, we inflate the layout,
// then we find the view of each known view id and save the view
inflateLayout();
findAllViewsById();
}
// Intent: Initialize animator properties
private void initializeAnimator(){
// TO initializeAnimator, we set how the animator will keep track of animation,
// then we set the animation repeat type, then we set the type of interpolation,
// then we set the animation duration, then we apply animation update listener
scrollAnimator = ValueAnimator.ofFloat(valueStart, valueEnd);
scrollAnimator.setRepeatCount(ValueAnimator.INFINITE);
scrollAnimator.setInterpolator(new LinearInterpolator());
scrollAnimator.setDuration(animationDuration);
addScrollAnimatorUpdateListener();
}
// Intent: Add an update listener to the scroll animator
private void addScrollAnimatorUpdateListener(){
// TO addScrollAnimatorUpdateListener, we add an update listener to scroll animator
scrollAnimator.addUpdateListener( new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
// Do something...
updateScrollAnimation();
}
});
}
private void updateScrollAnimation(){
float progress = (float)scrollAnimator.getAnimatedValue();
float widthOfTile = mainTile.getWidth();
float moveInXAxis = widthOfTile * progress;
mainTile.setTranslationX(moveInXAxis);
leftTile.setTranslationX(moveInXAxis - widthOfTile);
// Ignore the rest for now
topTile.setTranslationY(-1.0f);
diagonalTile.setTranslationX(-1.0f);
diagonalTile.setTranslationY(-1.0f);
}
}
I use my custom view like so in an activity:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.martianstudio.adivinaque.AppSettingsActivity">
<com.martianstudio.adivinaque.BackgroundScrollRelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/main_activity_animation_icon" />
</RelativeLayout>
The goal of this is to allowing the user to specify the background that they want to scroll with android:background. I do not want the parent RelativeLayout to take on this drawable as a background. Instead, I want the ImageViews to set the drawable as the background. I have overridden the setBackground in my custom view so that only the ImageViews set the drawable as the background and not the RelativeLayout (as it would by default).
#Override
public void setBackground(Drawable background) {
mainTile.setBackground(background);
leftTile.setBackground(background);
}
However, I get this error:
java.lang.NullPointerException at com.martianstudio.adivinaque.BackgroundScrollRelativeLayout.setBackground
This says that mainTile in setBackground has not been set (it is null). In my findAllViewsById() I explicitly write this.mainTile = (ImageView)this.findViewById(mainTileId);, where mainTileId = R.id.mainTile, and I inflate the layout in inflateLayout(). Both of these methods are called in the constructors of the class. It seems to me that for some reason, it cannot find the ImageView with the mainTile Id when I override the setBackground method like I do. If I don't override the setBackground method, I do not get a NullPointerException, however, it defaults to the default setBackground method, which is what I do not want. Am I missing somthing?
Any help will be appreciated :). Thank you!
Related
I have a class GameActivityas follows (I just post the relevant part):
public class GameActivity extends AppCompatActivity {
// initiate variables
private GameView mGameView;
private Display mDisplay;
private Point mSize;
public static int window_width = 0;
public static int window_height = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mDisplay = getWindowManager().getDefaultDisplay();
mSize = new Point();
mDisplay.getSize(mSize);
window_width = mSize.x;
window_height = mSize.y;
// initialize game view object
mGameView = new GameView(this);
// adding it to the content view
//setContentView(mGameView);
setContentView(R.layout.activity_game);
}
And I have the class GameView as follows with two constructors because of the custom view (also just the relevant parts to keep it clear):
private Context mContext;
//These objects will be used for drawing
private SurfaceHolder mSurfaceHolder;
private Paint mPaint;
public GameView(Context context){
super(context);
Log.d("TEST","Constructor 1");
init(context, null);
// clear all lists
...
// create object of map in beginning of game
...
// create objects of HQs
...
// create other sprite objects and add them to lists
...
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.d("TEST","Constructor 2");
init(context, attrs);
}
And I call in both of the constructors the init() method
private void init(Context context, AttributeSet attrs) {
mContext = context;
mSurfaceHolder = getHolder();
mPaint = new Paint();
mPaint.setColor(Color.DKGRAY);
}
My run() is like this:
#Override
public void run(){
Log.d("TEST","RUN");
long current_time = 0;
long old_time;
while (playing) {
old_time = current_time;
// update the game
update();
//to draw the frame
//onDraw(canvas);
draw();
//to control (lets the game sleep for some milliseconds)
control();
current_time = SystemClock.elapsedRealtime();
frametime = current_time - old_time;
framerate = 1000/ (current_time - old_time);
}
}
And the draw() (where the mistake happens) is like this:
private void draw() {
// BUG: mSurfaceHolder.getSurface.isValid ist niemals valid
Canvas canvas;
//if(true){
//checking if surface is valid
if (mSurfaceHolder.getSurface().isValid()) {
Log.d("Test","DRAW");
//locking the canvas
canvas = mSurfaceHolder.lockCanvas();
//drawing a background color for canvas
canvas.drawColor(Color.GREEN);
// call draw methods here
// bigger objects should be called before smaller objects for visibility
draw_ground(canvas);
draw_hq(canvas);
draw_soldiers(canvas);
draw_bullets(canvas);
draw_HUD(canvas);
//Unlocking the canvas
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
No the question is:
If I in the first class GameActivity pass the object mGameView of the class GameView to setContentView everything is fine and the draw() methods works normal which means mSurfaceHolder.getSurface().isValid()) is valid.
But if I put in the first class setContentView(R.layout.activity_game); then mSurfaceHolder.getSurface().isValid()) is never valid. Everything else works (e.g. I hear the game sound) but he doesn't draw. If I don't check for mSurfaceHolder.getSurface().isValid()) then the game crashes because in the code below in draw() the canvas is empty.
My aim is to create a custom view and overlay this with buttons.
Here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".GameActivity">
<!--Test-->
<com.example.mirco.battlefront.GameView
android:id="#+id/gameView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/button"
android:layout_width="119dp"
android:layout_height="wrap_content"
android:text="Button" />
<!--Test-->
</FrameLayout>
The XML seems to be correct because Android Studio recognizes it (custom view with overlayed button). But he doesn't draw in it.
Like I said with setContentView(mGameView); everything is fine except that I only have one view where I can't overlay buttons and so on which is not what I want.
What am I doing wrong? I tried to follow that LINK as good as possible. I am trying for days now to solve this but also can't find the solution anywhere.
Instead of
mGameView = new GameView(this);
// adding it to the content view
//setContentView(mGameView);
setContentView(R.layout.activity_game);
You should do
setContentView(R.layout.activity_game);
mGameView = (GameView)findViewById (R.id.gameView);
Then the mGameview is the one in your layout.
I need to create text display like the Confide app. What I tried is to use FlowLayout but then I'm not able to get the row so that I can hide show row wise. There seems to be various options but kinda confused and not able to think wwat exactly to do... Like break the TextView and show in ListView but then I don't know how to form lines and create adapter.
Kindly help me if anyone knows this thing. I tried to search on Google but nothing fruitful.
At present I'm showing TextView using FlowLayout and showing hiding each TextView.
Try creating custom view like this one, lest call it CustomTextView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:layout_marginTop="3dp"
android:background="#FF0000"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="3dp"
android:paddingBottom="3dp" >
<TextView
android:id="#+id/label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:maxLines="1"
android:textColor="#FFFFFF" />
</LinearLayout>
java code:
public class CustomTextView extends FrameLayout implements View.OnClickListener {
public CustomTextView(Context context) {
super(context);
init(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
LayoutInflater.from(context).inflate(R.layout.custom_text_view, this);
setOnClickListener(this);
mLabelTextView = (TextView) findViewById(R.id.label);
}
#Override
public void onClick(View v) {
mLabelTextView.setVisibility(View.VISIBLE)
}
}
To add this views to your container view try something like this:
Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
mContainerView.removeAllViewsInLayout();
mContainerView.removeAllViews();
int maxWidth = display.getWidth() - 20;
LinearLayout.LayoutParams params;
LinearLayout newLL = new LinearLayout(getContext());
newLL.setLayoutParams(new LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
newLL.setGravity(Gravity.LEFT);
newLL.setOrientation(LinearLayout.HORIZONTAL);
int widthSoFar = 0;
for (CustomTextView customTextView : CustomTextViewList) {
LinearLayout LL = new LinearLayout(getContext());
LL.setOrientation(LinearLayout.HORIZONTAL);
LL.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
LL.setLayoutParams(new ListView.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
customTextView.measure(0, 0);
params = new LinearLayout.LayoutParams(customTextView.getMeasuredWidth(), FrameLayout.LayoutParams.WRAP_CONTENT);
LL.addView(customTextView, params);
LL.measure(0, 0);
widthSoFar += customTextView.getMeasuredWidth();
if (widthSoFar >= maxWidth) {
mContainerView.addView(newLL);
newLL = new LinearLayout(getContext());
newLL.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
newLL.setOrientation(LinearLayout.HORIZONTAL);
newLL.setGravity(Gravity.LEFT);
params = new LinearLayout.LayoutParams(LL.getMeasuredWidth(), LL.getMeasuredHeight());
newLL.addView(LL, params);
widthSoFar = LL.getMeasuredWidth();
} else {
newLL.addView(LL);
}
}
mContainerView.addView(newLL);
Example of CustomTextViewList (to answer your question from comment)
ArrayList<CustomTextView> CustomTextViewList = new ArrayList<CustomTextView>()
CustomTextViewList.add(new CustomTextView(context));
CustomTextViewList.add(new CustomTextView(context));
CustomTextViewList.add(new CustomTextView(context));
I'm writing a simple UI activity, the intention of which is to create an offscreen OpenGL surface and have a GL test render into it in a background GL thread. The rendering should start at a click of the button and the TextView should display some environment information (OpenGL version, etc.) as well as the test progress (as it may take a while) and result. In the sample I'm providing I have a 5 second delay in the 'run' function of the Runnable, but in the actual app code I have a JNI call to a native library that has the GL rendering code.
I've verified that the problem I'm experiencing exists with the 5 second delay as well as the original JNI call. The problem is that while the test is running (a 5 second delay in this sample), UI seems to be frozen and does not update until the test is done. The text that I'm setting to my TextView only appears when the test is finished. I thought that "queueEvent(new TestRunnable());" starts the runnable in a separate GL thread so that UI thread can continue with no interruptions, but it does not seem to be the case, or something else is missing. Do you guys/gals have any idea what I'm doing wrong?
The simplified layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:keepScreenOn="true" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button
android:id="#+id/startTestButton" android:textSize="18sp"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="#string/startTestButton" android:onClick="onStartTestClick"/>
<ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:id="#+id/logView" android:textSize="18sp"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</ScrollView>
<com.company.name.MyView
android:id="#+id/myView"
android:layout_width="match_parent" android:layout_height="match_parent"/>
</LinearLayout>
The simplified activity class is defined as follows:
public class MyActivity extends Activity
{
private String m_LogText = null;
private Button m_StartTestButton = null;
private TextView m_TestLog = null;
private MyView m_SurfaceView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_StartTestButton = (Button) findViewById(R.id.startTestButton);
m_SurfaceView = (MyView) findViewById(R.id.myView);
m_TestLog = (TextView) findViewById(R.id.logView);
// Check for OpenGL 2.0 support.
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
ConfigurationInfo info = am.getDeviceConfigurationInfo();
m_LogText = "Open GLES version detected: " + Integer.toHexString(info.reqGlEsVersion) + "\n";
m_TestLog.setText(m_LogText);
}
public void onStartTestClick(View view)
{
//
// This text here does not appear until the end of the test:
//
m_LogText += "\nStarting the test: default\n";
m_LogText += "Test is running, please wait...\n";
m_TestLog.setText(m_LogText);
m_SurfaceView.StartTest();
}
}
Simplified MyView class is defined as follows:
public class MyView extends GLSurfaceView
{
private TestRenderer m_Renderer = null;
private class TestRenderer implements GLSurfaceView.Renderer
{
public void onDrawFrame(GL10 gl) { }
public void onSurfaceChanged(GL10 gl, int width, int height) { }
public void onSurfaceCreated(GL10 gl, EGLConfig config) { }
class TestRunnable implements Runnable
{
public void run()
{
try
{
Thread.sleep(5000);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
}
public void StartTest()
{
queueEvent(new TestRunnable());
}
}
public MyView(Context context)
{
this(context, null);
}
public MyView(Context context, AttributeSet attrs)
{
super(context, attrs);
m_Renderer = new TestRenderer();
setRenderer(m_Renderer);
}
public void StartTest()
{
m_Renderer.StartTest();
}
}
I have a subclass called AuditQuestionEntry that extends LinearLayout. In addition to other fields, this layout contains a button. On my main activity ("AuditActivity") I have multiple instances of AuditQuestionEntry. From AuditActivity, I am setting an OnClickListener for the button in each of the AuditQuestionEntry layouts. In the OnClick method I need to access a parameter that's in the AuditQuestionEntry associated with the button that was clicked.
In the OnClickListener I call getParent() and I can cast that to AuditQuestionEntry. However, when I try to access a parameter from there it returns null (or 0 in my case because it's an int). Basically, it appears that getParent() is returning a new instance of the class and not the actual instance that contains the button.
Layout - audit_question_entry.xml:
<TextView
android:id="#+id/auditQuestion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Audit Question..."
android:textSize="20dp" />
<Button
android:id="#+id/btnTakeAuditPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Photo" >
</Button>
Here's the AuditQuestionEntry.java:
public class AuditQuestionEntry extends LinearLayout {
// Define controls
private TextView auditQuestion;
private RatingBar auditRating;
private EditText auditComment;
private ImageView imageView;
private Button photoButton;
private static final int CAMERA_REQUEST = 1888;
private static final int REQUEST_IMAGE_CAPTURE = 1;
public int AuditQuestionNumber; // NEED TO GET THIS!
private Context _ctx;
public ClosetAuditQuestionObj AuditQuestion;
private String PhotoFolder;
private String FullPhotoPathAndName;
public AuditQuestionEntry(Context context) {
super(context);
// InflateView();
throw new RuntimeException("Valid AuditQuestionNumber must be passed to this class via the XML parameters: workbench:AuditQuestionNumber.");
}
public AuditQuestionEntry(Context context, AttributeSet attrs) {
super(context, attrs);
this._ctx = context;
initAttributes(attrs);
}
// Used to grab the AuditQuestionNumber attribute from the XML declaration
// for this layout
private void initAttributes(AttributeSet attrs) {
TypedArray a = _ctx.obtainStyledAttributes(attrs, R.styleable.AuditQuestionEntry);
AuditQuestionNumber = attrs.getAttributeIntValue("http://schemas.android.com/apk/com.bc.workbench", "AuditQuestionNumber", 0);
a.recycle();
}
Inside AuditActivity onCreate() I have the following:
OnClickListener btnPhotoListener = new OnClickListener() {
#Override
public void onClick(final View v) {
View viewParent = (View) v.getParent();
AuditQuestionEntry clickedAudit = (AuditQuestionEntry) viewParent;
// On the following line, clickedAudit.AuditQuestionNumber is always 0 even though it was initialized and
// displays properly on screen.
Toast.makeText(CurrentContext, "Audit Question #" + clickedAudit.AuditQuestionNumber, Toast.LENGTH_SHORT).show();
}
};
// Set event listener for photo buttons
// =============================================
((Button) AuditQuestion1.findViewById(R.id.btnTakeAuditPhoto)).setOnClickListener(btnPhotoListener);
((Button) AuditQuestion2.findViewById(R.id.btnTakeAuditPhoto)).setOnClickListener(btnPhotoListener);
Where in the XML are you setting the attribute for the AuditQuestionNumber? The constructor is defaulting the value of the class attribute to 0 if the XML being used to inflate the class doesn't include the attribute:
AuditQuestionNumber = attrs.getAttributeIntValue("http://schemas.android.com/apk/com.bc.workbench", "AuditQuestionNumber", 0);
I'm making an app that just displays a clock, but I want is so that everytime a user touches the screen it changes the color of the text (from a list of preselected colors in a colors.xml file) but I haven't got a clue where to start. Can someone point me in the right direction?
Here's the main activity:
public class MainActivity extends Activity {
private static final Random RANDOM = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
Handler handler = new RandomMoveHandler((TextView) findViewById(R.id.digitalClock1));
handler.sendEmptyMessage(0);
}
// Make the handler subclass static because of this: http://stackoverflow.com/a/11408340/111777
private static class RandomMoveHandler extends Handler {
private final WeakReference<TextView> textViewWeakReference;
private RandomMoveHandler(TextView textView) {
this.textViewWeakReference = new WeakReference<TextView>(textView);
}
#Override
public void handleMessage(Message msg) {
TextView textView = textViewWeakReference.get();
if (textView == null) {
Log.i(TAG, "WeakReference is gone so giving up.");
return;
}
int x = RANDOM.nextInt(350 - 100);
int y = RANDOM.nextInt(800 - 100);
Log.i(TAG, String.format("Moving text view to (%d, %d)", x, y));
textView.setX(x);
textView.setY(y);
//change the text position here
this.sendEmptyMessageDelayed(0, 30000);
}
}
private static final String TAG = MainActivity.class.getSimpleName();
}
and here's the layout xml:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity"
android:background="#color/black" >
<DigitalClock
android:id="#+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DigitalClock"
android:textColor="#color/ics_blue"
android:textSize="28sp" />
I haven't making deal with DigitalClock but I think, at first, you should reference DigitalClock variable, not TextView. And second, to intercept touch event you need to override onTouckEvent method of your activity, it will callback everytime user touches the screen.
You should follow these steps
Use a TimerTask to.continusly show the time
Implement a touchlistener on that clock view
like this
view.setOnTouchListener
Make an array Colors like this
int[] colr={Color.BLACK,Color.BLUE};
and use random index in your touch event andset it as your color of the view