Finish splash screen on website loading - java

I need to display splash screen until web page is loaded in webview.
I use following code. Is it possible to do?
public class SplashScreen extends Activity {
protected Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash_screen);
intent = new Intent(getApplicationContext(), MainActivity.class);
// MainActivity.class contains WebView
Thread myThread = new Thread() {
#Override
public void run() {
try {
sleep(5000);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}
}
And
public class MainActivity extends Activity {
private WebView view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
String url = "http://google.com";
view = (WebView)this.findViewById(R.id.webView1);
view.clearCache(true);
WebSettings s = view.getSettings();
s.setJavaScriptEnabled(true);
s.setCacheMode(WebSettings.LOAD_DEFAULT);
s.setDomStorageEnabled(true);
view.loadUrl(url);
}

Don't make a separate activity for splash screen, in MainActivity.java itself create a splash screen layout and webview layout and set visibility of webview to GONE.
On open of MainActivity initialize webView and set custom WebViewClient. Override onPageFinished() in your custom webViewClient and in this method make webview visible and splash screen layout to Gone.
Same here : Loading a WebView URL before splashscreen finishes

My solution is here
private WebView view;
private ImageView splashScreen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
splashScreen = (ImageView) this.findViewById(R.id.spscreen);
String url = "http://google.com";
view = (WebView)this.findViewById(R.id.webView1);
view.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
splashScreen.setVisibility(View.INVISIBLE);
view.setVisibility(View.VISIBLE);
}
});
WebSettings s = view.getSettings();
s.setJavaScriptEnabled(true);
s.setCacheMode(WebSettings.LOAD_DEFAULT);
s.setDomStorageEnabled(true);
view.loadUrl(url);
}
And layout
<ImageView
android:id="#+id/spscreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/splashscreen"
android:visibility="visible"
android:scaleType="fitXY"/>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView1"
android:visibility="invisible" />

Related

Closing ad popup window on webview not working onBackPressed

I'm trying to close the ad popup window on webview onBackPressed. But it keeps reloading the ad window. I am using exoclick and juicyads popunders on the website.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
WebView view = findViewById(R.id.webview);
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
view.setSystemUiVisibility(uiOptions);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
view.getSettings().setSupportMultipleWindows(true);
view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
//view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
view.setWebViewClient(new WebViewClient() {
view.loadUrl(url);
return false;
});
view.loadUrl("https://example.com");
}
#Override
public void onBackPressed() {
WebView view = findViewById(R.id.webview);
if (view.canGoBack()) {
view.goBack();
return;
} else {
moveTaskToBack(true);
}
}
}

Android / Java | Navigation Drawer's TextView .setText not working through SharedPreferences

I have a Fragment that allows the user to change their username that will be displayed in a TextView inside a Navigation Drawer. The Fragment, 'SettingsFragment' is inflated in the MainActivity.
SettingsFragment has an EditText where the user enters their username and a button to apply changes. The Navigation Drawer was created in the MainActivity, so I setup SharedPreferences to send the username over to MainActivity. I then proceeded to change the TextView in the Navigation Drawer with .setText(username), however nothing happens. But when I relaunch the app, the Navigation Drawer's TextView changes to the actual username that the user entered in the EditText.
SettingsFragment.java:
public class SettingsFragment extends Fragment {
public SettingsFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_settings, container, false);
final TextInputEditText teiUserName = view.findViewById(R.id.teiUserName);
final TextView tvEditTextSub = view.findViewById(R.id.tvEditTextSubstitute);
Button btnApplyChanges = view.findViewById(R.id.btnApplyChanges);
btnApplyChanges.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String user_name = String.valueOf(teiUserName.getText());
tvEditTextSub.setText(user_name);
SharedPreferences.Editor editor = getActivity().getSharedPreferences("myPrefs", MODE_PRIVATE).edit();
editor.putString("name", user_name);
editor.apply();
}
});
return view;
}}
SettingsFragment XML:
<FrameLayout 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:padding="10dp"
tools:context="com.miguelpeachey.marketplacesimulator.Fragments.SettingsFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputEditText
android:id="#+id/teiUserName"
android:layout_width="248dp"
android:layout_height="wrap_content"
android:layout_above="#+id/btnApplyChanges"
android:layout_marginBottom="28dp"
android:text="User"
android:textColorHint="#000" />
<Button
android:id="#+id/btnApplyChanges"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:background="#33993f"
android:padding="12dp"
android:text="Apply Changes"
android:textColor="#FFF" />
</RelativeLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Fragment fragment;
SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
final TextView tvDrawerUsername = headerView.findViewById(R.id.tvDrawerUsername);
setSupportActionBar(toolbar);
sharedPreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
final String usernameID = sharedPreferences.getString("name", "User");
if (drawer.isDrawerOpen(GravityCompat.START)) {
tvDrawerUsername.setText(usernameID);
}}
There is more code in MainActivity.java but there's no need in including it.
In MainActivity.java, I also tried:
if (drawer.isDrawerOpen(GravityCompat.START)) {
tvDrawerUsername.setText(usernameID);
}
But it didn't even change the Navigation Drawer's TextView (tvDrawerUsername) after relaunching the app.
In conclusion, the app won't update tvDrawerUsername straight after the button click, but only after I relaunch the app.
Try This one:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Fragment fragment;
SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);Toolbar toolbar = findViewById(R.id.toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
final TextView tvDrawerUsername = headerView.findViewById(R.id.tvDrawerUsername);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, null, "navigation_drawer_open", "navigation_drawer_close") {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
// Do whatever you want here
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
final String usernameID = sharedPreferences.getString("name", "User");
tvDrawerUsername.setText(usernameID);
}
};
drawer.addDrawerListener(toggle);
toggle.syncState();
setSupportActionBar(toolbar);
}
Try defining an TextView attribute on your SettingsFragment class definition :
public class SettingsFragment extends Fragment {
TextView tv;
public SettingsFragment() {
// Required empty public constructor
}
Define an setter for this attribute on the same class :
public void setTextView(TextView tv){
this.tv = tv
}
And from your activity, after initializing your fragment and your textView , pass the textView via the setter you just created :
fragment = new SettingsFragment();
fragment.setTextView(tvDrawerUsername);
Finally add textView.setText(user_name); iniside you onlick() method :
btnApplyChanges.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String user_name = String.valueOf(teiUserName.getText());
tvEditTextSub.setText(user_name);
SharedPreferences.Editor editor = getActivity().getSharedPreferences("myPrefs", MODE_PRIVATE).edit();
editor.putString("name", user_name);
editor.apply();
textView.setText(user_name);
}
});
Hope this helps

