I'm making my first official application to put on the market and i was wondering would over 120 new activities be too many if I'm only using them to pull text? If so is there a way I could turn MysecondActivity to a giant if or switch statement?
This is the example I found in a book I changed up case 0: I'm going to change up the rest be similar to case 0 once i get an answer.
Thank you in advance and sorry for messy code and writing
AndroidManifest.xml
<activity android:name=".MySecondActivity" />
ListActivityExample.java
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class ListActivityExample extends ListActivity{
static final String[] ACTIVITY_CHOICES = new String[] {
"Open new Actvity",
"Open Contacts",
"Open Phone Dialer Example",
"Search Google Example",
"Start Voice Command"
};
final String searchTerms = "superman";
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, ACTIVITY_CHOICES));
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setTextFilterEnabled(true);
getListView().setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3){
switch(arg2) {
case 0: //opens new screen
{Intent intent = new Intent(ListActivityExample.this,MySecondActivity.class);
startActivity(intent);
break;}
case 1: //opens phone dialer and fills in the given number
{
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("content://contacts/people/")));
break;}
case 2:
{
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("tel:12125551212")));
break;}
case 3: //
{
Intent intent1= new Intent(Intent.ACTION_WEB_SEARCH);
intent1.putExtra(SearchManager.QUERY, searchTerms);
startActivity(intent1);
break;}
case 4: //
{startActivity(new
Intent(Intent.ACTION_VOICE_COMMAND));
break;}
default: break;
}
}
});
}
}
MySecondActivity.java
mport android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MySecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("text here");
setContentView(tv);
}
}
There's nothing I can see about this code that will cause an issue with a large Activity stack. Start as many Activities as you like, as long as they aren't all stacked together at the same time. With this code you are starting a new Activity as a result of tapping an item in the list, but the user must finish the new Activity (press back) when returning to your list to start something else, so they aren't all in memory at once.
Does that help? Or did I misunderstand your question?
Related
Please Give Me some Solution to add back button in quiz app
What I want to create is back button in my quiz app, the questions come from database. I want button to go to previous question without messing up.I am new to android can any one help me to solve the problem..
package in.gtarena.myquizapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import in.gtarena.myquizapp.db.DBAdapter;
import in.gtarena.myquizapp.model.Question;
public class ComputerActivity extends AppCompatActivity {
private List<Question> questionsList;
private Question currentQuestion;
private TextView txtQuestion,tvNoOfQs;
private RadioButton rbtnA, rbtnB, rbtnC,rbtnD;
private Button btnNext,btnBack;
private RadioGroup grp;
private int obtainedScore=0;
private int questionId=0;
private int answeredQsNo=0;
ArrayList<String> myAnsList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_concept);
init();
btnBack = (Button) findViewById(R.id.btnBack);
grp=(RadioGroup)findViewById(R.id.radioGroup1);
//Initialize the database
final DBAdapter dbAdapter =new DBAdapter(this);
questionsList= dbAdapter.getQuestions();
currentQuestion =questionsList.get(questionId);
//Set question
setQuestionsView();
//Check and Next
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.e("Answer ID", "Selected Positioned value - "+grp.getCheckedRadioButtonId());
if(answer!=null){
Log.e("Answer", currentQuestion.getANSWER() + " -- " + answer.getText());
//Add answer to the list
myAnsList.add(""+answer.getText());
if(currentQuestion.getANSWER().equals(answer.getText())){
obtainedScore++;
Log.e("comments", "Correct Answer");
Log.d("score", "Obtained score " + obtainedScore);
}else{
Log.e("comments", "Wrong Answer");
}
if(questionId < dbAdapter.rowCount()){
currentQuestion =questionsList.get(questionId);
setQuestionsView();
}else{
Intent intent = new Intent(ComputerActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", obtainedScore);
b.putInt("totalQs", questionsList.size());
b.putStringArrayList("myAnsList", myAnsList);
intent.putExtras(b);
startActivity(intent);
finish();
}
}else{
Log.e("comments", "No Answer");
}
//Need to clear the checked item id
grp.clearCheck();
}//end onClick Method
});
//Check and Back
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//WHAT SHOULD I DO HERE FOR GO BACK TO PREVIOUS QUESTION//
}
});
}
public void init(){
tvNoOfQs=(TextView)findViewById(R.id.tvNumberOfQuestions);
txtQuestion=(TextView)findViewById(R.id.tvQuestion);
rbtnA=(RadioButton)findViewById(R.id.radio0);
rbtnB=(RadioButton)findViewById(R.id.radio1);
rbtnC=(RadioButton)findViewById(R.id.radio2);
rbtnD=(RadioButton)findViewById(R.id.radio3);
btnNext=(Button)findViewById(R.id.btnNext);
myAnsList = new ArrayList<String>();
}
private void setQuestionsView()
{
rbtnA.setChecked(false);
rbtnB.setChecked(false);
rbtnC.setChecked(false);
rbtnD.setChecked(false);
answeredQsNo=questionId+1;
tvNoOfQs.setText("Questions "+answeredQsNo+" of "+questionsList.size());
txtQuestion.setText(currentQuestion.getQUESTION());
rbtnA.setText(currentQuestion.getOptionA());
rbtnB.setText(currentQuestion.getOptionB());
rbtnC.setText(currentQuestion.getOptionC());
rbtnD.setText(currentQuestion.getOptionD());
questionId++;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Are you using separate activity for every question?
If yes, then you can use onBackPressed method.
It would be easy for you to go back to question if you provide a id(ex.1,2,3..) for every question in database and take a variable named id and increment id every time you click the next button and fetch the question matching to the id and when you press back button the decrement id and fetch the question again...
Just decrement questionid in back button listener and call the questions again
You can do this by two approaches :
Create a button and than link that button to the previous activity
Create a method onBackPressed like this code :
#Override
public void onBackPressed() {
Intent intent = new Intent(OrderList.this, MainActivity.class);
startActivity(intent);
}
You can simply use the second code in any activity to lead you back to previous activity.
Hope this helps !!
I have two button which starts new intents, and they are working fine.
In the same activity i am now trying to make a new button that opens a URL link, but i cant make it work.
The two buttons starting new intents, are btn_Calender and btn_Info. So the new button which should open URL is btn_button4.
Can someone plese see my code and tell me what i am doing wrong.
Thank you all.
package com.xxxxxx;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.chrfugl.stubhuset.R;
public class HomeActivity extends Activity implements OnClickListener{
private Button btn_Calender, btn_Info, btn_button4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
btn_Calender = (Button)findViewById(R.id.btn_Calender);
btn_Info = (Button)findViewById(R.id.btn_Info);
btn_button4 = (Button)findViewById(R.id.button4);
btn_Calender.setOnClickListener(this);
btn_Info.setOnClickListener(this);
btn_button4.setOnClickListener(this);
}
public void onClick(View view) {
Intent intent;
switch (view.getId()) {
case R.id.btn_Calender:
intent = new Intent(this,MainActivity.class);
startActivity(intent);
break;
case R.id.btn_Info:
intent = new Intent(this,InfoActivity.class);
startActivity(intent);
break;
case R.id.button4:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
break;
}
}
}
Your switch statement in your onClick is wrong..
The ID of your button is R.id.button4:
btn_button4 = (Button)findViewById(R.id.button4);
In your onClick you are checking for the ID:
case R.id.btn_button4:
Which means you never reach your call.. The Intent itself is correct.
EDIT
Replace case R.id.btn_button4: with case R.id.button4: in your onClick-method
I have a ListView with one TextView. List view is populated from a string array and array adapter(in the List class extending ListActivity & list_item layout). Now a click listener is set and using switch position statement and local html files from assets folder are linked to the list rows by uri parsing method. When the list row is clicked the html page is set to be displayed in a Webview set up with (TopicDisplay class & display_item layout). Below is my
PROBLEM: When the user clicks a row on the list view - html documents from that row to the last row are displayed with Last document first. I can use the android device back button to navigate through each html document loaded until the clicked row. HOW TO DISPLAY ONLY THE CLICKED ROW HTML DOCUMENT?
Code in List Class
package com.abcdef.list;
//import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
//import android.widget.AdapterView;
//import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
//import android.widget.TextView;
public class List extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, adobe_products));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
change(position);
}
void change(int position){
// Uri uri = null;
switch(position){
case 0 :{
Intent i0 = new Intent(getApplicationContext(), TopicDisplay.class);
Uri uri0=Uri.parse("file:///android_asset/File0.html");
i0.setData(uri0);
startActivity(i0);}
case 1 :{
Intent i1 = new Intent(getApplicationContext(), TopicDisplay.class);
Uri uri1=Uri.parse("file:///android_asset/Topic1.html");
i1.setData(uri1);
startActivity(i1);}
case 2 :{
Intent i2 = new Intent(getApplicationContext(), TopicDisplay.class);
Uri uri2=Uri.parse("file:///android_asset/Topic2.html");
i2.setData(uri2);
startActivity(i2);}
case 3:{
Intent i3 = new Intent(getApplicationContext(), TopicDisplay.class);
Uri uri3=Uri.parse("file:///android_asset/Topic3.html");
i3.setData(uri3);
startActivity(i3);}
case 4:{
Intent i4 = new Intent(getApplicationContext(), TopicDisplay.class);
Uri uri4=Uri.parse("file:///android_asset/Topic4.html");
i4.setData(uri4);
startActivity(i4);}
case 5:{
Intent i5 = new Intent(getApplicationContext(), TopicDisplay.class);
Uri uri5=Uri.parse("file:///android_asset/Topic5.html");
i5.setData(uri5);
startActivity(i5);}
case 6:{
Intent i6 = new Intent(getApplicationContext(), TopicDisplay.class);
Uri uri6=Uri.parse("file:///android_asset/Topic6.html");
i6.setData(uri6);
startActivity(i6);}
case 7:{
Intent i7 = new Intent(getApplicationContext(), TopicDisplay.class);
Uri uri7=Uri.parse("file:///android_asset/Topic7.html");
i7.setData(uri7);
startActivity(i7);}
case 8:{
Intent i8 = new Intent(getApplicationContext(), TopicDisplay.class);
Uri uri8=Uri.parse("file:///android_asset/Topic8.html");
i8.setData(uri8);
startActivity(i8);}
} } }
Here is my TopicDisplay class code
package com.abcdef.list;
import android.app.Activity;
//import android.app.Application;
//import android.content.Intent;
import android.os.Bundle;
import android.webkit.*;
public class TopicDisplay extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.topic_display);
WebView tabViewing = (WebView) findViewById(R.id.webView1);
tabViewing.loadUrl(getIntent().getDataString());
}
}
When the user clicks a row on the list view - html documents from that row to the last row are displayed with Last document first.
You simply forgot to add break; to each case in your switch statement.
You should also remove the common code from each case statement, try:
Intent intent = new Intent(getApplicationContext(), TopicDisplay.class);
switch(position){
case 0 :
intent.setData(Uri.parse("file:///android_asset/File0.html"));
break;
case 1 :
intent.setData(Uri.parse("file:///android_asset/Topic1.html"));
break;
case 2 :
intent.setData(Uri.parse("file:///android_asset/Topic2.html"));
break;
// etc
}
startActivity(intent);
how do i embed a tab layout within a button. My main layout is a linear layout, but i don't know how to program the main activity.java class. Could anybody help me get started this is what my main.java code looks like right now
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
public class Remote_DocActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
//private static final String TAG = "Remote_Doc";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View Patient_Button = findViewById(R.id.patientButton);
Patient_Button.setOnClickListener(this);
View Doctor_Button = findViewById(R.id.doctorButton);
Doctor_Button.setOnClickListener(this);
View About_Option = findViewById(R.id.aboutButton);
About_Option.setOnClickListener(this);
View Exit_Option = findViewById(R.id.exit);
Exit_Option.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.aboutButton:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
}
}
you can create tabs with the help of TabActivity class. You can take help from android-wireless-application-development book's unit 3 chapter 8 example.
hey all.. i have been looking at other questions to get help and answers without luck..
my problem is that i want to open different class from my listView. according to the specific name in the listView. . the names are in lv_arr[] ..
If anybody can give a detailed answer i will be very thankfull and happy..
Im new in android and not the best in java :-(
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class StatusActivity extends Activity implements OnItemClickListener {
public ListView lv1;
public String lv_arr[]= {"John", "Andrew","alex","alice","bob","bla bla"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabview);
lv1=(ListView)findViewById(R.id.ListView01);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
lv1.setOnItemClickListener(this);
}
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getApplication())
.inflate(R.menu.menu, menu);
return(super.onPrepareOptionsMenu(menu));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.close:
super.finish();
break;
case R.id.icontext:
Intent i = new Intent(this, InfoActivity.class);
startActivity(i);
break;
}
return true;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
if(position == 0){
//Intent w = new Intent (this, Seekbar.class);
//startActivity(w);
Toast.makeText(this, "You pressed the first item in the list", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "You pressed all other items in the list", Toast.LENGTH_SHORT).show();
}
}
}
try this....if you have doubts add comment.
edited:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabview);
lv1=(ListView)findViewById(R.id.ListView01);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if(position==0){
Intent i = new Intent(this, InfoActivity.class);
startActivity(i);
} else if(position==1){
start another activity here...
}
}
});
}
hope it helps..
well i think you want to know basically the flow of how it is possible.
If all the classes which should be opened are more likely the same with just text and image changes and same layout then you dont need to create separate class file for each and every item clicked.
Just create one class file and one xml file and on item click pass on the bundle to the class file which will just changes the content on different item clicks and will workk as a charm as with this flow you will have only one class file and one xml file for all the list items you have
If you dont want to keep the same layout then you can obviously make the some views gone, visible or invisible at runtime
try something like this
public String lv_class_names[]= {Activity1.class.getName(), Activity2.class.getName(), Activity3.class.getName(), .....};
In the onItemClick method write
Intent i = new Intent(this, Class.forName(lv_class_name[position]));
startActivity(i);
for more details see this