how to connect button with next screen on android studio - java

I am building this application on android studio and i am trying to link my button with next activity which is login screen. before i had it working with register screen but then i messed up with code now it just doesn't work when i run the application and click on register button my app crashes and shuts down and login button doesn't even do anything.
below is the code for main page activity and login page activity
first i will paste frontpage activity code where the button is, then its java class, then i will paste loginpage activity and then its java class.
can someone pls advice me how to call the login activity from the login button on the front page.
thank you so much in advance
frontpage activity
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Login">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:background="#drawable/bg3"
android:gravity="center|top">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Easy Booking"
android:id="#+id/textView"
android:textSize="33dp"
android:gravity="center"
android:textColor="#0c0c0c"
/>
<Button
android:layout_width="98dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Login"
android:id="#+id/btLogin"
android:onClick="bLogin"
android:background="#null"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Register"
android:id="#+id/btRegister"
android:onClick="bRegister"
android:background="#null"
android:layout_gravity="center_horizontal" />
</LinearLayout>
now its java class
public class Frontpage extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frontpage);
//OnclickButtonListener();
}
public void bLogin(View view) {
}
public void onButtonClick(View v){
if (v.getId() == R.id.btRegister) {
Intent i = new Intent(new Intent(Frontpage.this, Register.class));
startActivity(i);
}
}
/**
public void OnclickButtonListener(){
button = (Button)findViewById(R.id.bRegister);
button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("/Users/umairfarooq/AndroidStudioProjects/Easybooking/app/src/main/res/layo ut/activity_register");
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.menu_main_activity_login, 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);
}
*/}
below is the login activity
xmlns:tools="http://schemas.android.com/tools"
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="#635b5b"
android:orientation="vertical"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login Form"
android:textAppearance="?android:textAppearanceLarge"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="Email"
android:id="#+id/etUsername"
android:layout_gravity="center_horizontal"
android:layout_marginTop="70dp"
/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="Password"
android:id="#+id/etPassword"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:inputType="textPassword"
/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Login"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:onClick="userLogin"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register Now"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:onClick="userReg"
/>
</LinearLayout>
and now login java class
package com.example.umairfarooq.easybooking;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
public class Login extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void buttonOnClick (View v){
}
}

The problem is that your activities don't contain the methods that you've set for your buttons' android:onClick attribute.
For the layout that the Frontpage activity is using, you can either change btRegister button's android:onClick attribute to android:onClick="onButtonClick" or create a public void bRegister(View v){...} method in that activity.
For the Login activity, the layout has two buttons with their android:onClick attribute set to userReg and userLogin, you can either create those methods in the activity or change both of those attributes values to buttonOnClick.

when i run the application and click on register button my app crashes
and shuts down
It is because this button:
<Button
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Register"
android:id="#+id/btRegister"
android:onClick="bRegister"
android:background="#null"
android:layout_gravity="center_horizontal" />
in your frontpage activity needs a method with signature public void bRegister(View view) in your FrontPage.java class. As you do not have this method, it crashes.
login button doesn't even do anything
The reason is, this button
<Button
android:layout_width="98dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Login"
android:id="#+id/btLogin"
android:onClick="bLogin"
android:background="#null"
android:layout_gravity="center_horizontal" />
in your frontpage activity needs a method called public void bLogin(View view) in your Frontpage.java class. Though the method is present, you do not have any code in it, hence it does not do anything.
You need to add proper code in bLogin method so that your login button starts functioning, and even before that add a bRegister method so that your register button starts working.
Hope this helps you.

Related

Fragment Activity Display Back Button

I search to know how to display back button in a class which inherit from FragmentActivity,
public class GoogleMapsActivity extends FragmentActivity
I know there is the method onBackPressed() which can be override but i don't know how to display back button
#Override
public void onBackPressed() {
}
I tried this method : https://stackoverflow.com/a/43976262/7079226 but it doesn't work,
Thanks for your answers
Try this :-
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
and for back button to work.. use this :-
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
I have same problem with you. I already use ((AppCompatActivity)getApplicationContext()).setSupportActionBar(toolbar); but always getting force close.
Then my solution is create my own toolbar in the layout
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/overflow_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
app:srcCompat="?attr/homeAsUpIndicator" />
<TextView
android:id="#+id/textToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/overflow_back"
android:paddingLeft="32dp"
android:textSize="18sp"
android:textStyle="bold"
android:layout_toRightOf="#+id/overflow_back"
android:text="Toolbar Title"
android:textColor="#android:color/white" />
</RelativeLayout> </android.support.v7.widget.Toolbar>
overflow_back for backbutton, textToolbar for title.
In your activity:
imageBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});

