How to open layout on button click (android) - android

How can I open another layout xml file when I click on a button in main.xml file?
so if I have main.xml which has a button sying click here and I click it, it opens up second.xml file (layout).

First Create your two layout:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="This is Activity 1" />
<Button android:text="Next"
android:id="#+id/Button01"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
</LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="This is Activity 2" />
<Button android:text="Previous"
android:id="#+id/Button02"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
</LinearLayout>
Second Add your Activity to the manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rr"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Activity1"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity2"></activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
Activity1.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
To switch to Activity2 you have to:
Gets a reference to the button with ID Button01 on the layout using
(Button) findViewById(R.id.Button01).
Create an OnClick listener for the button.
And the most important part, creates an “Intent” to start another
Activity. The intent needs two parameters: a context and the name of
the Activity that we want to start (Activity2.class)
Activity2.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Button next = (Button) findViewById(R.id.Button02);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}

-Inflate the button from the xml
-add an onClickListener on it
-set a new layout in the onClick event
Button btn = (Button) findViewById(R.id.myButton);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
MyActivity.setContentView(R.layout.newlayout);
}
});
Something like this should work...

A Kotlin way:
-Add the onClick event directly in the designer.
Open the activity (Activity1.xml for example) file in the designer mode
Select the button that will trigger the transition
Add the function name on the onClick box of the right panel with all the button properties
-Open the Activity .kt file
Add the function with the name that you just defined in the designer
fun openActivity2(view: View) {
intent = Intent(view.context,Activity2::class.java)
startActivity(intent)
}
Now you have the function linked to the onClick event of your button

Related

assign button press to evoke a new page from sliding menu

I've implemented a sliding menu android framework by following a tutorial video, the menu works well but I can't figure out how to make the buttons generate new "fresh", i.e. distinct, pages wherein I can place subsequent components/"activities".
Right now all the buttons do is "toggle" back and forth between having the menu exposed, and hiding the menu, making visible the complete "landing page".
This is how it looks:
In my layout file, I think this is responsible for assigning functionality to the buttons:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Button 1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Button 2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Button 3" />
So as you can see the buttons evoke the method toggleMenu which looks like this:
public void toggleMenu() {
switch (this.menuCurrentState){
case CLOSED:
this.menuCurrentState = MenuState.OPENING;
this.menu.setVisibility(View.VISIBLE);
this.menuAnimatonScroller.startScroll(0, 0, this.getMenuWidth(),
0, menuAnimationDuration);
break;
case OPEN:
this.menuCurrentState = MenuState.CLOSING;
this.menuAnimatonScroller.startScroll(this.currentContentOffset,
0, -this.currentContentOffset, 0, menuAnimationDuration);
break;
default:
return;
}
this.menuAnimationHandler.postDelayed(this.menuAnimationRunnable, menuAnimationPollingInterval);
this.invalidate();
}
I guess I need to make a new method for generating fresh pages and then assign that to the button push rather than toggle menu, I've tried this a few times using intent but I've not been able to quite figure it out.
What do I need to account for in such an operation?
How should such a function look?
The complete code can be found on my github page.
Thank you for your consideration.
dig this tutorial, it's one of the only helpful ones, in my opinion:
http://www.mkyong.com/android/android-activity-from-one-screen-to-another-screen/
or check the pertinent info below"
XML Layouts
Create following two XML layout files in “res/layout/” folder :
res/layout/main.xml – Represent screen 1
res/layout/main2.xml – Represent screen 2
File : res/layout/main.xml
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I&apos;m screen 1 (main.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click me to another screen" />
File : res/layout/main2.xml
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I&apos;m screen 2 (main2.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
2. Activities
Create two activity classes :
AppActivity.java –> main.xml
App2Activity.java –> main2.xml
To navigate from one screen to another screen, use following code :
Intent intent = new Intent(context, anotherActivity.class);
startActivity(intent);
File : AppActivity.java
package com.mkyong.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class AppActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, App2Activity.class);
startActivity(intent);
}
});
}
}
File : App2Activity.java
package com.mkyong.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class App2Activity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
3. AndroidManifest.xml
Declares above two activity classes in AndroidManifest.xml.
File : AndroidManifest.xml
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".AppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".App2Activity" >
</activity>
</application>

Android — Add new tab when button click like Google Chrome new button?

