Method showAtLocation cannot be resolved - java

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

Related

Android app crashes with button click

I'm creating a simple quiz according to this tutorial, but the app crashes with the button "Next answer" which is related to a checked radio button.
Main code:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class Display extends Activity {
TextView tv;
Button btnNext;
RadioGroup rg;
RadioButton rb1, rb2,rb3;
String questions[]={"Capital of Portugal?", "Capital of Spain?", "Capital of France?"};
String answers[]={"Lisbn","Madrid","Paris"};
String options[]={"Zuriq", "London","Lisbon","Manchester","Buenos Aires","Madrid", "NY", "Paris","Moscow"};
int flag=0;
public static int marks,correct,wrong;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dispaly);
tv=(TextView) findViewById(R.id.tvque);
btnNext=(Button) findViewById(R.id.btnNext);
rb1= (RadioButton) findViewById(R.id.radioButton);
rb2= (RadioButton) findViewById(R.id.radioButton2);
rb3= (RadioButton) findViewById(R.id.radioButton3);
tv.setText(questions[flag]);
rb1.setText(options[flag*3]);
rb2.setText(options[(flag*3)+1]);
rb3.setText(options[(flag*3)+2]);
Toast.makeText(this, "Negative Marks: " + MainActivity.tbflag, 1000).show();
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//The error comes from the line bellow, i believe:
RadioButton user_answer= (RadioButton)findViewById(rg.getCheckedRadioButtonId());
String answerText=user_answer.getText().toString();
if(answerText.equalsIgnoreCase(answers[flag])){
correct++;
}else{
wrong++;
flag++;
if(flag<questions.length){
tv.setText(questions[flag]);
rb1.setText(options[flag*3]);
rb2.setText(options[(flag*3)+1]);
rb3.setText(options[(flag*3)+2]);
}
else
{
if(MainActivity.tbflag)
{
marks=correct-wrong;
}
else
{
marks=correct;
}
Intent in =new Intent(getApplicationContext(), ResultActivity.class);
startActivity(in);
}
}
}
});
}
}
Log message:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference
I don't understand this error, why isn't the method being called? Can someone help?
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.vtorferreira.anew.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvque"
tools:text="Questions" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/group">
<RadioButton
android:text="RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="32dp"
android:id="#+id/radioButton" />
<RadioButton
android:text="RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/radioButton2" />
<RadioButton
android:text="RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton3"
android:layout_below="#+id/radioButton2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RadioGroup>
<Button
android:text="Next Question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
android:layout_marginBottom="125dp"
android:id="#+id/btnNext" />
</RelativeLayout>
The reason you are getting the first NullPointerException is because you must inflate rg before it is used.
The second NullPointerException is possibly due to the fact that you are pressing the "Next" button with no radio button selected. To prevent this, you can add a null check inside the OnClickListener or select one of the radio buttons by default. To select a radio button by default, add the following to your XML:
<RadioGroup
...
android:checkedButton="#+id/some-radio-button-id">
...
</RadioGroup>
Alternatively, you can set the default checked RadioButton in your Activity:
rg.check(R.id.radioButton)

java.lang.IllegalStateException: Could not find method

