It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
First- sorry if my question a bit dumb, I'm totally new to Android development ...
I'm trying to start a new activity from my "LAUNCHER" activity and run it on the emulator, but every time I click on the "Next" button (which should start my second activity), I get an error msg saying: "unfortunately, Omis hang man free has stopped", and then closes the app...
also, when I re-launch the same app in the emulator without re-installing it from Eclipse, it doesn't even showing me the first screen...
I don't even know where my problem lies, so I attached 5 codes:
1'st activity layout (named "activity_open")
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".OpenActivity"
android:background="#DCDCDC" >
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_weight="30" >
<TextView
android:id="#+id/tvEnterName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="#string/stEnterName"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_weight="40" >
<EditText
android:id="#+id/etNameInput"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="#string/stHintNameEnter"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_weight="30" >
<Button
android:id="#+id/bNext"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/stNext"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
1'st activity class (named "OpenActivity")
package com.omi.hangmanfree;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class OpenActivity extends Activity {
TextView tvEnterYourName;
EditText etName;
Button bContinue;
String myName, tmp;
final String errorStr = "\nYou must enter a name!";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open);
initialize();
bContinue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
myName = etName.getText().toString();
if(etName.getText().toString().equals("") == true)
{
tvEnterYourName.setText(tmp + errorStr);
tvEnterYourName.setTextColor(Color.RED);
}
else
{
try {
Intent gameIntent = new Intent("com.omi.hangmanfree.GAMEACTIVITY");
startActivity(gameIntent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
private void initialize() {
// TODO Auto-generated method stub
tvEnterYourName = (TextView)findViewById(R.id.tvEnterName);
etName = (EditText)findViewById(R.id.etNameInput);
bContinue = (Button)findViewById(R.id.bNext);
myName = "~";
tmp = tvEnterYourName.getText().toString();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_open, menu);
return true;
}
}
2'nd activity class (named "GameActivity")
package com.omi.hangmanfree;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.GridView;
import android.widget.TextView;
public class GameActivity extends Activity {
TextView tt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
GridView gvGrid = (GridView)findViewById(R.id.gvHagdara);
tt.setText("_");
gvGrid.addView(tt);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_game, menu);
return true;
}
}
2'nd activity layout (named "layout_game")
<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GameActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<GridView
android:id="#+id/gvHagdara"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/textView1"
android:layout_centerInParent="false"
android:numColumns="auto_fit" >
</GridView>
</RelativeLayout>
5. manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.omi.hangmanfree"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".OpenActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GameActivity"
android:label="#string/title_activity_game" >
<intent-filter>
<action android:name="com.omi.hangmanfree.GAMEACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
I'm not looking for code improvements (I am only learning the language), so please do not send me suggestions to improve the code (I know there are a lot!) --- I just want this code to work.
The problem may be in the Intent. Try this:
Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class);
Please Remove finish(); from onPause() Method from OpenActivity and remove gvGrid.addView(tt); from GameActivity because gridview is not container layout and write below code
Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class);
startActivity(gameIntent);
instead of
Intent gameIntent = new Intent("com.omi.hangmanfree.GAMEACTIVITY");
startActivity(gameIntent);
it will solve your problem.
please replace in openActivity
Intent gameIntent = new Intent(getApplicationContext(), GameActivity.class);
startActivity(gameIntent);
Try changing here:
Intent gameIntent = new Intent("com.omi.hangmanfree.GAMEACTIVITY");
startActivity(gameIntent);
to
Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class);
startActivity(gameIntent);
Related
Error(s):
10-17 18:53:39.298: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.quizme/com.quizme.QuizMeActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.TextView
10-17 18:53:38.747: error opening trace file: No such file or directory (2)
Upon trying to run it I get the unfortunatly, the app does not work error as well.
Java file:
package com.quizme;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class QuizMeActivity extends Activity {
private TextView question;
private TextView result;
private ImageButton forward;
private ImageButton backward;
private Button bTrue;
private Button bFalse;
private TrueFalse mTrueFalse;
/*
* onCreate(Bundle savedInstanceState)
* Instantiates all private GUI elements to their corresponding Views
* in activity_quiz_me.xml
* Sets the string values of the questions in the TrueFalse object,
* and sets their truth values.
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_me);
Resources res = this.getResources(); //this is to be able to get strings
bTrue=(Button)findViewById(R.id.true_button);
forward=(ImageButton)findViewById(R.id.forward_button);
bFalse=(Button)findViewById(R.id.false_button);
backward=(ImageButton)findViewById(R.id.back_button);
result=(TextView)findViewById(R.id.result);
result.setText("");
question=(TextView)findViewById(R.id.question);
question.setText(mTrueFalse.getmQuestionSet()[0]);
mTrueFalse.setmQuestionSet(0, (String) res.getText(R.string.q1));
mTrueFalse.setmQuestionSet(1, (String) res.getText(R.string.q2));
mTrueFalse.setmQuestionSet(2, (String) res.getText(R.string.q3));
mTrueFalse.setmQuestionSet(3, (String) res.getText(R.string.q4));
mTrueFalse.setmQuestionSet(4, (String) res.getText(R.string.q5));
mTrueFalse.setmArrOfBols(0, false);
mTrueFalse.setmArrOfBols(1, true);
mTrueFalse.setmArrOfBols(2, true);
mTrueFalse.setmArrOfBols(3, false);
mTrueFalse.setmArrOfBols(4, true);
}
public void checkTrue(View view) {
Resources res = this.getResources(); //this is to be able to get strings
if(mTrueFalse.getCurrentBolValue()==true)
{
result.setText((String) res.getText(R.string.right));
}
else
{
result.setText((String) res.getText(R.string.wrong));
}
}
public void checkFalse(View view) {
Resources res = this.getResources(); //this is to be able to get strings
if(mTrueFalse.getCurrentBolValue()==true)
{
result.setText((String) res.getText(R.string.wrong));
}
else
{
result.setText((String) res.getText(R.string.right));
}
}
public void goForward(View view) {
if(mTrueFalse.getmCurrentQ()<mTrueFalse.getmQuestionSet().length-1)
{
mTrueFalse.setmCurrentQ(mTrueFalse.getmCurrentQ()+1);
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
else if(mTrueFalse.getmCurrentQ()==mTrueFalse.getmQuestionSet().length-1)
{
mTrueFalse.setmCurrentQ(0);
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
}
public void goBack(View view) {
if(mTrueFalse.getmCurrentQ()>0)
{
mTrueFalse.setmCurrentQ(mTrueFalse.getmCurrentQ()-1);
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
else if(mTrueFalse.getmCurrentQ()==0)
{
mTrueFalse.setmCurrentQ(mTrueFalse.getmQuestionSet().length-1); //for the purposes of the project, its setting currentQ to 4
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz_me, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
layout xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/activity_view"
tools:context="com.quizme.QuizMeActivity">
<Button
android:id="#+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sTrue"
android:layout_marginRight="180dp"
android:onClick="checkTrue"
/>
<ImageButton
android:id="#+id/forward_button"
android:contentDescription="#string/forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/back_button"
android:onClick="goForward"
android:src="#drawable/forward" />
<Button
android:id="#+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/question"
android:layout_toRightOf="#+id/true_button"
android:onClick="checkFalse"
android:text="#string/sFalse" />
<ImageButton
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="70dp"
android:contentDescription="#string/back"
android:onClick="goBack"
android:src="#drawable/back" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/resultStr"
android:textSize="30sp" />
<TextView
android:id="#+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/true_button"
android:layout_marginTop="34dp"
android:layout_toLeftOf="#+id/forward_button"
android:text="#string/questionStr"
android:textSize="25sp" />
</RelativeLayout>
Android Manifest:
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".QuizMeActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
sorry for the pile of code, I am just frustrated and stuck
Try cleaning the project
Eclipse: Project->Clean
When I ran the app the first activity worked but as soon as it went to the second activity it said treasure hunt has stopped working, and there was nothing under logcat or console. I am pretty sure it is a problem with level_1.java but can't figure out what. Please help thanks.
Main Activity:
package com.example.treasurehunt;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void Start(View view) {
Log.d("hi", "hello");
Intent intent = new Intent(this, Level_1.class);
startActivity(intent);
finish();
}
}
Mainxml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Treasure Hunt" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="82dp"
android:text="Start"
android:onClick="Start" />
</RelativeLayout>
`
level_1java:
package com.example.treasurehunt;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
public class Level_1 extends Activity {
private SharedPreferences settings;
private SharedPreferences.Editor editor;
TextView Clue = (TextView)findViewById(R.id.Clue);
EditText edittext = (EditText)findViewById(R.id.editText);
String hint;
String answer;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("hi","hello");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_1);
int level = settings.getInt("level", 1);
switch (level) {
case 1: level1();
break;
default:
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.level_1, menu);
return true;
}
public void level1() {
editor.putInt("level", 1); // only needs to be done for level 1
editor.commit();
Clue.setText("Bartholdi was my creator, I carry fire and knowledge, what am I?");
hint = "July IV MDCCLXXVI";
answer = "statue of liberty";
}
public void answer() {
String useranswer = edittext.toString();
if (useranswer.equalsIgnoreCase(answer)) {// if they get the correct answer
int leveltest = settings.getInt("level", 1);
editor.putInt("level", leveltest+1); //adds one level to the current level
editor.commit();
}
}
public void hint() { //function that displays the hint
}
}
level_1 xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".Level_1" >
<TextView
android:id="#+id/Clue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="116dp"
android:text="Clue" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Clue"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/editText"
android:layout_marginBottom="50dp"
android:text="Submit"
android:onClick="answer"/>
<Button
android:id="#+id/Hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Submit"
android:layout_alignBottom="#+id/Submit"
android:layout_alignLeft="#+id/editText"
android:text="Hint"
android:onClick="Hint"/>
</RelativeLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.treasurehunt"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.treasurehunt.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.treasurehunt.Level_1"
android:label="#string/title_activity_level_1" >
</activity>
</application>
</manifest>
Seems you're having NullPointerException.
On the field declaration
TextView Clue = (TextView)findViewById(R.id.Clue);
EditText edittext = (EditText)findViewById(R.id.editText);
Move the assignments to in onCreate()
public class Level_1 extends Activity
{
// Other members ....
TextView Clue = (TextView)findViewById(R.id.Clue);
EditText edittext = (EditText)findViewById(R.id.editText);
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("hi","hello");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_1);
// Assignemnts should be here
Clue = (TextView)findViewById(R.id.Clue);
edittext = (EditText)findViewById(R.id.editText);
int level = settings.getInt("level", 1);
switch (level) {
case 1: level1();
break;
default:
break;
}
}
}
I am new to the Android development world and I've built a simple "Hello World" app. First, activity requests a text. When the "Go" button is clicked, the app launches the second activity displaying the input text.
If I click the HOME button and then click the application icon, the app launches the first activity again but if I press-hold the home button and click the icon from the "Recent apps" bar, it resumes the app where I left.
How do I avoid this?
I need my app to resume even if the launcher icon is clicked.
MainActivity.java,
package com.example.myfirstandroidapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view){
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.txtName);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
DisplayActivity.java,
package com.example.myfirstandroidapp;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
#Override
public void onDestroy(){
super.onDestroy();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity" >
<EditText
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtName"
android:layout_alignParentRight="true"
android:onClick="sendMessage"
android:text="Go!" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtName"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:text="Please input your name:"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
activity_display_message.xml,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".DisplayMessageActivity" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
AndroidManifest.xml,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstandroidapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myfirstandroidapp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstandroidapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstandroidapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstandroidapp.MainActivity" />
</activity>
</application>
</manifest>
Problem:
I'm not qualified to say this a bug, but there is a behaviour with release builds when starting the application from the launcher. It seems that instead of resuming the previous Activity, it adds a new Activity on top. There is a related bug report on this topic here.
Solution:
I'm working around this this by closing the Launcher Activity if it's not the root of the task, as a result the previous Activity in that task will be resumed.
if (!isTaskRoot()) {
finish();
return;
}
Issue for me was whenever app in installed by an apk with the click on 'Open' option it used to relaunch every time when we minimize it.
resolved it by
SplashActivity.java:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (!isTaskRoot
&& intent.hasCategory(Intent.CATEGORY_LAUNCHER)
&& intent.action != null
&& intent.action.equals(Intent.ACTION_MAIN)) {
finish()
return
}
}
In general, everything is working on Google Chrome. When the new tab button is clicked, a new tab is generated. In the same way, I want to add a new tab in the Android browser. How to do this — does anyone have any idea?
First, is it possible in Android?
If possible, how to do this?
When I click on the + button, a new tab should be generated. How to do this?
Ok Here is the code but it's an example only you may need to modify the code as per your requirement. I am giving you all the files code here hope you getting answer.
Your Manifest.xml file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.dynamictab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BlankActivity"/>
</application>
</manifest>
here is your activity_main.xml file for Tab activity
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"
android:layout_weight="1">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dip"
android:layout_marginRight="0dip" />
</HorizontalScrollView>
<Button android:id="#+id/add_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="Add"/>
</LinearLayout>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="2dp" />
</LinearLayout>
</TabHost>
Put your TabWidget into the HorizontalScrollView so when more tabs are add it can scrolling and the Add but is out side of HorizontalScrollView. Every time you click on tab it will add new tab in TabWidget.
Here the code for tab_event.xml This layout will inflate and add into tab. You can change the style and its contain a single Button so you can add drawable image with text also.
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button_event"
android:clickable="true"
android:focusable="true"
android:text="Default Tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Here is your MainActivity.java file
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
private static int tabIndex = 0;
private TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
addTab();
((Button)findViewById(R.id.add_tab)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tabIndex++;
addTab();
}
});
}
private void addTab(){
LayoutInflater layoutInflate = LayoutInflater.from(MainActivity.this);
Button tabBtn = (Button)layoutInflate.inflate(R.layout.tab_event, null);
tabBtn.setText("Tab "+tabIndex);
Intent tabIntent = new Intent(MainActivity.this, BlankActivity.class);
setupTab(tabBtn, tabIntent,"Tab "+tabIndex);
}
protected void setupTab(View tabBtn, Intent setClass,String tag) {
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabBtn).setContent(setClass);
tabHost.addTab(setContent);
}
}
And here is the BlankActivity.java file
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class BlankActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(BlankActivity.this);
tv.setText("Blank activity");
setContentView(tv);
}
}
For button click create a new tabspec.
TabSpeec spec = th.newTabSpec("tag");
spec.setContent(new TabHost.TabContentFactory(){
//What ever thing you want to display inside the tab
TextView text = new TextView(CONTEXT);
text.setText("New tab");
return(text);
}
});
spec.setIndicator("New");
th.addTab(spec);
Use the following youtube tutorial as reference. Its simple.
http://www.youtube.com/watch?v=NcKSFlYEqYY
Create an Activity as below:
public class WebViewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView wv=new WebView(this);
setContentView(wv);
}
}
Now use #Partik's method to add a new tab, on each + button click:
private void addTab(){
Button tabBtn = new Button(MainActivity.this);
tabBtn.setText("Tab "+tabIndex);
Intent tabIntent = new Intent(MainActivity.this, WebViewActivity.class);
setupTab(tabBtn, tabIntent,"Tab "+tabIndex);
}
I am trying to make a tab bar application in android, after searching in google i found some tutorial and i succeeded. I want 3 tabs, when i click on 1st tab some data will be taken from server using asyntask and show the progressdialog in frontend, but the progressdialog is capturing the whole screen(with tabbar) and because of that i can't be able to switch the 2nd or 3rd tab.
Expected Output: Whenever i click on any tab a data will be taken from server in background, as i know data is loaded in background so for that time i want to switch 2nd tab or any other tab.
package com.tabexample;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
public class ArrowsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.arrowspage);
Button back = (Button) findViewById(R.id.BackButton2);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ProgressDialog p = new ProgressDialog(getParent());
p.setMessage("Loading");
p.setCancelable(false);
p.show();
p.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
}
});
}
}
package com.tabexample;
import android.app.ListActivity;
import android.content.Intent;
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 EditActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mListContent));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
startActivity(new Intent(EditActivity.this, OptionsActivity.class));
}
});
}
private static String[] mListContent={"Item 1", "Item 2", "Item 3"};
}
package com.tabexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class OptionsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.optionspage);
Button back = (Button) findViewById(R.id.BackButton1);
Button next = (Button) findViewById(R.id.NextButton1);
}
}
package com.tabexample;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class TabExmapleActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablayout);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("OPT")
.setContent(new Intent(this, ArrowsActivity.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("EDIT")
.setContent(new Intent(this, EditActivity.class)));
tabHost.setCurrentTab(0);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout02" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="#+id/TextView02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Arrows Page">
</TextView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/BackButton2"
android:text="Back" android:layout_below="#+id/TextView02">
</Button>
<Button android:id="#+id/Button01" android:layout_below="#id/BackButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Indeterminate Dialog"></Button>
<Button android:id="#+id/Button02" android:layout_below="#id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Select"></Button>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Options Page">
</TextView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/BackButton1"
android:text="Back" android:layout_below="#+id/TextView01">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/NextButton1"
android:text="Next" android:layout_toRightOf="#+id/BackButton1"
android:layout_below="#+id/TextView01"></Button>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</TabWidget>
</RelativeLayout>
</TabHost>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tabexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TabExmapleActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ArrowsActivity"></activity>
<activity android:name="EditActivity"></activity>
<activity android:name="OptionsActivity"></activity>
</application>
</manifest>
Just Put Progressbar for the child Activitys you are adding in TabHost .
First it will show progress bar of first activity in onCreate which you set as current activity and if you click on next tab it will show its progress dialog
if i get you correctly, you need to access the below activity components when the progress dialog is active.
for this you need to make non-modal/modeless progress dialog. non-modal dialogs will allow you to accept events by behind ui components
please refer the post below
timed modeless dialog