When I run my app on a rooted device the ListView is blank and I am not sure why?
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Intent;
import java.util.List;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.widget.Toast;
This is my main class:
public class Network_List extends AppCompatActivity {
WifiManager wifiM;
WifiScanReciever wifiR;
ListView networklist;
String wifis[];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_network__list);
Search box for searching networks (not yet implemented so don't worry about it)
EditText searchNet = (EditText) findViewById(R.id.networkEditText);
This is the main list to display the network information
networklist = (ListView) findViewById(R.id.networkList);
Search button for the EditText (This is for future development)
Button networkButton = (Button) findViewById(R.id.searchButton);
scanButton is a button that starts the scan
Button scanButton = (Button) findViewById(R.id.buttonScan);
Declares wifiM as wifi manager and gets service
wifiM = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Declares wifiR as WifiScanReciever
wifiR = new WifiScanReciever();
Button for starting the scan
scanButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
wifiM.startScan();
}
});
}
protected void onPause(){
unregisterReceiver(wifiR);
super.onPause();
}
protected void onResume(){
registerReceiver(wifiR, new IntentFilter(wifiM.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
This receives the scan results and puts it an array called wifis. onReceive captures scan results and sets the size of the array wifis.
private class WifiScanReciever extends BroadcastReceiver{
public void onReceive(Context c, Intent intent){
List<ScanResult> wifiSList = wifiM.getScanResults();
wifis = new String[wifiSList.size()];
for (int x = 0; x<wifiSList.size(); x++){
wifis[x] = ((wifiSList.get(x)).toString());
}
networklist.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,wifis));
}
}
}
Related
So just to practice I've created a Pythagorean theorem calculator app. I want each answer that I obtain to be stored in another activity(a "history" page). I'm trying to use intents to send/receive the arraylist and items. I get the most recent one, but it is overwritten after each press of the button.
package com.example.hypnotenusecalculatorrebuild;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
//ToDo history button is broken. doesn't display listview when pressed.
public void toHistory(View view) {
// Intent intent = new Intent(getApplicationContext(), HistoryActivity.class);
//
// startActivity(intent);
}
public void findHypno(View view) {
Log.i("Info", "button pressed");
EditText editTextNumberA = (EditText) findViewById(R.id.editTextNumberA);
EditText editTextNumberB = (EditText) findViewById(R.id.editTextNumberB);
TextView textViewAnswer = (TextView) findViewById(R.id.textViewAnswer);
String message;
String cSquared;
if (editTextNumberA.getText().toString().isEmpty() || editTextNumberB.getText().toString().isEmpty()) {
message = "Please enter a number for each dimension";
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
} else {
Number numberA = new Number();
Number numberB = new Number();
Number numberC = new Number();
numberA.number = Double.parseDouble(editTextNumberA.getText().toString());
numberB.number = Double.parseDouble(editTextNumberB.getText().toString());
numberA.squareNumber();
numberB.squareNumber();
numberC.number = numberA.number + numberB.number;
numberC.squareRoot();
Log.i("test numberA", String.valueOf(numberA.number));
Log.i("test numberB", String.valueOf(numberB.number));
Log.i("test numberC", String.valueOf(numberC.number));
cSquared = Double.toString(numberC.number);
textViewAnswer.setText(cSquared);
ArrayList<String> historyList = new ArrayList<String>();
historyList.add("test");
Intent intent = new Intent(getApplicationContext(), HistoryActivity.class);
intent.putExtra("answer", cSquared);
intent.putStringArrayListExtra("arrayList", historyList);
startActivity(intent);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
package com.example.hypnotenusecalculatorrebuild;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import java.util.function.ToDoubleBiFunction;
public class HistoryActivity extends AppCompatActivity {
public void back(View view) {
finish();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
Intent getList = getIntent();
ArrayList<String> historyList = getList.getStringArrayListExtra("arrayList");
historyList.add(getList.getStringExtra("answer"));
ListView listViewHistory = (ListView) findViewById(R.id.listViewHistory);
ArrayAdapter historyListArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, historyList);
listViewHistory.setAdapter(historyListArrayAdapter);
// ToDo - intent isn't working as attended, only saves one item and gets overwritten each time
//Intent historyIntent = getIntent();
// get extra needs a name from put extra
// String testGetIntent = historyIntent.getStringExtra("answer");
// Log.i("testIntentString", testGetIntent);
// historyList.add(historyIntent.getStringExtra("answer"));
}
}
here is a different approach. make a public interface in a separate file named "extensions" or whatever and initialize your arraylist there like so:
public interface ext {
ArrayList<String> myArray = new ArrayList<String>();
}
this interface is created when the app runs and lives for as long as the app lives meaning you can always access and manipulate it anywhere in your app without worrying whether the activity is being killed or not. so you can add or remove elements to or from it wherever you want.
Access the array this way:
ext.myArray.add("new item")
this is my code I don't know how to use if else statement with two spinner
//(spinner1.equals"THB" && spinner2.equals"USD") <--something like that
https://drive.google.com/file/d/1e_TbY6e-KAE7Iq6GLq2CFy9WA1VBy1qx/view?fbclid=IwAR1s7C9SVm2S202uAmURjwTilHu2ppwkHclvN7gDT3cEFFwZkqyktZv6MRE
If you are talking about the values of the spinner that the user has chosen:
Spinner spinner1 = findViewById(R.id.your_spinner1);
Spinner spinner2 = findViewById(R.id.your_spinner2);
if(spinner1.getSelectedItem().toString().equals("THB") && spinner2.getSelectedItem().toString().equals("USD"){
//do stuff here
}
Like the user f1sh in the comment said, post your code in the question.
Extend an interface AdapterView.OnItemSelectedListener
package com.example.acer.spinner;
import android.accessibilityservice.GestureDescription;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.lang.ref.SoftReference;
public class MainActivity extends AppCompatActivity {
Spinner spn,spn1;
EditText inmoney;
TextView answer;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inmoney=findViewById(R.id.text);
answer=findViewById(R.id.textView);
btn=findViewById(R.id.button);
// first spinner
spn=findViewById(R.id.spinner);
spn1=findViewById(R.id.spinner2);
String [] legion ={"THB","USD","EUR","JPY","SGD","MYN","CNY"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,legion);
spn.setAdapter(adapter);
spn1.setAdapter(adapter);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
exrate();
}
});
}
public void exrate(){
double num=Double.parseDouble(inmoney.getText().toString());
if (spn1.getSelectedItem().toString().equals("THB") && spn.getSelectedItem().toString().equals("USD")){
double ans=num*5;
answer.setText(String.valueOf(ans));
}
else{
//do other currency conversions
}
}
}
You can also use else if(condition) instead of just else, to do other comparison and conversions
It is called as Ladder-if in Java jargon
I want to display the scan result from ZXING. I integrated ZXING into my android app, the scan works ok. Now I want to display the barcode number result in textview. I'm using zxing library in my project. I set up result.setText(resultCode) but it's not works. so this is code i m'follow from the tutorial.
package com.example.norhanom.barcodeqrcode;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; //view button or textfield
import android.widget.Toast; //to show and create message for user,appears
as floating view over app
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.google.zxing.Result;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class MainActivity extends AppCompatActivity {
private ZXingScannerView scannerView;
TextView result;
private ProgressDialog pd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView)findViewById(R.id.textView1);
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("loading");
pd.setCancelable(false);
pd.setCanceledOnTouchOutside(false);
}
public void scanCode(View view){
scannerView = new ZXingScannerView(this); // Programmatically initialize
the scanner view
scannerView.setResultHandler(new ZXingScannerResultHandler());
setContentView(scannerView); //Set the scanner view as the content view
scannerView.startCamera(); //scannerView open camera
}
#Override
public void onPause()
{
super.onPause();
scannerView.stopCamera(); //stop camera on pause
}
class ZXingScannerResultHandler implements ZXingScannerView.ResultHandler
{
#Override
public void handleResult(Result result1)
{
String resultCode = result1.getText(); //get the result
Toast.makeText(MainActivity.this,resultCode,Toast.LENGTH_LONG).show();
//show result
setContentView(R.layout.activity_main);
scannerView.stopCamera(); //camera stop
}
}
}
After using setContentView the next time, you should reassign the views.
//inside handleResult
setContentView(R.layout.activity_main);
result = (TextView)findViewById(R.id.textView1);
result.setText(resultCode);
scannerView.stopCamera();
I'm trying to get an mp3 file to play a sound when a button is clicked (Android app). However, despite following tutorials with similar code, I get this error message 'MediaPlayer.create cannot be resolved as a type. I have refreshed the project and cleaned the project and it made no difference. Here is my code:
package com.example.h.a.t;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Hypo_Info extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hypo_info);
final MediaPlayer mediaPlayer = new MediaPlayer.create(this, R.raw.pill_bottle);
Button a = (Button) findViewById(R.id.treatment_hypo);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Hypo_Info.this, Hypo_Treatment.class);
startActivity(i);
mediaPlayer.start();
}
});
}
The problem is the "new" keyword here:
new MediaPlayer.create(this, R.raw.pill_bottle);
because the MediaPlayer.create is a static method.
==> remove the new
I wanted to navigate to a new page when clicking a button. Below are my both activities I have created. I am using eclipse and I am getting an error saying The method onClick(View) of type new View.OnClickListener(){} must override a superclass method on the main activity.
This is my main activity.
package com.example.grammer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button grammerButton = (Button)findViewById(R.id.grammar);
grammerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Grammer.class);
startActivity(intent);
}
});
}
}
This is my second activity.
package com.example.grammer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.os.Build;
public class Grammer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grammer);
Button grammerButton = (Button) findViewById(R.id.grammar);
grammerButton.setOnClickListener(new View.OnClickListener() {
// #Override
public void onClick(View view) {
finish();
}
});
}
}
Removing the #override will remove the error, but then app is not working as intended.
Check this
OnClickListener() must override a superclass method?
Uncheck "Enable project specific settings", click "Configure Workspace Settings..." and change "Compiler Compliance Level" to 1.6 or above
Have this import statement
import android.view.View.OnClickListener;
There is no need to remove #Override Annotation.
Also calling finish() is not necessary. The hardware back button does the job.
When you press back button in Grammar Activity your current activity is popped from the back stack and the previous activity in the back stack takes focus. So there is no need to call finish() on button click.
http://developer.android.com/guide/components/tasks-and-back-stack.html
Also if you have a Button with id grammar in activity_grammer.xml it is ok.
Make sure you have a button with id grammar in activity_grammer.xml
You can read the topic id
http://developer.android.com/guide/topics/ui/declaring-layout.html
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching.
Change "Compiler Compliance Level" to 1.6. of java from project properties.
Place this code::
Just replace then name of layout and button id by your
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alayout);
Button grammerButton = (Button)findViewById(R.id.aId);
grammerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Grammer.class);
startActivity(intent);
}
});
}
}
Just replace then name of layout and button id by your
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Grammer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blayout);
Button grammerButton = (Button) findViewById(R.id.aId);
grammerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
}
You are suppose to import the ViewClickListener namespace in your code.
Just Press ctrl + shift + O and it will add the relevant and missing namespaces in your project.
All you need is to import the library for OnClickListener. Just press ctrl + Shift + O in your eclipse and it will import the import android.view.View.OnClickListener file for you.
Try this..
First, You havn't import OnClickListener
import android.view.View.OnClickListener;
and second one
Button grammerButton = (Button) findViewById(R.id.grammar);
you are giving same name for both Button Ids in different layouts. Make sure you have a button with id grammar in activity_grammer.xml present are not.