First of all I'm totally new with Eclipse so my Code maybe full of errors *
Second I wrote a simple App about two buttons (Add,Sub) with One TextView , the idea is about click one of these buttons and then the TextView changes and gives a number (Add=+1 , Sub = -1) , I'm Sure Compiler gave No Errors
But I don't know why I have a lot of problems :
1-The Apk file is not generated in the bin folder .
2-I tried to Export it using Android tools , and then tried it on my mobile and it gave black Screen then stopped working.
3-I got 3 warnings in The Activity_Main.xml about buttons and textview "Hardcoded String Should use #String Resource "
I'm totally Confused , I don't know how to fix it i tried a lot and searched on the internet but i couldn't
Here is the Main_Activity.Java :
package com.example.counter;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int Counter = 0;
Button A = (Button) findViewById(R.id.Add);
Button S = (Button) findViewById(R.id.Sub);
TextView C = (TextView) findViewById(R.id.Calculate);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
A.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Counter ++ ;
C.setText("Your Total Counter Is " + Counter);
}
});
S.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Counter -- ;
C.setText("Your Total Counter Is " + Counter);
}
});
}}
And here is the Activity_main.xml :
<TextView
android:id="#+id/Calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="Waiting To Calculate"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/Sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Add"
android:layout_alignBottom="#+id/Add"
android:layout_alignRight="#+id/Calculate"
android:text="Sub" />
<Button
android:id="#+id/Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Calculate"
android:layout_below="#+id/Calculate"
android:layout_marginTop="66dp"
android:text="Add" />
The app suppose work even on API1 and Targeted API 17 (Jelly Bean) I tested it on Galaxy S3 Mini (have Android 4.2) and it crashed , tried simple app before with same settings and worked
*Any Help is Appreciated , thanks for your time and Sorry for make it Long *
The 2nd problem is on the View (e.g. Button, TextView) initialization.
Since they are declared and initialized at class scope, they are initialized before onCreate() (and thus setContentView()) is called. Because there is no layout inflated at the moment, the initialization will return null. Then, when setOnClickListener() is called, it will throw NullPointerException and crashes the app.
The solution is to move the initialization after setContentView() is called.
public class MainActivity extends Activity {
int Counter = 0;
Button A;
Button S;
TextView C;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
A = (Button) findViewById(R.id.Add);
S = (Button) findViewById(R.id.Sub);
C = (TextView) findViewById(R.id.Calculate);
A.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Counter++;
C.setText("Your Total Counter Is " + Counter);
}
});
S.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Counter--;
C.setText("Your Total Counter Is " + Counter);
}
});
}
}
For the 3rd problem, just as Juwee posted, you can define the String resources to be used in XML layout file (it can be used in the code too!).
Create an XML file in /values folder with <resources> root element (or just use strings.xml if it's already there). Then, you can declare a string resource with String value
Example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="add">Add</string>
</resources>
Then, on the XML layout, you can refer it as android:text="#string/add" (Or in code as setText(R.string.add))
Since this is just only a warning, you can actually ignore it. But if you decide your app to support localization, or if you use the same String quite often, then it is better to define it as app's resource.
Third problem : About this property android:text="Sub" ,you can define the text in the resource file "string.xml",like <string name='subString'>sub</string>,then make reference to it.android:text="#string/R.string.subString" . Have a try .
Related
I want to be able to open 3 buttons within the same java class in Android studio , a map , button leading to text and pictures and the last one a video ..
i used the following codes :
public void location(View view) {
Button great = (Button) findViewById(R.id.location);
great.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),location`enter code here`.class);
startActivity(intent);
}
});
}
the same for the other 2 buttons . as for the .xml file
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="#string/history"
android:id="#+id/button"
android:onClick="location"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"/>
and the string is
<string name="map">Map</string>
<string name="location">Location</string>
<string name="info">information about related subject
</string>
the thing is how can i open all 3 buttons in the same main layout ? do i use
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),location`enter code here`.class);
}
in all 3 buttons ? or do i have to assign each button to a different java class ? specially when all API setting (import google map ) shows up grey in the main java ?
I'm new to Android development, and to this site!
I have done a few tutorials etc and am working on a project at the moment, and had a good look through other answers to similar questions, but haven't been able to find quite what i'm looking for (but loads of good suggestions!)
I am trying to get buttons on my main screen linking to individual pages. I am using my phone instead of an emulator, but every time i click on a button, the app dies... can you help me?
This is my main Screen code for button1:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declaring and defining the buttons used
Button student1 = (Button) findViewById(R.id.button1);
// Setting the onClickListener for button1
student1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//calling the page1 function
page1(view);
}
});
This is the page1 function:
public void page1(View view) {
Intent intent = new Intent(this, Page1.class);
startActivity(intent);
}
Here is the code for the Page1 class file:
public class Page1 extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_1);
}
}
This is the code for the layout file: (page_1.xml)
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="shannon.white.finalyear.DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
If you need anything else, let me know
Any ideas?
Thanks :)
Your coding looks to be correct.
The next thing to check would be to look inside your AndroidManifest.xml file to ensure you have added the activity to it so the android OS knows it exists. You add it like so:
<activity android:name="Page1" />
If your activity resides in a different package then the one declared inside the manifest file, then you need to specify the full package inside the "name" like so:
<activity android:name="some.other.package.name.Page1" />
Thats about all i can say from the provided code. If you are simply starting another activity which is Page1.class then your code looks correct and you might be missing the manifest declaration as i stated above.
Try moving this following code
// Declaring and defining the buttons used
Button student1 = (Button) findViewById(R.id.button1);
// Setting the onClickListener for button1
student1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
underneath your page1 function so your code looks like this:
public void page1(View view) {
Button student1 = (Button) findViewById(R.id.button1); // Declaring and defining the buttons used
student1.setOnClickListener(new View.OnClickListener() { // Setting the onClickListener for button1
#Override
public void onClick(View v) {
startActivity(intent);
Intent intent = new Intent(this, Page1.class);
}
}
}
and your onCreate look like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
page1(view); // NOTE I'm now declaring it on the onCreate instead of onClick
}
If that doesn't help, well your code still looks cleaner. Could just be my OCD though...
This is a very good tutorial by Mkyong on how to achieve what you are trying to do. If no other answers help, restarting using this tutorial will likely help you succeed. On multiple occasions I've tested his code and its worked.
first I would like to say thank you to every one out here as i am a nOOb and have learned a lot just by reading questions and answers that you post. I am trying to pass a great deal of text to the end users and while being able to do this with new classes and .xml files this is becoming cumbersome. i thought of stream lining the app by just having a single xml layout for a particular set of text strings and just change the #string/????? via button onclick and setText but have learned that I can not change the initial value of #string in an xml file. question is that TRUE? and is there a more efficient way to do this ie (setting android:text to a var and setting var in java to a particular string) or do i need a new xml layout for each string? (that's a lots of waste if you ask me) and a little insight, there are at this time approx 250 different strings with min 5 paragraphs and growing.
here is my code thus far.
snippet of first java
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Monlt extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.monolt);
final MediaPlayer buttonsound = MediaPlayer.create(Monlt.this, (R.raw.buttonclick));
Button button1 = (Button) findViewById(id.button1);
Button button2 = (Button) findViewById(id.button2);
Button button3 = (Button) findViewById(id.button3);
Button button4 = (Button) findViewById(id.button4);
Button button5 = (Button) findViewById(id.button5);
Button button6 = (Button) findViewById(id.button6);
Button button7 = (Button) findViewById(id.button7);
Button button8 = (Button) findViewById(id.button8);
Button button9 = (Button) findViewById(id.button9);
Button button11 = (Button) findViewById(id.button11);
Button button12 = (Button) findViewById(id.button12);
Button button13 = (Button) findViewById(id.button13);
Button button14 = (Button) findViewById(id.button14);
Button button15 = (Button) findViewById(id.button15);
Button button16 = (Button) findViewById(id.button16);
Button button17 = (Button) findViewById(id.button17);
Button button18 = (Button) findViewById(id.button18);
Button button19 = (Button) findViewById(id.button19);
Button button21 = (Button) findViewById(id.button21);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
buttonsound.start();
final TextView mview = (TextView) findViewById(R.id.solayout2);
mview.setText("mono1"); //this was my first string to pass
startActivity(new Intent("com.nvar.Sorders.Mono.ASO1"));
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
buttonsound.start();
startActivity(new Intent("com.nvar.Sorders.ASO1"));
final TextView mview = (TextView) findViewById(R.id.solayout2);
mview.setText("#string/mono2");/this is the second string to pass
}
});
`
now this code works when i remove the 2 text view lines in the onclick
so then it call another class Aso1 that I would like to keep in place for later use.
Aso1 java code
`
package com.nvar.Sorders.Mono;
import com.nvar.Sorders.R;
import android.app.Activity;
import android.os.Bundle;
public class Aso1 extends Activity {
// Called when the activity is first created.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.solayout2);
}
}
`
and then the first xml
`
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/solayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mview"/>
<!-- android:text="#string/monoaso1"/>
-->
<!-- this is were i was playing with the strings />
-->
</LinearLayout>
</ScrollView>
`
any help in this matter would be greatly appreciated, and remember "noob" to java / android! so if there is a sample of what i could or should be looking at, don't hesitate to smack me in the head and point me in the right direction. i don't mind reading :)
thanks again.
When displaying the Strings, What you could use is a TextView and have multiple lines and then just update the text value of it in code. It doesn't even need default text
<ScrollView
...some params...>
<TextView
android:id="#+id/my_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="10"/>
</ScrollView>
This will give you a TextView 10 lines long. Then in code you can do something like this to update the value:
String longText = getVeryLongText();
((TextView)findViewById(R.id.my_text)).setText(longText);
Then you'll have something that looks like a scrollable paragraph of text
*#string/string_name* in xml are not designed to change the value. They are there to help you with localization. Currently that xml is located here http://developer.android.com/guide/topics/resources/localization.html
res/values/string.xml
you can have another string xml with different language like in the following location, for example france
res/values-fr/strings.xml
you can read more about that over here.
Now, let's move on to how to reference that string_name from java
Resources res = Monlt.this.getResources();
mview.setText(res.getString(R.string.string_name));
//do something
mview.setText(res.getString(R.string.mono2));
I used to have some experience with developing for android but I started up again after 6 months and forgot most of it. I am now using a macbook to do my developing on and had to set up Eclipse, the Android SDK and AVD all over again and I'm worried I messed something up.
When I start a new project with the default activity that displays "Hello World" on my screen the app runs fine. I then tried to put in two buttons that cause the text in a new TextView to change. But whenever I include the textView part I get a runtime error. When I comment it out, the app runs but obviously nothing happens. Based upon the tutorials I've been reading, this is the appropriate place and way to declare/create the textView but I can't figure out what's wrong. Any suggestions?
[Edit] I was messing around and found that I can make the mytext a field instead of a TextView and that worked. So in my onCreate(), I put
mytext = (TextField)findViewById(R.id.TextView1);
but that doesn't seem the right way to do things.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class IntroActivity extends Activity {
TextView myText = (TextView)findViewById(R.id.textView1);
//i've tried this with final added on to it as well (recommended by eclipse)
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setButtonClickListener();
}
private void setButtonClickListener() {
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myText.setText("Hello");
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// myText.setText("Goodbye");
}
});
}
}
This:
TextView myText = (TextView)findViewById(R.id.textView1);
should be separated. The declaration should be at the same place:
private TextView myText;
But the assignment should come only after setContentView:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView)findViewById(R.id.textView1);
This is done since before setContentView, Dalvik doesn't know from which layout to take the view that matchs the id R.id.textView1
I have a EditText that I have set to invisible by default. I would like to make this box visible onclick of a ImageView, but cant find any documentation online to help me, how would I go about doing this?
In your xml
<EditText
android:id="#+id/my_edit_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:inputType="number"
android:visibility="gone"
android:paddingRight="8dp" />
In your Activity Class
import android.widget.Button;
import android.widget.EditText;
public class MyActivity extends Activity {
private EditText editTxt = null;
private Button myBtn = null;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.my_xml_layout);
editTxt = (EditText) findViewById(R.id.my_edit_text);
myBtn = (Button) findViewById(R.id.my_button);
myBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
editTxt.setVisibility(View.VISIBLE);
}
});
}
}
well instead of invisible you can make it disable. This way you can prevent it from user input. And it is very easy to enable or disable EditTex.
You can use JQuery to toggle display (display:none vs. display:block) on your EditText field. You probably want to start out with display:none on the EditTextBox. You will also need to have JQuery loaded in your page. Can't make it a detailed tutorial here so giving you enough to get to next level.
$jq(document).ready(function() {
$jq("#yourImageView").click(function(e) {
$jq("#yourEditTextBox").fadeToggle("slow", "linear");
});