how to use timepicker to make alarm clock - java

Right now I am making a simple alarm clock. It has to activities. One is main and the other is "test". On the main I have created one button to set an alarm. The button is called "new" and it lead to the second activity test. In test I have a time picker and two buttons. One called "cancel" which leads back to main activity. The other is "done" which should save the time that I have picked on the time picker. I have the done button working but I don't know how to do the rest. I am very new to programming. Right now I want to know how to safe the time that I have picked on the time picker so I can access it later on to compare it with the real time. I hope to save the time when I click the "done" button. Please be as specific as possible. I am really new to this. Thank you!!!!!!!!
below are the codes I have so far
activity_test.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=".Test" >
<TimePicker
android:id="#+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp" />
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/timePicker1"
android:layout_centerHorizontal="true"
android:layout_marginTop="51dp"
android:onClick="doneButton"
android:text="Done" />
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/done"
android:layout_below="#+id/done"
android:layout_marginTop="52dp"
android:onClick="cancelButton"
android:text="Cancel" />
</RelativeLayout>
Test.java
package com.example.alarmclock;
import java.util.Calendar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
public class Test extends Activity implements View.OnClickListener{
Button cancelButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
cancelButton = (Button)findViewById(R.id.cancel);
cancelButton.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test, menu);
return true;
}
private void cancelButton(View v){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public void onClick (View v){
switch (v.getId()){
case R.id.done:
cancelButton(v);
break;
}
}
}

Related

Method showAtLocation cannot be resolved

I am attempting to create a popup menu that comes from a button at the bottom right corner of the screen. The issue is that the menu would need to be displayed above the button. Ive botched together some code from Android. How to show popup window directly above button. However, the showAtLocation method does not resolve. Another issue that I believe is related to this is that the activity crashes ever since I added my onMenuItemClick() switch statements. Any help would be greatly appreciated :) and more code can be found at my Github
--edit---
So it turns out that showAtLocation only works for popup windows, however I am using a popup menu. So the question becomes, ho wdo you make a popup display above a button rather than below?
WorkoutsCreater.java:
package com.example.workoutapp;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;
public class WorkoutsCreater extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workouts_creater);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Workout Creater"); //change text at top of screen accordingly
Button btn=findViewById(R.id.BtnNew);
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
PopupMenu popup = new PopupMenu(WorkoutsCreater.this, v); //display menu when button clicked
popup.setOnMenuItemClickListener(WorkoutsCreater.this);
popup.inflate(R.menu.workout_new_popup_menu);
popup.showAtLocation(v, Gravity.TOP, 0, (int) v.getY()); //show popup above button
}
});
}
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(this, "Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show();
switch (item.getItemId()) { //testing which button is pressed in menu
case R.id.RepBased:
System.out.println("Rep Based");
return true;
case R.id.RunBased:
System.out.println("Run Based");
return true;
case R.id.TimeBased:
System.out.println("Time Based");
return true;
default:
return false;
}
}
}
workout_new_popup_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/RepBased"
android:icon="#drawable/ic_workouts"
android:title="Rep Based" />
<item android:id="#+id/RunBased"
android:icon="#drawable/ic_run_black"
android:title="Run Based" />
<item android:id="#+id/TimeBased"
android:icon="#drawable/ic_time_based"
android:title="Time Based" />
</menu>
activity_workouts_creater.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="#+id/ConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WorkoutsCreater">
<TextView
android:id="#+id/NameWorkoutTextView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:gravity="center"
android:text="Name of Workout:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textSize="15dp"
app:layout_constraintBottom_toBottomOf="#+id/NameWorkoutInput"
app:layout_constraintEnd_toStartOf="#+id/NameWorkoutInput"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/NameWorkoutInput" />
<EditText
android:id="#+id/NameWorkoutInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="40dp"
android:layout_marginRight="40dp"
android:inputType="text"
android:maxLength="20"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/NameWorkoutTextView"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/BtnNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#drawable/ic_add_white" />
</androidx.constraintlayout.widget.ConstraintLayout>
showAtLocation() is a PopupWindow method, not a PopupMenu method.
The code you copied uses it on PopupWindow as well.
You can read more about PopupWindow here and about the specific method here
v.setOnTouchListener(popup.getDragToOpenListener()); fixes the popup issue and the crashing was caused by using the wrong button type. Turns out you cannot use floating action buttons with popup menus

java - Android app crashes when button is pressed

