I cant display the image on the screen. Android Studio - java

I wanted to make a program that would output a image fron the internet by a link to the screen, but the image either appears as a cropped strip, or does not appear at all. I will be very grateful for you help
code
package com.example.myapplication2;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private EditText s;
private TextView d;
private Button button1;
private URL link;
private InputStream j;
private Bitmap bitmap;
private ImageView image4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
d = findViewById(R.id.textView12);
s = findViewById(R.id.editText1);
button1 = findViewById(R.id.button1);
image4 = findViewById(R.id.imageView3);
}
#Override
protected void onResume() {
super.onResume();
}
public void onClickStart(View view){
new Thread(new Runnable() {
#Override
public void run() {
try {
link = new URL(s.getText().toString());
try {
j = (InputStream)link.getContent();
}
catch (IOException e){
d.setText("fail");
}
}
catch (MalformedURLException e){
d.setText("fail");
}
runOnUiThread(new Runnable() {
#Override
public void run() {
bitmap = BitmapFactory.decodeStream(j);
image4.setImageBitmap(bitmap);
}
});
}
}).start();
}
}
enter image description here
enter image description here
It is possible in incorrect handling of the permission or file type, I would appreciate your help

Related

Continuously outputting index of an array

I have an array that I want to output each index continuously and randomly
here is my code :
package com.example.testtingskripsi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public String [] ArrayA ={"80","81","82","83","84","85"};
public TextView mViewLabel;
#Override
protected void onCreate(Bundle savedInstanceState) {
int rando =(int) ((Math.random())*ArrayA.length);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewLabel = (TextView) findViewById(R.id.textChanger);
Button startHeartbeat = (Button) findViewById(R.id.start);
Button stopHeartbeat = (Button) findViewById(R.id.stop);
startHeartbeat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
for(int mIfCounter = 0;mIfCounter<ArrayA.length-1;mIfCounter++){
mViewLabel.setText(ArrayA[mIfCounter]);
}
}
}, 1000);
}
});
}
}
the output doesn't change only showing one index and doesn't change to the next index
edit:
it does work but on the output, it only shows the last index but still doesn't change
edit 2:
Okay so I fixed it with new code and try some of you guys help
this is new code
package com.example.testtingskripsi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Arrays;
import java.util.List;
import java.util.Collections;
public class MainActivity extends AppCompatActivity {
public String [] ArrayA ={"80","81","82","83","84","85"};
public TextView mViewLabel;
boolean continueThread=true;
int count =0;
Thread t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewLabel = (TextView) findViewById(R.id.textChanger);
List<String> list = Arrays.asList(ArrayA);
Collections.shuffle(list);
t = new Thread(){
#Override
public void run() {
while(continueThread){
try{
Thread.sleep(1000);
runOnUiThread(new Runnable() {
#Override
public void run() {
if(count<=6){
count++;
}else{
count--;
}
mViewLabel.setText(list.get(count));
}
});
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
}
public void BtnStart(View view){
t.start();;
}
public void BtnStop(View view){
continueThread=false;
}
}
it stop at end of array, so i want it to doing the loop until i pressed stop button any help
Already figured it out so okay thank you all for helling me
You probably need to add some delay inside the for loop.
Try Thread.sleep inside the loop.
The device changes 60 fps so its hard to view the changes for a small loop

int obtained from editText is not displayed

I am doing a saving system with sharedPreferences, the problem is that when I run the application it seems that if the String is saved but when converting it to int later it returns null values or 0...
the edit text only receives numbers, and what I try is to obtain its value as a string and then convert it to int, since I want to use that value in another activity
package com.example.ml_prototipe_a;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class MainActivity extends AppCompatActivity {
private EditText inputLimiter;
private TextView mostrarLimite;
public String limiterStr;
public int limiterInt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mostrarLimite = (TextView)findViewById(R.id.MostrarAciertos);
inputLimiter = (EditText) findViewById(R.id.InLimAciertos);
limiterStr = inputLimiter.getText().toString().trim();
Button btnRecNotas = findViewById(R.id.recNotasBtn);
Button btnGuardarLim = findViewById(R.id.GuardarLimite);
btnRecNotas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intRecNotas = new Intent(MainActivity.this, MainRecNotas.class);
startActivityForResult(intRecNotas, 0);
}
});
btnGuardarLim.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
public void Cargar(View view){
SharedPreferences limitPrefs= getBaseContext().getSharedPreferences("limitData", Context.MODE_PRIVATE);
String recovaStr = limitPrefs.getString("numbreLimiter", "");
if(!"".equals(recovaStr)) {
limiterInt = Integer.parseInt(recovaStr);
mostrarLimite.setText("aciertos: " + limiterInt);
System.out.println(limiterInt);
}
}
public void Guardar(View view){
if(mostrarLimite!=null) {
SharedPreferences limitPrefs = getBaseContext().getSharedPreferences("limitData", Context.MODE_PRIVATE);
SharedPreferences.Editor editer = limitPrefs.edit();
editer.putString("numberLimiter", limiterStr);
editer.apply();
}
}
}
NEW CODE!!!!********************************
package com.example.ml_prototipe_a;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class MainActivity extends AppCompatActivity {
private EditText inputLimiter;
private TextView mostrarLimite;
public String limiterStr;
public int limiterInt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mostrarLimite = (TextView) findViewById(R.id.MostrarAciertos);
inputLimiter = (EditText) findViewById(R.id.InLimAciertos);
Button btnRecNotas = findViewById(R.id.recNotasBtn);
Button btnGuardarLim = findViewById(R.id.GuardarLimite);
Button btnCargarLimTEMP = findViewById(R.id.cargarTemp);
btnRecNotas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intRecNotas = new Intent(MainActivity.this, MainRecNotas.class);
startActivityForResult(intRecNotas, 0);
}
});
btnGuardarLim.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Guardar();
}
});
btnCargarLimTEMP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Cargar();
}
});
}
private void Cargar() {
SharedPreferences limitPrefs = getBaseContext().getSharedPreferences("limitData", Context.MODE_PRIVATE);
String recovaStr = limitPrefs.getString("numbreLimiter", "");
try {
limiterInt = Integer.parseInt(recovaStr);
} catch (NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
mostrarLimite.setText("aciertos: " + limiterInt);
System.out.println(limiterInt);
Toast.makeText(this, "Limite de aciertos: " + limiterInt, Toast.LENGTH_SHORT).show();
}
private void Guardar() {
if (mostrarLimite != null) {
SharedPreferences limitPrefs = getBaseContext().getSharedPreferences("limitData", Context.MODE_PRIVATE);
SharedPreferences.Editor editer = limitPrefs.edit();
editer.putString("numberLimiter", limiterStr);
editer.apply();
limiterStr = inputLimiter.getText().toString().trim();
Toast.makeText(this, "Limite de aciertos actualizado: " + limiterStr, Toast.LENGTH_SHORT).show();
System.out.println(limiterStr);
}
}
}

