instructions for Facebook Like- Button - java

I have the following problem. I am trying to create a like button for my facebook page, but I got stuck. I followed the instructions but it's not working! I did nowhere see a value to set the coordinates of this button. Where is it? thanks
EDIT2: I edited it again, now, the first onActivityResult has this problem:
void is an invalid type for the variable onActivityresult
what is wrong?
main activity:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import ch.OptiLab.visuscontroll.R;
import com.facebook.UiLifecycleHelper;
import com.facebook.widget.LikeView;
public class MainActivity extends Activity implements OnClickListener {
//UiLifecycleHelper uiHelper;
TextView textView;
// Button buttonende;
Button tipps;
Button btn1;
Button rate;
LikeView LikeView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
tipps = (Button) findViewById(R.id.tipps);
btn1 = (Button) findViewById(R.id.buttonSTART);
btn1.setOnClickListener(this);
rate = (Button) findViewById(R.id.rate);
rate.setOnClickListener(this);
AppRater.app_launched(this);
Settings.sdkInitialize(this);
LikeView = (LikeView) findViewById(R.id.LikeView);
LikeView.setObjectId("http://shareitexampleapp.parseapp.com/photo1/");
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
likeView.handleOnActivityResult(this, requestCode, resultCode, data);
}
tipps.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
protected Context getActivity() {
return null;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
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) {
Toast.makeText(getApplicationContext(), "Easteregg lololol", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
startActivityForResult(new Intent(this,VisusActivity.class), 0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == R.drawable.v1)textView.setText(getResources().getString(R.string.nopercent));
else if (resultCode == R.drawable.v2)textView.setText(getResources().getString(R.string.tenpercent));
else if (resultCode == R.drawable.v3)textView.setText(getResources().getString(R.string.twentypercent));
else if (resultCode == R.drawable.v4)textView.setText(getResources().getString(R.string.thirtypercent));
else if (resultCode == R.drawable.v5)textView.setText(getResources().getString(R.string.fourtypercent));
else if (resultCode == R.drawable.v6)textView.setText(getResources().getString(R.string.fiftypercent));
else if (resultCode == R.drawable.v7)textView.setText(getResources().getString(R.string.sixtypercent));
else if (resultCode == R.drawable.v8)textView.setText(getResources().getString(R.string.seventypercent));
else if (resultCode == R.drawable.v9)textView.setText(getResources().getString(R.string.eightypercent));
else if (resultCode == R.drawable.v10)textView.setText(getResources().getString(R.string.ninetypercent));
else if (resultCode == R.drawable.v11)textView.setText(getResources().getString(R.string.onehundretpercent));
}
#Override
protected void onResume() {
super.onResume();
// Logs 'install' and 'app activate' App Events.
}
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="ch.OptiLab.visustest.MainActivity" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:gravity="center_horizontal"
android:singleLine="false"
android:text="#string/Text1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/buttonSTART"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/button1"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="142dp" />
<Button
android:id="#+id/buttonende"
android:layout_width="180dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textView"
android:layout_marginBottom="14dp"
android:text="#string/beenden" />
<Button
android:id="#+id/tipps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonSTART"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp"
android:text="#string/tipps_tricks" />
<com.facebook.widget.LikeView
android:id="#+id/LikeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tipps"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp" >
</RelativeLayout>

void is an invalid type for the variable onActivityresult occure because you are using onActivityresult method inside onCreate method. remove this method from onCreate and call
likeView.handleOnActivityResult(this, requestCode, resultCode, data);
from your onActivityResult method. Also check your LikeView variable name it should be same everywhere you are using.

I ran into issues implementing LikeView until I upgraded the Facebook API from 3.18 to 3.20.
Also, you can't use a regular button. You need to use the Facebook LikeView widget like this:
<com.facebook.widget.LikeView
android:id="#+id/lvLike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

Related

How to fix activity not showing when run

I am creating a chat app in which you can create groups. to create groups I have created a new activity. it shows up in preview but won't show up when I run the app and open the activity. I tried on another phone and only the CircleImageView showed up. but it's OnClickListner won't work. there are no errors while compiling, logging or debbugging.
I have even tried breakpoints to check the error, but no output. Exept for the ad shows up. tried changing layouts but nothing shows up.
I don't know the error so I am pasting the CreateGroupActivity.java file as well as activity_create_group.xml
CreateGroupActivity.java:
package com.satyamedh.chitchatmessenger;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
import de.hdodenhof.circleimageview.CircleImageView;
public class CreateGroupActivity extends AppCompatActivity
{
private CircleImageView circleImageView;
private EditText editText;
private Button buttonconf, buttoncanc;
private final static int chosenProfileImage = 1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_group);
initializeFields();
buttoncanc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent profileIntent = new Intent(CreateGroupActivity.this, MainActivity.class);
startActivity(profileIntent);
}
});
buttonconf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (TextUtils.isEmpty(editText.getText())){
Toast.makeText(CreateGroupActivity.this, "Please enter text..", Toast.LENGTH_SHORT).show();
}
}
});
circleImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, chosenProfileImage);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == chosenProfileImage && resultCode == RESULT_OK && data != null)
{
Uri imageUri = data.getData();
CropImage
.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
{
final CropImage.ActivityResult result = CropImage.getActivityResult(data);
if(resultCode == RESULT_OK) {
assert result != null;
Uri resultUri = result.getUri();
DatabaseReference rootref = FirebaseDatabase.getInstance().getReference();
Intent settingsIntent = new Intent(CreateGroupActivity.this, chooseUserActivity.class);
String key = rootref.push().getKey();
settingsIntent.putExtra("groupKey", key);
settingsIntent.putExtra("groupImage", resultUri);
settingsIntent.putExtra("groupName", editText.getText().toString());
startActivity(settingsIntent);
}
}
}
private void initializeFields()
{
MobileAds.initialize(this, "ca-app-pub-3127817354023186~6842500243");
final AdView mAdView;
mAdView = findViewById(R.id.my_adView_createGroup);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
#Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}
#Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
}
#Override
public void onAdClicked() {
// Code to be executed when the user clicks on an ad.
}
#Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
#Override
public void onAdClosed() {
// Code to be executed when the user is about to return
// to the app after tapping on an ad.
}
});
circleImageView = findViewById(R.id.create_group_image);
editText = findViewById(R.id.create_group_name);
buttonconf = findViewById(R.id.confirm_button);
buttoncanc = findViewById(R.id.cancel_button);
}
}
activity_create_group.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CreateGroupActivity">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="51dp"
android:layout_marginTop="87dp"
android:layout_marginEnd="259dp"
android:layout_marginBottom="551dp"
android:src="#drawable/profile_image"
android:id="#+id/create_group_image"/>
<EditText
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="167dp"
android:layout_marginTop="88dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="596dp"
android:background="#drawable/inputs"
android:id="#+id/create_group_name"
android:inputType="text"
android:hint="Group name here"/>
<Button
android:id="#+id/confirm_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="168dp"
android:layout_marginTop="141dp"
android:layout_marginEnd="156dp"
android:layout_marginBottom="542dp"
android:text="Confirm"
android:background="#drawable/buttons"/>
<Button
android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="263dp"
android:layout_marginTop="140dp"
android:layout_marginEnd="60dp"
android:layout_marginBottom="546dp"
android:text="Cancel"
android:background="#drawable/buttons"/>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_adView_createGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
android phone(redmi 2) screenshot:
https://drive.google.com/file/d/18MMzLvcdIi8iLog1nR_NrJPcJf-NZG51/view?usp=sharing
pc android studio screenshot:
https://drive.google.com/file/d/1E3Suy967AS3aQS9TEkUlAh5kPjWr86u-/view?usp=sharing
I am newbie to stackoverflow so I can't attach images. sorry.
no errors also.
it's normal because you used so much alignment value and attribute which made your layout not responsive , please use the xml below and don't forget to add the image:src and background you wanted
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CreateGroupActivity">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="20dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:id="#+id/create_group_image"/>
<EditText
android:layout_toRightOf="#id/create_group_image"
android:layout_width="250dp"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:id="#+id/create_group_name"
android:inputType="text"
android:hint="Group name here"/>
<Button
android:layout_below="#id/create_group_name"
android:layout_toRightOf="#id/create_group_image"
android:id="#+id/confirm_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confirm"/>
<Button
android:layout_below="#id/create_group_name"
android:layout_toRightOf="#id/confirm_button"
android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_adView_createGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</RelativeLayout>

