8 Replies - 5447 Views - Last Post: 26 December 2011 - 03:54 PM

#1 Sepharas   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-December 11

How to create an Analog Clock in java (without xml)

Posted 25 December 2011 - 07:29 AM

Hi,

I'm making my own clock widget with my own skins. While using only one layout (declared in xml) there were no problems.
But now i'm implementing different layouts so i created a "set up activity" where the user can select his prefered layout options. Therefor i tried to create a AnalogClock without XML-declarations:
AnalogClock clock = new AnalogClock(context);


But i don't know how to set the dial and hands for hour and minute.

Is This A Good Question/Topic? 0
  • +

Replies To: How to create an Analog Clock in java (without xml)

#2 H3R3T1C   User is offline

  • Android Expert
  • member icon

Reputation: 278
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: How to create an Analog Clock in java (without xml)

Posted 25 December 2011 - 10:06 AM

Is AnalogClock a View or an Activity?
Go ahead and post your code for your AnalogClock so we can see what your trying to do.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12657
  • View blog
  • Posts: 45,830
  • Joined: 27-December 08

Re: How to create an Analog Clock in java (without xml)

Posted 25 December 2011 - 10:12 AM

AnalogClock doesn't seem to add much in the way of XML attributes in addition to those it inherits. Start with the documentation. There are usually corresponding getter/setter methods associated with the XML elements and attributes.
Was This Post Helpful? 0
  • +
  • -

#4 Sepharas   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-December 11

Re: How to create an Analog Clock in java (without xml)

Posted 25 December 2011 - 11:00 AM

here's my set up:

public class set_up_Activity extends Activity {
    private int mAppWidgetId;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Set the result to CANCELED.  This will cause the widget host to cancel
        // out of the widget placement if they press the back button.
        setResult(RESULT_CANCELED);

        setContentView(R.layout.set_up);
       
        // get App Widget ID from intent that launched activity
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(
                    AppWidgetManager.EXTRA_APPWIDGET_ID, 
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }
        
        
        final Button button = (Button) findViewById(R.id.button_create);
        button.setonclickListener(new View.onclickListener() {
			public void onclick(View v) {
                // Perform action on click
				final Context context = set_up_Activity.this;				
            	
            	// get an instance of the AppWidgetManager
            	AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            	
            	/**
            	// update the App Widget
            	RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            	appWidgetManager.updateAppWidget(mAppWidgetId, views);
            	**/
            	
            	RelativeLayout rLayout = new RelativeLayout(context);
            	rLayout.setGravity(17);  // 17 = CENTER            	          	            	
            	AnalogClock clock = new AnalogClock(context);
            	rLayout.addView(clock);           	
            	appWidgetManager.updateAppWidget(mAppWidgetId, rLayout);    // this line isn't working cause of rLayout

            	
            	// create the return Intent, set it with the Activity result, and finish the Activity
            	Intent resultValue = new Intent();
            	resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            	setResult(RESULT_OK, resultValue);
            	finish();
            }
        }); 

    }
}



So this would apply the defined XML-layout:
                /**
            	// update the App Widget
            	RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            	appWidgetManager.updateAppWidget(mAppWidgetId, views);
            	**/




And i'm trying to create a dynamic clock like this:
                RelativeLayout rLayout = new RelativeLayout(context);
            	rLayout.setGravity(17);  // 17 = CENTER            	          	            	
            	AnalogClock clock = new AnalogClock(context);
            	rLayout.addView(clock);           	
            	appWidgetManager.updateAppWidget(mAppWidgetId, rLayout);    // this line isn't working cause of rLayout



So what i need is:
1) how can i set the dial etc. ?
2) get this RelativeLayout (rLayout) into a RemoteViews so i can update the widget?

PS: this is my first android app... i tried to solve these problems on my own by using the search function on multiple forums and by trying to get helpful information out of the documentation.
Was This Post Helpful? 0
  • +
  • -

