Object , Android - android

I created the simple application App, the user enters his name in EditText and click on "Enter here" button and store his name in String variable that is instance variable then will move to another activity "activity_welcome" and will display his name in TextView
My problem:
when I create an object from Main Class to get the variable name in welcome Class when running this app, the name do not store in TextView that means that
the object main refers to null
My question:
why object main refer to null?
When I make the variable name as a static variable the app will display the name in TextView that mean that object main store the value of name variable
Note:
Main class
package code.app;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class Main extends AppCompatActivity {
EditText editText ;
String name ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.et) ;
}
public void Go (View v){
name=editText.getText().toString() ;
Intent intent= new Intent(this,Welcome.class);
startActivity(intent);
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ba"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:hint="Enter Your Name"
android:textColorHint="#000"
android:id="#+id/tv1"
android:textColor="#000"
android:background="#ffffff"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:text="Enter Here"
android:background="#645353"
android:onClick="Go"/>
</LinearLayout>
Welcome Class
package code.app;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
public class Welcome extends AppCompatActivity {
TextView textView ;
#Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
textView =findViewById(R.id.tv);
Main main =new Main () ;
Toast.makeText(this,main.name, Toast.LENGTH_SHORT).show(); // this Toast donot display any thing
textView.setText(main.name);
}
}
activity_welcome
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tv"
android:layout_margin="50dp"
/>
</LinearLayout>

Just keep it simple and put string inside your intent when you go to another activity, obtain intent, and then string from the intent.
public void Go (View v){
name=editText.getText().toString() ;
Intent intent= new Intent(this,Welcome.class);
intent.putExtra("name", name);
startActivity(intent);
}
Obtain it in your welcome activity like this.
#Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
textView =findViewById(R.id.tv);
Intent intent = getIntent();
if(intent!=null) {
String name = intent.getStringExtra("name");
Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
textView.setText(name);
}
}

Related

Access string_array from activity and send selected item to another activity in android

hello friends today my question is about string_array.On my activity i created two buttons.Now on (strings.xml) i created string array name with two items and i have inserted some text as shown below.On each button click i would like to access each item individually.for example on button1 click show me first item and on button 2 click give me the 2nd item on.Can you please give me the code for button click as i have little idea on how to access item.I have created a textview activity to display my item with every click.my code is working fine till now but i need help with extra code that i have to add.Please help me .
// Button activity page
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="50dp"
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:text="#string/OKILA"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginRight="50dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
//strings.xml
<string-array name="chapters">
<item>this is tes1</item>
<item>this is test2</item>
</string-array>
//main
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class Lipok extends AppCompatActivity implements View.OnClickListener {
Toolbar mActionBarToolbar;
Button btnOne;
Button btnTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipok);
mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("CHAPTERS");
String[] chapters=getResources().getStringArray(R.array.chapters);
btnOne = findViewById(R.id.btn1);
btnTwo = findViewById(R.id.button1);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String text="";
switch(v.getId()){
case R.id.btn1 : {
text = getResources().getStringArray(R.array.chapters)[0];
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.chapters)[1];
break;
}
}
Intent intent = new Intent(Lipok.this,lipokchapters.class);
intent.putExtra("DATA", text);
startActivity(intent);
}
}
//textview activity page
package com.Aolai.temeshilai;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class lipokchapters extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipokchapters);
textView=findViewById(R.id.textv);
String text= getIntent().getStringExtra("Data");
textView.setText(text);
}
}
Try this :
xml file
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="50dp"
android:id="#+id/OKILA"
android:layout_width="wrap_content"
android:text="#string/OKILA"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginRight="50dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
strings.xml
<string-array name="chapters">
<item>this is tes1</item>
<item>this is test2</item>
</string-array>
Main Class
public class Lipok extends AppCompatActivity implements View.OnClickListener {
Toolbar mActionBarToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipok);
mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("CHAPTERS");
}
#Override
public void onClick(View v) {
String text="";
switch(v.getId()){
case R.id.OKILA : {
text = getResources().getStringArray(R.array.testArray)[0];
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.testArray)[1];
break;
}
}
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, YOUR_ACTIVITY_NAME.class);
intent.putExtra("DATA", text);
startActivity(intent);
// Or else you can do whatever you want to do here with that text
}
}
Then in your other activity
String data = getIntent().getStringExtra("DATA");
YOUR_TEXTVIEW_NAME.setText(data);
This is basics of Android so before answering I suggest you to go
through basic tutorials before posting on Stackoberflow.
Now,
Here is how you can access the items from your string_array.
String[] chapters= getResources().getStringArray(R.array.chapters);
Now,here is pseudo code that might help you.
btn1.setOnClickListener(
your_text_view.setText(chapters[0])
)
//Here 0 means your first item, likewise you can access items by their index in
array