How to pass information between activities using rating bar

Ok, so im pretty new to android and the whole app making but im just doing like a generic app rating thing just for practice. Here are the details I have just 2 generic pictures on the main activity with a rate button beside each and a text view below them both so when you click the rate button it takes you to the second activity which has the rating bar. This is my problem in the view with the rating bar I just have the display for the rating as a toast but i want to be able to take that rating and display it inside the textview in my main activity and its posing more of a problem than i thought it was going to so im looking for a bit of help
this is the code for my main activity
package com.example.brent.appmanagement;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingapp1();
ratingapp2();
getrating();
}
private void getrating(){
}
private void ratingapp1(){
Button rateapp1 = findViewById(R.id.rateapp1);
rateapp1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, firstapp.class));
}
});
}
private void ratingapp2(){
Button rateapp2 = findViewById(R.id.rateapp2);
rateapp2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, secondapp.class));
}
});
}
}
and this is the code for the activity with the rating bar
public class firstapp extends AppCompatActivity {
public RatingBar ratingBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firstapp);
ratingBar = findViewById(R.id.ratingBar);
return1();
}
public void rateMe(View view){
Toast.makeText(getApplicationContext(),
String.valueOf(ratingBar.getRating()),
Toast.LENGTH_LONG).show();
}
private void return1(){
Button returntomain1 = findViewById(R.id.returntomain1);
returntomain1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(firstapp.this, MainActivity.class));
}
});
}
}
also I have the XML which i dont know if you need to see but here it is
main activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="com.example.brent.appmanagement.MainActivity">
<Button
android:id="#+id/rateapp1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="96dp"
android:layout_marginTop="124dp"
android:text="Rate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/rateapp2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="176dp"
android:layout_marginEnd="96dp"
android:text="Rate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="96dp"
android:layout_marginTop="112dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/ic_launcher" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="164dp"
android:layout_marginStart="96dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/savedrating1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="96dp"
android:layout_marginTop="12dp"
android:text=""
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView3" />
<TextView
android:id="#+id/savedrating2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="96dp"
android:layout_marginTop="12dp"
android:text=""
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView4" />
</android.support.constraint.ConstraintLayout>
and the second activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="com.example.brent.appmanagement.firstapp">
<Button
android:id="#+id/returntomain1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:text="Return to main menu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/rate_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Rate Me"
android:textSize="18dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/rate_me"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:numStars="5"
android:stepSize="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rate_me" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ratingBar"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="rateMe"
android:text="Submit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ratingBar" />
</android.support.constraint.ConstraintLayout>
I would like to have the rating from the rating bar inside firstactivity be displayed in the textview savedrating1 inside my main activity
thanks all in advance for the help
for primitive values you can stuff them inside the intent using key value pairsIntent intent =new Intent(); String myValue="something";intent.putExtra("keyMyValue",myValue);startActivity(intent); for passing custom objects use parcelable and stuff the parcelable into the intent as above or here

Fragment replacing returns NullPointerException

