Unable to access whatsapp and fb from another android app - android

I want to develop a simple android app that has text view and few buttons.I have put 4 buttons on my app 2 whatsapp and 2 facebook.I want that whenever a person clicks on whatsapp button the text in text view is forwarded to contacts on whatsapp whoever user wants to send message to.Similarly I want that user is able to send private message in fb to his friends.The message would be the same as the text in textview.My MainActivity.java file is below
package com.example.shalabh.gratbites;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public TextView textView;
public TextView textView2;
public Button button;
public Button button2;
public Button button3;
public Button button4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickListenerButton2()
{
textView=(TextView)findViewById(R.id.textView);
button2=(Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String fb = textView.getText().toString();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, fb);
startActivity(Intent.createChooser(shareIntent,"Share gratitude"));
}
});
}
public void onClickListenerButton3()
{
textView2 = (TextView)findViewById(R.id.textView2);
button3= (Button)findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String whatsAppMessage = textView2.getText().toString();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
sendIntent.setType("text/plain");
// Do not forget to add this to open whatsApp App specifically
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
}
});
}
public void onClickListenerButton()
{
textView= (TextView)findViewById(R.id.textView);
button= (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String whatsAppMessage = textView.getText().toString();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
sendIntent.setType("text/plain");
// Do not forget to add this to open whatsApp App specifically
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
}
});
}
}
My main_activity.xml file is
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#ffffff">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="At times our own light goes out and is rekindled by a spark from another person. Each of us has cause to think with deep gratitude of those who have lighted the flame within us. "
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_marginTop="38dp"
android:editable="false"
android:elegantTextHeight="false"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Whatsapp"
android:id="#+id/button"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignRight="#id/button"
android:layout_alignTop="#+id/button"
android:text="Facebook" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="I wanted to say thanks... and share my gratitude for everything I&apos;ve been blessed with. Family, friends, and continued support from everyone."
android:id="#+id/textView2"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="74dp"
android:layout_alignRight="#+id/button2"
android:layout_alignEnd="#+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="whatsapp"
android:id="#+id/button3"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Facebook"
android:id="#+id/button4"
android:layout_alignTop="#+id/button3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
The problem is that all the buttons are unresponsive.I am unable to understand the reason.Kindly help me find solution to my problem if possible suggest alternative approach.
P.S. I have not done coding for button4

You have not call methods which you have created. So onClickListeners are not set. So just call this method in onCreate just like this and you are good to go.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onClickListenerButton2();
onClickListenerButton3();
onClickListenerButton();
}

Related

How to edit and get the updates of the editText input fields from another activity?