string aren't passing between my activities maybe xml issue in android studio

Please help me figure out why my string isn't passing between my activities in android studio. I ran this script on my phone but the string I'm passing from my main activity is passing to my secondary activity. I'm going through an android teach yourself book and I believe the issue is in my XML.
Here's my XML code:
activity_main.xml
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.owner.hour3application.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
activity_secondary.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
tools:context="com.example.owner.hour2application.SecondaryActivity">
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceLarge" />
</android.support.design.widget.CoordinatorLayout>
Main Activity
package com.example.owner.hour2application;
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);
Button activityButton = (Button) findViewById(R.id.Button);
if (activityButton != null) {
activityButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent startIntent = new Intent(MainActivity.this, SecondaryActivity.class);
startIntent.putExtra("com.example.owner.MESSAGE", "Hello SecondaryActivity");
startActivity(startIntent);
}
});
}
}
}
Secondary Activity
package com.example.owner.hour2application;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class SecondaryActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondary);
Intent intent = getIntent();
String message = intent.getStringExtra("com.example.owner.MESSAGE");
TextView messageTextView = (TextView) findViewById(R.id.message);
if (messageTextView != null) {
messageTextView.setText(message);
}
}
}
Thanks guys sorry I posted the wrong main activity. The issue was in string.xml the string name was wrong. Thanks!

Android app keeps 'closing', but no errors being shown in logcat

This is my main class that will be forwarding the username to my GoalActivity class. I cannot figure out where the issue is. It keeps crashing for an unknown reason to me. I've followed various tutorials, and I cannot figure out the issue. It seems that I retrieve the username correctly, and then convert it into a string. Then create an Intent and pass the username value with a key.
MainActivity.java
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.agray.carpediem.LoginDataBaseAdapter;
import com.example.agray.carpediem.R;
import com.example.agray.carpediem.SignUPActivity;
public class MainActivity extends Activity
{
Button btnSignIn,btnSignUp;
LoginDataBaseAdapter loginDataBaseAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create instance of SQLite Database
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
//create reference to the buttons used
btnSignIn=(Button)findViewById(R.id.buttonSignIN);
btnSignUp=(Button)findViewById(R.id.buttonSignUP);
// Signup button w/onclick listener
btnSignUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
/// Create Intent for SignUpActivity
Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
//start the activity w/intent
startActivity(intentSignUP);
}
});
}
// Methods to handleClick Event of Sign In Button
public void signIn(View View)
{
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.login);
dialog.setTitle("Login");
//get the References of views
final EditText editTextUserName=
(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
final EditText editTextPassword=
(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);
Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn);
//Signin Button w/ onClickListener
btnSignIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//store username and password as strings
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
//fetch the Password from the DB for respective username
String storedPassword=loginDataBaseAdapter.getSingleEntry(userName);
// check if the Stored password matches with Password entered by user
if(password.equals(storedPassword))
{
Toast.makeText(MainActivity.this, "Congrats: Login is Successful " + userName,
Toast.LENGTH_LONG).show();
dialog.dismiss();
// final EditText editTextUserName=
// (EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
// String userName=editTextUserName.getText().toString();
//create intent that will start the goals activity w/some data
Intent intro = new Intent(getApplicationContext(), GoalActivity.class);
//put the username into intent
intro.putExtra("USER_NAME", userName);
startActivity(intro);
}
else
{
Toast.makeText(MainActivity.this, "Access Denied: User Name or Password " +
"does not match",
Toast.LENGTH_LONG).show();
}
}
});
dialog.show();
}
#Override
protected void onDestroy() {
super.onDestroy();
// Close The Database
loginDataBaseAdapter.close();
}
}
Here is my GoalActivity class that receives info from the MainActivity class.
GoalActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class GoalActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.goals_page);
//get the username from the intent
String enteredUserName = getIntent().getStringExtra("USER_NAME");
final TextView tv = (TextView)findViewById(R.id.user_name_forwarded);
tv.setText(enteredUserName);
}
}
login.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" >
<EditText
android:id="#+id/editTextUserNameToLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User Name"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editTextPasswordToLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:hint="Password" />
<Button
android:id="#+id/buttonSignIn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sign In" />
</LinearLayout>
goals_page.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"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/welcome_goals"
android:textSize="50sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/user_name_forwarded"
android:text="#string/emptyString"
android:layout_weight="0.09"/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.agray.carpediem" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="CarpeD"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.agray.carpediem.MainActivity"
android:label="CarpeD" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SignUPActivity"/>
<activity android:name=".GoalActivity"/>
</application>
</manifest>
Try this, e.g. in ActivityA:
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.putExtra("USER_NAME", userNameString);
startActivity(i);
In ActivityB:
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
String USERNAME = extras.getString("USER_NAME");
Solved. It was a very careless mistake. I had updated all the other activities in the Manifest but the activity tag for the GoalActivity.java. Thanks guys for all your help!

