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
Related
This link says that to make the app display a layout, you create a main_layout.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
and then load it in onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// main_layout.xml is compiled into the R.layout.main_layout object
setContentView(R.layout.main_layout);
}
My problem is that I am coding in C, and have the C version of "onCreate" method:
JNIEXPORT
void ANativeActivity_onCreate(ANativeActivity *activity, void *savedState,
size_t savedStateSize) {
....
}
is it possible to load the layout and make the app display it in the C language?
C uses "ANativeActivity" that it doesn't has any of Java methods and resources/objects. Even if you manually parse that XML you should MANUALLY implement all Widgets/Components in C.....so it's near impossible due to high amount of work involved in it.
Usually someone chooses C on Android to do "some special work" that is not available on normal Java or due to performance issues on it.
So I just started playing around with android and I'm trying to see what things I can do. I was following the very first android tutorial: http://developer.android.com/training/basics/firstapp/index.html and at the very end, you programmatically define a TextView. I wanted to change this to be defined in a new layout, so I wrote this (it is named display_message.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" >
<TextView android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
And in the DisplayMessage class, I changed it to this:
public class DisplayMessage extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.display_message);
// Get message from intent
Intent intent = getIntent();
String message = intent.getStringExtra(FirstActivity.EXTRA_MESSAGE);
// Get the text view
TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(message);
}
}
However, Eclipse says that it doesn't know what R.layout.display_message is, nor does it know what R.id.text_view is. Is there somewhere else I need to define them or something? Where did I mess up?
It seems correct but be sure that the R class imported is the correct one.
Sometimes Eclipse imports android.R but the R file you need to import is your.package.name.R
I'm sure this is something dead simple as it's my first android app with code (the hello world example I did was just assigning a value to a string in XML). My problem is when trying to get the reference for my button in my variable then the id as in R.id is not defined?
The compiler error is in a comment in the code below where it occurs:
package com.geeksonhugs.simplecalc;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
private Button AddButton;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AddButton = (Button)findViewById(R.id.btnAdd);
//"Unknown member 'id' of 'com.geeksonhugs.simplecalc.R'"
//AddButton.setOnClickListener(this);
}
}
XML layout file:
<?xml version='1.0' encoding='utf-8' ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LayoutRoot"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/txtFirstNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/strFirstNum" />
<EditText
android:id="#+id/edtFirstNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="" />
<TextView
android:id="#+id/txtSecondNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/strSecondNum" />
<EditText
android:id="#+id/edtSecondNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="" />
<Button
android:id="#+id/btnAdd"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/strAdd"
android:gravity="center" />
<TextView
android:id="#+id/txtResult"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="" />
</LinearLayout>
I resolved the issue by deleting the bin folder and recompiling using the AIDE App.
The R class is code-generated for you by the Android build tools when you build your project. This means there are four possibilities:
You have not tried building the project yet, in which case, try that.
Eclipse doesn't think it needs to build the project for some reason -- try Project > Clean from the Eclipse menu (only relevant if you are using Eclipse, of course).
There is some bug in your manifest or one of your resources that is preventing R from being built. There should be Eclipse error indicators for this.
You do not have an android:id attribute in your layout, and therefore there is no R.id available.
Please make sure the xml file you pasted is called main.xml and under layout folder.
And try to generate the R file again.
That may help.
For those using AIDE having issues referencing Id, try deleting the "gen folder" located in the bin folder and recompile. The problem is the R.java class did not create a constructor called Id. So deleting the gen folder containing the R.java class and recompiling solves this issue.
You must add id to a component in the XML file. Like this:
android:id="#+id/buton1"
Now can you click mainActivity.java and write code "R.id." you a see hint to write "buton1" and no error of "id"
First Basically I need to have layout when application starts.
Second I am getting data from views i have in this layout.
Third Im setting next layout and doing hard code work with data, that I got in step 2.
I can't figure out how to do this.
You can re-use the framework class ViewAnimator without any animations. It's not the prettiest solution, but should get the job done.
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">
<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/viewFlipper" />
</LinearLayout>
MyActivity.java:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewAnimator = (ViewAnimator)findViewById(R.id.viewFlipper);
viewOne = createViewOne();
viewTwo = createViewTwo();
viewAnimator.addView(viewOne);
viewAnimator.addView(viewTwo);
...
}
private void someMethod() {
... do my background task ...
viewAnimator.setDisplayedChild(1); // viewTwo
}
have you gone through the google notepad tutorials? They explain using a list screen and loading a edit/add screen used to modify you sqlite database
I've created a countdown timer that counts Days:Hrs:Mins:Sec. using regular text view and updating it using my handler works fine for me.
however I want to create a cool animation for the changing digits:
1st Q:
I have 2 options as I see it to draw the numbers:
1. using a home made font applied with a style
2. using a textview/btn with no text on them and applying a backrgound image using setBackgroundResource()
--what should I choose?
2nd Q:
I've created a wrapper for ViewFlipper(not extending it)
public class transitionair extends Activity {
SpecialFlipperWrapper m_thousand;
SpecialFlipperWrapper m_hundred;
SpecialFlipperWrapper m_tens;
SpecialFlipperWrapper m_ones;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
ViewFlipper flipper_Tsnds=(ViewFlipper)findViewById(R.id.yearThousand);
m_thousand = new SpecialFlipperWrapper(flipper_Tsnds, this, 9);
ViewFlipper flipper_Hndrds=(ViewFlipper)findViewById(R.id.yearHundread);
m_hundred = new SpecialFlipperWrapper(flipper_Hndrds, this, 9);
ViewFlipper flipper_Tns=(ViewFlipper)findViewById(R.id.yearTens);
m_tens = new SpecialFlipperWrapper(flipper_Tns, this, 9);
ViewFlipper flipper_Ons=(ViewFlipper)findViewById(R.id.yearOnes);
m_ones = new SpecialFlipperWrapper(flipper_Ons, this, 9);
m_thousand.startFlipping();
m_hundred.startFlipping();
m_tens.startFlipping();
m_ones.startFlipping();
}
}
however trying to fetch one of the latter views that come after the yearThousand id are retrieving null
XML I'm Using is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ViewFlipper
android:id="#+id/yearThousand"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ViewFlipper
android:id="#+id/yearHundread"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ViewFlipper
android:id="#+id/yearTens"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ViewFlipper
android:id="#+id/yearOnes"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ps xml's for the animation itself are working so I left them out
Main questions is: can I make it better?
second question is: how?
third question is why is my layout returning null on the second call to it and how can I avoid it?
10XX