I'm trying to play around with two activities. Edit and View activity. I would like to get the inputs from the edit activity and show in the view activity. In the edit activity I have the ok/submit button, which approves the changes and take back to the view activity, in this case the input text fields should be updated with the entered data. If the cancel button is pressed, then obviously no changes being done and the user is being taken back to the view activity.
I've most of the implementations done right, but I can't get the entered data to be shown on the view activity. What am I missing?
This is my codes for edit and view activities.
ViewActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ViewActivity extends AppCompatActivity {
public static final String EXTRA_FNAME = "EXTRA_TEXT";
public static final String EXTRA_LNAME = "EXTRA_TEXT";
public static final String EXTRA_EMAIL = "EXTRA_TEXT";
String Fname, Lname, email;
EditText FNInput, LNInput, emailInput;
Button editButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
FNInput = (EditText) findViewById(R.id.FNInput);
LNInput = (EditText) findViewById(R.id.LNInput);
emailInput = (EditText) findViewById(R.id.emailInput);
editButton = (Button) findViewById(R.id.okButton);
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openEditActivity();
}
});
Fname = FNInput.getText().toString();
Lname = LNInput.getText().toString();
email = emailInput.getText().toString();
}
public void openEditActivity(){
Intent intent = new Intent(this, EditActivity.class);
intent.putExtra(EXTRA_FNAME, Fname);
intent.putExtra(EXTRA_LNAME, Lname);
intent.putExtra(EXTRA_EMAIL, email);
startActivity(intent);
}
}
EditActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class EditActivity extends AppCompatActivity {
public static final String EXTRA_FNAME = "EXTRA_TEXT";
public static final String EXTRA_LNAME = "EXTRA_TEXT";
public static final String EXTRA_EMAIL = "EXTRA_TEXT";
String Fname, Lname, email;
EditText FNInput, LNInput, emailInput;
Button okButton, cancelButton;
private static final String TAG = "EditActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
FNInput = (EditText) findViewById(R.id.FNInput);
LNInput = (EditText) findViewById(R.id.LNInput);
emailInput = (EditText) findViewById(R.id.emailInput);
okButton = (Button) findViewById(R.id.okButton);
cancelButton = (Button) findViewById(R.id.cancelButton);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
updateViewActivity();
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FNInput.setText("");
LNInput.setText("");
emailInput.setText("");
finish();
}
});
}
public void updateViewActivity(){
Fname = FNInput.getText().toString();
Lname = LNInput.getText().toString();
email = emailInput.getText().toString();
FNInput.setText(Fname);
LNInput.setText(Lname);
emailInput.setText(email);
Intent intent = new Intent(this, ViewActivity.class);
intent.putExtra(EXTRA_FNAME, Fname);
intent.putExtra(EXTRA_LNAME, Lname);
intent.putExtra(EXTRA_EMAIL, email);
startActivity(intent);
}
}
activity_view.xml
<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=".ViewActivity">
<LinearLayout
android:layout_width="270dp"
android:layout_height="374dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="57dp"
android:layout_marginTop="75dp"
android:orientation="vertical">
<EditText
android:id="#+id/FNInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="First Name"
android:inputType="textPersonName" />
<EditText
android:id="#+id/LNInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Last Name"
android:inputType="textPersonName" />
<EditText
android:id="#+id/emailInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="info#mail.com"
android:inputType="textEmailAddress" />
<Button
android:id="#+id/okButton"
android:layout_width="153dp"
android:layout_height="wrap_content"
android:text="Edit" />
</LinearLayout>
<TextView
android:id="#+id/viewTV"
android:layout_width="134dp"
android:layout_height="33dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="135dp"
android:layout_marginBottom="18dp"
android:text="View Activity"
tools:layout_editor_absoluteX="15dp"
tools:layout_editor_absoluteY="687dp" />
</RelativeLayout>
activity_edit.xml
<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=".EditActivity">
<LinearLayout
android:layout_width="270dp"
android:layout_height="374dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="57dp"
android:layout_marginTop="75dp"
android:orientation="vertical">
<EditText
android:id="#+id/FNInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="First Name"
android:inputType="textPersonName" />
<EditText
android:id="#+id/LNInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Last Name"
android:inputType="textPersonName" />
<EditText
android:id="#+id/emailInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="info#mail.com"
android:inputType="textEmailAddress" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/okButton"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:textColor="#03A9F4" />
<Button
android:id="#+id/cancelButton"
style="#style/Widget.AppCompat.Button.ButtonBar.AlertDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:textAlignment="center" />
</TableRow>
</LinearLayout>
<TextView
android:id="#+id/viewTV"
android:layout_width="108dp"
android:layout_height="38dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="169dp"
android:layout_marginBottom="18dp"
android:text="Edit Activity" />
</RelativeLayout>
I'm sorry this post is going to be lengthy.
When you want to do something like this, you have to follow some steps and follow them properly. which are:-
First, making yourself clear what you want to do exactly, that mean your goal.
Secondly, try to understand what you should do to achieve that goal, like - what thing might have need to do that, resources, tutorial (for this scenario) etc.
Finally let's start searching and learn how to do that.
Here, I can tell you where the problem is, you started learning but didn't completed the learning. I can see you just copied and pasted into two different activities without a reason.
Well, I am sharing what problems I found out from the above code of yours :-
Your ViewActivity.java should be consist of some TextView where you're intended to show your data from your EditActivity.java, which is not there.
You're sending Data with same key every time (another proof of copy pasting, not knowing what is happening) which is -
public static final String EXTRA_FNAME = "EXTRA_TEXT"; // use it as EXTRA_FNAME public static final String EXTRA_LNAME = "EXTRA_TEXT"; // use it as EXTRA_LNAME public static final String EXTRA_EMAIL = "EXTRA_TEXT"; // use it as EXTRA_EMAIL
When you are sending data to your view activity, you need to receive what you were sending by using getIntent() something like :- String s = getIntent().getStringExtra("EXTRA_FNAME"); which will return the value assigned to this key from your previous activity while sending to the present activity.
After receiving the desired value populate your TextView in the next line like this :- textView.setText(s); // fetched from getIntent() previously
For more information you can check this tutorial, which has showed how to pass and view data from one activity to another. Hope you understand.

