I am trying to add values to a string and then send it to the next activity. Here is the only relevant part of the code. adding() is the id of the button which appends values to the String msg and sendMessage() sends the msg altogether. When I just send a dummy value, the message is being passed, even when I add something like 1,2,3,4,5 at once instead of adding, it goes. But when I try to add, append and then send, it gives me an "Unfortunately stopped" error.
I tried couple of tweaks, but still the same error. How to fix this?
public void adding(View view){
EditText et1=(EditText) findViewById(R.id.editText1);
msg=msg+","+et1.getText().toString();
TextView tv1=(TextView) findViewById(R.id.textView1);
tv1.setText(msg);
et1.setText("");
}
public void sendMessage(View view){
Intent intent=new Intent(this,Next.class);
msg+=",";
intent.putExtra("message", msg);
startActivity(intent);
}
EDIT: Activity Code-
package com.example.newlist;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
String msg="";
int count=0;
#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;
}
#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);
}
public void adding(View view){
EditText et1=(EditText) findViewById(R.id.editText1);
msg=msg+","+et1.getText().toString();
TextView tv1=(TextView) findViewById(R.id.textView1);
tv1.setText(msg);
et1.setText("");
}
public void sendMessage(View view){
Intent intent=new Intent(this,Next.class);
msg+=",";
intent.putExtra("message", msg);
startActivity(intent);
}
}
Not sure I understand your question correctly
move your findViewById to onCreate
EditText et1;
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText) findViewById(R.id.editText1);
tv1=(TextView) findViewById(R.id.textView1);
}
in your adding method do it like this
public void adding(View view){
msg = msg + "," + et1.getText().toString();
final String showMessage = msg;
tv1.setText(showMessage);
et1.setText("");
}
in your sendMessage
public void sendMessage(View view){
Intent intent=new Intent(this,Next.class);
msg+=",";
final String msgThatWillSend = msg;
intent.putExtra("message", msgThatWillSend);
startActivity(intent);
}
didn't test yet, but it should be work
try this :
ActivityA.java
public class ActivityA extends Activity{
StringBuilder msg=new StringBuilder();
private EditText et1;
private TextView tv1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
et1=(EditText)findViewById(R.id.et1);
tv1=(TextView)findViewById(R.id.tv1);
}
public void adding(View view){
msg.append(" "+et1.getText().toString());
tv1.setText(msg);
et1.setText("");
}
public void sendMessage(View view){
Intent intent=new Intent(this,Next.class);
intent.putExtra("message", msg.toString());
startActivity(intent);
}
}
Next.java
public class Next extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(getIntent().getStringExtra("message"));
}
}
test.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"
>
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:textSize="12sp" />
<EditText
android:id="#+id/et1"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="#+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="adding"
android:text="adding"
/>
<Button
android:id="#+id/btnsent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="SendMessage" />
</LinearLayout>
next.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"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="msg"
android:textSize="25sp" />
</LinearLayout>
Related
I am creating an Android app - using Android Studio.
The app launches in MainActivity.java which fetches activity_main.xml . On activity_main, the user can select one of 3 buttons. No matter which button they select, it will take them to the SAME layout - primary_layout.xml and the Java class associated with that is PrimaryClass.java .
I have a placeholder in primary_layout. I want this placeholder (id: placeholder) to change according to what button was previously selected.
Eg. If Button 1 (id: button1) is clicked, then the placeholder must say “Button 1 was clicked.”
Or let’s say Button 2 (id: button2) was clicked, then the placeholder must say “Button 2 was clicked”. And the same goes for Button 3.
I have created an intent to open PrimaryClass but I'm not too sure how to code the intent for when the button is clicked. I just don’t know how to change the text of the placeholder, depending on which button the user has clicked. I’ve tried using an ‘if statement’ but it doesn’t seem to work.
Or instead of creating another activity, should I rather create a fragment, and if I should, how would I code the fragment in this particular app?
I have attached images and code to better understand.
And here is my code of my classes and layout files:
MainActivity:
package com.msp.exampleapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main:
<?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:id="#+id/activity_main"
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="com.msp.exampleapplication.MainActivity">
<Button
android:text="Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/button1"
android:onClick="clickedButton1" />
<Button
android:text="Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_alignParentStart="true"
android:layout_marginTop="27dp"
android:id="#+id/button2"
android:onClick="clickedButton2" />
<Button
android:text="Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button2"
android:layout_alignParentStart="true"
android:layout_marginTop="32dp"
android:id="#+id/button3"
android:onClick="clickedButton3" />
</RelativeLayout>
PrimaryClass:
package com.msp.exampleapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class PrimaryClass extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.primary_layout);
}
}
primary_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="{holder}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35sp"
android:id="#+id/placeholder" />
</LinearLayout>
Your first Activity must look like this:
package com.msp.exampleapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
Button button1,button2,button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this, PrimaryClass.class);
intent.putExtra("message","Button 1 selected");
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this, PrimaryClass.class);
intent.putExtra("message","Button 2 selected");
startActivity(intent);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this, PrimaryClass.class);
intent.putExtra("message","Button 3 selected");
startActivity(intent);
}
});
}
}
Code the second activity as below:
package com.msp.exampleapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class PrimaryClass extends AppCompatActivity {
TextView placeholder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.primary_layout);
placeholder=(TextView) findViewById(R.id.placeholder);
placeholder.setText(getIntent().getStringExtra("message"));
}
}
Here is solution of your problem. You have to use Intent to make action in another activity.
Code for MainActivity, create a intent on each button click like below
button_1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,PrimaryClass.class);
intent.putExtra("button_text", "Button 1 Clicked");
startActivity(intent);
}
});
Same for button 2 and 3
button_2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,PrimaryClass.class);
intent.putExtra("button_text", "Button 2 Clicked");
startActivity(intent);
}
});
button_3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,PrimaryClass.class);
intent.putExtra("button_text", "Button 3 Clicked");
startActivity(intent);
}
});
Now in PrimaryClass use this code inside onCreate Method
String btn_text;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
btn_text = bundle.getString("button_text");
}
Now set text on lable
textView.setText(btn_text);
Hope you understand this, if not let me know, I'll help you.
I want to make an app that takes input as feet and inches, and cost per feet, and calculates the cost of rod. It is a pretty simple app,I have done most of the work, but when I click at the calculate Button, in place of "result", my answer should come.
Now here is the XML code:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/ft"
android:id="#+id/feet"
android:layout_marginTop="41dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/feet1"
android:layout_alignBottom="#+id/feet"
android:layout_toRightOf="#+id/feet"
android:inputType="text" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/inches"
android:layout_alignBottom="#+id/feet"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/in"
android:id="#+id/Inches"
android:layout_alignBottom="#+id/feet"
android:layout_toLeftOf="#+id/inches"
android:layout_toStartOf="#+id/inches" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/cost"
android:id="#+id/cost"
android:layout_marginTop="60dp"
android:layout_below="#+id/feet"
android:layout_toRightOf="#+id/feet"
android:layout_toEndOf="#+id/feet" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/calcost"
android:layout_alignBottom="#+id/cost"
android:layout_toRightOf="#+id/cost"
android:layout_toEndOf="#+id/cost"
android:inputType="text" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cal"
android:id="#+id/calculate"
android:layout_below="#+id/cost"
android:layout_centerHorizontal="true"
android:layout_marginTop="73dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/ans"
android:id="#+id/result"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="123dp" />
And here is the Java code:
package com.example.baqir.saecost;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void add(View v)
{
RelativeLayout rel= (RelativeLayout) findViewById(R.id.rel);
final TextView result= (TextView)findViewById(R.id.result);
EditText et1= (EditText)findViewById(R.id.feet1);
EditText et2= (EditText) findViewById(R.id.inches);
EditText ct= (EditText)findViewById(R.id.calcost);
final double a=Double.parseDouble(String.valueOf(et1.getText()));
final double b=Double.parseDouble(String.valueOf(et2.getText()));
final double c=Double.parseDouble(String.valueOf(ct.getText()));
final Button button = (Button) findViewById(R.id.calculate);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View g) {
double i;
i=(a+(b/12))*c;
result.setText(i+" Rs");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Change your code like this:
public void add(View v)
{
RelativeLayout rel= (RelativeLayout) findViewById(R.id.rel);
final TextView result= (TextView)findViewById(R.id.result);
EditText et1= (EditText)findViewById(R.id.feet1);
EditText et2= (EditText) findViewById(R.id.inches);
EditText ct= (EditText)findViewById(R.id.calcost);
final double a=Double.parseDouble(String.valueOf(et1.getText()));
final double b=Double.parseDouble(String.valueOf(et2.getText()));
final double c=Double.parseDouble(String.valueOf(ct.getText()));
final Button button = (Button) findViewById(R.id.calculate);
// button.setOnClickListener(new View.OnClickListener() {
// public void onClick(View g) {
double i;
i=(a+(b/12))*c;
result.setText(i+" Rs");
// }
// });
}
and in your XML change add onClick, as:
<Button
android:onClick="add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cal"
android:id="#+id/calculate"
android:layout_below="#+id/cost"
android:layout_centerHorizontal="true"
android:layout_marginTop="73dp" />
I got it your issue just add this line in XML layout where your Button is
android:onClick="add"
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rel= (RelativeLayout) findViewById(R.id.rel);
final TextView result= (TextView)findViewById(R.id.result);
EditText et1= (EditText)findViewById(R.id.feet1);
EditText et2= (EditText) findViewById(R.id.inches);
EditText ct= (EditText)findViewById(R.id.calcost);
final double a=Double.parseDouble(String.valueOf(et1.getText()));
final double b=Double.parseDouble(String.valueOf(et2.getText()));
final double c=Double.parseDouble(String.valueOf(ct.getText()));
final Button button = (Button) findViewById(R.id.calculate);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View g) {
double i;
i=(a+(b/12))*c;
result.setText(i+" Rs");
}
});
}
}
remove method and write your code in oncreate method.
package com.example.baqir.saecost;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rel= (RelativeLayout) findViewById(R.id.rel);
final TextView result= (TextView)findViewById(R.id.result);
EditText et1= (EditText)findViewById(R.id.feet1);
EditText et2= (EditText) findViewById(R.id.inches);
EditText ct= (EditText)findViewById(R.id.calcost);
final Button button = (Button) findViewById(R.id.calculate);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View g) {
double i;
final double a=Double.parseDouble(String.valueOf(et1.getText()));
final double b=Double.parseDouble(String.valueOf(et2.getText()));
final double c=Double.parseDouble(String.valueOf(ct.getText()));
i=(a+(b/12))*c;
result.setText(i+" Rs");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
As long as the XML files are ok (The IDs are the one you actually used ,not mixed up or anything) ,this should fix your problem.
You were taking the values from the editTexts before pressing the button...so practically, after you wrote anything in there, the code that was grabbing the values, was executed already.
Initialize your all views in oncreate method it should work
I have just started programming in Android and I couldn't get the result on clicking button that I expected. Here s the code.
.java file
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class AndroidLove extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_love);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.android_love, menu);
return true;
}
public void onButtonClicked(View view) {
TextView textView=(TextView)findViewById(R.id.textView1);
textView.setVisibility(View.VISIBLE);
}
}
.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: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=".AndroidLove" >
<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_marginLeft="17dp"
android:layout_marginTop="104dp"
android:text="#string/hello_world"
android:visibility="invisible" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:text="#string/Button"
android:onClick="onButtonClicked" />
strings.xml file
<resources>
<string name="app_name">AndroidLove</string>
<string name="action_settings">Settings</string>
<string name="hello_world">First Line\nSecond Line\nThird Line</string>
<string name="Button">Get Lyrics</string>
On clicking the button, the following text should be displayed but its not displayed:-
First Line
Second Line
Third Line
Please help
Edit: Maybe and probably your binding is wrong about onclick, try below code instead of top. Put that code into your onCreate method.
Button button= (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView textView=(TextView)findViewById(R.id.textView1);
textView.setVisibility(View.VISIBLE);
}
});
You need to get the string value from the strings.xml file which can be acheived by
String st =getResources.getString(R.string.hello_world);
Then set that to textview by
textView.setText(st);
Try this code:
public class AndroidLove extends Activity {
private Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_love);
textView=(TextView)findViewById(R.id.textView1);
button1 = (Button)findViewByid(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
// do your stuff here
String _data =getResources.getString(R.string.hello_world);
textView.setText(_data);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Hey Guys this may a silly question but I am a bit confused . So I need a help and I am new to android . Here Is my code that I have that I have tried
Main.java
package com.example.bluetooth;
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int REQUEST_ENABLE_BT = 1;
private Button onBtn;
private Button offBtn;
private Button listBtn;
private Button findBtn;
private TextView text;
private BluetoothAdapter myBluetoothAdapter;
private Set<BluetoothDevice> pairedDevices;
private ListView myListView;
private ArrayAdapter<String> BTArrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
onBtn = (Button) findViewById(R.id.turnOn);
offBtn = (Button) findViewById(R.id.turnOff);
listBtn = (Button) findViewById(R.id.paired);
findBtn = (Button) findViewById(R.id.search);
myListView = (ListView) findViewById(R.id.list1);
BTArrayAdapter = new ArrayAdapter<String>(this,R.layout.simple_list_item_1);
myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(myBluetoothAdapter == null){
onBtn.setEnabled(false);
offBtn.setEnabled(false);
listBtn.setEnabled(false);
findBtn.setEnabled(false);
text.setText("Status: not supported");
Toast.makeText(getApplicationContext(), "Bluetooth is not supported on your device.", Toast.LENGTH_SHORT).show();
}else{
text = (TextView) findViewById(R.id.text);
onBtn = (Button) findViewById(R.id.turnOn);
onBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
on(v);
}
});
offBtn = (Button) findViewById(R.id.turnOff);
offBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
off(v);
}
});
listBtn = (Button) findViewById(R.id.paired);
listBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
list(v);
}
});
findBtn = (Button) findViewById(R.id.search);
findBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
find(v);
}
});
myListView = (ListView) findViewById(R.id.list1);
BTArrayAdapter = new ArrayAdapter<String>(this,R.layout.activity_main);
}
}
public void on(View view) {
if (!myBluetoothAdapter.isEnabled()) {
Intent turnOnIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);
Toast.makeText(getApplicationContext(), "Bluetooth turned on",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Bluetooth is already on",
Toast.LENGTH_LONG).show();
}
}
public void onActivityResult(int requestCode,int resultCode, Intent data ){
if(requestCode == REQUEST_ENABLE_BT){
if(myBluetoothAdapter.isEnabled()){
text.setText("Status : Connected");
}else{
text.setText("Status : Disconnected");
}
}
}
public void list(View view){
pairedDevices = myBluetoothAdapter.getBondedDevices();
for(BluetoothDevice device : pairedDevices)
BTArrayAdapter.add(device.getName()+ "\n" + device.getAddress());
Toast.makeText(getApplicationContext(),"Show Paired Devices",
Toast.LENGTH_SHORT).show();
}
final BroadcastReceiver bReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
BTArrayAdapter.add(device.getName() + "\n" + device.getAddress());
BTArrayAdapter.notifyDataSetChanged();
}
}
};
public void find(View view) {
if (myBluetoothAdapter.isDiscovering()) {
myBluetoothAdapter.cancelDiscovery();
}else {
BTArrayAdapter.clear();
myBluetoothAdapter.startDiscovery();
registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}
}
public void off(View v) {
// TODO Auto-generated method stub
myBluetoothAdapter.disable();
text.setText("Bluetooth is disconnected");
Toast.makeText(getApplicationContext(), "Bluetooth is disconnected", Toast.LENGTH_LONG).show();
}
#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;
}
}
I am getting an error with simple_list_item_1. I don't know to resolve this problem .So kindly help me out with this.
And this is my xml code
<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:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Status"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="30dp" >
<Button
android:id="#+id/turnOn"
android:text="#string/on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/turnOff"
android:text="#string/off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="80dp" >
<Button
android:id="#+id/paired"
android:text="#string/paired"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/search"
android:text="#string/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ListView
android:id="#+id/list1"
android:layout_height="200dp"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
</RelativeLayout>
Thanks in advance.
Since that is an Android resource and not one you created, append the android prefix to it like so
BTArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
i am new in android .. I want to ask question about Link 2 layouts using buttons. I have 2 xml layout and first layout can link to 2nd layout but 2nd layout can't go back to 1st layout. Please help me.
below are my codes...
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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="120dp"
android:text="Link to page 1" />
</RelativeLayout>
page1.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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Page 1 test"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back to main page" />
</LinearLayout>
MainActivity.java
package com.example.linktest2;
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 {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 =(Button)findViewById(R.id.button1);
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.page1);
}
});
}}
page1.java
package com.example.linktest2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class page1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page1);
Button btn1 =(Button)findViewById(R.id.button2);
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);
}
});
}}
You're doing it wrong.
To go from MainActivity to page1 (by convention it should be Page1), you should start a new Activity (instead of changing the contentview of the current activity). Then to go back from Page1 to MainActivity, you can programmatically finish() the Activity, or the user can touch Back.
You can either keep your both layout in same layout file and make the show or hide the layout accordingly.
Also another way is you can create two activity which contains two separate layout and load a second layout on click of button using intent and start activity as below
To Go to other activity you can do like this :
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);
Normally android deals with that itself.
However you can override OnBackPressed in your second activity and launch an intent that leads to your first activity
Going to other activity you can do like this :
Intent intent = new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
For each XML you need to have another Activity.
In Main Activity :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 =(Button)findViewById(R.id.button1);
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),page1.class);
startActivity(intent);
finish();
}
});
}
Replace this
And in your Page1 Activity replace this
public class page1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
Button btn1 =(Button)findViewById(R.id.button2);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
Hope this will help you
If you want to come back to activity A from activity B with some result,you can use like this
activity B
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
activity A
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}