unfortunately app has stopped , android

i have given the code here . and when i run it in genymotion it just stops working. please help .
i dont see any display in logcat also . it remains blank and the emulator only shows an error that is "unfortulately, SharedprefrencEexample has stoped working. and the app dont even starts. so i dont get any clue of what might be the error in this code .
here i have used the reference creation at the start and i think it might be the error. so please do see the code and help me.
thanks.
Main_Activity
package com.example.sharedprefrenceexample;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText name,phone,email;
Button submit;
Intent intent;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText) findViewById(R.id.editText1);
phone=(EditText) findViewById(R.id.editText2);
email=(EditText) findViewById(R.id.editText3);
submit=(Button) findViewById(R.id.button1);
submit.setText("#string/button_name_activity_one");
name.setHint("name");
phone.setHint("phone");
email.setHint("email id");
sharedPreferences=getSharedPreferences("#string/database_name", MODE_PRIVATE);
editor=sharedPreferences.edit();
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
editor.putString("name", name.getText().toString());
editor.putString("phone", phone.getText().toString());
editor.commit();
intent=new Intent(MainActivity.this,Secondactivity.class);
intent.putExtra("email", email.getText().toString());
startActivity(intent);
}
});
}
#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);
}
}
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="com.example.sharedprefrenceexample.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:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1"
android:ems="10"
android:inputType="numberPassword" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText2"
android:layout_below="#+id/editText2"
android:ems="10"
android:inputType="textEmailAddress" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="#string/button_name_activity_one" />
</RelativeLayout>
secondactivity
package com.example.sharedprefrenceexample;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class Secondactivity extends Activity {
TextView result;
SharedPreferences sharedPreferences;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondactivity);
result=(TextView) findViewById(R.id.textView1);
sharedPreferences=getSharedPreferences("database", MODE_PRIVATE);
String res = null;
res=res+sharedPreferences.getString("name", "no name");
res=res+sharedPreferences.getString("phone", "no phone");
intent=getIntent();
res=res+intent.getStringExtra("email");
result.setText(res);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.secondactivity, 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);
}
}
activity_secondactivity.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="com.example.sharedprefrenceexample.Secondactivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
submit.setText("#string/button_name_activity_one");
// (...)
sharedPreferences=getSharedPreferences("#string/database_name", MODE_PRIVATE);
That syntax is only valid in XML. In Java code, to use a string reference you should use
submit.setText(R.string.button_name_activity_one);
So fix those string references and it should work.
More info: String resources # Android Developers