SwipeRefreshLayout with Webview not working

This is my webview app code with SwipeRefreshLayout. But SwipeRefresh not working please check it. But all other things are working correctly. Like webview, menu button.
Below is my activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:id="#+id/webView"
/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
And this is my MainActivity.java file
public class MainActivity extends AppCompatActivity {
WebView webView;
SwipeRefreshLayout swipe;
#Override
public boolean onCreateOptionsMenu (Menu manu){
getMenuInflater().inflate(R.menu.main, manu);
return true;
}
#Override
public boolean onOptionsItemSelected (MenuItem item){
int id=item.getItemId();
if(id==R.id.id_about){
Intent intentabout=new Intent(MainActivity.this, About.class);
startActivity(intentabout);
return true;
}
if(id==R.id.id_terms){
return true;
}
if(id==R.id.id_privacy){
return true;
}
return true;
}
#Override
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
LoadWeb();
}
});
LoadWeb();
}
public void LoadWeb(){
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webView.loadUrl("https://google.com");
swipe.setRefreshing(true);
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");
}
public void onPagefinished(WebView view, String url){
swipe.setRefreshing(false);
}
});
}
}
You gotta put your WebView inside SwipeRefreshLayout:
public class MainActivity extends AppCompatActivity {
WebView webView;
SwipeRefreshLayout swipeRefreshLayout;
String currentUrl = "https://www.google.co.in/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
webView.loadUrl(currentUrl);
webView.setWebViewClient(new MyWebViewClient());
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.loadUrl(currentUrl);
}
});
}
public class MyWebViewClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
currentUrl = url;
super.onPageFinished(view, url);
}
}
}
swipeRefreshLayout.setRefreshing(false) stops the animation.
you can put your Webview inside a NestedScrollView to make this work.
Example:
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
You should add
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
LoadWeb();
}
});
swipe.setRefreshing(true); //add
LoadWeb();
}
LoadWeb();

How to load url in webview in background while splashscreen is showing?

