How to display my facebook page in my app - android-studio

I know how to get particular data like posts in my app.But, I want to display my entire facebook page in my app like this :
Thank you in advance :)

You can do this by using WebView.
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"
tools:context=".MainActivity" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
</RelativeLayout>
MainActivity.java
package com.example.webview;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webView1);
mywebview.loadUrl("http://www.facebook.com/");
}
}

Related

Android Studio Changing TextView using a Button

I want to change the text in message when the button is clicked to "Hello World!".The idea would be that it would use a method inorder to change the text from a blank TextView into one that has writing on.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="Button"
tools:layout_editor_absoluteX="158dp"
tools:layout_editor_absoluteY="282dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.clickmeapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View message){
message.text="Hello World!";
}
}
Any help in solving this issue would be amazing.
In my code, when you click the button, you will see the "Hello World!" text.
xml
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//add this code
//Button Click
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Set TextView
setMessage("Hello World!");
}
});
}
public void setMessage(String message){
findViewById(R.id.message).setText(message);
}

android studio: everything correct on an activity but lunching it cause malfunctioning

I'm working on an android application, i added a new activity, that activity launches when i click on some button from my app, the IDE (android studio) isn't showing any errors, except that the name of the java class and the XML file i added is red (more like orange), nothing goes wrong till i click the button that launches the activity I'm talking about, it makes the app shut down.
here's my XML and java class that i added:
activity_bbel.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"
android:fitsSystemWindows="true"
android:background="#drawable/background"
tools:context="molfix.dev.molfix.Activities.Menu.BBEstLa.BBELActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:background="#91d0f0"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#drawable/toolbar_bb_e_l"
android:scrollbars="none"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:weightSum="1">
<Button
android:layout_width="19dp"
android:background="#drawable/button_back"
android:layout_height="35dp"
android:id="#+id/b_back"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<Button
android:layout_width="250dp"
android:layout_height="55dp"
android:background="#drawable/button_conseils"
android:id="#+id/b_conseils"
android:layout_marginTop="125dp"
android:layout_below="#+id/appbar"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="250dp"
android:layout_height="55dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#drawable/button_carnet_sante"
android:id="#+id/b_carnet_sante"
android:layout_marginTop="20dp"
android:layout_below="#+id/b_conseils"
android:layout_centerHorizontal="true" />
BBELActivity.java
package molfix.dev.molfix.Activities.Menu.BBEstLa;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import molfix.dev.molfix.Activities.Menu.BBEnRoute.BBERActivity;
import molfix.dev.molfix.R;
/**
* Created by AminLRoy on 21-Jul-17.
*/
public class BBELActivity extends AppCompatActivity{
Button b_back;
String pseudo;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bbel);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
Intent intent = getIntent();
pseudo = intent.getExtras().getString("pseudo");
b_back =(Button) findViewById(R.id.b_back);
b_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BBELActivity.this.finish();
}
});
}
}
Any ideas about how to solve this?
I found the solution, the problem was that the new Activity added wasn't mentioned in the manifest file.

java.lang.RuntimeException: Unable to start activity ComponentInfo and calling of another activity in Android Studio