I am trying to get a part of a simple Android app working. I am confused as to why I am getting the error "Unfortunately, this app has stopped working".
Basically, I need the total value to increase when I click on a button based on what value they have. For example coffee adds 2 to the total. Every time I click a button that adds to the total value, the app crashes.
Here is the source code. I have just copied the parts that are specific to this problem.
package com.example.pp.application;
import android.content.Intent;
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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements AdapterView.OnItemSelectedListener {
TextView tv01;
Double Total = 0.00;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv01 = (TextView) findViewById(R.id.tv01);
}
#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);
}
public void sendMessage(View view)
{
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
public void addCoffee() {
Total = Total + 2.20;
tv01.setText(""+Total+"");
}
public void addBus() {
Total = Total + 1.90;
tv01.setText(""+Total+"");
}
public void addMilk() {
Total = Total + 1.50;
tv01.setText(""+Total+"");
}
}
And here is the Manifest:
<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=".MainActivity"
android:id="#+id/label">
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Coffee"
android:id="#+id/button"
android:onClick="addCoffee"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="42dp" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Bus"
android:onClick="addBus"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Milk"
android:onClick="addMilk"
android:id="#+id/button4"
android:layout_below="#+id/button3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:onClick="sendMessage"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/button5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="------------------"
android:id="#+id/tv01"
android:layout_alignRight="#+id/button"
android:layout_alignEnd="#+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Total"
android:id="#+id/textView"
android:layout_toStartOf="#+id/tv01"
android:layout_toLeftOf="#+id/tv01" />
Instead of public void addCoffee() your onClick method definition should look like public void addCoffee(View v), where v is the widget you clicked on (in this case the Button). Take the same approach when updating other onClick methods. This requirement applies only to callbacks defined via android:onClick attribute in XML layout.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv01 = (TextView) findViewById(R.id.tv01);
public void addCoffee(View v) {
Total = Total + 2.20;
tv01.setText(""+Total+"");
}
}
and continue with another function with same way
Since you haven't posted the stack trace of the LogCat, I have no way of telling when in the code your error has been.
But it's likely to have occured before. These links contain similar stances and documentations about exceptions.
My Android App Keeps Crashing
https://developers.google.com/analytics/devguides/collection/android/v4/exceptions
https://developer.android.com/distribute/essentials/optimizing-your-app.html
Android App Crashes on Button Click

Getting Data from Multiple EditText after a click of single Button

I'm totally new to this forum and need help from you guys. I've been developing an Android application to find the efficiency of an Induction Motor. I'm new to Android App developement so I need your help.
I've created the layout and I have multiple edit text to get the inputs from the user, all I want to do is get the input from the edittext on the press of a single button. Can anyone help me with the java coding
The XML Part of the layout is as follows:
<LinearLayout 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:orientation="vertical"
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=".StartingPoint" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="THE EFFICIENCY IS 0"
android:gravity="center"
android:textSize="25dp"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="ENTER VOLTAGE"
android:gravity="center"
android:id="#+id/etVoltage"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="ENTER CURRENT"
android:gravity="center"
android:id="#+id/etCurrent"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="ENTER FREQUENCY"
android:gravity="center"
android:id="#+id/etFrequency"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="ENTER STATOR PHASE RESISTANCE"
android:gravity="center"
android:id="#+id/etStatorresistance"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="ENTER NO LOAD POWER"
android:gravity="center"
android:id="#+id/etLoadpower"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text= "FIND EFFICIENCY"
android:gravity="center"
android:id="#+id/bFind"
/>
</LinearLayout>
The Java Part of the coding is StartingPoint.java
package com.blitzkrieg.vishwas;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class StartingPoint extends Activity {
EditText current, voltage, frequency, loadpower, statorresistance;
Button find;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
EditText current = (EditText) findViewById(R.id.etCurrent);
EditText voltage = (EditText) findViewById(R.id.etVoltage);
EditText frequency= (EditText)findViewById(R.id.etFrequency);
EditText loadpower= (EditText)findViewById(R.id.etLoadpower);
EditText statorresistance= (EditText)findViewById(R.id.etStatorresistance);
Button find = (Button) findViewById(R.id.bFind);
find.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.starting_point, menu);
return true;
}
}
Doubts that I've in my mind regarding this coding
Is the use of findViewbyId for edittext correct over here, I want to get the input values from the user using edit text.
Where in this Java coding shall I put the line of code to fetch the data from all the editText.
I want to get the result that is the efficiency of the motor just with the press of a single button which "Find Efficiency" in case of this program.
Is the data entered into the editText automatically fetched after enterin the data in that edit Text or do I need to add some buttons for fetching the data?
Please Help me guys.
Just replace your code with this code.It think this code will do your job.
1.You have declared Edittext reference correctly but you dont have to again n again declared in your code.Before your onCreate() method referencing Editext is all right. This may give you quite a handle say global which you can use in your this class everywhere you want.Inside your onCreate() you write this;
EditText current = (EditText) findViewById(R.id.etCurrent);
But declaring like this you cannot get this EditText value inside a button or where ever because this EditText has limited scope.Write like this;
current = (EditText) findViewById(R.id.etCurrent);
2.When you click on button you wanna get the EditText values.You should have to do like below i have done inside onClick().
3.As i get values in String inside onClick().Show them in LogCat.Display it here the result.
4.When you click the button all the data in every EditText will you get in String.Now do whatever you want.
package com.blitzkrieg.vishwas;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class StartingPoint extends Activity {
EditText current, voltage, frequency, loadpower, statorresistance;
Button find;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
current = (EditText) findViewById(R.id.etCurrent);
voltage = (EditText) findViewById(R.id.etVoltage);
frequency= (EditText)findViewById(R.id.etFrequency);
loadpower= (EditText)findViewById(R.id.etLoadpower);
statorresistance= (EditText)findViewById(R.id.etStatorresistance);
Button find = (Button) findViewById(R.id.bFind);
find.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
/* This code get the values from edittexts */
String currentvalue = current.getText().toString();
String voltagevalue = voltage.getText().toString();
String frequencyvalue = frequency.getText().toString();
String loadpowervalue = loadpower.getText().toString();
String statorresistancevalue = statorresistance.getText().toString();
/*To check values what user enters in Edittexts..just show in logcat */
Log.d("currentvalue",currentvalue);
Log.d("voltagevalue",voltagevalue);
Log.d("frequencyvalue",frequencyvalue);
Log.d("loadpowervalue",loadpowervalue);
Log.d("statorresistancevalue",statorresistancevalue);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.starting_point, menu);
return true;
}
}