#5 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: How to create an Analog Clock in java (without xml)

Posted 25 December 2011 - 11:57 PM

View PostSepharas, on 25 December 2011 - 06:00 PM, said:

here's my set up:

public class set_up_Activity extends Activity {
    private int mAppWidgetId;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Set the result to CANCELED.  This will cause the widget host to cancel
        // out of the widget placement if they press the back button.
        setResult(RESULT_CANCELED);

        setContentView(R.layout.set_up);
       
        // get App Widget ID from intent that launched activity
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(
                    AppWidgetManager.EXTRA_APPWIDGET_ID, 
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }
        
        
        final Button button = (Button) findViewById(R.id.button_create);
        button.setonclickListener(new View.onclickListener() {
			public void onclick(View v) {
                // Perform action on click
				final Context context = set_up_Activity.this;				
            	
            	// get an instance of the AppWidgetManager
            	AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            	
            	/**
            	// update the App Widget
            	RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            	appWidgetManager.updateAppWidget(mAppWidgetId, views);
            	**/
            	
            	RelativeLayout rLayout = new RelativeLayout(context);
            	rLayout.setGravity(17);  // 17 = CENTER            	          	            	
            	AnalogClock clock = new AnalogClock(context);
            	rLayout.addView(clock);           	
            	appWidgetManager.updateAppWidget(mAppWidgetId, rLayout);    // this line isn't working cause of rLayout

            	
            	// create the return Intent, set it with the Activity result, and finish the Activity
            	Intent resultValue = new Intent();
            	resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            	setResult(RESULT_OK, resultValue);
            	finish();
            }
        }); 

    }
}



So this would apply the defined XML-layout:
                /**
            	// update the App Widget
            	RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            	appWidgetManager.updateAppWidget(mAppWidgetId, views);
            	**/




And i'm trying to create a dynamic clock like this:
                RelativeLayout rLayout = new RelativeLayout(context);
            	rLayout.setGravity(17);  // 17 = CENTER            	          	            	
            	AnalogClock clock = new AnalogClock(context);
            	rLayout.addView(clock);           	
            	appWidgetManager.updateAppWidget(mAppWidgetId, rLayout);    // this line isn't working cause of rLayout



So what i need is:
1) how can i set the dial etc. ?
2) get this RelativeLayout (rLayout) into a RemoteViews so i can update the widget?


1. You're going to need to create your own analog clock view, if you want to be able to set the time, as the current api is no help.

This post has been edited by farrell2k: 25 December 2011 - 11:59 PM

Was This Post Helpful? 1
  • +
  • -

#6 Sepharas   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-December 11

Re: How to create an Analog Clock in java (without xml)

Posted 26 December 2011 - 06:20 AM

Perhaps i could use the AttributeSet. But i don't know how to set a AttributeSet without parsing it out of a XML-file.
Was This Post Helpful? 0
  • +
  • -

#7 Sepharas   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-December 11

Re: How to create an Analog Clock in java (without xml)

Posted 26 December 2011 - 06:34 AM

AnalogClock test = new AnalogClock(this);
test.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,  LayoutParams.FILL_PARENT));



can i set the dial picture like above
Was This Post Helpful? 0
  • +
  • -

#8 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: How to create an Analog Clock in java (without xml)

Posted 26 December 2011 - 02:50 PM

View PostSepharas, on 26 December 2011 - 01:20 PM, said:

Perhaps i could use the AttributeSet. But i don't know how to set a AttributeSet without parsing it out of a XML-file.


You misunderstand AttributeSet. http://www.w3schools..._attributes.asp
Was This Post Helpful? 1
  • +
  • -

#9 Sepharas   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-December 11

Re: How to create an Analog Clock in java (without xml)

Posted 26 December 2011 - 03:54 PM

ok yes i misunderstood attributeSet... Thanks a lot.

Well i think there's no way to avoid creating my own clock view.

This topic can be closed. I don't know how to close it so it would be nice if an admin would do that =P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1