I'm new to android development and trying to create a simple register application but the application crash every time I click the Register Here TextView. Other than that, I code the login button to try the calling of another activity but it has no respond after I click the button. I tried so hard to find the mistakes but I'm not sure that what I missed in the coding.
This is the main activity
package com.fyp4201.universityguide;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
public class MainActivity extends AppCompatActivity {
TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabHost host = (TabHost) findViewById(R.id.tabHost);
host.setup();
//Tab 1
TabHost.TabSpec spec = host.newTabSpec("tab1");
spec.setContent(R.id.tab1);
spec.setIndicator("Home");
host.addTab(spec);
//Tab 2
spec = host.newTabSpec("tab2");
spec.setContent(R.id.tab2);
spec.setIndicator("Search Courses");
host.addTab(spec);
//Tab 3
spec = host.newTabSpec("tab3");
spec.setContent(R.id.tab3);
spec.setIndicator("Search University");
host.addTab(spec);
//Tab4
spec = host.newTabSpec("tab4");
spec.setContent(R.id.tab4);
spec.setIndicator("Compare Universities");
host.addTab(spec);
//Tab5
spec = host.newTabSpec("tab5");
spec.setContent(R.id.tab5);
spec.setIndicator("Ranking of Universities");
host.addTab(spec);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).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.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.
switch( item.getItemId()){
//noinspection SimplifiableIfStatement
case R.id.action_settings: {
return true;
}
case R.id.action_login:{
Intent intent= new Intent(getApplicationContext(),LoginActivity.class);
startActivity(intent);
}
default:
return super.onOptionsItemSelected(item);
}
}}
This is codes for login activity
package com.fyp4201.universityguide;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView regis = (TextView) findViewById(R.id.register);
Button login = (Button) findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Homeafterlogin.class);
LoginActivity.this.startActivity(intent);
}
});
regis.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, Register.class);
LoginActivity.this.startActivity(intent);
}
});
}}
This is codes for Register activity
package com.fyp4201.universityguide;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import android.widget.Toast;
import java.util.HashMap;
import java.util.Map;
public class Register extends AppCompatActivity {
private static final String REGISTER_URL = "http://jrfyp4201.site88.net/volleyRegister.php";
public static final String KEY_USERNAME = "username";
public static final String KEY_NAME = "name";
public static final String KEY_PASSWORD = "password";
public static final String KEY_EMAIL = "email";
public static final String KEY_CONTACTNO = "contactno";
private EditText etname,etuname,etpass,etconpass,etemail,etcontactno;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
etname= (EditText) findViewById(R.id.name);
etuname= (EditText) findViewById(R.id.uname);
etpass= (EditText) findViewById(R.id.pass);
etconpass= (EditText) findViewById(R.id.conpass);
etemail= (EditText) findViewById(R.id.email);
etcontactno= (EditText) findViewById(R.id.contactno);
}
public void onRegisterbtnClick(View view ){
registerUser();
}
public void registerUser(){
final String username = etuname.getText().toString().trim();
final String password = etpass.getText().toString().trim();
final String name = etname.getText().toString().trim();
final String email = etemail.getText().toString().trim();
final String contactno = etcontactno.toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(Register.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Register.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_USERNAME,username);
params.put(KEY_PASSWORD,password);
params.put(KEY_NAME, name);
params.put(KEY_EMAIL, email);
params.put(KEY_CONTACTNO, contactno);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
This is the logcat error file
Process: com.fyp4201.universityguide, PID: 20646
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fyp4201.universityguide/com.fyp4201.universityguide.Register}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2264)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313)
at android.app.ActivityThread.access$1100(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.fyp4201.universityguide.Register.onCreate(Register.java:20)
at android.app.Activity.performCreate(Activity.java:5340)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2228)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313) 
at android.app.ActivityThread.access$1100(ActivityThread.java:141) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5333) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711) 
at dalvik.system.NativeStart.main(Native Method) 
XML files of register activity
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.fyp4201.universityguide.Register"
tools:showIn="#layout/activity_register">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name :"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editname"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username :"
android:id="#+id/textView3" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/edituname"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password :"
android:id="#+id/textView4" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editpass"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confirm Password :"
android:id="#+id/textView7" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editconpass"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-mail address :"
android:id="#+id/textView5" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/editemail"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact Number :"
android:id="#+id/textView6" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/editcontactno"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:id="#+id/registerbtn"
android:layout_gravity="center_horizontal"
android:onClick="onRegisterbtnClick" />
</LinearLayout>
XML file of login activity
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.fyp4201.universityguide.LoginActivity"
tools:showIn="#layout/activity_login2">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:id="#+id/textView1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/loginuname" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:id="#+id/textView" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/loginpass" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/login"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:focusableInTouchMode="false"
android:onClick="onLoginClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Register Here!"
android:id="#+id/register"
android:layout_gravity="center_horizontal"
android:textColor="#color/accent_material_light"
android:clickable="true"
android:onClick="onRegisterClick" />
</LinearLayout>
Please tell me what I missed. Thank you.
Check all the ids in the xml files and correct their name in java.
It seems like you are referencing an id of another xml file thats not associated with this activity like you might be refering a username field of login xml file in registration activity and vice versa
Nvm, i fixed the problem. There's no problem with the codings just the application is not updated after changes had been made. It is fixed after i clean and rebuild the project. Thank you .

Android - Add Linearlayout above ListView

