Can someone point me in the right direction - I'm trying to create an Android Activity that looks like a technical manual that ALSO can take some user input(I know how to do simple buttons etc.) and the user input part can wait until I have a few basic pages.
My goal (if possible) would be to create a text-heavy activity (like a technical manual) but I'm not sure what the best GENERAL method is for doing this.
To start - rather than having multiple activities I want one large activity that a User may be able to swipe through from left to right (Perhaps use ViewFlipper here??)
But how can I make an Activity that looks like a manual or is Text Heavy??
Thanks!
This is what I did with my application's instructions. I started with ViewFlipper that has next and back buttons. Then as the application grew in size, code got messier and there are a lot of formatting that I needed for the text. So I turned to use WebView and just store the text files as raw assets.
WebView manual=new WebView();
manual.loadData(Utilities.getData(this, R.raw.update),"text/html", "utf-8");
getData method:
public static String getData(Context c,int res) throws IOException{
InputStream ins = c.getResources().openRawResource(res);
byte[] buffer = new byte[ins.available()];
ins.read(buffer);
ins.close();
String html=new String(buffer);
StringBuilder buf = new StringBuilder(html.length());
for (char c1 : html.toCharArray()) {
switch (c1) {
case '#': buf.append("%23"); break;
case '%': buf.append("%25"); break;
case '\'': buf.append("%27"); break;
case '?': buf.append("%3f"); break;
default: buf.append(c1);
break;
}
}
return buf.toString();
}
Then you can attach buttons either to the webview or the application in response to user inputs and load new views.
Advantage:
1. Easy to format and integrate with Android. Very quick development.
2. No need to worry about the view flipper bugs that happens sometimes.
3. It will look like you want.
Disadvantage:
1. Doesn't look too much like an Android Application by itself.
Related
I'm currently creating a soundboard app. I have about 100+ sound files to import.
I have code lines (android:onClick="song1") and (MediaPlayer mysound1).
Just wondering if there is a way to copy+paste these lines and have android studio auto change the line to "song2" and "song3" all the way to "song100"? Same goes for the "mysound1" all the way to "mysound100". I hope I do not have to do it manually :(
Thank you!
The approach you're using seems like a brute force method. There are much more elegant ways to approach this. For one, I would recommend creating dynamic Android components programmatically instead of in XML. Then you could hold all of your elements in a List or a Map, and you could tie them to a generic onClick listener. I'd recommend taking a look around online to figure out how to do some of these things.
But, if you would like to stick with you original method, I don't believe that Android has a way to auto number your click events. You could, however, write code to write your code. Here's a simple example using Java - since you're writing an Android app (utilizing the Eclipse console to print out to):
public class WriteMyCode {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
for (int i = 1; i <= 100; i++) {
sb.append("<Button android:id=\"#+id/mybutton\"\n");
sb.append(" android:layout_width=\"wrap_content\"\n");
sb.append(" android:layout_height=\"wrap_content\"\n");
sb.append(" android:text=\"Click me!\"\n");
sb.append(" android:onClick=\"" + "song" + i + "\" />\n\n");
}
System.out.println(sb.toString());
}
}
You can then copy the output from the Eclipse console and paste it into your Android XML resource file.
I went to use Microsoft Excel to auto fill the numbers all the way to 100. Then CONCATENATE the ; to the end of "Mediaplayer mysound1".
I think the best is to first say, that I recently started Android programming. Despite the fact of me getting better now, I can't seem to find good ways to optimize my piece of code.
I've written this piece of code. It's a soundboard. And when you long click a button you're able to save it as a ringtone, as a alarm, as a notification or share it with your friends. For every method I've made a string.
This string is set by the corresponding button to "btn1" till "btn20". After this I open the method (in the example below showSelectedSaveDialog(). And in that method I've made a if or if else statement to open the correct sound.
This way of coding makes a very long code. Because for every button I have to make an if else statement. Is there a better way to code kind codes? Is there a good tutorial, or something like that? Or someone who can post an example?
Setting the string:
ringToneManager = "btn1";
showSelectSaveDialog();
Setting the correct sound:
if (str.equals("btn1")) {
fIn = getSherlockActivity().getBaseContext().getResources()
.openRawResource(R.raw.sound01);
Starting the method to share the sound file
shareButton("btn14");
Getting the corresponding sound file
private void shareButton(String str) {
// SAVE THE FILE
byte[] buffer = null;
if (str.equals("btn1")) {
fIn = getSherlockActivity().getBaseContext().getResources()
.openRawResource(R.raw.sound01);
[...] etc
Thanks in advance! :)
You can simplify your code by using the "tag" property, available on all Views and widgets. The tag property is a general purpose container.
Load the tag property for each button with the id of the sound file associated with the button, this can be done on an activities onCreate:
findViewById(R.id.btn1).setTag(R.raw.sound01);
findViewById(R.id.btn2).setTag(R.raw.sound02);
//etc.
findViewById(R.id.btn20).setTag(R.raw.sound20);
Each button can now share the same onClick handler and all run the same piece of code, no ifs required:
public void onClick(View arg0) {
fIn = getSherlockActivity().getBaseContext().getResources().openRawResource((Integer)arg0.getTag());
}
Likewise change the shareButton method to take an integer instead of a string:
shareButton((Integer)arg0.getTag());
private void shareButton(int soundID) {
// SAVE THE FILE
byte[] buffer = null;
fIn = getSherlockActivity().getBaseContext().getResources().openRawResource(soundID);
[...] etc
I have multi EditTexts.
How can i validate the EditText from having "" in the EditText and if the EditText is "" then i want the user to must enter a number before it goes to edittext2
How can this be done?
case R.id.P2Throw1Set2:
p212r.setText(String.valueOf(Integer.parseInt(p2score.getText().toString()) - Integer.parseInt(p212.getText().toString())));
p2score.setText(String.valueOf(Integer.parseInt(p212r.getText().toString())));
break;
case R.id.P2Throw2Set2:
p222r.setText(String.valueOf(Integer.parseInt(p2score.getText().toString()) - Integer.parseInt(p222.getText().toString())));
p2score.setText(String.valueOf(Integer.parseInt(p222r.getText().toString())));
break;
case R.id.P2Throw3Set2:
p232r.setText(String.valueOf(Integer.parseInt(p2score.getText().toString()) - Integer.parseInt(p232.getText().toString())));
p2score.setText(String.valueOf(Integer.parseInt(p232r.getText().toString())));
break;
case R.id.P2Throw4Set2:
p242r.setText(String.valueOf(Integer.parseInt(p2score.getText().toString()) - Integer.parseInt(p242.getText().toString())));
p2score.setText(String.valueOf(Integer.parseInt(p242r.getText().toString())));
break;
You're going to have to disable all but the first edit text and only enable them when the user has provided satisfactory input. Then wrap your parseInt() calls in try/catch blocks like so:
case R.id.P2Throw2Set2:
try
{
p222r.setText(String.valueOf(Integer.parseInt(p2score.getText().toString()) -
Integer.parseInt(p222.getText().toString())));
p2score.setText(String.valueOf(Integer.parseInt(p222r.getText().toString())));
// if the previous lines worked, this will work
p232r.setEnabled(true);
}
catch(NumberFormatException e)
{
// user entered "" or the value was null
// in this case, we leave the next edit text disabled
}
break;
A little bit late answer, but you might take a look onto the declarative form validation techniques and advanced validation implemented in the BARACUS framework for Android applications coping with the problems of validation in Android apps. Since the stuff is open source feel free to use source fragments coping with validation if you don't want to use a framework.
I have a problem with a jQuery Mobile listview not refreshing, however only in one extremely particular instance. The issue occurs when I'm testing my app on an android phone (specifically a Google Nexus). The issue occurs after my listview loads properly from view 1 to view 2. I then go to view 3. When I go from view 3 to view 2 the listview does not entirely load. What I mean by this is that it does actually load my code properly, however it is never displayed until I attempt to select one of the rows. Once I select one of the rows they all suddenly appear. I am creating my listview by appending html with javascript in document.ready(). I know this is said to be bad practice currently, however when I use the jQuery suggested method of document.bind('pageinit') it will work from 3 to 2 however everytime I leave view 2 document.bind('pageinit') is fired again. Is there anyway around this? Thanks!
One of the most important things to realize when developing with jQuery Mobile is that your javascript isn't unloaded when navigating between pages. What this in effect means is that when you use .bind() (or .on(), which is the preferred way) it is not unbound when leaving the page. This means you will have to manage this yourself.
After some struggling I found the following solution to work best for me. Load all your javascript in the <head> and create a separate javascript file which handles things like bindings. These files aren't unloaded by page navigations. An outline of this file is as follows.
$(document).on('pagebeforeshow', function(event){
currentPageId = $(event.target).attr("id");
//Give every <div data-role="page"> an id and switch on it.
switch (currentPageId) {
case "index":
loadIndex();
break;
case "fooPage":
loadFooPage();
break;
case "barPage":
loadBarPage();
break;
default:
break;
}
});
On each pageload you will bind new functions you want to that page in the following way:
function loadIndex(){
$(document).one("pageshow", function pageshow(event){
console.log("Entered the index");
$(window).on("scrollstart", someFunction);
//Unbind everything when a new page is loaded.
$(document).one("pagebeforeshow", function pagebeforeshow(event){
console.log("Exited the index")
$(window).off("scrollstart", someFunction);
});
});
}
Do this for every page you want to bind stuff to. When this is done you can execute javascript functions as you are used to, because they are loaded and unloaded on each page show.
First of all, I am very new at Android and teaching myself so I am not asking you to code it for me. Just give me some terms, topics or a subject to study and I will hit the books and figure it out.
I am making an app where the user sets what appears on screen by pressing a button or selecting an image. The only way I know how to do this is to have an onClickListener set a variable and use a switch statement to display an imageView that corresponds to the variable selected.
example:
//on click listeners set variable userPic1 and userPic2
switch(userPic1){
case 1:
pic11.setVisibility(View.VISIBLE);
pic12.setVisibility(View.GONE);
pic13.setVisibility(View.GONE);
pic14.setVisibility(View.GONE);
break;
case 2:
pic11.setVisibility(View.GONE);
pic12.setVisibility(View.VISIBLE);
pic13.setVisibility(View.GONE);
pic14.setVisibility(View.GONE);
break;
case 3:
pic11.setVisibility(View.GONE);
pic12.setVisibility(View.GONE);
pic13.setVisibility(View.VISIBLE);
pic14.setVisibility(View.GONE);
break;
case 4:
pic11.setVisibility(View.GONE);
pic12.setVisibility(View.GONE);
pic13.setVisibility(View.GONE);
pic14.setVisibility(View.VISIBLE);
break;
}
switch(userPic2){
case 1:
pic21.setVisibility(View.VISIBLE);
pic22.setVisibility(View.GONE);
pic23.setVisibility(View.GONE);
pic24.setVisibility(View.GONE);
break;
case 2:
pic21.setVisibility(View.GONE);
pic22.setVisibility(View.VISIBLE);
pic23.setVisibility(View.GONE);
pic24.setVisibility(View.GONE);
break;
case 3:
pic21.setVisibility(View.GONE);
pic22.setVisibility(View.GONE);
pic23.setVisibility(View.VISIBLE);
pic24.setVisibility(View.GONE);
break;
case 4:
pic21.setVisibility(View.GONE);
pic22.setVisibility(View.GONE);
pic23.setVisibility(View.GONE);
pic24.setVisibility(View.VISIBLE);
break;
}
}
I feel like there is probably a better way to code this, but the bigger problem is in my XML layouts. I have all of these imageViews stacked on top of each other and it is getting difficult to see how they will look because it's just a mess.
Any help is appreciated
http://developer.android.com/reference/android/widget/ViewFlipper.html will make this a bit easier for you to manage, without such unruly code.
Using the ViewFlipper as suggested may help if you're trying to switch between many views. However, if you're really just changing the image and don't need to change your layout instead of creating lots of views, I would simply just set the image on your ImageView (I'm assuming picXX are ImageViews). You can store your IDs in an array or two and get them from there. Something like:
int[] userPics1 = new int[] {R.drawable.pic1, R.drawable.pic2, ... }
and then:
ImageView pic = (ImageView)findViewById(R.id.pic);
pic.setImageResource(userPics1[userPic1]);