Blank Activity ( Android ) waiting for debugger - android

I've got the "waiting for debugger to attach" message and a blank activity that nothing happen for whatever time im waiting!I'm trying to do a simple main activity that shows a question and "+" is the true and "-" is the false. Like true-false game.
here is my manigest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.calculator"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.SET_DEBUG_APP"></uses-permission>
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:debuggable="true"
>
<activity
android:name="com.example.calculator.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>
</application>
here is my java :
package com.example.calculator;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements android.view.View.OnClickListener {
Button button;
Button button2;
Button button3;
TextView text;
String[] questions ;
boolean[] answers;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
text = (TextView) findViewById(R.id.textView1) ;
questions[0]="Are u hungry?";
answers[0]=true;
text.setText(questions[0]);
button.setOnClickListener(this);
button2.setOnClickListener(this);
}
public void onClick(View v) {
//parsing should be here
switch(v.getId()){
case R.id.button1:
if( answers[0]==true){
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Your result:");
alertDialog.setMessage("True !Congratulations");
alertDialog.show();}
else{ AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Your result:");
alertDialog.setMessage("false !You are idiot!");
alertDialog.show();}
break;
case R.id.button2:
if( answers[0]==false){
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Your result:");
alertDialog.setMessage("Correct !Congratulations");
alertDialog.show();}
else{ AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Your result:");
alertDialog.setMessage("Incorrect !You are idiot!");
alertDialog.show();}
break;
}
}
}
here is my 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"
android:background="#99FFFF">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:text="#string/num1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="#FFFFFF"/>
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/button1"
android:text="#string/minus"
android:background="#FFFFFF"/>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button2"
android:background="#FFFFFF"
android:text="#string/add" />
</RelativeLayout>

You're declaring your variables as arrays
String[] questions ;
boolean[] answers;
and then you're using them without initializing them, so java doesnt't know the size of the arrays.
questions[0]="Are u hungry?";
answers[0]=true;
One possible solution is defining the size of the array:
String[] questions = new String[10]; // define the size of the array
Another solution would be using an ArrayList.

Related

Treasure hunt has stopped working

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;
}
}
}

Changing an Activity in Android

I'm just learning android here, and Java to be honest. I'm just trying to switch to a different activity via a button click, however, it keeps crashing. It crashes when I click the button and go to make the switch. Can someone please help me figure where I'm going wrong?
First Activity:
package com.example.killacatoe;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import android.content.*;
public class TicTacToe extends Activity {//Start TicTactToe Class
//CONSTANTS
//Variables
Button mainButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tic_tac_toe);
mainButton = (Button) findViewById(R.id.bPlayNow);
mainButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), playerMenu.class);
startActivity(i);
}
});
}
}//End TicTacToe Class
Activity I'm jumping to:
package com.example.killacatoe;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
public class playerMenu extends Activity {
Button bOnePlayer, bTwoPlayer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
bOnePlayer = (Button) findViewById(R.id.bOnePlayer);
bOnePlayer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
bTwoPlayer = (Button) findViewById(R.id.bTwoPlayer);
bTwoPlayer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
Here is the XML for the first activity:
<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:background="#000000"
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=".TicTacToe" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textSize="50dp"
android:text="Welcome to \nTic-Tac-Toe"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/bPlayNow"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="137dp"
android:textSize="34dp"
android:text="Play now!" />
</RelativeLayout>
XML for the second activity:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_x="78dp"
android:layout_y="30dp"
android:text="GAME MODE"
android:textColor="#FFFFFF"
android:textSize="50dp" />
<Button
android:id="#+id/bOnePlayer"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="160dp"
android:text="Single Player"
android:textSize="25dp" />
<Button
android:id="#+id/bTwoPlayer"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="220dp"
android:text="Two Player"
android:textSize="25dp" />
</AbsoluteLayout>
For starting a new activity, it is necessary to add it in the manifest file.
<application >
...
<activity
android:name="com.example.killacatoe.playerMenu" >
</activity>
</application>
then in onCreate(), for starting a new activity when the button is pressed, you can do:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
bOnePlayer = (Button) findViewById(R.id.bOnePlayer);
bOnePlayer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(TicTacToe.this, playerMenu.class);
startActivity(intent);
}
});
}
Make sure that you define playerMenu in the AndroidManifest file.
<application ... >
...
<activity
android:name="com.example.killacatoe.playerMenu" >
</activity>
</application>
Check this page for more information
http://developer.android.com/training/basics/firstapp/starting-activity.html
Are you sure that you have register your PlayerMenu activity in AndroidManifest.xml ?
...
please paste the error log.
use this code in your first Activity to jump to another
onClick(View v){
Intent ps = new Intent (TicTacToe.this,
PlayerMenu.class);
startActivity(ps);
}
Manifest File :
<Activity
android:name="com.example.killacatoe.TicTacToe">
</Activity>

Android conversion program crashes on button click

