connect activitys - android

Can some one tell me were i am going wrong. i have three activity's that i want to connect togever. This code is my first java file and first xml file. i think my onclick code some where is not right. my end result is that all 3 activity connect with 3 image buttons...thank
java 1.code
package my.hope;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.content.Intent;
public class NewhopeActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView myImage = (ImageView) findViewById(R.id.imageButton1);
myImage.setOnClickListener(new OnClickListener() {
intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
);
}
}
xml.code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bt" />
<ImageButton
android:id="#id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:onClick="Act2"/>

You have to put the startActivity() into the actual onClick() method.
myImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
}
}

Change
ImageView myImage = (ImageView) findViewById(R.id.imageButton1);
myImage.setOnClickListener(new OnClickListener() {
intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
to
ImageView myImage = (ImageView) findViewById(R.id.imageButton1);
myImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
}
}

Related

Button not working in android app?

I'm building an app which has 3 functions a converter, a calculator and a notes section. When I click on the converter button on the home page it brings me to the converter activity / page. But when I click on the calculator button on the home page it won't open. Here is the code below. Any reason as to why? Thanks in advance.
MainActivity
package com.qub.buildersbuddy;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button buttonConverter;
Button buttonCalculator;
Button buttonNotePad;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button ConvertBtn = (Button) findViewById(R.id.butonConverter);
ConvertBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,CentInch.class);
startActivity(intent);
}
});
}
public void setupConverterButton(){
buttonConverter = (Button) findViewById(R.id.butonConverter);
// Button messageButton = (Button) findViewById(R.id.butonConverter);
}
public void CentToInch(){
buttonConverter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//opening the
try{
Class centClass = Class
.forName("com.qub.buildersbuddy.CentInch");
Intent myintent = new Intent(MainActivity.this,centClass);
startActivity(myintent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
});
}
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button CalcBtn = (Button) findViewById(R.id.buttonCalc);
CalcBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Calculator.class);
startActivity(intent);
}
});
}
public void setupCalculatorButton(){
buttonCalculator = (Button) findViewById(R.id.buttonCalc);
// Button messageButton = (Button) findViewById(R.id.butonConverter);
}
public void Calculator(){
buttonCalculator.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//opening the
try{
Class calcClass = Class
.forName("com.qub.buildersbuddy.Calculator");
Intent myintent = new Intent(MainActivity.this,calcClass);
startActivity(myintent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
});
}
}
activity_main:
<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=".CentInch" >
<Button
android:id="#+id/butonConverter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:text="Converter" />
<Button
android:id="#+id/buttonCalc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/butonConverter"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="Calculator" />
<Button
android:id="#+id/buttonNotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonCalc"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:text="Notes" />
</RelativeLayout>
Is this the exact code you're using? Your #onCreate1 method will never get called. #onCreate gets called because it overrides a method from the class you extended (Activity), and something in Activity calls the method when the activity first starts. Move your calculator button logic into the first #onCreate method.

Adding ListView item from another Layout

