I am new to android development and am using android studio in ubuntu to write a simple Android app.
Right now the main activity activity_go is an xml file which describes the first set of objects being displayed. By adding a click listener to a button object described on activities_go.xml
I am able to change the activity to another set of objects, 'two' which corresponds with two.xml
The Following is the java code I am currently using to switch between xml files
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_go);
Button nxtButton = (Button) findViewById(R.id.nextButton);
nxtButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0){
setContentView(R.layout.two);
}
});
}
Upon running this I am able to display two.xml once the button is clicked.
How Can I do this again, upon clicking a button on two.xml change to three.xml ?
or is there a better method to shift between the layout xml for android applications?
I guess, you need to view trainings by Google. This article is about your case.
Related
What I want to do:
I want to have multiple activities each prefaced with a page explaining to the user what the activity is about.
What I'm currently doing:
So my main class BaseModuleActivity extends Activity and I am trying to write a function called showTutorial() which will explain the next steps to the users.
Here is my attempt in doing so:
public void showTutorial(String title, String explanation){
setContentView(R.layout.tutorial_screen);
TextView tv1 = (TextView)findViewById(R.id.tutoTextTitle);
tv1.setText(title);
TextView tv2 = (TextView)findViewById(R.id.tutoTextExplanation);
tv2.setText(explanation);
findViewById(R.id.tutoButton).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//remove the tutorial's view
findViewById(R.id.tutoLayout).setVisibility(View.GONE);
}
});
}
And this method is called in the following:
public class myFirstActivity extends BaseModuleActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
//First show tuto
super.showTutorial(getString(R.string.upTitle),getString(R.string.upExplanation));
//TODO then actually do the activity stuff
/*
(findViewById(R.id.next_button)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
*/
}
}
Problem:
I think the problem is mainly conceptual. I don't know the best approach to do this and the approach I'm taking is not working.
What I'm doing is not working because the view just become empty. I thought setting the visibility of the linearLayout to gone would make it disappear and the actual activity will able to take place.
What I need:
I need to understand if I can do what I want with my approach or not? Or what approach should I take.
I found some similar questions. However, the answer to these questions didn't seem to fit my problem.
I also looked into layout inflater and fragment, but layout inflater seem to be more for listView and fragment uses layout inflater.
Well, there are some approaches to show a guide for your activity (or application).
First one, and probably the easiest, is to show a dialog/TextView when user enters an activity and explain the activity guide in that dialog/TextView using plain text. From your explanation, I think this one is what your are trying to do.
Second one is to use something like slides with pictures to explain about your activity (like Google Sheets application).
Third one is to explain each control in your activity separatly by highlighting them (similar to how Go Launcher explains its feature on first launch)
You can find more info in below links:
How to implement first launch tutorial like Android Lollipop apps: Like Sheets, Slides app?
Android - first launch interactive tutorial
Seems that what you want is actually an introduction. Take a look at this project:
https://github.com/rubengees/introduction
From each introduction page you can launch the correspondent activity.
When I first ran the sample HelloWorld app, it displays the hello world text on the emulator. I decided then to delete that and make a button. What I wanted is that when I click the button, it will show a text "This is the second activity". I made another XML file and another class to handle the second activity to display the text. But when I ran again, I cannot see the changes on the UI for the emulator. The text "This is the second activity" does not show after I clicked the button. I saved everything. How would I automatically update the UI of the emulator after some of the changes made on the design? I am new to android development. Please help me. Btw I cannot post images so it requires 10 reputation that's why I used online image viewing. Sorry for that.
Here is my Graphical layout on eclipse: activity_main.xml
http://s16.postimg.org/wusm4qrp1/image.png
second.xml
http://s29.postimg.org/qft17p5on/image.png
Running the emulator:
http://s28.postimg.org/f9dn32ku5/image.png
After clicking the button (in which case the text I edit does not show):
http://s16.postimg.org/75r62dodx/image.png
Because you didn't post your code, I'll try to explain it from the beginning.
I don't know if you can do it in an other way, but the following answer assumes we aim the good programming practice.
Both of the activities have their distinct layouts set in the following way, right?
public class MyFirstActivity extends Activity{
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
...
public class MySecondActivity extends Activity{
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
...
Then, in your first activity, define onClickListener of your button
...
setContentView(R.layout.activity_first);
Button myButton = (Button)findViewById(R.id.my_button);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MyFirstActivity.this, MySecondActivity.class);
startActivity(intent);
}
});
That being said, I don't recommend you to have two activities for this functionality. You should have different activities when you need different layouts. Putting the button and the textview in the same layout and updating textview inside the button's onclicklistener is a better solution.
I ran the HelloWorld android app and now I moved on to making buttons and stuff like that. I am able to create the buttons in the layout xml and all that, but I ran into some confusion over Eclipse not recognizing my Intent declarations.
Here is a snippet of code:
addProblemButton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
The CurrentActivity and NextActivity classes do not seem to be recognized and Eclipse and it doesn't give me the option to automatically create the import statements for it.
What is the package that these classes are in? Is it an issue of some things not recognized? Or some package that needs to be installed/downloaded? Whats the best practice way to handle such a situation?
Also, do I need to add listeners if I already added the buttons to the layout?
Thanks!
I believe CurrentActivity and NextActivity are just being used as example names for classes for launching an activity in whatever code snippet you were looking at.
CurrentActivity should be the name of whatever the Activity class is that you're launching the new activity from, and NextActivity would be the name of some new Activity class that you want to navigate to next.
It seems you are trying a tutorial. In your project, you should create your own classes extends Activity, named CurrentActivityand NextActivity, so Eclipse will know what they are.
2.If you just declare a button in the layout xml file, the app only show it, but doesn't know how to handle the click event on it, so you still have to register the listener for it. You can:
a. Set the android:onClick attribute for the button in the layout file, and then implement the method to handle the click event. I.e. android:onClick="click" in the xml, and add a function with that name in your code:
public void click(View v){
//Process click event here
}
b. register the listener fully in code:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Process click event here
}
});
I have been trying to figure this thing out with no luck in eclipse. I have created 3 screens. One being the main menu which a button leading to another button which then leads to an Activity. I can get the button from the main menu to lead onto the button onto the second screen but i can't get the second button to lead onto the third screen.
Can anybody help me?
appproject
You should really post some code so we can help you...but it seems like you need to look at your onClick() method for the second button.
Providing you have three separate Activities for each of these three screens (I'll call them ActivityA, ActivityB, and ActivityC), you'll also probably need an XML layout for each. There are ways of doing it without an XML layout, but for now just stick with that.
The next thing is you want to make sure your Button is initialized properly. ActivityB should look like:
public class ActivityB extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Button b = new (Button) findViewById(R.id.button2);
b.setOnClickListener(new OnClickListener() {
#Override
protected void onClick() {
startActivity(new Intent(this, ActivityC.class);
}
});
}
The first thing you should do is check to make sure your button functions in a way similar to how I've described here. If that doesn't change anything, make sure you are initializing your Button in accordance to how it is defined in the layout XML. You must use an id for a Button that is in the same XML layout as the one you set in setContentView(). If not, it will do nothing, no matter what you put in the onClick() method.
I hope that helps!!
In my app I am trying to calculate an operation using timer. For controlling those operations I am using four buttons as Start, Stop, Pause and resume.
But I want to show only 2 buttons. At the beginning I have only two buttons Start and Pause.
When the start button is clicked timer gets started and immediately in Start button's place I want to show the Stop button.
I want to do the same for the other stop and pause buttons. How to do this please help me......
Using ToggleButton is a good solution for you. Do something like:
ToggleButton first = new ToggleButton(getContext());
ToggleButton second = new ToggleButton(getContext());
first.setTextOff("start");
first.setTextOn("stop");
second.setTextOff("pause");
second.setTextOn("resume");
and use setOnCheckedChangeListener() to implement your actions.
In your onClick(View v), v is the button that gets clicked. You can cast it like:
Button b = (Button) v;
so you can change its text with setText(), and set another listener. You can declare the alternate listeners once as members of the activity, and set them without re-declaring them each time.
Your application needs to maintain states, such as "Idle/Stopped", "In Progress", "Paused", etc. If you want to hide buttons, you can use View.setVisibility, and dynamically show and hide the buttons when your state changes (when other buttons are pressed). You would need to set your layout appropriately so that the buttons display nicely as they are shown/hidden dynamically
Or, you can change the text of the buttons, and their associated click listeners dynamically. This method is not very ideal becuase you may run in to cases where you want different amount of buttons for all your different states, and also, you're associating variable behavior with a single control. Also, you must manage your click listeners, adding and removing them dynamically.
here is a simple implementation
public class Demo extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
button.setText("stop");
}
});
}
}
In the main.xml have a Button widget like this,
<Button android:id="#+id/button"
android:text="start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>