This is my splashscreen activity.
public class Splash extends Activity {
private static int SPLASH_TIME_OUT=10000;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Timer r=new Timer();
new Handler().postDelayed(r,SPLASH_TIME_OUT);
}
class Timer implements Runnable{
#Override
public void run() {
// TODO Auto-generated method stub
Intent i=new Intent(Splash.this,MainActivity.class);
startActivity(i);
finish();
}
}
}
This is my MainActivity. This activity Should be performed in background while splashscreen is on the front. Is it advisable to use AsyncTask. How to do that?
If not AsyncActivity, What can I use?
public class MainActivity extends Activity {
private WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.output);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadsImagesAutomatically(true);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.getSettings().setAppCacheEnabled(false);
webview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if(url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
}
);
webview.loadUrl("http://www.example.com");
}
//this is to go back to previous pages if exists
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack()){
webview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
If possible can I set the time for the splashscreen to view as long as the url is loaded without giving time?
The second attempt
public class MainActivity extends Activity {
WebView webview;
private boolean isSplashOn = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview.setBackgroundColor(0);
webview.setBackgroundResource(R.drawable.activity_splash);
webview = (WebView)findViewById(R.id.output);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadsImagesAutomatically(true);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.getSettings().setAppCacheEnabled(false);
webview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if(url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
public void onPageFinished(WebView view, String url) {
if(isSplashOn) {
webview.setBackgroundDrawable(null);
webview.setBackgroundColor(0);
isSplashOn = false;
}
super.onPageFinished(view, url);
}
}
);
webview.loadUrl("http://www.example.com");
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack()){
webview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
Guys. Still the problem is't solved. Need some help
You can use the simple trick -
I was showing a loading spinner till the page was being loaded in background.
Thus I had used two web views.
In your case, you can use ImageView or any other view.
XML file will 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"
tools:context="${relativePackage}.${activityClass}" >
//Actual webview
<WebView
android:id="#+id/actual_webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
//Any view you want to show while webpage is loading
<WebView
android:id="#+id/spinner_webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
...
</RelativeLayout>
Now, in your JAVA file-
//initially, make actual_webview invisible
actual_webview.setVisibility(View.INVISIBLE);
Also, in
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// make spinner_webview invisible
spinner_webview.setVisibility(View.INVISIBLE);
// make actual_webview visible
actual_webview.setVisibility(View.VISIBLE);
}
Also,
shouldOverrideUrlLoading(...)
onPageStarted(...)
onPageFinished(...)
In all these methods, you get the URL and can check which URL is being loaded or finished loading.
According to the URL you can decide whether to show / hide the splash screen.
You can do it without AsyncTask and you can hide the splash screen when the page loaded without a timer.
WebViewClient class has a method onPageFinished() which will be called once the page has been loaded. You can make use of it.
In you project folder, place your splash screen images with name 'splash_screen.png' (or whatever name you want. If you want to use a different name then change this line webView.setBackgroundResource(R.drawable.splash_screen); to map to your splash screen image file) under res/drawable-xxx with corresponding resolutions.
For Eg:
I followed these resolutions for my app's splash screen.
hdpi - 400x800
ldpi - 240x320
mdpi - 320x480
xdpi - 640x960
And try this code:
public class MainActivity extends Activity {
final Activity activity = this;
private WebView webView;
private boolean isSplashOn = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.setBackgroundColor(0);
webView.setBackgroundResource(R.drawable.splash_screen);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.setWebViewClient(new MyWebClient());
webView.loadUrl("http://www.google.com");
}
public class MyWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if(url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
if(!isSplashOn) {
webView.setBackgroundDrawable(null);
webView.setBackgroundColor(0);
isSplashOn = true;
}
super.onPageFinished(view, url);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
if(webView.canGoBack()) {
webView.goBack();
return true;
}else {
activity.finish();
}
}
return super.onKeyDown(keyCode, event);
}
}
activity_main.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" >
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY" />
</LinearLayout>
EDIT:
By the way i found the issue with your code. You are trying to set the splash screen to the webview before instantiating it.
Change your code from this:
webview.setBackgroundColor(0);
webview.setBackgroundResource(R.drawable.activity_splash);
webview = (WebView)findViewById(R.id.output);
to this:
webview = (WebView)findViewById(R.id.output);
webview.setBackgroundColor(0);
webview.setBackgroundResource(R.drawable.activity_splash);
Why does the splash has to be an activity?
You can make it a full screen ImageView in the bottom of your layout and hide it using a handler:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (isDestroyed())
return;
mSplashView.setVisibility(View.GONE);
}
},2000);

Transfer data from EditText to TextView of another activity using setText and onClickListener?

I am a newbie.. I am trying to get text on another activity which I have created, from an activity created apriori. I could do this using onClickListener, but I am unable to get it done using onClick and setText(). The code runs well until the second activity, but when I type and send button, the code crashes. Help me!
Here's the XML file:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#+id/tv1"
android:text="#string/hello_world" />
Here's the Java file:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.bt1);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
DisplayMessageActivity.class);
EditText et = (EditText) findViewById(R.id.et1);
String message = et.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
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.main, menu);
return true;
}
}
This code can help you
MainActivity.java
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText et=(EditText) findViewById(R.id.editText2);
Button bt=(Button) findViewById(R.id.button2);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(Main.this,DisplayMessageActivity.class);
intent.putExtra("theText", et.getText().toString());
startActivity(intent);
}
});
}
DisplayMessageActivity.java
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.theme.R.layout.DisplayActivity);
TextView tv=(TextView) findViewById(com.example.theme.R.id.textView2);
tv.setText(getIntent().getExtras().getString("theText"));
}
Replace
android:layout="#+id/tv1"
with
android:id="#+id/tv1"

Categories

Resources