I am trying to develop a very simple Android app for rapid data collection, where an activity displays a fragment containing a form with a number of RadioGroup elements. I would like to have the user be able to quickly tap out the radio selections, then be able to swipe the form to the right to dispose of the fragment, record the data, and bring up another empty form.
What I can't figure out is how to set up the "swipe and replace" feature. I'm thinking of something similar to the ViewPager support class, except no scrolling backwards and an unlimited number of pages. What classes can I use to achieve this kind of behavior? Here's my stripped-down code:
fragment_record.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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/mainScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
<LinearLayout
android:id="#+id/mainLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Group 1"
android:id="#+id/textView" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/Group1">
<!-- Radio Buttons /-->
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Group 2"
android:id="#+id/textView2" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/Group2">
<!-- Radio Buttons /-->
</RadioGroup>
</LinearLayout>
</ScrollView>
RecordFragment.java:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class RecordFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_record, container, false);
return view;
}
}
activity_record.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container" >
</FrameLayout>
RecordActivity.java
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.View;
public class RecordActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) return;
RecordFragment theFragment = new RecordFragment();
theFragment.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(R.id.fragment_container, theFragment).commit();
}
}
}
Ok, the simple answer to this was delivered obiter dicta in the tech talk a month or two ago where Google showed the preview of Android Studio 2.0. One of the presenters said 'we see a lot of people out there using Fragments in places where they should be using ViewGroups.' I was one of those people too.
A wizard is a complex problem: you are usually building something incrementally, so there is state, dependencies, and flow. It's hard to make really generic because what is being built is completely different. Also, it's a clear anti pattern to partially construct the target object leaving a bunch of nullable fields to be filled in as the process grinds on.
If each group can contain a part, then you can assemble the parts and build the target from the parts. There's no shame in it if those parts are not necessarily organic domain objects: if you are modeling the process of obtaining a job, there can be an Application (the thing the user has to fill out) and the CV or Submission (what is built in the form to represent a specific request for consideration).
Related
I'm writing a simple Android App using AIDE (Android IDE). I gave one of my layout elements an ID, but when I try to access the element using findViewById(), I get an error tht says: "Unknown member 'id' of 'com.mycompany.mailscomunes.R'. I haven't seen this error outside of AIDE.
This is the Java code:
package com.mycompany.mailscomunes;
import android.app.*;
import android.os.*;
import android.content.Intent;
import android.provider.ContactsContract;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.one);
}
}
And this is the relevant XML:
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/one"/>
Remove these lines:
import android.app.*;
import android.os.*;
You're literally importing the entire Android framework, which includes layout files, which has ID's in them.
And you're not including your own ID file (called "R.java").
So remove those two lines, and include this one:
import com.mycompany.mailscomunes.R;
The root of an activity xml should be of Layout type.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Text"/>
</LinearLayout>
Goto your project and delete the build folder and do a rebuild. I had the same problem and is fixed
I have got an idea to get rid of some coding when we do initializion of views.
When we create an xml layout in android. At that movement same name class is created with UpperCase Letters with a dialogue with permission. And as i created views with ids. It should create Views and initialize them automatically.
for e.g
close.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/closeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/close_img" />
<TextView
android:id="#+id/closeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing Text" />
</LinearLayout>
And automatically generated java code should be.
public class Close extends Activity
{
TextView CloseText;
Button CloseBtn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CloseText = (TextView)findViewById(R.id.closeText);
CloseBtn = (Button)findViewById(R.id.closeBtn);
}
}
I can read xml and do the other stuff as I explained. But how could i add this module to eclipse and How could i create a service which work in Background all the time in eclipse.
Suggest me how to add this module(Plugin) to eclipse and i am going to use JAXB to generate Java Objects from XML document. Is there any better option.
Here in this website i find that just paste the xml and you will get your java code ready for activity class.
i had attached the link
I've been making a soundboard app of my infant son's sounds. The app simply opens to MainActivity class where it displays a scrollable list of buttons that, when pressed, will playback a pre-recorded MP3 of my son screaming something.
Once it's debugging or running on my Nexus 7 there is a Choreographer error and somewhere between 50 and 90 frames are skipped. The warning is that the 'application may be doing too much work on its main thread'. On my device three random buttons refuse to play sound until the app is closed and re-opened but then another three random buttons refuse to work. This problem didn't occur when my app only had eight buttons in a simple LinearLayout (without ScrollView).
I'm a beginner to both java and Android programming but, from what I guess, my laughably poor code is taking too much memory to properly function. My question is either, how would you code a scrollable list of 33 buttons that play their own sound, or could someone teach me how to better my code, please?
MainActivity.java (showing only the first three buttons/sounds for this question):
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer ahh = MediaPlayer.create(this, R.raw.ahh);
final MediaPlayer dededeh = MediaPlayer.create(this, R.raw.dededeh);
final MediaPlayer neganegabunbunbug = MediaPlayer.create(this, R.raw.naganagbunbun);
Button bahh = (Button) this.findViewById(R.id.Ahhhbutton);
bahh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ahh.start();
}
});
}
}
And the activity_main.xml (again with only the first three buttons for this question):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/Ahhhbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#layout/button_shape"
android:text="#string/button1a"
android:textColor="#d1d1d1"
android:textSize="20sp" />
<Button
android:id="#+id/dededehbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#layout/button_shape"
android:text="#string/button1b"
android:textColor="#d1d1d1"
android:textSize="20sp" />
<Button
android:id="#+id/neganegabunbunbugbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#layout/button_shape"
android:text="#string/button1c"
android:textColor="#d1d1d1"
android:textSize="20sp" />
Button bdededeh = (Button) this.findViewById(R.id.dededehbutton);
bdededeh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dededeh.start();
}
});
Button bnaganagbunbun = (Button) this.findViewById(R.id.naganagabunbutton);
bnaganagbunbun.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
neganegabunbunbug.start();
}
});
</LinearLayout>
</ScrollView>
</LinearLayout>
It's not the layout. As your error said, you shouldn't do any long-term work on the Main-Thread.
The main-thread (or on Android the UI-Thread) handles interaction with the user. For the UI to not freeze and feel fast, you should execute long-term actions off the UI-Thread.
Also, the problem is not the actual playback, but you loading all media-files:
MediaPlayer.create(this, R.raw.ahh);
As the docs for the create()-method suggest:
[...] When done with the MediaPlayer, you should call release(), to
free the resources. If not released, too many MediaPlayer instances
will result in an exception.
For the "off the UI-Thread"-thing, you have three options:
Directly can create a Runnable-implementing class that plays your sound and submit it to a ScheduledThreadPoolExecuter (maximum control, more code).
Use an AsyncTask (Android exclusive) to play the song (fairly simple).
If you want the sound-playback to continue even when the app is not currently in the foreground, use a Service (most work)
So, what you should do is:
Store a List of your raw-file references (the R.raw.ahh) and don't load them directly.
When a song is selected for playback, call create() on that songs ID and start playback off the UI-Thread.
When done, call release() to release the resources.
I feel like I've looked everywhere and I still don't know how to accomplish my goal. I have an android app that I made with Eclipse, and it has a table layout within a linear layout. The table does some calculations and provides answers like an excel spreadsheet. The link to my app is below. I simply want to add a button to export to an image file that can be stored on the SD drive, that, or a PDF file. I thought it would be simple, but I'm completely lost here. I am new to programming, but the app has close to 500 downloads now, and I've had a request to add this function. I made the activity display in XML. Thanks for helping.
![enter image description here][1]
https://play.google.com/store/apps/details?id=com.bestserialdilutioncalculator&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5iZXN0c2VyaWFsZGlsdXRpb25jYWxjdWxhdG9yIl0.
So I already have the button made in XML (JPEGB), as you can see further below. In Java code I haven't yet added anything related to this button or to the exporting to image. The "Button Calculate" is for a different purpose, and is referred to later on in the code. So basically, this is starting from scratch in the Java code, with a simple button made in XML. I truncated the code where I considered it unnecessary for this post. Thanks so much!!
Java Code
package com.example.bestserialdilutioncalculator;
import java.math.BigDecimal;
import java.math.RoundingMode;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.bestserialdilutioncalculator.R;
public class InitialLayout extends Activity {
Button Calculate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table_calculator);
XML for Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:orientation="vertical"
android:background="#000000"
android:layout_width="42dp"
android:layout_height="fill_parent">
<View
android:layout_width="25dp"
android:layout_centerHorizontal="true"
android:background="#drawable/customicon"
android:layout_height="25dp"
></View>
<Button
android:id="#+id/CalculateButton"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="55dp"
android:background="#drawable/goodbutton"
android:text=" " />
<Button
android:id="#+id/JPEGB"
android:layout_width="match_parent"
android:layout_height="28dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:background="#drawable/jpegbutton"/>
</RelativeLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollerForTable"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:fillViewport="true"
android:background="#000000" >
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/HorizontalScrollerForTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000">
<TableLayout
android:id="#+id/Table"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#000000" >
rest of code is table layout details, and is very long.
So, you can capture your application's view (screen) and write it to a file.
First create a Bitmap of your application's root view:
mRootView.setDrawingCacheEnabled(true);
Bitmap bmp = Bitmap.createBitmap(mRootView.getDrawingCache());
mRootView.setDrawingCacheDisabled(false);
mRootView would be the view you passed into setContentView() in your activity's onCreate() method.
Then write it to disk:
// This should be done off the main (UI) thread!
String externalStoragePath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
outFile = new File(externalStoragePath, "myfilename.jpg");
try {
OutputStream os = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, os);
os.flush();
os.close();
} catch (Exception e) {
// handle error here
}
For brevity I have not included the necessary checks for ensuring that external storage is indeed available and mounted. I can add if needed.
Ex: I have a project with UI contains a button..
Activity:
package android.tuanshaker.myproject.com;
import android.app.Activity;
import android.os.Bundle;
public class MyProjectActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
And file main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
I want to display code and xml of my project by textview..How do i can to get them?
Copy the xml, js code files to res/raw. Use Context.getResources().openRawResource() to read them.
In addition, you can use jHighlight to format those source codes (in a web view).