very slow performance when i click a button on my android app

when i testing it on my mobile (huawei G8) in vertical view it takes like 5 second to click on any of this 4 buttons in my app
also it leave dark spot in the button for second or 2
in horizontal view it works faster
in emulator it also works fine
java file
package com.example.kemo.videoplayer;
import android.content.Context;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import android.widget.ImageButton;
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
final ImageButton facebook1 = (ImageButton) findViewById(R.id.imageButton);
facebook1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here image 1
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com"));
startActivity(browserIntent);
}
});
final ImageButton youtube1 = (ImageButton) findViewById(R.id.imageButton2);
youtube1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here image 1
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com"));
startActivity(browserIntent);
}
});
final Button button = (Button) findViewById(R.id.btnabout);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here
Intent intent = new Intent(getBaseContext(), About.class);
startActivity(intent);
}
});
final Button button1 = (Button) findViewById(R.id.btnaikido);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here
Intent intent = new Intent(getBaseContext(), Aikido.class);
startActivity(intent);
}
});
final Button button2 = (Button) findViewById(R.id.btnv);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here
Intent intent = new Intent(getBaseContext(), MainActivity.class);
startActivity(intent);
}
});
final Button button3 = (Button) findViewById(R.id.btnpic);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here
Intent intent = new Intent(getBaseContext(), Gview.class);
startActivity(intent);
}
});
}
}
XML file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout 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:id="#+id/activity_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.example.kemo.videoplayer.Home">
<ImageView
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/imageView3"
android:layout_height="140dp"
android:scaleType="fitXY"
android:background="#drawable/aikidobanner" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnabout"
android:layout_margin="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/btnaikido"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="#string/aikido" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnaikido"
android:layout_margin="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/btnv"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="#string/videos" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnv"
android:layout_margin="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/btnpic"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="#string/gallery" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnabout"
android:text="#string/about"
android:layout_below="#+id/imageView3"
android:layout_margin="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/imageButton"
app:srcCompat="#drawable/facebook"
android:layout_width="120dp"
android:layout_height="60dp"
android:scaleType="fitXY"
android:layout_marginTop="10dp"
android:layout_marginRight="25dp"
android:layout_marginLeft="50dp"
android:background="#android:color/transparent" />
<ImageButton
android:id="#+id/imageButton2"
app:srcCompat="#drawable/youtube"
android:layout_width="120dp"
android:layout_height="60dp"
android:scaleType="fitXY"
android:layout_marginTop="10dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="50dp"
android:background="#android:color/transparent" />
</LinearLayout>
</LinearLayout>
</ScrollView>
It's hard to know for sure without seeing the rest of the app, but this sounds like the new Activities that you are launching are probably doing too much work in their onCreate methods, or doing something heavy-weight in some other method on the UI thread during startup.
From the code you've shown, I think that it's not very likely that it's anything in your Home activity, unless that Activity has something intensive going on in it's UI thread.
Cannot find any obvious wrong from the code you have shown. So If there's no more other code there and problem still exist, I suggest that you should find it through a process of elimination. For example, delete all listener in onCreate and add just one, then keep trying.

OnClickListener on non-Mainactivity & change first activity

