I have a very simple question about menu control in Android. I'm writing a program that performs some simple numeric calculations and outputs an answer. My question is how do make the program move to second screen when a button is pressed, and how can I let the user move back to the original screen.
Here's a quick example
Screen 1
"Add Numbers"
Input 1st # ____
Input 2nd # ____
(Add)
Screen 2
The answer is "____"
The user inputs two integers. presses add, and then the program moves to the second screen which displays the answer. If they user wants they can return to the first screen and start over.
I know this has been covered but with so many resources I don't know what to look for. Answers or links to resources would be most helpful. Thank you!
You can use the following layout for the screen to input numbers. Name the file first.xml.
<TextView
android:id="#+id/firstnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Input Ist:"
android:textSize="20px"
android:textStyle="bold"
/>
<EditText
android:id="#+id/first"
android:layout_height="wrap_content"
android:layout_width="250px"
/>
<TextView
android:id="#+id/secondnumber"
android:text = "Input 2nd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20px"
android:textStyle="bold"
/>
<EditText
android:id="#+id/second"
android:layout_height="wrap_content"
android:layout_width="250px"
/>
<Button
android:id="#+id/add_button"
android:text="Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15px"
android:layout_centerHorizontal="true"
/>
</LinearLayout>
To add these numbers
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(this);
EditText num1 = (EditText)findViewById(R.id.first);
EditText num2 = (EditText)findViewById(R.id.second);
}
The click listener code
public void onClick(View v) {
int n1 = Integer.parseInt(num1.getText().toString());
int n2 = Integer.parseInt(num2.getText().toString());
int result = n1 + n2;
String res = Integer.toString(result);
Intent intent = new Intent(getApplicationContext(), Result.class);
intent.putExtra("result", result); //Passing the result to the second activity using intent
startActivity(intent);
}
In the Result.java class.
public class Result extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
Intent intent = getIntent();
String result = intent.getStringExtra("result");
TextView t1 = (TextView)findViewById(R.id.result);
t1.setText(result);
}
}
Hope this helps..
May I know what type of language are u using?
I recently just did it with C#.
My flow is that, when I start a program, it will auto be forced to the second screen, with some power options.
But if yours is just moving the program the diff screens, it will be easy in C#.
Screen Srn = Screen.PrimaryScreen; // gives your information of Primary Screen.
Screen[] Srn = Screen.AllScreens; //gives u an array of all your available screen( srn[0] is primary screen, etc)
So, using the intellisense from the IDE, should be able to get the width, height, etc.
Cant remember exactly, but something like src.width or src.bounds.width.
Easiest way to move your program will be to move the program x axis to the respective screen.
ViewFlipper will work well. You could also try "startActivityForResult" to pass your answers to the next screen.
Take a look at the ViewFlipper class. It works like tabs, but without the tabs UI. You can use an xml layout to display your 2 (or more) screens, and showNext() or showPrevious() to flip between the screens.
Related
I have a very strange behaviour of two buttons:
<Button
android:id="#+id/main_button_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/main_button_measure"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:text="#string/button_logout" />
<Button
android:id="#+id/main_button_measure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/main_button_logout"
android:layout_alignStart="#+id/main_button_logout"
android:layout_alignParentTop="true"
android:layout_marginTop="194dp"
android:text="#string/button_measure" />
I initialize them in my main activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.sessionManager = new SessionManager(this);
this.sessionManager.login();
this.setContentView(R.layout.activity_main);
this.buttonMeasure = (Button)this.findViewById(R.id.main_button_measure);
this.buttonMeasure.setOnClickListener(this);
this.buttonLogout = (Button)this.findViewById(R.id.main_button_logout);
this.buttonLogout.setOnClickListener(this);
}
My click listener:
public void onClick(View view) {
switch(view.getId()) {
case R.id.main_button_measure : this.readNFC(view); break;
case R.id.main_button_logout : this.sessionManager.logout(); break;
}
}
Now to the problem: Everytime i push my measure button the logout is called and everytime
i call my logout button the readNFC is called. Whats wrong there?
It happens when you change a layout for example that only part of the application is rebuilt (the rest, e.g. the sources will use the previous build artifacts if they haven't changed) and a resource ID with the same name in the xml layout and the java sources will actually translate to different int IDs.
Try to put a breakpoint in your onCreate() and check the details of the 2 Buttons you set the on click listener of, most likely this.buttonMeasure will reference the logout button
(i.e. will have a top margin of 76dp for example), and vice-versa.
So just clean and rebuild, it should solve your problem.
You can blame it on the build tools ;)
I have read a number of tutorials on this topic but not able to make it working. Can anyone please help me with the following
a) A home screen widget which has a button, Imageview and a TextView
b) The textview updates periodically via a service
c) On Click of the button the Image Changes.
Can anyone please help with a sample code or point to codes which do this functionality
check out this book, it explains very clear how to make a widget "silent mode toggle"
http://iit.qau.edu.pk/books/Android.Application.Development.for.For.Dummies.pdf
a) In your xml, the following will be the code.
<Button
android:id="#+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/go" />
<ImageView
android:id="#+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image" />
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#string/welcome" />
Of course, you need to arrange them according to your fancy.
b) Updating the text view is no big deal. Getting the text from the source is what is important. If it is from a web page, you could refer to usage of JSON. If it is from a database, then try SQLite. It all depends on your necessity.
Lets assume that you get your text. Your code for updation will be:
TextView t =(TextView) findViewById(R.id.tv);
t.setText(your_string);
To do it periodically you can use a parallel threading concept such as a runnable.
c) Set up an on Click Listener for this purpose.
Button b1;
b1 = (Button) findViewById(R.id.b);
b1.setOnClickListener(button_func);
This comes in your onCreate method.
View.OnClickListener button_func = new View.OnClickListener() {
#Override
public void onClick(View arg0) {
image = (ImageView) findViewById(R.id.iv);
iv.setImageResource(R.id.new_image);
}
};
Hope that you find this useful
I have an int that depends on users input. How do I display that int on the screen?
Here is what I have been attempting:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#int/HW" // What goes here, This is where i am confused
tools:context=".DisplayMessageActivity" />
Also here are my defined variables, you may have seen my post not too long ago about this program:
// Receive messages from options page
Intent intent = getIntent();
int HW = intent.getIntExtra("MESSAGE_HW", 0);
int OTW = intent.getIntExtra("MESSAGE_OTW", 0);
int HPD = intent.getIntExtra("MESSAGE_HPD", 0);
I am trying to display the int HW on the screen, any help is greatly appreciated! Thank you!
android:text="#int/HW" // What goes here, This is where i am confused
A default value can go there. Something that would tell you that the TextView has not been populated yet.
How do i display that int on the screen?
To display the int on the screen inside the TextView you have shown us. You need to get the TextView in your Activity with something similar to the following:
TextView textView = (TextView) this.findViewById(R.id.textViewId);
Then you can set the text on that textView with the following:
textView.setText(String.valueOf(HW));
Here is an example of adding the id to your xml:
<TextView
android:id="#+id/textViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#int/HW" // What goes here, This is where i am confused
tools:context=".DisplayMessageActivity" />
When you get the users input, do this:
TextView hwTextView = (TextView)findViewById("R.id.hwTextView")
hwTextView.setText(String.valueOf(yourIntVariable));
Note this means you'll have to add an id attribute to your TextView with the value of "hwTextView"
Building on prolink007's answer, you could also omit that line then call setText, java side.
<TextView
android:id="#+id/hw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
tools:context=".DisplayMessageActivity" />
TextView HWgetText = (TextView)findViewById("R.id.hw");
HWgetText.setText(String.valueOf(HW));
I've seen some similar questions here but haven't found what I was really looking for. I need to create a simple activity where the user must enter a number and return it to the main activity. The layout should contain only an edit text on the upper half of the screen and a software keyboard on the lower half of the screen. The activity should finish when the Done key is pressed on the keyboard. Will appreciate any links or code snippets to help resolve this issue.
I recommend you use a Custom dialog to do it.
The point is, you want a keyboard to interaction, and return when a number is pressed, aren't you?
If want an example, you can create a Dialog Activity like:
public class Keypad extends Dialog
protected static final String TAG = "Keypad" ;
private final View keys[] = new View[9];
private View keypad;
private int tecla = 0;
Then set the this content on create:
setContentView(R.layout.keypad);
findViews();
setListeners();
find views will be something like this:
keypad = findViewById(R.id.keypad);
keys[0] = findViewById(R.id.keypad_1); ...
And the dialog XML must have a table:
<TableRow>
<Button android:id="#+id/keypad_1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="keypadClick"
android:text="keypadClick"></Button>
<Button android:id="#+id/keypad_2"
android:text="2" >
</Button>
<Button android:id="#+id/keypad_3"
android:text="3" >
</Button>
</TableRow> ... Etc
So, when you launch the dialog, appear a menu with 9 numbers (in my case) whom dismiss when push 1 of then, and dismiss the dialog (return to the point where was throw)
I hope it help!!
I'm trying to set up a "close to start" button in a game. The game takes the user from view to view like a maze. The user could be on any view, but I want a way to get them back to the start screen. Is there a way to find and return the ID of the current view for my findViewByID? I've found a I've tried the following code in various forms:
OnClickListener closetomain = new OnClickListener() {
#Override
public void onClick(View v) {
int currentid = v.findFocus().getId();
RelativeLayout hideme = (RelativeLayout)findViewById(currentid);
hideme.setVisibility(View.GONE);
setContentView(R.layout.main);
// RelativeLayout showme = (RelativeLayout)findViewById(R.id.startLayout);
// showme.setVisibility(View.VISIBLE);
}
};
Okay, it turns out I should have given each close button the same ID and used theisenp's suggestion for the simplest code (well, that I could understand). It also turns out that I should have started by putting each level/layout in its own activity. You live and you learn I guess. Below is my XML and java. It may not be the elegant solution but I'm not sure I care all that much as long as it works.
<ImageButton android:id="#+id/closeButton"
android:layout_height="wrap_content"
android:background="#drawable/close"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:onClick="closetomain">
</ImageButton>
And here's my Java:
public void closetomain(View view) {
switch(view.getId()) {
case R.id.closeButton:
setContentView(R.layout.main);
break;
}
}
Why do you need to retrieve the id of the current view? Is it just so that you can set it's visibility to GONE? If so, there are probably better ways of implementing that same functionality.
Instead of just changing the layout with setContentView(), it would probably be a better idea to have the Start Screen be its own separate activity. When you are in any of the "maze views" you could simply use an intent to start your home screen activity like this
Intent startScreenIntent = new Intent(this, StartScreen.Class);
startActivity(startScreenIntent);
Then you won't have to worry about finding id's or changing visibilities, plus the code is cleaner, because it separates the code for your Maze levels and your Start Menu.