In my app there is two layout first.xml and activity_main.xml. In first Layout there is a button and ListView (Code):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ListView
android:id="#+id/listView_items"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
And this Layout is connected with MainActivty:
package com.example.listview2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView lvItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button but = (Button) findViewById(R.id.but);
lvItem = (ListView)this.findViewById(R.id.listView_items);
but.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ListActivity.class);
startActivity(intent);
}
});
}
}
And then there is second layout 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=".ListActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="#+id/input">
<EditText
android:id="#+id/editText_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Add" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="#+id/input"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
And this activity is connected to ListActivity:
package com.example.listview2;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class ListActivity extends Activity {
private EditText etInput;
private Button btnAdd;
private ListView lvItem;
private ArrayList<String> itemArrey;
private ArrayAdapter<String> itemAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpView();
}
private void setUpView() {
// TODO Auto-generated method stub
etInput = (EditText)this.findViewById(R.id.editText_input);
btnAdd = (Button)this.findViewById(R.id.button_add);
lvItem = (ListView)this.findViewById(R.id.listView_items);
itemArrey = new ArrayList<String>();
itemArrey.clear();
itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,itemArrey);
lvItem.setAdapter(itemAdapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addItemList();
}
});
etInput.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_ENTER) {
addItemList();
}
return true;
}
});
}
protected void addItemList() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if (isInputValid(etInput)) {
itemArrey.add(0,etInput.getText().toString());
etInput.setText("");
itemAdapter.notifyDataSetChanged();
}
}
protected boolean isInputValid(EditText etInput2) {
// TODO Auto-generatd method stub
if (etInput2.getText().toString().trim().length()<1) {
etInput2.setError("Please Enter Item");
return false;
} else {
return true;
}
}
}
The idea of app is very simple. When you start the app, it start MainActivity which is button with ListView. Then you press button to open ListActivity to create new ListView item and this is my question: how can I add to ListView an item from another Activity?
I think you got the logic a little bit messed up there. For example, you are referencing in ListActivity a view that does not exist in its layout:
lvItem = (ListView)this.findViewById(R.id.listView_items);
listView_items is defined in first.xml layout, which means that the lvItem reference in ListActivity will be null.
In any case, here is a better way to design your app:
1- Have your MainActivity act as the central component of the app. Since it is displaying the listView, it should also be responsible of adding/removing items from it. MainActivity would then start ListActivity when a user wants to add a new item to the list.
2- Have your ListActivity act as a simple interface to get the user's information, and retuning it to your MainActivity.
In this case, the solution is to use startActivityForResult(). Here is a small rewrite to make it work:
public class MainActivity extends Activity {
private ListView lvItem;
private ArrayList<String> itemArrey;
private ArrayAdapter<String> itemAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button but = (Button) findViewById(R.id.but);
lvItem = (ListView)this.findViewById(R.id.listView_items);
itemArrey = new ArrayList<String>();
itemArrey.clear();
itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,itemArrey);
lvItem.setAdapter(itemAdapter);
but.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ListActivity.class);
startActivityForResult(intent, 1);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
itemArrey.add(0,result);
itemAdapter.notifyDataSetChanged();
}
else if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
}
Now, for ListActivity, you will keep the same code, but add the following in your btnAdd onClickListener:
Intent returnIntent = new Intent();
returnIntent.putExtra("result",etInput);
setResult(RESULT_OK,returnIntent);
finish();
You obviously will want to add input verification and other niceties.
First of all you cannot be doing
lvItem = (ListView)this.findViewById(R.id.listView_items);
you will get Null Pointer exception when lvItem is used as listView_items does not exist in your activity_main layout.
The right way to make your design work is to send "itemArrey" back as bundle extra in the intent you create for setResult.

intent not working when swiching from one activity to another

