how to add simple code to an android application - java

I have some confusion regarding where you put regular java code in an android application.
I'm using the Eclipse SDK and by default when you create an application it makes a .java file with an OnCreate() method. Is this where I would put my code, inside this method?
Right now in my layout I have an imageButton, once this button is clicked I want to open a new WebView page that gets it's HTML code from the index.html file found in the assets folder. This is what I have so far...
Button button = (Button)findViewById(R.id.imagebutton1);
if(button.isPressed())
{
WebView webview = new WebView(null);
setContentView(webview);
try {
InputStream fin = getAssets().open("index.html");
byte[] buffer = new byte[fin.available()];
fin.read(buffer);
fin.close();
webview.loadData(new String(buffer), "text/html", "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
}
I have this block of code at the end of the onCreate() method right under the line:
setContentView(R.layout.main);
However, once I run the program it crashes and tells me that it failed to start. I'm assuming that it has to do with the fact that the code is in the onCreate. I know that its not where I'm supposed to put it, but I can't think of anywhere else the code should go. Am I supposed to make a new .java file and have a main method there? I'm currently taking classes for C++ and C# so this android thing is still new to me.

Have you done the tutorials? If not I would start there to learn about the basics of creating and working with an Android Activity. Once you have worked your way through them, read the application fundamentals to understand the lifecycle more fully.

You shouldn't be calling setContentView more than once in onCreate. The WebView should be in your main.xml layout file or else launch a new Activity that's layout contains the WebView. And also to hand the onClick on the button you need to call setOnClickListener().

Please see Handling UI Events.
Also, if your program crashes, it would be helpful if you provided error messages from logcat.

Related

How to click button inside method Adapter in RecyclerView?

I am working on stripe-terminal-android-app, to connect to BBPOS 2X Reader device,
wanted to click-item from list,(recyclerView).
I am trying to do:
when list of devices appears(readers), I am checking if readers.size()==1, then click first-device from list,else show recyclerView();
I have very less experience in Android(coming from JS, PY), :)
After going through debugger to understand flow of program-running, I used F8 key, or stepOver the functions one by one,
and where value is assigned to convert in displayble-format in adapter as here.
public ReaderAdapter(#NotNull DiscoveryViewModel viewModel) {
super();
this.viewModel = viewModel;
if (viewModel.readers.getValue() == null) {
readers = new ArrayList<>();
} else {
readers = viewModel.readers.getValue();
if(readers.size() == 1){
Log.e(TAG, "readers.size() is 1 "+ readers.size());
}
}
}
then in ReaderHolder-file, values are bind() as
void bind(#NotNull Reader reader) {
binding.setItem(reader);
binding.setHandler(clickListener);
binding.executePendingBindings();
}
}
I tried assigining button and manually clicking when only-one device appears, by clicing on reader[0], can't do that by findViewById inside Adapter file, to call onClick() method manually,
I tired another StackOverflow's answer but didn't understood, from here.
Main fragment is discovery-fragment,
how can I click first-device by checking readers.size()==1, then click onClick()?
my final-goal is to automate, whole stripe-terminal-payment process on android.
extra-info:
I am fetching data from python-odoo server, then using url, will open app through browser, (done this part), then device will be selected automatically as everytime-no any devices will be present except one,
so will automatically select that from recyclerView, then proceed.
I have asked for help in detailed way on GitHub-issues, and started learning Android's concepts for this app(by customizing stripe's demo app, which works great, but I wanted to avoid manually clicking/selection of devices).

How do I display a PDF after pushing a button in the Android app I'm making?