In general, everything is working on Google Chrome. When the new tab button is clicked, a new tab is generated. In the same way, I want to add a new tab in the Android browser. How to do this — does anyone have any idea?
First, is it possible in Android?
If possible, how to do this?
When I click on the + button, a new tab should be generated. How to do this?
Ok Here is the code but it's an example only you may need to modify the code as per your requirement. I am giving you all the files code here hope you getting answer.
Your Manifest.xml file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.dynamictab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BlankActivity"/>
</application>
</manifest>
here is your activity_main.xml file for Tab activity
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"
android:layout_weight="1">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dip"
android:layout_marginRight="0dip" />
</HorizontalScrollView>
<Button android:id="#+id/add_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="Add"/>
</LinearLayout>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="2dp" />
</LinearLayout>
</TabHost>
Put your TabWidget into the HorizontalScrollView so when more tabs are add it can scrolling and the Add but is out side of HorizontalScrollView. Every time you click on tab it will add new tab in TabWidget.
Here the code for tab_event.xml This layout will inflate and add into tab. You can change the style and its contain a single Button so you can add drawable image with text also.
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button_event"
android:clickable="true"
android:focusable="true"
android:text="Default Tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Here is your MainActivity.java file
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
private static int tabIndex = 0;
private TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
addTab();
((Button)findViewById(R.id.add_tab)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tabIndex++;
addTab();
}
});
}
private void addTab(){
LayoutInflater layoutInflate = LayoutInflater.from(MainActivity.this);
Button tabBtn = (Button)layoutInflate.inflate(R.layout.tab_event, null);
tabBtn.setText("Tab "+tabIndex);
Intent tabIntent = new Intent(MainActivity.this, BlankActivity.class);
setupTab(tabBtn, tabIntent,"Tab "+tabIndex);
}
protected void setupTab(View tabBtn, Intent setClass,String tag) {
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabBtn).setContent(setClass);
tabHost.addTab(setContent);
}
}
And here is the BlankActivity.java file
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class BlankActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(BlankActivity.this);
tv.setText("Blank activity");
setContentView(tv);
}
}
For button click create a new tabspec.
TabSpeec spec = th.newTabSpec("tag");
spec.setContent(new TabHost.TabContentFactory(){
//What ever thing you want to display inside the tab
TextView text = new TextView(CONTEXT);
text.setText("New tab");
return(text);
}
});
spec.setIndicator("New");
th.addTab(spec);
Use the following youtube tutorial as reference. Its simple.
http://www.youtube.com/watch?v=NcKSFlYEqYY
Create an Activity as below:
public class WebViewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView wv=new WebView(this);
setContentView(wv);
}
}
Now use #Partik's method to add a new tab, on each + button click:
private void addTab(){
Button tabBtn = new Button(MainActivity.this);
tabBtn.setText("Tab "+tabIndex);
Intent tabIntent = new Intent(MainActivity.this, WebViewActivity.class);
setupTab(tabBtn, tabIntent,"Tab "+tabIndex);
}

ProgressDialog in Tabbar android

I am trying to make a tab bar application in android, after searching in google i found some tutorial and i succeeded. I want 3 tabs, when i click on 1st tab some data will be taken from server using asyntask and show the progressdialog in frontend, but the progressdialog is capturing the whole screen(with tabbar) and because of that i can't be able to switch the 2nd or 3rd tab.
Expected Output: Whenever i click on any tab a data will be taken from server in background, as i know data is loaded in background so for that time i want to switch 2nd tab or any other tab.
package com.tabexample;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
public class ArrowsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.arrowspage);
Button back = (Button) findViewById(R.id.BackButton2);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ProgressDialog p = new ProgressDialog(getParent());
p.setMessage("Loading");
p.setCancelable(false);
p.show();
p.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
}
});
}
}
package com.tabexample;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class EditActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mListContent));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
startActivity(new Intent(EditActivity.this, OptionsActivity.class));
}
});
}
private static String[] mListContent={"Item 1", "Item 2", "Item 3"};
}
package com.tabexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class OptionsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.optionspage);
Button back = (Button) findViewById(R.id.BackButton1);
Button next = (Button) findViewById(R.id.NextButton1);
}
}
package com.tabexample;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class TabExmapleActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablayout);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("OPT")
.setContent(new Intent(this, ArrowsActivity.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("EDIT")
.setContent(new Intent(this, EditActivity.class)));
tabHost.setCurrentTab(0);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout02" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="#+id/TextView02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Arrows Page">
</TextView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/BackButton2"
android:text="Back" android:layout_below="#+id/TextView02">
</Button>
<Button android:id="#+id/Button01" android:layout_below="#id/BackButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Indeterminate Dialog"></Button>
<Button android:id="#+id/Button02" android:layout_below="#id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Select"></Button>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Options Page">
</TextView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/BackButton1"
android:text="Back" android:layout_below="#+id/TextView01">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/NextButton1"
android:text="Next" android:layout_toRightOf="#+id/BackButton1"
android:layout_below="#+id/TextView01"></Button>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</TabWidget>
</RelativeLayout>
</TabHost>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tabexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TabExmapleActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ArrowsActivity"></activity>
<activity android:name="EditActivity"></activity>
<activity android:name="OptionsActivity"></activity>
</application>
</manifest>
Just Put Progressbar for the child Activitys you are adding in TabHost .
First it will show progress bar of first activity in onCreate which you set as current activity and if you click on next tab it will show its progress dialog
if i get you correctly, you need to access the below activity components when the progress dialog is active.
for this you need to make non-modal/modeless progress dialog. non-modal dialogs will allow you to accept events by behind ui components
please refer the post below
timed modeless dialog

Android: Cannot Start New Activity Using Intents

I am trying to launch a new activity when a button is pressed but nothing happens and I get no errors. Here is my code:
Main activity
public class CSLearn_Python_AppActivity extends Activity {
String tag = "Events";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
//get content from main.xml
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button login = (Button) findViewById(R.id.loginBtn);
login.setOnClickListener(new OnClickListener(){
public void onClick(View v){
// Intent intent = new Intent("com.Main.Verification");
// startActivity(intent);
Intent myIntent = new Intent(getBaseContext(), Verification.class);
startActivity(myIntent);
}
});
}
The new activity
import android.app.Activity;
import android.os.Bundle;
public class Verification extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.verification);
}
}
Verification XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/linearLayout1" android:layout_width="match_parent" android:baselineAligned="true" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:orientation="vertical">
<TextView android:text="#string/Verification" android:id="#+id/Verrification" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:layout_height="wrap_content" android:id="#+id/password" android:inputType="textPassword" android:layout_width="112dp">
<requestFocus></requestFocus>
</EditText>
<Button android:text="#string/LoginBtn" android:id="#+id/loginBtn" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
I added this to the android manifesto
<activity android:name=".Verification"
android:label="Verification">
<intent-filter>
<action android:name="com.Main.VERIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
If anyone can point me in the right direction it would be really appreciated.
Try the code below and register your activity in the AndroidManifest.xml file:
Intent myIntent = new Intent(CSLearn_Python_AppActivity.this, Verification.class);
startActivity(myIntent);
Is CSLearn_Python_AppActivity in the same package than Verification.
May be you can try in the Manifest with:
<activity android:name="yourpackage.Verification"
android:label="#string/verification" >
</activity>
The intent-filter in your Verification activity is not necessary.
I believe the problem lies in the getBaseContext(). Use getApplicationContext() instead (the activity's context is also an option but would cause a leak). I havent quite been able to wrap my head around the base context, but it seems to be some sort of proxy doing more or less nothing in its raw implementation.
A more through explanation of the different contexts is given here.
I would try to change it like this:
Button login = (Button) findViewById(R.id.loginBtn);
login.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Verification.class);
startActivity(myIntent);
}
});
try removing the intent filter, and everything in between, from your manifest
do you have all your imports?
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Intent myIntent = new Intent(CSLearn_Python_AppActivity.this, Verification.class);
startActivity(myIntent);
In your Android manifest file,
<activity android:name=".Verification" android:label="Verification"></activity>
You include the other activity in Manifest file:
Intent intent = new Intent(First.this, Second.class);intent.putExtra("userData",registeredUsersData);startActivity(intent);