i've a problem. I want to make a app with a loggin activity and a main activity. (To the OnClickListiner later)
fist:
What ive done so far:
i've created a login. java and login.xml
login.java:
public class Login extends Activity implements OnClickListener {
Button btnStartAnotherActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
boolean hasLogedIn = true;
if (hasLogedIn) {
Intent i = new Intent(Login.this, MainActivity.class);
startActivity(i);
finish();
} else {
}
}
public void onClick(View view) {
//calling an activity using <intent-filter> action name
Intent inent = new Intent("android.name.MainActivity ");
startActivity(inent);
}
}
i've created a MainActivity.java and activity_main.xml
moreover i've some other java and xml files to make a materiel designed Tab view for my MainActivity.
i have 3 tabs thats how it looks so far
[![tab1 with buttons][1]][1]
-now i'be added to the first tab 3 buttons.
example of one button: (the others are the same just a other id )
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kommt"
android:clickable="false"
android:textColor="#190707"
android:id="#+id/kommt"
android:layout_alignParentStart="true"
android:layout_below="#+id/space" />
-So the app is running now without problems. ( All Tabs and there content is showing like i want it )
when i build the project the mainactivity is open first( see that link of the picture )
HOW TO SET: the login interface to be started once, when starting the app for the first time. After login is succesfully, then the mainactivity will always open. Thats my first problem. What should i add to the Login.java ? and how to set that the login.xml starts before the mainactivity ?
Second:
As i told you ive added some buttons. To test the buttons i've tried to implement a code for toast notification when clicking on button. But every time i build the project with the toast notifitcation code, the app doenst start anymore. Here the code for the toast notification i'm using:
public class MainActivity extends ActionBarActivity implements OnClickListener {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // but my main_activity doesnt have buttons ... tab1.xml have b
Button kommtbutton;
kommtbutton= (Button) findViewById(R.id.kommt);
kommtbutton.setOnClickListener(this);}
#Override
public void onClick(View v) {
setContentView(R.layout.tab1); // not sure if this is right ive did it cause the buttons are in the tab1 layout and not main_activity
switch(v.getId())
{
case R.id.kommt:
{
Toast toast1 = Toast.makeText(getApplicationContext(),
"Eingestempelt",
Toast.LENGTH_SHORT);
toast1.show();
break;
} ....}
here i have implemented all 3 buttons in a switch case.
It looks right but the onclicklistinier seems to kill my application before it can start. Maybe someone can help me.
i have following files:
Login.java, MainActivity.java, Tab1.java,Tab2.java,Tab3.java, SlidingTabsLayout.java, SlidingTabStrip.java and ViewPagerAdaper.
and i have this layouts.
acitivy_main.xml, login.xml, tab1,tab2,tab3.xml, toolbar.xml.
I'm not allowed to send pictures cause i'm new here.
Where have i do implement the code for the toast notification ?
thats my activity_main:xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<android_package.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="#color/ColorPrimary"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"/>
and this is my tab1.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Tabs"
android:background="#FDFDFE"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/buchungen"
android:textColor="#190707"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Geht"
android:clickable="false"
android:textColor="#190707"
android:id="#+id/geht"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/textClock"
android:layout_marginBottom="36dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name:"
android:paddingBottom="7dp"
android:paddingTop="7dp"
android:textColor="#190707"
android:id="#+id/name"
android:layout_alignParentStart="true"
android:layout_below="#+id/buchungen" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#190707"
android:paddingBottom="7dp"
android:paddingTop="7dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Status:"
android:id="#+id/status"
android:layout_below="#+id/name"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#190707"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Letzte Buchung:"
android:paddingBottom="4dp"
android:paddingTop="7dp"
android:id="#+id/letzteBuchung"
android:layout_below="#+id/status"
android:layout_alignParentStart="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_alignBottom="#+id/buchungen"
android:weightSum="1"
android:id="#+id/linearLayout">
<ImageView
android:layout_width="314dp"
android:layout_height="310dp"
android:id="#+id/profilbild"
android:layout_gravity="center_horizontal"
android:src="#drawable/time" />
</LinearLayout>
<Space
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_above="#+id/geht"
android:id="#+id/space" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#190707"
android:id="#+id/textClock"
android:layout_alignTop="#+id/name"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kommt"
android:clickable="false"
android:textColor="#190707"
android:id="#+id/kommt"
android:layout_alignParentStart="true"
android:layout_below="#+id/space" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Pause An/Aus"
android:textColor="#190707"
android:id="#+id/textView"
android:layout_alignTop="#+id/textView3"
android:layout_alignStart="#+id/pause" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Einstempeln"
android:textColor="#190707"
android:id="#+id/textView2"
android:layout_above="#+id/kommt"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Ausstempeln"
android:textColor="#190707"
android:id="#+id/textView3"
android:layout_alignBottom="#+id/geht"
android:layout_alignStart="#+id/geht"
android:layout_alignTop="#+id/textView2" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:textColor="#190707"
android:id="#+id/pause"
android:layout_alignBottom="#+id/kommt"
android:layout_centerHorizontal="true"
android:checked="false" />
</RelativeLayout>
The buttons are in the tab1.xml. Where have i to acces them to make a Interaction ( show toast when pressing the button ) ? in the MainActivity.java or the Tab1.java or somewhere else ?
When i try to add toast notification my app just kills itself ...
You have to put all the findViewById and setContentView in the onCreate method or they won't do the job.
First point :
public class MainActivity extends ActionBarActivity
{
// Layout elements
private Button kommtbutton = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Attach layout
setContentView(R.layout.activity_main); // but my main_activity doesnt have buttons ... tab1.xml have b
// Retrieve layout elements
kommtbutton= (Button) findViewById(R.id.kommt);
// Attach listeners
kommtbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view)
{
// Do not use getApplicationContext(), this is an activity
Toast.makeText(MainActivity.this, "Eingestempelt", Toast.LENGTH_SHORT).show();
}
});
}
[...]
}
Second point :
public class LoginActivity extends Activity
{
// Layout elements
private EditText edit_login = null;
private EditText edit_password = null;
private Button btn_login = null;
// Class variables
private SharedPreferences prefs = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Check if the user is already logged in
prefs = getSharedPreferences(getPackageName(), MODE_PRIVATE);
if (prefs.getBoolean("isLoggedIn", false))
{
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
return;
}
// Attach layout
setContentView(R.layout.login);
// Retrieve layout elements
edit_login = (EditText) findViewById(R.id.edit_login);
edit_password = (EditText) findViewById(R.id.edit_password);
btn_login = (Button) findViewById(R.id.btn_login);
// Attach listeners
btn_login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view)
{
// Retrieve information
String login = edit_login.getText().toString();
String password = edit_password.getText().toString();
// Do job
boolean canConnect = true; // TODO
if (canConnect)
{
// Update prefs
prefs.edit().putBoolean("isLoggedIn", true).commit();
// Move to activity
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
else
{
// Update prefs
prefs.edit().putBoolean("isLoggedIn", false).commit();
// Display error message
Toast.makeText(LoginActivity.this, "Wrong crendentials", Toast.LENGTH_LONG).show();
}
}
});
}
}