I have made a simple android program with two activities,in that 1st activity contains an edittext and a button,and second activity contain a textview.Now when the button in 1st activity pressed the text from Edittext should go to 2nd activity's textView.I have tried code as below,but it's not working:
MainActivity.java
package com.example.myweb;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myweb";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b =(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText ed = (EditText)findViewById(R.id.edit_msg);
Intent i = new Intent(getApplicationContext(),Act2.class);
String s= ed.getText().toString();
i.putExtra("EXTRA_MESSAGE", s);
startActivity(i);
}
});
}
}
Act2.java
package com.example.myweb;
import org.w3c.dom.Text;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Act2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView)findViewById(R.id.tv1);
Intent i =getIntent();
String msg = i.getStringExtra(MainActivity.EXTRA_MESSAGE);
tv.setText(msg);
setContentView(tv);
}
});
}
}
please help me.thank you
Just try this:
In MainActivity.java:
declare Button b and EditText ed as a class field (i.e. keep it outside onCreate())
class MainActivity.java
{
Button b;
EditText ed;
...
onCreate() {
...
b =(Button)findViewById(R.id.button1);
ed = (EditText)findViewById(R.id.edit_msg);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,Act2.class);
String s= ed.getText().toString();
i.putExtra("EXTRA_MESSAGE", s);
startActivity(i);
}
});
...
In Act2.java:
...
Intent i = getIntent();
String msg = i.getStringExtra("EXTRA_MESSAGE");
TextView tv = (TextView)findViewById(R.id.tv1);
tv.setText(msg);
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
...
You are using
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
So, in MainActivity
i.putExtra(EXTRA_MESSAGE, s);
startActivity(i);
(OR)
String message = intent.getStringExtra("EXTRA_MESSAGE");
also,
i.putExtra("EXTRA_MESSAGE", s);
Change
String msg = i.getStringExtra(MainActivity.EXTRA_MESSAGE);
to
String msg = i.getStringExtra("EXTRA_MESSAGE");
MainActivity.EXTRA_MESSAGE would mean a static variable of MainActivity.java class. Hope you get the difference. You need the variable EXTRA_MESSAGE which you had put in intent i
Edit: For your crash, we need a logcat o/p and activity, manifest code. But possible reasons:
Activity Act2.java is not declared in manifest file.
You said you have only a textview in second activity. But you are trying to get button1 from act2.xml. So you are getting Force close.
Change Act2.java 's onCreate() as
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
TextView tv = (TextView)findViewById(R.id.tv1);
Intent i =getIntent();
String msg = i.getStringExtra("EXTRA_MESSAGE");
tv.setText(msg);
}
You don't need to call setContentView(tv); as it is already there in act2.xml and you are using setContentView(R.layout.act2);
Hope your problem gets solved.
Use this code
**MainActivity.java**
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button1);
final EditText editText = (EditText)findViewById(R.id.editText1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("EXTRA_MESSAGE", editText.getText().toString());
startActivity(intent);
}
});
}
}
**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/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="74dp"
android:layout_marginTop="26dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1"
android:layout_marginLeft="69dp"
android:layout_marginTop="47dp"
android:text="Button" />
</RelativeLayout>
**SecondActivity.java**
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setContentView(R.layout.second);
Intent intent = getIntent();
final String message = intent.getStringExtra("EXTRA_MESSAGE");
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView textView = new TextView(SecondActivity.this);
textView.setText(message);
setContentView(textView);
}
});
}
}
**second.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tester"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.tester.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=".SecondActivity"></activity>
</application>
</manifest>
Try
Intent i = new Intent(MainActivity.this,Act2.class);
You are using
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
So, in MainActivity
i.putExtra(EXTRA_MESSAGE, s);
startActivity(i);
please review and change your code its works
package com.example.activityact;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b =(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText ed = (EditText)findViewById(R.id.editText1);
Intent i = new Intent(getApplicationContext(),Act2.class);
String s= ed.getText().toString();
i.putExtra("EXTRA_MESSAGE", s);
startActivity(i);
}
});
}
}
Act2.java
package com.example.activityact;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Act2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.textView1);
Intent i =getIntent();
// String msg = i.getStringExtra(EXTRA_MESSAGE);
//String receiver = getIntent().getStringExtra(EXTRA_MESSAGE);
String message = i.getStringExtra("EXTRA_MESSAGE");
tv.setText(message);
}
}
Value of the EXTRA_MESSAGE variable in MainActivity.java is different then the key value that you are putting message in to intent.
MainActivity.java:
public final static String EXTRA_MESSAGE = "com.example.myweb";
i.put("EXTRA_MESSAGE",s);
Act2.java: (you are accessing by extra_message variable which is not correct)
Intent i = getIntent();
String text = i.getStringExtra(MainActivity.EXTRA_MESSAGE); //WHICH IS DIFFER FROM THE KEY VALUE
So key value should be the same at the time of setting and getting the value. here while setting the value key is "EXTRA_MESSAGE" and while getting "com.example.myweb" so you will get null pointer exception.
SOLUTION:
just change the line in Act2.java
Intent i = getIntent();
String text = i.getStringExtra("EXTRA_MESSAGE");
Also check your menifest file for the activities declaration.
Hope it may help you.
Did you remember to correctly define your second activity in the manifest XML?
Check out this great guide for anything else you might have missed: http://developer.android.com/training/basics/firstapp/starting-activity.html

Setting Buttons to Reference Multiple xml layouts