how to check radio buttons programatically when the scanned qr match with text in the listview

I am developing attendance system and i have a listview with students name i want to programatically check or uncheck the radio buttons in the listview when the scanned qr matchs the student name I tried to do it in the ontextchengeListner method of txtresult but the text is changing many time even in reading a single qr
This are my java classes
CustomAdapter.java
package com.attendance.olana.qr_scanner;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by olana on 23/03/2018.
*/
public class CustomAdapter extends BaseAdapter {
Context context;
String[] questionsList;
public static String message;
LayoutInflater inflter;
public static TextView question ;
public static RadioButton yes,no ;
public static ArrayList<String> selectedAnswers;
public CustomAdapter(Context applicationContext, String[] questionsList) {
this.context = context;
this.questionsList = questionsList;
// initialize arraylist and add static string for all the questions
selectedAnswers = new ArrayList<>();
for (int i = 0; i < questionsList.length; i++) {
selectedAnswers.add("Not Attempted");
}
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return questionsList.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(final int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.main, null);
// get the reference of TextView and Button's
question = (TextView) view.findViewById(R.id.question);
yes = (RadioButton) view.findViewById(R.id.yes);
no = (RadioButton) view.findViewById(R.id.no);
// perform setOnCheckedChangeListener event on yes button
yes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set Yes values in ArrayList if RadioButton is checked
if (isChecked)
selectedAnswers.set(i, "Present");
}
});
// perform setOnCheckedChangeListener event on no button
no.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set No values in ArrayList if RadioButton is checked
if (isChecked)
selectedAnswers.set(i, "Absent");
}
});
// set the value in TextView
question.setText(questionsList[i]);
return view;
}
}
list.java
package com.attendance.olana.qr_scanner;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;
/**
* Created by olana on 23/03/2018.
*/
public class list extends AppCompatActivity {
ListView simpleList;
public static String[] questions;
Button submit,scan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
scan =(Button)findViewById(R.id.scan);
// get the string array from string.xml file
questions = getResources().getStringArray(R.array.student);
// get the reference of ListView and Button
simpleList = (ListView) findViewById(R.id.simpleListView);
submit = (Button) findViewById(R.id.submit);
// set the adapter to fill the data in the ListView
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), questions);
simpleList.setAdapter(customAdapter);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i;
i = new Intent(list.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}
});
// perform setOnClickListerner event on Button
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String message = "";
// get the value of selected answers from custom adapter
for (int i = 0; i < CustomAdapter.selectedAnswers.size(); i++) {
message = message + "\n" + (i + 1) + " " + CustomAdapter.selectedAnswers.get(i);
}
// display the message on screen with the help of Toast.
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
}
}
MainActivity.java
package com.attendance.olana.qr_scanner;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Vibrator;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import java.io.IOException;
import java.util.jar.Manifest;
public class MainActivity extends AppCompatActivity {
BarcodeDetector barcodeDetector;
SurfaceView camerapreview;
CameraSource cameraSource;
TextView txtresult;
String message;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button finish = (Button) findViewById(R.id.finish);
//Button scan = (Button) findViewById(R.id.scan);
finish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
camerapreview = (SurfaceView) findViewById(R.id.camerapreview);
txtresult = (TextView) findViewById(R.id.txtresult);
barcodeDetector = new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();
cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.build();
//add event
camerapreview.getHolder().addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
cameraSource.start(camerapreview.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
#Override
public void release() {
try {
cameraSource.start(camerapreview.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> qrcodes = detections.getDetectedItems();
if (qrcodes.size() != 0) {
txtresult.post(new Runnable() {
#Override
public void run() {
//create vibration
Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(100);
txtresult.setText(qrcodes.valueAt(0).displayValue);
//barcodeDetector.release();
}
});
}
}
});
}
}
Please tell me where i can implement these the programatically checking or unchecking the radio button when the name in the listview matchs the scanned qr please help me