Methods in extended class are not working

I created a menu, where you can swipe up and down. I created this menu as another activity. Now, I need this menu to be added to on other activities.
Following is the code of my menu(SlidingDrawer)
Java
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class DropDownMenu extends Activity {
private TextView addInquiry, addEvent, additionalInfo, addToContacts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drop_down_menu);
//Intializing instance variables
addInquiry = (TextView)findViewById(R.id.menu_add_inquiry);
addEvent = (TextView)findViewById(R.id.menu_add_event);
additionalInfo = (TextView)findViewById(R.id.menu_additional_info);
addToContacts = (TextView)findViewById(R.id.menu_add_to_contacts);
//Register the Listeners
addInquiry.setOnClickListener(new AddInquiryEvent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.drop_down_menu, menu);
return true;
}
//Test Button
private class AddInquiryEvent implements OnClickListener
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Intent intent = new Intent(DropDownMenu.this,NewLead.class);
startActivity(intent);
}
}
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SlidingDrawer
android:id="#+id/SlidingDrawer"
android:layout_width="wrap_content"
android:layout_height="250dip"
android:layout_alignParentBottom="true"
android:content="#+id/contentLayout"
android:handle="#+id/slideButton"
android:orientation="vertical" >
<Button
android:id="#+id/slideButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/bottom_bar" >
</Button>
<LinearLayout
android:id="#+id/contentLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip"
android:background="#cbcbcc" >
<TextView
android:id="#+id/menu_add_inquiry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="#string/add_inquiry"
android:textColor="#ffffff"
android:clickable="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
Following is the Code of the other activity
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/sales_inqury_main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
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=".SalesInqury" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:text="#string/sales_inqury"
android:textSize="40sp" />
<include layout = "#layout/activity_drop_down_menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Java
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SalesInqury extends DropDownMenu {
private ImageView addNewSalesInqury;
private RelativeLayout salesInquryMainLayout;
private TextView testEditSales;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sales_inqury);
//Intializing instance variables
addNewSalesInqury = (ImageView)findViewById(R.id.add_new_sales_inqury_btn);
salesInquryMainLayout = (RelativeLayout)findViewById(R.id.sales_inqury_main_layout);
testEditSales = (TextView)findViewById(R.id.testWord);
}
}
But, there is an issue. Even thougn I can open the menu in this other activity, I can't click on it's Buttons (TextViews) and navigate to other activities. Why is this?
I think it's because after you've set a contentView in your DropDownMenu activity and bound the listener to the button, in SalesInquiry you set a different contentView. Although this includes the menu-layout, the Views are created anew, so there's no Listener bound to the button anymore.
A solution would either be to specify the menu callbacks in the layout xml via android:onClick="..." or by having a separate method in DropDownMenu that adds the Listener and that you call from your SalesInquiry class after setting the content view.
Example 1, specifying callback in XML
Add android:onClick="onMenuItemClicked" to menu item TextView
<TextView
android:id="#+id/menu_add_inquiry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="#string/add_inquiry"
android:textColor="#ffffff"
android:clickable="true"
android:onClick="onMenuItemClicked"
android:textAppearance="?android:attr/textAppearanceMedium"/>
Add this method to DropDownMenu class and remove the AddInquiryEvent class and the registering of the listeners.
public void onMenuItemClicked(View view) {
switch (view.getId()) {
case R.id.menu_add_inquiry:
Intent intent = new Intent(DropDownMenu.this,NewLead.class);
startActivity(intent);
break;
case R.id.menu_other:
// handle other menu item
break;
// ... and so on ...
default:;
}
}
Example 2, bind listeners in a separate method
In DropDownMenu move the registering of the Listeners to a separate method:
protected void registerListeners() {
//Register the Listeners
findViewById(R.id.menu_add_inquiry).setOnClickListener(new AddInquiryEvent());
}
Note, that you have to find the view again, as the one kept in the local variable is the wrong one, after SalesInquiry set it's content view.
Call the method in SalesInquiry after setting the content view.
setContentView(R.layout.activity_sales_inqury);
registerListeners();
I'm not sure. but can you please try with extending the DropDownMenu-Activity instead of Activity.
public class NewActivity extends DropDownMenu {
// override which you want to override
}
And make sure DropDownMenu in such a way that you can extends what you want to implements. Hope you have got my point.
Consistency Issue (Recommendation)
There is a HUGE misnomer in your class name, you should NOT call an Activity DropDownMenu.
This is confusing, you should instead call it DropDownActivity or something like that. Also do you really need to use an Activity? You do realize a View can still listen to the click of a button right? Not only an Activity can listen to button clicks. If you encapsulate all of this in a View and have a dedicated listener for that view, then you wouldn't have any need for another Activity.
Possible Solution
It's likely related to the activity/component you have registered to listen to the buttons is not correct. Validate your code so that you can at least get the correct response of the onClick.
The problem is that you are attaching the listener to addInquiry in the DropDownMenu:
//Register the Listeners
addInquiry.setOnClickListener(new AddInquiryEvent());
This listener is not attached to the TextView in the extended class, since the setContentView is called and layout is refreshed.
Possible solution, if you are not using the DropDownMenu activity on its own then simply (remove setContentView):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //in DropDownMenu
// setContentView(R.layout.activity_drop_down_menu);
...
...
Not the best solution but would work.
Update: Better Solution
(1) Let the subclass decide which layout to show. This layout must contain the menu (which is true in your case).
public class DropDownMenu extends Activity {
private TextView addInquiry, addEvent, additionalInfo, addToContacts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_setContentView();
//Intializing instance variables
addInquiry = (TextView)findViewById(R.id.menu_add_inquiry);
addEvent = (TextView)findViewById(R.id.menu_add_event);
additionalInfo = (TextView)findViewById(R.id.menu_additional_info);
addToContacts = (TextView)findViewById(R.id.menu_add_to_contacts);
//Register the Listeners
addInquiry.setOnClickListener(new AddInquiryEvent());
}
protected void _setContentView() {
setContentView(R.layout.activity_drop_down_menu);
}
...
...
}
#override the _setContentView method so that we do not set the view twice.
public class SalesInqury extends DropDownMenu {
...
...
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
//Intializing instance variables
addNewSalesInqury = (ImageView)findViewById(R.id.add_new_sales_inqury_btn);
salesInquryMainLayout = (RelativeLayout)findViewById(R.id.sales_inqury_main_layout);
testEditSales = (TextView)findViewById(R.id.testWord);
}
#Override
protected void _setContentView() {
// need to make sure this layout contains the menu (otherwise NullPointerException may arise)
setContentView(R.layout.activity_sales_inqury);
}
}

Android - full screen transparent button

Is it possible to make a full screen button that is transparent so no matter where the user clicks the button is activated?
Below is some basic java that shows a button and when pressed starts a new intent. I also added the main.xml layout but I am not sure how to update it so the buttons fills the screen but is transparent.
package com.MeetingManager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class MeetingManager extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
layout.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
Intent myIntent = new Intent(MeetingManager.this,CreateMeeting.class);
startActivityForResult(myIntent, 0);
}
});
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/meetingbg"
>
<TableRow android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/tableRow5">
<Button android:text="Button" android:id="#+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</TableRow>
</LinearLayout>
If listening for user interaction via tapping on screen is your aim, I would recommend added a touch listener to your LinearLayout. This removes the need for a Button.
E.g.
LinearLayout layout = (LinearLayout) findViewById(R.id.your_layout);
layout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
});
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/your_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/meetingbg"
>
<TableRow android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/tableRow5">
<Button android:text="Button" android:id="#+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</TableRow>
</LinearLayout>
You may have to include the following imports:
import android.view.View.OnTouchListener;
import android.view.MotionEvent;
try to use null in the background android:background="#null"

Categories