I think that is a dumb error, and I am new to programming. I was making an app to learn: there are 2 radio buttons to choose what text show in an hidden TextView, 2 check box to choose the background and text color of the TextView and a button, that do a check of the check box and the radio buttons and print the text in the TextView. But when in the emulator I try to click something, the app crashes, and the logcat says that it can't find the id and the onClick. Why do I get this?
The program:
package com.bauapp.lory.bauapp;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
import android.graphics.Color;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton)
findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action",Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#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 cambiaTesto (View v) {
TextView baubau = (TextView) findViewById(R.id.bauText);
RadioButton bau1 = (RadioButton) findViewById(R.id.bauRadio1);
RadioButton bau2 = (RadioButton) findViewById(R.id.bauRadio2);
CheckBox testoRosso = (CheckBox)findViewById(R.id.c1);
CheckBox sfondoGiallo = (CheckBox)findViewById(R.id.c2);
if(bau1.isChecked()) {
baubau.setText("BAU");
}
else
if (bau2.isChecked()) {
baubau.setText("BAUUUUUUU");
}
if(testoRosso.isChecked())
baubau.setTextColor(Color.RED);
else
baubau.setTextColor(Color.BLACK);
if (sfondoGiallo.isChecked())
baubau.setBackgroundColor(Color.YELLOW);
else
baubau.setBackgroundColor(Color.WHITE);
}
}
The XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.bauapp.lory.bauapp.MainActivity"
tools:showIn="#layout/activity_main">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="43dp">
<TextView
android:layout_width="342dp"
android:layout_height="191dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/bauText"
android:layout_gravity="center_horizontal|bottom"
android:onClick="baubau" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testo rosso"
android:id="#+id/c1"
android:layout_gravity="left|center_vertical"
android:onClick="testoRosso"
android:checked="false"
android:clickable="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sfondo giallo"
android:id="#+id/c2"
android:layout_gravity="right|center_vertical"
android:onClick="sfondoGiallo"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BAU BAU?"
android:id="#+id/bauButton"
android:layout_gravity="right|top"
android:onClick="cambiaTesto" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|top">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BAU"
android:id="#+id/bauRadio1"
android:layout_gravity="left|top"
android:onClick="bau1"
android:checked="false"
android:clickable="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BAUUUUUUU"
android:id="#+id/bauRadio2"
android:layout_gravity="left|top"
android:onClick="bau2"
android:checked="false"
android:clickable="true" />
</RadioGroup>
</FrameLayout>
And the logcat:
http://textuploader.com/5blrn
(I leave an external link because it doesn't paste correctly for a bug in stackoverflow).
The crash logcat is the same for all the other buttons, but, obviously, change the ID and the OnClick.
Sorry for my bad English.
My suggestion would be to remove android:onClick=... from your RadioButtons, and instead use onCheckedChangeListener on the RadioGroup and then determine which RadioButton was checked and then do whatever you need to do. Your code changes are then as follows:
<RadioGroup
android:id="#+id/bauRadioGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|top">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BAU"
android:id="#+id/bauRadio1"
android:layout_gravity="left|top"
android:checked="false"
android:clickable="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BAUUUUUUU"
android:id="#+id/bauRadio2"
android:layout_gravity="left|top"
android:checked="false"
android:clickable="true" />
</RadioGroup>
Then in your MainActivity onCreate you add this code:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.bauRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.bauRadio1){
//radio button bauRadio1 was clicked
baubau.setText("BAU");
}
else if(checkedId == R.id.bauRadio2 ){
//radio button bauRadio2 was clicked
baubau.setText("BAUUUUUUU");
}
}
});
//The rest of your code ...
Please give this a try and let us know if this helps.
As already suggested, How to set On click listener on the Radio Button in Android is a good example for this.
The Radio Button needs a method to handle its onClick event.
You have given the onClick method as android:onClick="bau1" for android:id="#+id/bauRadio1", but the MainActivity class needs to implement the bau1 method.
Hence the IllegalStateException of cannot find method bau1.
Check this link for more details on RadioButton and Responding to Click Events:
https://developer.android.com/guide/topics/ui/controls/radiobutton.html
Ciao Lorenzo,
I noticed at the end of your XML file: you forgot to close the RelativeLayout with the proper closetag </RelativeLayout> .
It's only a copy-paste mystake or your xml file is incorrect?
I give you a tip. Avoid filling the xml file with too many layout when with a few changes you can get the same view with fewer elements.
In your example the FrameLayout is completely useless.
EDIT: In your layout i didn't see the <FloatingActionButton> .
Make sure to add the support library dependence in your build.grandle file:
Remember to change the version based on your api target *
compile 'com.android.support:design:23.2.1'
After that you can add the FAB in you layout like this:
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:src="#drawable/my_icon" />
Either create
public void bau1(View view){
//Perform some code
}
In your MainActivity
OR
Remove android:onClick="bau1" from XML
Specially I prefer using OnCheckedChangeListener instead of onClick in case of RadioGroup like shown here.