I have a button bar that links different xml layouts, but I can only get to one and not return to the others when I try to click on a different button. Sorry, in advance for the hassle, still bit of a novice. This is my first post here, but been referencing this site a lot. Thanks in advance.
activity_main.xml
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:stretchColumns="*"
android:background="#6B1414">
<TableRow
android:id="#+id/tableRow1"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<Button
android:id="#+id/btn1"
android:text="#string/Str"
android:textStyle="bold"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:padding="18dip"
android:layout_weight="1"
android:background="#424242"
android:textColor="#ffffff"
android:gravity="center"/>
<Button
android:id="#+id/btn2"
android:text="#string/Agl"
android:textStyle="bold"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:padding="18dip"
android:layout_weight="1"
android:background="#424242"
android:textColor="#ffffff"
android:gravity="center"/>
<Button
android:id="#+id/btn3"
android:text="#string/Int"
android:textStyle="bold"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:padding="18dip"
android:layout_weight="1"
android:background="#424242"
android:textColor="#ffffff"
android:gravity="center"/>
<Button
android:id="#+id/btn4"
android:text="#string/Misc"
android:textStyle="bold"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#424242"
android:textColor="#ffffff"
android:padding="18dip"/>
</TableRow>
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 =(Button)findViewById(R.id.btn1);
Button btn2 =(Button)findViewById(R.id.btn2);
Button btn3 =(Button)findViewById(R.id.btn3);
Button btn4 =(Button)findViewById(R.id.btn4);
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.activity_main);
return;
}
});
btn2.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.agil_main);
return;
}
});
btn3.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.int_main);
return;
}
});
btn4.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.misc_main);
return;
}
});
}}
Update. The XML looks good, thanks. I'm still stuck with the same problem I had before. I added activities for each layout, but I'm still having the same problem of not being about to cycle through the layouts. Each java file looks the same as this.
MainActivity.java (update with new .class Intents)
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 =(Button)findViewById(R.id.btn1);
Button btn2 =(Button)findViewById(R.id.btn2);
Button btn3 =(Button)findViewById(R.id.btn3);
Button btn4 =(Button)findViewById(R.id.btn4);
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent = new Intent(getApplicationContext(), MainActivity.class);
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.activity_main);
return;
}
});
btn2.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent = new Intent(getApplicationContext(), MainAgil.class);
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.agil_main);
return;
}
});
btn3.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent = new Intent(getApplicationContext(), MainInt.class);
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.int_main);
return;
}
});
btn4.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent = new Intent(getApplicationContext(), MainMisc.class);
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.misc_main);
return;
}
});
}}
Define your button bar in a separate XML layout. In every XML you need that bar include that button bar using <include/> tag. And in every activity in which XML you have included the button bar handle the onclick of the buttons separately.

Android app hangs when referencing the back button on Secondary screen

I have a small app that when button presses navigates when moving from main screen to next screen this works fine, but when I added a button on the next page (to go back) it breaks.
Fun.java
package com.forcetechnology.OptusApp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Fun extends Activity {
OnClickListener backListener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fun);
Button backButtonf = (Button)findViewById(com.forcetechnology.OptusApp.R.id.backtoMainf);
backListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.OptusAppMain");
startActivity(i);
}
};
backButtonf.setOnClickListener(backListener);
}
}
Fun.Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/backtoMainf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/val5" />
</LinearLayout>
Main.xml
<ImageButton
android:id="#+id/funbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/val5"
android:src="#drawable/val5" />
<ImageButton
android:id="#+id/executionbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/funbutton"
android:layout_alignParentRight="true"
android:background="#drawable/val2"
android:src="#drawable/val2" />
<ImageButton
android:id="#+id/performancebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/executionbutton"
android:layout_toLeftOf="#+id/funbutton"
android:background="#drawable/val3"
android:src="#drawable/val4" />
<ImageButton
android:id="#+id/innovationbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/executionbutton"
android:layout_alignParentLeft="true"
android:background="#drawable/val3"
android:src="#drawable/val3" />
<ImageButton
android:id="#+id/peoplebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/innovationbutton"
android:layout_toLeftOf="#+id/executionbutton"
android:background="#drawable/val1"
android:src="#drawable/val1" />
</RelativeLayout>
OPtusAppMain.java
package com.forcetechnology.OptusApp;
import android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class OptusAppMain extends Activity
{
OnClickListener funListener,executionListener,innovationListener,peopleListener,performanceListener;;
TextView testView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.forcetechnology.OptusApp.R.layout.main);
ImageButton funButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.funbutton);
ImageButton executionButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.executionbutton);
ImageButton innovationButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.innovationbutton);
ImageButton peopleButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.peoplebutton);
ImageButton performanceButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.performancebutton);
funListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Fun");
startActivity(i);
}
};
executionListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Execution");
startActivity(i);
}
};
innovationListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Innovation");
startActivity(i);
}
};
peopleListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.People");
startActivity(i);
}
};
performanceListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Performance");
startActivity(i);
}
};
funButton.setOnClickListener(funListener);
executionButton.setOnClickListener(executionListener);
innovationButton.setOnClickListener(innovationListener);
peopleButton.setOnClickListener(peopleListener);
performanceButton.setOnClickListener(performanceListener);
}
}
Edit: I have traced the error to this line Button backButtonf = (Button)findViewById(com.forcetechnology.OptusApp.R.id.backtoMainf); in fun.java.
In the onClick() of backListener, just call finish() to go back to the previous activity.
Lets take an example: You have two activities "A" & "B", You start your "B" activity from "A" that means your "A" activity is in stack so there is no need to start it again, just finish your "B" activity with "finish()" method.
public class A extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
public void onAbuttonClick()
{
startActivity(new Intent(A.this,B.class));
}
}
}
public class B extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
public onBbuttonclick(View v)
{
finish();
}
}
}

Categories

Resources