There are many topics on this subject, but I couldn't find any solution that helped my case.
IDEA BEHIND:
What I have is a ViewPager, for swipping between 3 fragments. That works. But in one of the mentioned fragments, I do some functionalities - which then I check in another one of those before mentioned Fragments. Everything works.
Then, when I do the check, I open an AlertDialog, telling the user that his configuration is succesfull. By button click, I want that app transfers the user to one different Fragment (not one of the ViewPager fragments).
CODE SNIPPET - FragmentTwo:
public void displayAlertSuccess(Context ctx) {
new AlertDialog.Builder(ctx)
.setTitle("ACCESS GRANTED!")
.setMessage("Congratulations!")
.setPositiveButton("Great!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with
replaceFragment();
}
})
.setNegativeButton("Format my Card", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mainActivity.setNFC_ACTION("FORMAT");
}
})
.setIcon(android.R.drawable.ic_dialog_info)
.show();
}
private void replaceFragment() {
Fragment frag = new CardDesfire1();
getFragmentManager().beginTransaction().replace(R.id.content_frame, frag).commit();
}
CODE SNIPPET - XML Layouts:
I. activity_holder.xml
<?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">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
II. activity_access.xml
<!-- activity_screen_slide.xml -->
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
III. fragment_access_slide2.xml (fragment that has the Dialog)
<!-- fragment_screen_slide_page.xml -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0080ff">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/imageButton"
android:src="#drawable/img_access_slide2_2"/>
</ScrollView>
IV. activity_desfire1.xml (Fragment that I want to launch onClick)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#drawable/bg_des2">
<Button
android:layout_width="fill_parent"
android:layout_height="25dp"
android:text="You have discovered..."
android:id="#+id/btn_des1_discovered"
android:background="#3A5FCD"
android:typeface="monospace"
android:textColor="#ffffffff"
android:enabled="false"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/btn_loy"
android:text="Loyalty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttons_shape"
android:shadowColor="#000000"
android:textColor="#FFFFFF"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_above="#+id/btn_pay"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textAllCaps="false"
android:visibility="invisible"/>
<Button
android:id="#+id/btn_pay"
android:text="µPay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttons_shape"
android:shadowColor="#000000"
android:textColor="#FFFFFF"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:textAllCaps="false"
android:layout_alignParentBottom="true"
android:layout_marginBottom="70dp"
android:layout_centerHorizontal="true"
android:visibility="invisible"/>
<Button
android:id="#+id/btn_acc"
android:text="Access"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttons_shape"
android:shadowColor="#000000"
android:textColor="#FFFFFF"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:textAllCaps="false"
android:layout_above="#+id/btn_pay"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:visibility="invisible"/>
<ImageView
android:layout_width="wrap_content"
android:visibility="invisible"
android:layout_height="wrap_content"
android:id="#+id/img_card_des1"
android:layout_below="#+id/btn_des1_discovered"
android:src="#drawable/img_card_des1"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:visibility="invisible"
android:textSize="18dp"
android:gravity="center"
android:text="#string/des1_desc"
android:id="#+id/tv_des1_desc"
android:textColor="#ffffffff"
android:textStyle="italic"
android:background="#ff8db6cd"
android:layout_below="#+id/img_card_des1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp" />
</RelativeLayout>
LINE OF ERROR:
getFragmentManager().beginTransaction().replace(R.id.content_frame, frag).commit();
Error Description from LogCat:
java.lang.NullPointerException
Error occurs when:
My error occurs when the user clicks the button "Great" in AlertDialog, which should then invoke the Fragment replacement.
I have tried:
I have tried to fix it with the getChildFragment , but then I get different kind of error: IllegalStateException : Activity has been destroyed.
Any help is kindly appreciated.
Have you declared default constructor in your CardDesfire1 fragment. If not then declare it
public CardDesfire1(){}
Make your parent Activity extend FragmentActivity
class ParentActivity extends FragmentActivity
The problem occurs because you call getFragmentManager() in a Fragment, you should delegate displaying the dialog to the activity.
Make activity implement an interface, and have the fragment call this interface method when it's time to show the dialog.
Example: You define a new inner interface in FragmentTwo(this code goes in FragmentTwo):
public interface OnReplaceFragmentListener {
void onReplaceFragment();
}
You link your activity to the fragment(this code goes in FragmentTwo):
private OnReplaceFragmentListener mCallback;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnReplaceFragmentListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnReplaceFragmentListener");
}
}
private void replaceFragment() {
mCallback.onReplaceFragment();
}
Then make activity implement OnReplaceFragmentListener.
And(this code goes into FragmentTwo's hosting activity):
#Override
void onReplaceFragment() {
Fragment frag = new CardDesfire1();
getFragmentManager().beginTransaction().replace(R.id.content_frame, frag).commit();
}

Moving buttons with animation and changing their position afterwards

I am learning Java and Android SDK. I want to create a simple application that is swapping 2 buttons so the left one is moving to the place of right one and v.v. with simple animation. I have tried the ObjectAnimator because I have read that it is moving the views objects permanently. But it's not :-( The objects stays there I mean the left one on the right and v.v. but their getLeft(), getTop() values are the same, and after next animations starts the objects are returning to the start position immediately. I have read that the ObjectAnimator needs some additional function to work properly but in the documentation there is no example :-( I have tried to add the setTranslationX function but it hasn't worked. Could somebody provide me with some simple example of how to do this?
http://developer.android.com/guide/topics/graphics/prop-animation.html#object-animator
"The object property that you are animating must have a setter function (in camel case) in the form of set(). Because the ObjectAnimator automatically updates the property during animation, it must be able to access the property with this setter method. For example, if the property name is foo, you need to have a setFoo() method. If this setter method does not exist, you have three options:
Add the setter method to the class if you have the rights to do so.
Use a wrapper class that you have rights to change and have that wrapper receive the value with a valid setter method and forward it to the original object.
Use ValueAnimator instead."
Thanks in advance.
try this code:
public class ActivityStartup extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("hello");
setContentView(R.layout.main);
SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.SLIDING_WINDOW);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setBehindOffset(100);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
View view = G.layoutInflater.inflate(R.layout.menu, null);
view.findViewById(R.id.layout_crop).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(G.context, "Crop Clicked", Toast.LENGTH_SHORT).show();
}
});
view.findViewById(R.id.img_logo).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://example.com"));
startActivity(browserIntent);
}
});
menu.setMenu(view);
}
}
public class G extends Application {
public static LayoutInflater layoutInflater;
public static Context context;
#Override
public void onCreate() {
super.onCreate();
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
context = getApplicationContext();
}
}
main.xml is:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
menu.xml is:
<ImageView
android:id="#+id/img_logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="8dip"
android:scaleType="centerInside"
android:src="#drawable/hlogo" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#00ff00"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:id="#+id/layout_crop"
android:layout_width="match_parent"
android:layout_height="48dip"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dip"
android:scaleType="centerInside"
android:src="#android:drawable/ic_menu_crop" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Crop"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_day"
android:layout_width="match_parent"
android:layout_height="48dip"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/ImageView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dip"
android:scaleType="centerInside"
android:src="#android:drawable/ic_menu_day" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_delete"
android:layout_width="match_parent"
android:layout_height="48dip"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/ImageView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dip"
android:scaleType="centerInside"
android:src="#android:drawable/ic_menu_delete" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:textColor="#000000" />
</LinearLayout>