How to append to a String and send through intents in Android?

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>

Search for Bluetooth devices and display using android programming

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

Android get captured image meta data

I am trying to capture the latitude of an image that has been captured through the camera and stored into the ImageBox variable. Using the ExifInterface class, I am trying to get the latitude of the image and display that information into the LatitudeBox variable but my syntax is incorrect so please let me know what I am doing wrong! full Code below
MainActivity.java
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
//variables
Button CameraButton;
ImageView ImageBox;
EditText LatitudeBox, LongitudeBox;
String IMG = ImageBox.toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CameraButton = (Button) findViewById(R.id.CameraButton);
ImageBox = (ImageView) findViewById(R.id.imageView1);
LatitudeBox = (EditText) findViewById(R.id.LatitudeBox);
LongitudeBox = (EditText) findViewById(R.id.LongitudeBox);
//Camera Button Onclick
CameraButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
try {
ExifInterface exif = new ExifInterface(IMG);
LongitudeBox.setText(exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF)); // get longitude
LatitudeBox.setText(exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF)); //get latitude
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// try catch
}// end onclick
});// end camera button onclick
}// close oncreate
//display image inside ImageBox variable using bitmap
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == 0)
{
Bitmap TheImage = (Bitmap) data.getExtras().get("data");
ImageBox.setImageBitmap(TheImage);
}
}
#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;
}
}// close main
App now crashes on launch and not sure what's causing it. Here is an image of the logcat errors
http://s7.postimg.org/4p67papyj/logcat1.png
activity_main.XML
<ImageView
android:id="#+id/imageView1"
android:layout_width="250dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/CameraButton"
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="#string/Camera_Button" />
<TextView
android:id="#+id/LatitudeBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/CameraButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="#string/LatitudeBox"
android:textColor="#ff0000"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/LongitudeBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/LatitudeBox"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="#string/LongitudeBox"
android:textAppearance="?android:attr/textAppearanceLarge" />
There is no .get() method for ExifInterface.
LatitudeBox = exif.get(ExifInterface.TAG_GPS_LONGITUDE_REF);
Did you perhaps mean:
LatitudeBox.setText(exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF));
EDIT:
I found it.
Look at the last variable here:
Button CameraButton;
ImageView ImageBox;
EditText LatitudeBox, LongitudeBox;
String IMG = ImageBox.toString();
You haven't assigned a value to your ImageView ImageBox. So when you ImageBox.toString(), you receive a NullPointerException.
Change initialization of IMG to something like:
String IMG = "filename.txt";

Categories