How to set dynamic string as user select in android?

I'm beginner in Android. I'm trying to make an app where when user tap on button he should go to another activity let say if user tap on button 1 he see new activity with information-A and if he press button 2 he see information-B in same activity he saw information A.
How can I set that.
If I am understanding your question correctly, here's a solution.
You can use the putExtras method of Intent to pass data in key/value pairs to the next activity.
Add the following to activity 1 layout:
<Button
android:id="#+id/btnA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button A"
android:onClick="goToActivityFromButtonA" />
<Button
android:id="#+id/btnB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/btnA"
android:text="Button B"
android:onClick="goToActivityFromButtonB" />
Add the following methods to Activity 1:
public void goToActivityFromButtonA(View view) {
Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("buttonData", "You clicked button A");
startActivity(intent);
}
public void goToActivityFromButtonB(View view) {
Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("buttonData", "You clicked button B");
startActivity(intent);
}
Add the following to Activity 2 layout:
<TextView
android:id="#+id/txtText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Add the following to the onCreate method of activity 2:
TextView txtText = (TextView)findViewById(R.id.txtText);
txtText.setText(getIntent().getExtras().getString("buttonData"));
I just created a sample project for you:
https://drive.google.com/file/d/0B0S6sddMC_rMSUNieUxkR2lFRzQ/view?usp=sharing
you have first create ui for first activity like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".FirstActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="77dp"
android:text="Button 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="110dp"
android:layout_marginLeft="38dp"
android:layout_toRightOf="#+id/button1"
android:text="Button 2" />
</RelativeLayout>
ui of second activity like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second actvity" />
</LinearLayout>
activity first code where i set index with intent for both button click(you also put string double ling and message with intent)
package com.example.teststart;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity implements OnClickListener{
Button b1,b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id=v.getId();
switch(id){
case R.id.button1:
Intent i1=new Intent(FirstActivity.this,secondactivity.class);
i1.putExtra("one", 1);
startActivity(i1);
break;
case R.id.button2:
Intent i2=new Intent(FirstActivity.this,secondactivity.class);
i2.putExtra("one", 2);
startActivity(i2);
break;
}
}
}
and second activity i get that integer value to identify which button is clicked and fire operation on that condition
package com.example.teststart;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class secondactivity extends Activity{
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactvity);
tv=(TextView)findViewById(R.id.textView1);
Bundle extras = getIntent().getExtras();
int index = extras.getInt("one");
if(index==1){
tv.setText("nformation-A "+index);
}else if(index==2){
tv.setText("nformation-B "+index);
}
}
}

Input modify URL Webview?

users have an activity with an edittext and a button.
in the edittext they have to insert their login ID. and press go to display a other activity with webview loading the right site.
an example. this is the link i want users to visit . www.example.com/time/id=1/type=student
if sombody enters a 2 in the edittext it wil open the times for student id 2 and so on.
this is what i have.
LOGIN
package com.beter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
public class LoginActivity extends Activity {
Button go_btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
go_btn = (Button) findViewById(R.id.go_btn);
go_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(LoginActivity.this, WebActivity.class);
LoginActivity.this.startActivity(myIntent);
}
});
}
and the layout
<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" >
<EditText
android:id="#+id/idfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="#string/id"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="#+id/go_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/idfield"
android:text="#string/login" />
The Webview Activity
package com.beter;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl("http://roosters.gepro-osi.nl/roosters/rooster.php?wijzigingen=1&leerling=116277&type=Leerlingrooster&afdeling=12-13_OVERIG&school=905");
}
}
and again the layout
<?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" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
How do i make the user input. display the good webview. ?
First of all get a reference to your edittext in your Login Activity as
go_btn = (Button) findViewById(R.id.go_btn);
ed_txt = (EditText)findViewById(R.id.youredittextid);
The change your onClick to
public void onClick(View v) {
Intent myIntent = new Intent(LoginActivity.this, WebActivity.class);
myIntent.putExtra("id", ed_txt.getText());
LoginActivity.this.startActivity(myIntent);
}
Then retrive this id in your WebActivity onCreate as
String id = getIntent().getExtras().getString("id");
Then your url will be
String myURL = "www.example.com/time/id="+id+"/type=student";

Categories

Resources