I'm currently working on making an Android app, but have been having some trouble. I want to be able to push a button with the title of the document as it's TextView and then have that document open to be read. I've looked around for guides but everything I've found is either out of date or doesn't explain any of the code shown. Does anyone know how I can put such a thing in my app? At this point, I'm not even sure where to start with the process.
Note, I'm working in Java not Kotlin.
UPDATE: I was directed to a solution using intents. Now there seems to be an issue loading the file. My code is this:
public class atotf_pdf extends literature {
File file = new File("/storage/emulated/0/AToTF Preview.pdf");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Uri pdfURI = FileProvider.getUriForFile(atotf_pdf.this, "net.whispwriting.whispwriting.provider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(pdfURI, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
Is it something here that's causing the error?
It looks like the file is somehow not being stored in the filesystem when the app is installed.
Just like #ruben said, you could use a WebView for such a task: https://developer.android.com/reference/android/webkit/WebView. For more information about using WebView to display content, you can take a look at this post: How can I display a pdf document into a Webview?
Another potential solution could be using intents, which has also been discussed before: How to open a PDF via Intent from SD card
Hopefully this helps!

What causes the LoginActivity to run for the first time in the Socket.io android application example by nkzawa?

I have been trying to fully understand nkzawa's android example on github for socket.io
Relative link:
https://github.com/nkzawa/socket.io-android-chat/tree/master/app/src/main/java/com/github/nkzawa/socketio/androidchat
(AndoridManifest.xml is in the "main" folder)
I have been told that android decides which activity to run first from a tag inside AndroidManifest.xml that would have < action ... .MAIN and < category ... .LAUNCHER inside the activity. When I look at AndroidManifest.xml I see the aforementioned tags inside of the MainActivity declaration. Great! So MainActivity must be the first to run. Now, when I look at MainActivity.java I see an onCreate() method that just sets the content view with not much else to be seen.... So how does the LoginActivity start?
I have setup the server side of things without an issue and I can compile and run the example, connect to my server and all that... I just don't understand how the onCreate() method is first called for the LoginActivity.
There is definitely something I am missing. The LoginActivity must be started somewhere else besides AndroidMainifest.xml or MainActivity.java..
If anyone could point me in the right direction even it would be great! Thanks so much.
Alright I think I got it.
1. App starts at MainActivity.onCreate()
2. Sets content view with: setContentView(R.layout.activity_main)
3. Activity_main.xml opens a fragment that uses class MainFragment
4. MainFragment.onCreate() does some work then calls startSignIn()
5. startSignIn() calls startActivityForResult(intent, REQUEST_LOGIN) with the intent as the LoginActivity!
Now the LoginActivity has begun :)

Implementing google places autocomplete in android

Im creating an android app where im reusing the google places api to get the autocomplete function. I way i have succesfully implemented functionality is by using this code.
int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1;
...
try {
Intent intent =
new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
.build(this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
This code is launched when the user clicks on a menu_button from a popupmenu in the firstscreen (mainscreen)
This code is able to do what i want it to do, but i dont know how to edit the fragment it creates. I want to set a hint thats different from "search" which is by default. The reason i used this code was because its able to create a sort of dialog or dim the background and creating the the autocompletefragment on top of the first screen which is what i want. Is there an another way to implement the autocomplete?
tl:dr
I want to use the google places autocomplete, and i want it to pop up like a dialog (Dim the first screen and the searchbar goes on top of the first screen).
Does anyone have a solution?

Sign APK on Client Phone

This is my first question on StackOverflow, and my english is poor, so please :D. I do this just for fun. I want to change the icon per user, letting users change the icon of the app. I think if I want to do this, I must read the apk self, unzip it, change che drawable/icon.png and rebuild it, at last to sign.
I try some code, but failed. The import reason is that, package sun.securate.* is not in Android Java framework. I write some code to do this, but it failed-_-
Who can help me? I can share my already written code.
Help me,Please!
Well another work around will be to create shortcut icon for Home Screen which launches the same Activity as the launcher. With this your launcher icon in drawer will be the actual one but on the home screen you will have different icon. You can also explore the idea of creating shortcut for other apps if you know the package info.
Ref:
For package info here are the doc.
PackageManager pkgMgr = getPackageManager();
For creating short cut here is the snippet (this is just for illustration):
// Setting the intent class ActivityClassToLaunch you need to try PackageManager
Intent i = new Intent(activity, ActivityClass.class);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
Intent result = new Intent();
result.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i);
result.putExtra(Intent.EXTRA_SHORTCUT_NAME, activity.getString(R.string.app_name));
ShortcutIconResource iconResource = null;
iconResource = ShortcutIconResource.fromContext(activity, R.drawable.new_app_icon);
result.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
result.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
activity.sendBroadcast(result);
If you just want user to change the app icon, why dont you load the icon from the SD card. And the icon that you ship with your package will be there if no icon is specified by user.
Here is the snippet that can help you.
onCreate(...)
setContentView(...);
setFeatureDrawable(Window.FEATURE_LEFT_ICON,
<your_drawable>);
....
}
If this does not work, try calling setFeatureDrawable before setContentView
You cannot change the launcher icon of a signed-and-sealed APK, except through a software upgrade.

Categories

Resources