how to assign a string value associated with an image to a textwidget when image is clicked

package com.me.trial;
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class TrialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setClickable(true);
OnClickListener l;
}
private void hasBeenClicked(<method invoked when the user has clicked>){
}
}
<?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" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<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="56dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="64dp" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitEnd"
android:tag="Employee name"
android:src="#drawable/img18" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
I need the application to pass some string value associated to it to be passed into the text as soon as the user clicks on the image.How do i use the click Listeners in android to achieve the following task.
It is not necessary to fill in the code snippet that i have given.Any new ideas regarding the design are most welcome.
U can set text as Tag to the Image so that u can get that text onClick event. Tag can be set either in XML or dynamically.
public class TrialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setTag("YOur text");
img.setClickable(true);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String urText= arg0.getTag().toString();
EditText edt = (EditText)findViewById(R.id.editText1);
edt.setText(urText);
}
});
}
}
This code demonstrates how to achieve your requirement dynamically.

Adding text from edit text field into an email

I have an app and want to have a contact us for where the user inputs their name, address, phone number, and a comment section. Then they will click on compose mail button and it will load the text into the email automaticlly. have some of the code worked out but not sure how to get the text from the edit text into my email msg. anyone have any suggestions on how i can do this. I have attached the code from my xml file and my java file.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Contactform extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contactform);
Button email = (Button) findViewById(R.id.button30);
email.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//TODO Auto-generated method stub
Intent email = new Intent(android.content.Intent.ACTION_SEND);
/* Fill it with Data */
email.setType("plain/text");
email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"ladsoffice#lads-to-leaders.org"});
email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Lads to Leaders/Leaderettes Questions and/or Comments");
email.putExtra(android.content.Intent.EXTRA_TEXT, "Sent from the Lads to Leaders/Leaderettes Android App");
/* Send it off to the Activity-Chooser */
startActivity(Intent.createChooser(email, "Send mail..."));
}
});
}
}
and here is the xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#bf311a" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:text="#string/name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_marginLeft="37dp"
android:layout_toRightOf="#+id/textView1"
android:inputType="textPersonName" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1"
android:layout_marginTop="35dp"
android:text="#string/addy"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignLeft="#+id/editText1"
android:layout_alignParentRight="true"
android:inputType="textPostalAddress" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText2"
android:layout_marginTop="30dp"
android:textColor="#000000"
android:text="#string/cell"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignLeft="#+id/editText2"
android:inputType="phone" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:textColor="#000000"
android:text="#string/questions"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView4"
android:layout_marginTop="25dp"
android:inputType="textMultiLine" />
<Button
android:id="#+id/button30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText4"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp"
android:background="#drawable/composemail" />
</RelativeLayout>
and here is a ss of what my form looks like if that helps
Thank you for any help you can give. Still learning a bit on how to do some of this stuff so i apologize if this has been asked before but been searching for two days and can't seem to find what i need to do this.
Ok for those who want to know here is the corrected code. The only code i needed to modify was the code for the Java file so thats the only code i will post.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Contactform extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contactform);
final EditText name = (EditText) findViewById(R.id.editText1);
final EditText addy = (EditText) findViewById(R.id.editText2);
final EditText cell = (EditText) findViewById(R.id.editText3);
final EditText questions = (EditText) findViewById(R.id.editText4);
Button email = (Button) findViewById(R.id.button30);
email.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//TODO Auto-generated method stub
Intent email = new Intent(android.content.Intent.ACTION_SEND);
/* Fill it with Data */
email.setType("plain/text");
email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"ladsoffice#lads-to-leaders.org"});
email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Lads to Leaders/Leaderettes Questions and/or Comments");
email.putExtra(android.content.Intent.EXTRA_TEXT,
"name:"+name.getText().toString()+'\n'+"address:"+addy.getText().toString()+'\n'+"phone:"+cell.getText().toString()+'\n'+'\n'+questions.getText().toString()+'\n'+'\n'+"Sent from the Lads to Leaders/Leaderettes Android App.");
/* Send it off to the Activity-Chooser */
startActivity(Intent.createChooser(email, "Send mail..."));
}
});
}
}
Ok so just a quick explaintion. you have to declare your edit text in the code after your oncreate method. It must say final in front of it as well. Then you input your strings into the intent for EXTRA TEXT. also i added the '\n' in single quotes. this action will act as an enter button giving each item its own line so for example i only wanted name on one line so i did name+mystring+'\n'. this will make your name and string appear on one line and then go down one line for the next line. Hope this makes since and it helps someone else out.
You can put all data inside email.putExtra(android.content.Intent.EXTRA_TEXT, "Sent from the Lads to Leaders/Leaderettes Android App");
First Get all EditText by ID here in oncreate Like
EditText name = (EditText) findViewById(R.id.editext1);
EditText address = (EditText) findViewById(R.id.editext2);
EditText phone = (EditText) findViewById(R.id.editext3);
Then put all data
email.putExtra(android.content.Intent.EXTRA_TEXT,
"name:"+name.getText().toString()+"address:"+address.getText().toString()+"phone:
"+phone.getText().toString());
Enjoy :)

Categories

Resources