I would like to add a LinearLayout above a ListView, so that when you scroll the screen, it scrolls the entire layout. In my LinearLayout, I have an AChartEngine GraphicalView, which I want above the ListView. I added a ScrollView, and inside that a Spinner, LinearLayout ListView, but when I run the app, only the ListView and the Spinner show up. The LinearLayout doesn't show up on the screen.
Here is my xml file. Thanks.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/spending_report_cycle_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/spending_report_graph"
android:layout_width="wrap_content"
android:layou`enter code here`t_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal" />
<ListView
android:id="#+id/spending_report_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
When I call the method that returns my graph, I am given a View, which I need to add to the Layout. The only way I know of to do this is to add it to a LinearLayout.
graphLayout = (LinearLayout)view.getViewById(R.id.spending_report_graph);
graph = PieGraph.getNewInstance(getActivity(), items, items.size());
graphLayout.addView(graph);
Is is this you want to achieve ?
Test Image http://imageshack.com/a/img43/3327/f091.png
IF yes, then description is like, Item1 is the spinner, Item 2 on the bottom is the ListView and the black line in between is the View and the extire layout is in the linear layout and it has one child (Scroll View) and only one direct child of scroll view that's Linear layout.
Here's the xml for it. I am not sure if this is what you are looking for.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Spinner
android:id="#+id/spending_report_cycle_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="#+id/spending_report_graph"
android:layout_width="wrap_content"
android:layout_height="3dp"
android:layout_gravity="center_horizontal"
android:background="#000000"
android:orientation="vertical" />
<ListView
android:id="#+id/spending_report_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</ScrollView>
</LinearLayout>
Here's the edited answer :
Test Image http://imageshack.com/a/img809/7741/ti70.png
Here's I am using fragment just to confirm everything was working fine and yes, everything's working perfect.
as you see, there's the fragment I am using, the spinner is at the top, list view on the bottom and after executing the code, here's how the view looks like :
Test Image 2 http://imageshack.com/a/img713/685/qpbi.png
Here, the view inside two green lines is the fragment. Here's the code for everything:
First : the yourmainlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Spinner
android:id="#+id/spending_report_cycle_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="#+id/SomeView2"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center_horizontal"
android:background="#008080"
android:orientation="vertical" />
<fragment
android:id="#+id/fragment_content_1"
android:name="com.mike.passintents.Fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="#+id/SomeView2"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center_horizontal"
android:background="#008080"
android:orientation="vertical" />
<ListView
android:id="#+id/spending_report_listview"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:background="#333333" >
</ListView>
</LinearLayout>
</ScrollView>
</LinearLayout>
Second : The Fragment1
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mike.stackoverflowquestions.R;
public class Fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_fragment_1, container, false);
}
}
Third: Main Activity
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import com.mike.stackoverflowquestions.R;
public class ActivityA extends Activity {
String somevalue1 = "Hello";
String somevalue2 = "World";
ListView mListView;
String[] numbers_text = new String[] { "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen" };
ArrayList<String> mArrayList;
ArrayAdapter<String> mAdapter;
ArrayAdapter<String> spinnerAdapter;
Spinner spinner1;
TextView tV;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_view_stack);
mArrayList = new ArrayList<String>();
for (String s : numbers_text) {
mArrayList.add(s);
}
spinner1 = (Spinner) findViewById(R.id.spending_report_cycle_spinner);
tV = (TextView) findViewById(R.id.tv);
mListView = (ListView) findViewById(R.id.spending_report_listview);
mAdapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, mArrayList);
mListView.setAdapter(mAdapter);
spinnerAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item,
mArrayList);
spinner1.setAdapter(spinnerAdapter);
}
public void selectFragment(View view) {
Fragment fr;
if (view == findViewById(R.id.btnSayHi)) {
fr = new Fragment1();
} else {
fr = new Fragment1();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction mFragmentTransaction = fm.beginTransaction();
mFragmentTransaction.replace(R.id.fragment_content_1, fr);
mFragmentTransaction.commit();
}
}
I made the edit. Please let me know if this works. Good luck..:)

My buttons do not appear when I try to findViewById

I know that there are tons of similar questions because I have read them ALL but my I cannot solve my problem. I have the simpliest setup ever...
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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="#dimen/padding_medium"
android:text="#string/hello_world"
tools:context=".MainActivity" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_marginBottom="60dp"
android:layout_marginRight="46dp"
android:text="Button" />
java
import android.widget.Button;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button my_btn = (Button) findViewById(R.layout.button1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Error
button1 cannot be resolved or is not a field MainActivity.java
I don't know if this is make any difference but I am using the latest API and the Eclipse Version is Juno
Any kind of help is much appreciated!!!!!
Idiot alert....
Button my_btn = (Button) findViewById(R.layout.button1);
This should be
Button my_btn = (Button) findViewById(R.id.button1);
Sorry for wasting your time!

Resources