how to link the image button to a facebook page in android

i am new to android ....
i have designed a view where i have created 4 image buttons
in that one has to go to facebook url
second has to go twitter url
third has to go to youtube url etc like that
fourth to linkedin
but i dont know how to configure the button to send a url and the page must inside the same design view shown below
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/apple" >
<Button
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2C3539"
android:gravity="center"
android:text="#string/apple"
android:textColor="#FFFFFF"
android:textSize="22sp" />
<Button
android:id="#+id/video"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/music"
android:background="#2C3539"
android:text="#string/video"
android:textColor="#FFFFFF"
android:textColorHint="#color/black"
android:textSize="20sp" />
<Button
android:id="#+id/music"
android:layout_width="160dp"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#2C3539"
android:text="#string/music"
android:textColor="#FFFFFF"
android:textColorHint="#color/black"
android:textSize="20sp" />
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/main"
android:layout_marginLeft="41dp"
android:layout_marginTop="72dp" >
</TableLayout>
<ImageButton
android:id="#+id/twitter1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/facebook1"
android:src="#drawable/twitter" />
<ImageButton
android:id="#+id/facebook1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/main"
android:src="#drawable/facebook" />
<ImageButton
android:id="#+id/youtube1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/twitter1"
android:src="#drawable/youtube" />
<ImageButton
android:id="#+id/instagram1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/youtube1"
android:src="#drawable/instagram_icon" /></RelativeLayout>
the above code looks like this if i click on facebook image then it has to populate this link
https://www.facebook.com/apple.fruit
And link has to populate onto body (shown in image)
can anyone write the backend code how to use the button
i have created one java file
package com.lay.background;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class AppleActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.apple);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
} }
Can anyone please resolve the code if any queries please comment
for each button you can pass the intent with specific URL like this
Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.facebook.com/apple.fruit"));
startActivity(browserIntent);
For example if your button is facebookButton then use it in your Activity like this:
ImageButton facebookButton = (ImageButton)findViewById(R.id.facebook1);
facebookButton.setOnClickListener(new View.onClickListener()
{
public void onClick(View arg0)
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.facebook.com/apple.fruit"));
startActivity(browserIntent);
}
});
other way would be loading your URL into a WebView.
myWebView.loadUrl(http://"+tempUrl);
As for the missing "http://" I'd just do something like this:
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
Adding WebView in xml layout file:
<WebView android:id="#+id/webview"
android:layout_width="fill_parent" android:layout_height="50dip"/>
Add the WebView content with your Url in MainActivity :
// Enable javascript for the view
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(this, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://www.facebook.com/apple.fruit");
try this.
first, add Webview in you layout file that visible is gone.
if button is clicking, set webview visible is visible, and using webview is connect you want connect url.

Categories

Resources