Here are my files (Conversion.xml, Conversion.java and AndroidManifest.xml). It is a small part of a project. I am able to get to the screen where I want to convert the power in watts to decibels. But when I enter the power value and click the button "Convert", the app crashes and goes back to the my app home screen.
conversion.xml
<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=".ConvertTodB" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter the power in Watts"
android:inputType="number"
android:text="#string/et1" />
<Button
android:id="#+id/bConvert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Convert" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Result in dB"
android:text="#string/et2"
android:textSize="20sp"/>
</LinearLayout>
Conversion.java
package com.example.rfconcepts;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.Math;
public class Conversion extends Activity {
EditText entered_val;
TextView result_val;
Button bCon;
#Override
protected void onCreate(Bundle convertTodB) {
super.onCreate(convertTodB);
setContentView(R.layout.conversion);
entered_val = (EditText) findViewById(R.string.et1);
bCon = (Button) findViewById(R.id.bConvert);
result_val = (TextView) findViewById(R.string.et2);
bCon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(entered_val.getText() != null && entered_val.getText().length() != 0){
result_val.setText(String.valueOf(10 * Math.log10(Double
.valueOf(entered_val.getText().toString()))));
}
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rfconcepts"
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.rfconcepts.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.rfconcepts.Conversion"
android:label="#string/app_name">
</activity>
</application>
</manifest>
I thank you in advance for the advise.
P.S:
Here is a list of things I did after reading a lot of solutions for various questions posted here.
Instead of using onClickListener, in conversion.xml, i added the attribute "android:onClick = "onClickConvert" and added the follwoing onClickConvert method in the Activity class.
public void onClickConvert(View v){
if(entered_val.getText() != null && entered_val.getText().length() != 0){
result_val.setText(String.valueOf(10 * Math.log10(Double
.valueOf(entered_val.getText().toString()))));
}
}
Changed Double.parseDouble() to Double.valueOf() (Although, I do not think this is the issue)
Checked if the editText itself has null, hence the if statement.
entered_val = (EditText) findViewById(R.string.et1);
Why are you referring to a string? It should be an ID.
You did not declare an ID for your EditTexts. Do something like this:
<EditText
[...]
android:id="#+id/MyEditText"
/>
Then you can refer to it like this:
entered_val = (EditText) findViewById(R.Id.MyEditText);
Also do the same with your TextView.
Second issue:
EditText.getText() returns an Editable, but you need a String. Convert it like this:
if(entered_val.getText().toString() != null && entered_val.getText().toString().length() != 0
You shall an ID in EditText of your conversion.xml and then access it in Java code. Post your logcat error for more concrete answer
you need to add reference id to the EDITTEXT in power and result
<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=".ConvertTodB" >
<EditText
android:id="#+id/et1" // add the reference id
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter the power in Watts"
android:inputType="number"
android:text="#string/et1" />
<Button
android:id="#+id/bConvert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Convert" />
<TextView
android:id="#+id/et2"// add the reference id
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Result in dB"
android:text="#string/et2"
android:textSize="20sp"/>
Also wired up those edittext with referenceid in your java code
entered_val = (EditText) findViewById(R.id.et1); // wired up with the xml id of the component
bCon = (Button) findViewById(R.id.bConvert);
result_val = (TextView) findViewById(R.id.et2);// wired up with the xml id of the component

Android — Add new tab when button click like Google Chrome new button?

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);
}

How to open layout on button click (android)

How can I open another layout xml file when I click on a button in main.xml file?
so if I have main.xml which has a button sying click here and I click it, it opens up second.xml file (layout).
First Create your two layout:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="This is Activity 1" />
<Button android:text="Next"
android:id="#+id/Button01"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
</LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="This is Activity 2" />
<Button android:text="Previous"
android:id="#+id/Button02"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
</LinearLayout>
Second Add your Activity to the manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rr"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Activity1"
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=".Activity2"></activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
Activity1.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
To switch to Activity2 you have to:
Gets a reference to the button with ID Button01 on the layout using
(Button) findViewById(R.id.Button01).
Create an OnClick listener for the button.
And the most important part, creates an “Intent” to start another
Activity. The intent needs two parameters: a context and the name of
the Activity that we want to start (Activity2.class)
Activity2.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Button next = (Button) findViewById(R.id.Button02);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
-Inflate the button from the xml
-add an onClickListener on it
-set a new layout in the onClick event
Button btn = (Button) findViewById(R.id.myButton);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
MyActivity.setContentView(R.layout.newlayout);
}
});
Something like this should work...
A Kotlin way:
-Add the onClick event directly in the designer.
Open the activity (Activity1.xml for example) file in the designer mode
Select the button that will trigger the transition
Add the function name on the onClick box of the right panel with all the button properties
-Open the Activity .kt file
Add the function with the name that you just defined in the designer
fun openActivity2(view: View) {
intent = Intent(view.context,Activity2::class.java)
startActivity(intent)
}
Now you have the function linked to the onClick event of your button

Categories

Resources