Friends
I am new on Android App Developing. I have nearly completed my app.
In the last stage, I'm facing difficulties and trying for the last three days to solve it but could not.
I need to get the current hour and minutes from a time picker (without Dialog) when the user clicks a button. I want to save this data to a database on this button click.
Would you anybody please help me providing just an XML and JAVA file as below.
XML:
<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.example.timepicker.MainActivity" >
<TimePicker
android:id="#+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="21dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/timePicker1"
android:layout_centerHorizontal="true"
android:layout_marginTop="77dp"
android:text="Button" />
</RelativeLayout>
Java:
package com.example.timepicker;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Using Calendar instance you can get current hour and minutes-
Calendar c;
private int mhour;
private int mminute;
initialize -
c = Calendar.getInstance();
mhour = c.get(Calendar.HOUR);
mminute = c.get(Calendar.MINUTE);
// you will get current hour and minutes of device time
Also refer this Calendar
Related
I am doing Android programming for the first time, I want to display a GeoServer layer using the ArcGis SDK map layer. I have done the code but it is not working, please tell me what i am doing wrong.
activity_main.xml
<?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:id="#+id/activity_main"
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.example.subrata.mymap.MainActivity">
<com.esri.android.map.MapView
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
mapoptions.MapType="Aerial"
mapoptions.center="28, 77"
mapoptions.ZoomLevel="10">
</com.esri.android.map.MapView>
</RelativeLayout>
mainactivity.java
package com.example.subrata.mymap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.esri.android.map.MapView;
import com.esri.android.map.ogc.WMSLayer;
public class MainActivity extends AppCompatActivity {
MapView mMapView;
WMSLayer wmsLayer;
String wmsURL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapView = (MapView)findViewById(R.id.map);
wmsURL = "http://localhost:8081/geoserver/NFR-SR/wms";
wmsLayer = new WMSLayer(wmsURL);
wmsLayer.setImageFormat("application/openlayers");
// available layers
String[] visibleLayers = {"NFR-SR:Ridgeline_SR"};
wmsLayer.setVisibleLayer(visibleLayers);
wmsLayer.setOpacity(0.5f);
mMapView.addLayer(wmsLayer);
mMapView.setEsriLogoVisible(false);
mMapView.enableWrapAround(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Can't really tell what you mean by not working without any log or explanation.
Here are some possible reason:
cannot reach server at url http://localhost:8081/geoserver/NFR-SR/wms
layer name NFR-SR:Ridgeline_SR not found
map service doesn't support application/openlayers
It could even be it is working but you are viewing the map extent which does not contain any data.
I am new to Android and building a small test program and the program is not running and giving me Could not create the view: org.eclipse.pde.runtime.LogView error
here is the code from my android_main.xml file
<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.example.myapplication.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
Here is the java code from MainActivity.Java
package com.example.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I apologize that I am posting this as an answer instead of a comment, but commenting requires 50 reputation and I don't have that yet. Can you post your java code too? The problem is likely in there, it may be the method by which you are loading the view.
I need to create three buttons in Android Studio and each button shows different text. When the user clicks on first button it shows "Welcome" at the right side of the button. When the user click on the second button the "Welcome" message will disappear, and in the same place a"Hello" message will appear. Third button is the same with difference message"Bye". Here my code and thanks in advance.
XML Code:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.android.Recipes">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R1"
android:id="#+id/click_btn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/response"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/click_btn"
android:layout_toEndOf="#+id/click_btn" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R2"
android:id="#+id/button15"
android:layout_below="#+id/click_btn"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="34dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R3"
android:id="#+id/button16"
android:layout_centerVertical="true"
android:layout_alignRight="#+id/button15"
android:layout_alignEnd="#+id/button15" />
Java Code:
package com.example.android;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import static com.example.android.R.*;
public class Recipes extends ActionBarActivity implements View.OnClickListener {
TextView resp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_recipes);
resp = (TextView)this.findViewById(id.response);
Button b = (Button)this.findViewById(id.click_btn);
b.setOnClickListener(this);
resp.setText("Welcome ");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_recipes, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
resp.setText("Welcome ");
}
}
Set setOnClickListener for all Button's in same way as currently doing for click_btn Button .
To change text of TextView clicked use switch-case inside onClick method like:
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.click_btn: /** on click_btn button click */
resp.setText("Welcome ");
break;
case R.id.button15: /** on button15 button click */
resp.setText("Hello ");
break;
case R.id.button16: /** on button16 button click */
resp.setText("Bye ");
break;
}
}
Actually i am trying to make an android app to display a text when the battery becomes 50% ,m using text view to display the msg,but while loading the app it gets unfortunately stops.
here is my code:
Java file.
package com.example.abhi.batteryalarm;
import android.content.Intent;
import android.os.BatteryManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class Battery_Alarm extends ActionBarActivity {
Intent batteryStatus;
TextView txtView;
String hello;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_battery__alarm);
txtView=(TextView)findViewById(R.id.txtView);
hello="This is my first project";
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
if (level==50)
{
txtView.setText(hello);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_battery__alarm, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity file:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Battery_Alarm">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textSize="62sp"
android:id="#+id/txtView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="125dp" />
</RelativeLayout>
What things i need to do?
Add this,
batteryStatus = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
before,
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
and it will work.
This is the line which is causing the error :
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
It's because the batteryStatus intent is never initialized and is hence null.
Check out this answer about fetching the battery level : https://stackoverflow.com/a/4047790/1239966
You need to register batteryStatus.
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
I am writing my first android app after reading a tutorial. I am excited and thought its easy. However, after adding a simple edit text and a hello world message, i run and was expecting to see something yet nothing showed up. Neither the textbox nor the default hello world. Pls where do go wrong ? Below is my layout (i.e activity_main.xml)
<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:paddingLeft="16dp"
android:paddingRight="16dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:textColor="#FF00FF00"/>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textColor="#FF0000FF"
android:id="#+id/et_Text" />
</RelativeLayout>
My java code is the default/code generated one. I was expecting to see atleast the message for a start yet didn't show any control. Just a screen with a phone dialers on the rigth hand.
package com.example.mytest;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Since you are using RelativeLayout, you need to tell Android how each control is relative to each other. Change the layout as follow:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:id="#+id/textView1"
android:textColor="#FF00FF00"/>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#+id/textView1"
android:textColor="#FF0000FF"
android:id="#+id/et_Text" />
This will tell the system to place the EditText below the TextView.