how to use timepicker to make alarm clock

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;
}
}
}

Phone app: item cannot be resolved or is not a field [duplicate]

This question already has answers here:
"id cannot be resolved or is not a field" error?
(17 answers)
Closed 7 years ago.
I am trying to follow a manual (which had some bugs and I fixed it) however I am receiving this error btstart cannot be resolved or is not a field and I don't know how to fix it. Here's the LINK to that manual (but I have done changes in layout as well as string xml files) Here is the MainActivity.java code :
package com.example.stopwatch;
//import com.example.stopwatch.R;
import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
public class MainActivity extends Activity implements OnClickListener {
private Button start;
private Button stop;
private Button reset;
private Chronometer mydChronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uI();
}
public void uI() {
start = (Button) findViewById(R.id.btstart);
start.setOnClickListener(this);
stop = (Button) findViewById(R.id.btstop);
stop.setOnClickListener(this);
reset = (Button) findViewById(R.id.btreset);
reset.setOnClickListener(this);
mydChronometer = (Chronometer) findViewById(R.id.chronometer1);
}
#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 void onClick(View v) {
if (v == start) {
mydChronometer.start();
} else if (v == stop) {
mydChronometer.stop();
} else if (v== reset) {
mydChronometer.setBase(SystemClock.elapsedRealtime());
}
}
}
Here comes my "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: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=".MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/chronometer1"
android:layout_centerHorizontal="true"
android:layout_marginTop="67dp"
android:src="#drawable/myd" />
<Chronometer
android:id="#+id/chronometer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:gravity="center"
android:text="Chronometer"
android:textSize="35dp" />
<Button
android:id="#+id/btstart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView1"
android:layout_below="#+id/chronometer1"
android:layout_marginTop="18dp"
android:text="#string/start" />
<Button
android:id="#+id/btreset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btstop"
android:layout_alignBottom="#+id/btstop"
android:layout_toRightOf="#+id/btstop"
android:text="#string/reset" />
<Button
android:id="#+id/btstop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btstart"
android:layout_alignBottom="#+id/btstart"
android:layout_toRightOf="#+id/btstart"
android:text="#string/stop" />
</RelativeLayout>
And here's the "strings.xml" file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">stopwatch</string>
<string name="btn1">Start</string>
<string name="btn2">Stop</string>
<string name="btn3">Reset</string>
<string name="menu_settings">Settings</string>
<string name="action_settings">Settings</string>
</resources>
I haven't done any change in main.xml and here is my main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
Could you please let me know what am I missing and what's the fix to this error?
P.S.: I am receiving similar error for these lines:
btstop cannot be resolved or is not a field in stop = (Button) findViewById(R.id.btstop);
btreset cannot be resolved or is not a field in reset = (Button) findViewById(R.id.btreset);
chronometer1 cannot be resolved or is not a field in mydChronometer = (Chronometer) findViewById(R.id.chronometer1);
Thanks a ton.
Like Sotirios said, try clean the project, then in Project => uncheck clean automatically, then now you can build your project individually.
See this "id cannot be resolved or is not a field" error? it may help you.
Look if you re importing android .R
That could be the error
R not being resolved is a result of the R.java file not being generated automatically.
Check for errors in your resources folder, as this is what the R.java automatic generation is based on. In my case, in my AndroidManifest.xml file, I had to change one of the lines from
<application android:icon="#drawable/*icon"* android:label="#string/app_name">
to
<application android:icon="#drawable/*ic_launcher"* android:label="#string/app_name">
(i.e. change the #drawable/icon to the name of the .png file in the resources (res) folder

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