Does anyone know how to get the value of edittext from one activity to a textvew in another using a button?

GET EDITTEXT INPUT TO TEXTVIEW IN ANOTHER CLASS USING A BUTTON
I am fairly new to android and I am trying to use an edittext to get user input on one screen (Activity), actually not just one edittext a few like a couple edit texts and maybe a spinner, kind of like a create a new user screen. But I know how to use a button to getText() and setText() from the edittext to the textview if they are in the same activity but can not find anywhere how to accomplish this. Here is something like what the first class's bare bones would be:
public class UserInput extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText edit = (EditText) findViewById(R.id.editText1);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
Now I know the magic will be in the onClick(View v){} method, but what magic exactly do I use to 1-open a new Activity that houses the textview and 2- open the Activity?
Here is the second Activity for visual reference:
public class GetText extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
TextView view = (TextView) findViewById(R.id.textView1);
}
}
Again please if anyone can even chop up the code I will use just trying to get it to work right now. Hopefully everyone can rally and give their input as to help others out that may be stuck as well. Thanks ahead of time.
Here is what I have and if force closes on me:
Main Activity:
package com.mandam.ok;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class UserInput extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText edit = (EditText) findViewById(R.id.editText1);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
EditText edit = (EditText)
findViewById(R.id.editText1);
Intent intent = new Intent(UserInput.this, GetText.class);
intent.putExtra("com.mandam.ok.GETTEXT",
edit.getText().toString());
startActivity(intent);
}
});
}
}
Second Activity:
package com.mandam.ok;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class GetText extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
TextView view = (TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
String name = intent.getStringExtra("com.mandam.ok.MAIN");
//edit.setText(view.getText());
}
}
I will even attach my xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Second xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
And manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mandam.ok"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".OkActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".GetText" >
<intent-filter >
<action android:name="com.mandam.ok.GETTEXT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Now I know I am missing something very simple, just don't know what. I appreciate the help so far. :)
Your second activity HAS a textview... saying that it IS a textview is wrong since Activities are not Views.
Here's how you can pass arguments to a new Activity... this code would be in your first Activity's onClick:
EditText edit = (EditText) findViewById(R.id.editText1);
Intent intent = new Intent(UserInput.this, GetText.class);
intent.putExtra("com.package.name.NAME", edit.getText().toString());
startActivity(intent);
where "com.package.name" is your package name. An intent is an abstraction that performs an operation. In this case, the intent tells android to create a new Activity. The putExtra method allows you to put extra information into the intent before telling Android to create a new Activity. When you put an extra variable into an intent, you need to give it a unique string identifier that preferably starts with the package name.
Once the other Activity is created, here's how you can retrieve the string:
Intent intent = getIntent();
String name = intent.getStringExtra("com.package.name.NAME");
// do whatever you want with name

Categories

Resources