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.
Related
I need show a TextView in all Activities, but is much work to do it one by one, because I have +10 Activities.
My objective is when I click in a button, show a textview ("Importing ...") at the bottom of the application. This textview will disappear when I receive a push notification, and I owe a pop up with the response (the pop up also has to appear in any activity).
My project has a custom abstract BaseActivity and all activities extends it.
public abstract class BaseActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
protected void setActionBar(#IdRes int idResToolbar) {
Toolbar toolbar = (Toolbar) findViewById(idResToolbar);
setSupportActionBar(toolbar);
updateFont(toolbar);
}
// ...
}
I think I could use for my purpose but not how to do it.
If anyone has any suggestions I will be happy to hear it.
Thanks in advance.
Use fragments for your content (instead of different activites) you then can add global views to the activity, which holds the fragments.
If you don't want to do that, you'd have to modify the layout(s) in your Base class.
I would suggest you to use a PopupWindow that contains the text view and create a separate class that initializes the PopupWindow on the basis of context given to it.
Now in all your Activities you will have the control of showing and hiding the window as you want. Make sure to make all utility methods required in the separate class to avoid coherence for example hiding and showing the window. setting text of text view of the window and etc.
You can write in onCreate() of your base activity something like
setContentView(R.layout.base_layout);
And in every other Activity at start of onCreate() method, just use super.onCreate()
And more than that to support different layouts add something like this in onCreate() (example for one of activities)
LayoutInflater inflater = getLayoutInflater();
inflater.inflate(R.layout.activity_1_layout,rootGroup)
where rootGroup is a ViewGroup in your Base Activity, in which you will add additional components for every other activity
Create a service, which creates a View which can be drawn over other apps (will require the relevant permission in the manifest)
You could use one of the open source libraries available like this or refer to this example
It's better you use fragments instead of using many activities. However, if you don't wanna do so, I suggest you create a factory which will generate a textview to all activities. Then you must add it into each activity's view.
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 want to know why do people keep recommending starting new Activities when you want to display another screen?
Let's say I want to display a screen with a label and an edit_text to ask for username, then another similar screen to ask for age, then another screen to display the data entered and ask for confirmation.
I did this:
main_layout.xml: has a button let's say mainButton, onClick="startRegistration"
name_layout.xml: edittext asking for name
age_layout.xml: edittext asking for age
confirm_layout.xml: display info + button to confirm
and in:
public class MainActivity extends Activity {
onCreate(...) {
...
setContentView(R.layout.main_layout);
}
public void startRegistration(View clickedButton) {
setContentView(R.layout.name_layout);
}
..
}
... and so on, all button handlers are public void methods in main class and each method contains setContentView() with the next layout as parameter.
I have a feeling this is bad programming style, however it works perfectly fine. Is it ok to do this? If not, is there any other easy way? Starting a new activity for such things feels really stupid to me.
Normally you group 'activities' together in an Activity. For you, registration uses multiple screens but are linked to each other. I would suggest using 1 Activity with a ViewFlipper.
Having 1 Activity for all will screw up the navigation for the user. The Back key has to be handled specially. "if back key, set this content, else set this content, etc"
If you code different layouts for the same type of screen then its not really an ideal idea. The better idea is to have the same layout and point to the same layout from the classes where the layouts are the same. In the screen where you want to have an extra/less control or different control then just have unique IDs to such controls.
Refer the controls from their IDs and you will have a single layout file. Writing different layout classes where the controls are same will pave way to code repetition and hence is not an ideal way of coding.
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!!
I am trying to learn how to do stuff in Android, and I'm not sure of the best way to build the interface.
I've been working on porting an iPhone app, which uses navigation controllers and table views for looking at the different sections: basically, someone touches a cell in the table, which drills down to another table. when they touch a cell on that table it drills down to a webview that displays the information.
I want to do something similar for the android app, but I don't know how, or if there is a better way native to Android. I've figured out how to use the webview to my purposes, but moving forward and backward in the table tree is unclear.
So on an iphone when you say drill down I guess you mean when a user touches-up on a list row and it slides a new view on from the right, most of the time it has a nav bar at the top to give the user the option to go back?
The way android handles this is simply by starting a new activity. So you would have your 'Books' ListActivity when a listItem is clicked you would define a new intent that starts your 'Chapters' ListActivity and so on. The nav bar at the top of an iphone is not standard UI in android as most people see the dedicated 'back' key as a way of getting back to the previews screen.
This is how you start an intent in case you haven't seen it before:
Intent chaptersIntent = new Intent(this, Chapters.class);
this.startActivity(chaptersIntent);
This article is worth a quick read through as it explains Activities perfectly
http://d.android.com/guide/topics/fundamentals.html
Also have a look at the android version of TableView - ListView:
http://d.android.com/reference/android/widget/ListView.html
and ListActivity:
http://d.android.com/reference/android/app/ListActivity.html
EDIT:: Sample Code
I would do it something like this
public class Books extends ListActivity {
private String[] mBooks = new String[]{ "Book1", "Book2", "Book3", "Book4" };
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> booksAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
mBooks);
this.setListAdapter(booksAdapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent mViewChaptersIntent = new Intent(this, Chapters.class);
mViewChaptersIntent.putExtra("BookName", mBooks[position]);
startActivity(mViewChaptersIntent);
}
}
So you pass through the id of the book as an extra to the Intent then in your Chapters Activity you get that extra in the onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
if(extras != null) {
String bookId = extras.getString("BookName");
}
}
Finally make sure all new activities are added to your AndroidManifest.xml file:
<activity android:name=".YourClassName"
android:label="#string/activity_name"
>
</activity>
Hope that helps
The primary way that the Android ui is created is using xml. I'm not exactly sure what you mean when you say drill down but if you want it to change views its as simple as making one set of xml elements visible and another set not. Check out the developer pages for more help.
http://developer.android.com/guide/topics/ui/index.html
Forgot to mention this is also a very good beginner resource.
http://mobiforge.com/designing/story/understanding-user-interface-android-part-1-layouts
As already pointed out XML way of doing layouts is most preferred.
basically, someone touches a cell in
the table, which drills down to
another table. when they touch a cell
on that table it drills down to a
webview that displays the information.
From what I understood from the term drills down, this may be wat you need
http://developer.android.com/guide/topics/ui/ui-events.html
From official docs
An event listener is an interface in
the View class that contains a single
callback method. These methods will be
called by the Android framework when
the View to which the listener has
been registered is triggered by user
interaction with the item in the UI.