How to save multiple user inputted data to a file in Android Studio

I am having trouble trying to figure out how to save user inputted data into my app. I'm attempting to create a mock basketball statistics app that will store stats per each day and then average out the different stats. Before I can do any of the actual math, though, I cannot figure out how to save the data and load it.
This is the code that I have so far for both activities:
Main Activity
package com.miahollins.basketballfinal;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
int gamesp, gamest,points,perfoul;
Button btn;
int year_x,month_x,day_x;
static final int DIALOG_ID=0;
EditText games, game2, game3, game4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText game1 = (EditText) findViewById(R.id.games);
final EditText game2 = (EditText) findViewById(R.id.games2);
final EditText game3 = (EditText) findViewById(R.id.games3);
final EditText game4 = (EditText) findViewById(R.id.games4);
Button results = (Button) findViewById(R.id.button);
final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
results.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
gamesp = Integer.parseInt(game1.getText().toString());
gamest = Integer.parseInt(game2.getText().toString());
points = Integer.parseInt(game3.getText().toString());
perfoul = Integer.parseInt(game4.getText().toString());
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("key1", gamesp);
editor.putInt("key2", gamest);
editor.putInt("key3", points);
editor.putInt("key4", perfoul);
editor.commit();
startActivity(new Intent(MainActivity.this, Status.class));
}
});
final Calendar cal = Calendar.getInstance();
year_x = cal.get(Calendar.YEAR);
month_x = cal.get(Calendar.MONTH);
day_x = cal.get(Calendar.DAY_OF_MONTH);
showDialogOnButtonClick();
}
public void save(View view){
String value = games.getText().toString();
String file_name="stats";
try {
FileOutputStream fileOutputStream = openFileOutput(file_name,MODE_PRIVATE);
fileOutputStream.write(value.getBytes());
fileOutputStream.close();
Toast.makeText(getApplicationContext(),"Message Saved",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void showDialogOnButtonClick(){
btn=(Button)findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(DIALOG_ID);
}
});
}
#Override
protected Dialog onCreateDialog(int id){
if (id==DIALOG_ID)
return new DatePickerDialog(this,dPickerListener,year_x,month_x,day_x);
return null;
}
private DatePickerDialog.OnDateSetListener dPickerListener= new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
year_x=year;
month_x=monthOfYear+1;
day_x=dayOfMonth;
Toast.makeText(MainActivity.this,year_x+" / " + month_x+" / "+day_x,Toast.LENGTH_LONG).show();
}
};
}
Second Activity
package com.miahollins.basketballfinal;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class Status extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status);
}
public void load(View view){
try {
String Stats;
FileInputStream fileInputStream = openFileInput("stats");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
while ((Stats=bufferedReader.readLine())!=null){
stringBuffer.append(Stats + "\n");
}
TextView load = (TextView)findViewById(R.id.textView);
load.setText(stringBuffer.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Any help would be greatly appreciated!

EditText saving to file, but no file created!

package com.russell.saw;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
public class notetaker extends Activity {
public EditText edittext;
public Button save;
public String input;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notetaker);
edittext = (EditText) findViewById(R.id.edittext);
edittext.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
save = (Button)findViewById(R.id.save);
save.setOnClickListener(btnListener);
}
private OnClickListener btnListener = new OnClickListener() {
public void onClick(View v) {
input = edittext.getText().toString();
File file = new File(Environment.getExternalStorageDirectory(), "fileOut.txt");
try{
BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));
writer.write(input);
writer.newLine();
writer.flush();
writer.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
I'm not getting any file come up when i click save with this code, anyone know where i'm going wrong .
(warning: i am VERY new to android, so please don't be mean if it's blindingly obvious :3)

Categories

Resources