I have come across another prickly little problem.
I am writing an app with tabs, but I have a textbox (EditText) at the top of the screen that I want to be able to receive text data from any of the tabs. As each of the tabs has it's own activity and layout, this is proving difficult to achieve.
I want to be able to use:
editText1.setText("Hello World");//sample text
from any Tab/Activity.
Does anyone know how to make a textbox from one layout public and able to recieve text?
I am using TabActivity, yes I know it's deprecated but as this is my first app with tabs, it's easier to learn than Fragments. I will try them next time, unless they are the answer to my problem, in which case I have a lot of re-coding to do!!
ok, new part.
package com.epsilonitsystems.epecsandroid;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;
import android.view.Menu;
import android.speech.tts.TextToSpeech;
public class MainActivity extends TabActivity {
public EditText editText1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
TabHost tabHost = getTabHost();
// Tab for Core
TabSpec corespec = tabHost.newTabSpec("Core");
corespec.setIndicator("", getResources().getDrawable(R.drawable.ic_i));
Intent coreIntent = new Intent(this, CoreActivity.class);
corespec.setContent(coreIntent);
// Tab for Drink
TabSpec drinkspec = tabHost.newTabSpec("Drink");
drinkspec.setIndicator("", getResources().getDrawable(R.drawable.ic_drink));
Intent drinkIntent = new Intent(this, DrinkActivity.class);
drinkspec.setContent(drinkIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(corespec); // Adding Core tab
tabHost.addTab(drinkspec); // Adding Drink tab
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
That's the Main Activity, I'll just show the Core Activity as they will all be the same when I get it working.
package com.epsilonitsystems.epecsandroid;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.speech.tts.TextToSpeech;
public class CoreActivity extends Activity {
private TextToSpeech mTts;
// This code can be any value you want, its just a checksum.
private static final int MY_DATA_CHECK_CODE = 1234;
EditText editText1;
String Spch,Str;
ImageButton imageButton1,imageButton2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.core_layout);
//button1 = (Button) findViewById(R.id.button1);
editText1 = (EditText) findViewById(R.id.editText1);
mTts = new TextToSpeech(this, null);
}
public void onimageButton1Click(View view) {
//mTts.speak("I",TextToSpeech.QUEUE_FLUSH,null);
//Spch=editText1.toString();
//Spch=Spch+"I ";
editText1.setText("Hello World");
}
}//Activity End
I can't post a screenshot as I'm still a new user, sorry.
Any ideas please?
Gary,
What you are looking for probably is how to share data between different Activities.
You can do this in a couple of ways, but the cleanest is using the Intent you start the new Activity with.
With this you can set extra data via the Intent.putXXXXX methods.
You could use something as Intent.putExtra("my_private_key", mTextVar); and fetch that out in your other Activity with this.getIntent().getStringExtra("my_private_key");
ps. As tempting as it might seem to start with the TabActivity, you're actually making it a lot more difficult for yourself by making a bad start and learning classes which you should not use anymore. I'd advise you to pick up some good fragments tutorials which will be just as easy once correctly explained. You should take a look at per example http://www.vogella.com/android.html
Related
I could solve how to hide or show a Floating Action Button from a fragment when I call it.
But I faced with another problem that I didn't know how to solve it, when I rotate my phone,
the FAB appears again.
You can see my code below and how I did to hide my FAB, but how to keep it when my phone rotate from
Portrait to Landscape?
package com.example.cursobaralhocigano.ui.deck;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.example.cursobaralhocigano.MainActivity;
import com.example.cursobaralhocigano.R;
import com.example.cursobaralhocigano.classes.cBaralhos;
import com.example.cursobaralhocigano.dao.uLibSql;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
/**
* A simple {#link Fragment} subclass.
*/
public class DeckFragment extends Fragment implements View.OnClickListener {
private uLibSql DB;
private cBaralhos baralho = new cBaralhos();
CheckBox ck01, ck02, ck03, ck04, ck05;
ImageButton Img;
public DeckFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
LinearLayout ln;
View view = inflater.inflate(R.layout.fragment_deck, container, false);
final FloatingActionButton fab = ((MainActivity) getActivity()).findViewById(R.id.fab);
if (fab.isShown()) {
fab.hide();
}
return view;
}
Thanks a lot for help
Regards
Alex
You have to correctly save the instance state of Fragment you should do the following:
In the fragment, save instance state by overriding onSaveInstanceState() and restore in onActivityCreated():
Below link may help you
https://stackoverflow.com/a/17135346/10239870
Hello i have a problem with running the application, when ill run it, its opening but when i will press the button saying me that the application is stopped and cant run: “Unfortunately, App has Stopped”. Here is my simple code and i hope i will get the answer fast...
Thanks allot :)
package com.stefan.stefan1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textView1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView1.setText("Hello Im Stefan !");
}
});
}
#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;
}
}
Hello again i have new problem i have added another label(textView2) and im trying to add again this line textView2 = (TextView) findViewById(R.id.textView2);
but giving me error for adding something in class "R.java"
please help .. :/
You forgot to initialize textView1.
So when you're doing :
textView1.setText("Hello Im Stefan !");
it throws a NullPointerException and stop your app.
If you defined your textview in the layout, add after setContentView
textView1 = (TextView) findViewById(R.id.idOfYourTextView);
P.S : You should always provide the stacktrace(logcat) when you have such an error.
I want to show webpage when we click an image in viewpager. Can you please help me in this. Also I want to know if multiple images are there in viewpager so how will I get the respective webpages of them.
Thanks in advance.
**MainActivity.java**
package com.example.viewimage;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
// open the desired page
Intent browserIntent = new Intent("android.intent.action.VIEW",
Uri.parse("http://www.craftsvilla.com/anvi-s-classic-nawabi-earrings-studded-with-white-stones-and-emeralds.html"));
startActivity(browserIntent);
}
});
}
}
If you want to show this webpage inside your app, you would have to add a WebView. Set its visibility to View.GONE. Add OnClickListener to your viewPager. On click set the visibility of the webView to be View.VISIBLE. To go back, override the functionality of 'back' key - and on its click hide the webpage.
Or you can open other activity that holds the webView.
You can also open the webpage with android's browser.
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri);
startActivity(browserIntent);
Edited :
Add to your xml before closing your RelativeLayout
<LinearLayout
android:id="#+id/cover_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"/>
instead of 'viewPager.setOnClickListener' write
(findViewById(R.id.cover_layout))
I have a CUSTOM tab host the uses the addTab() method instead of tabspec. one of my tabs in a MapViewthat shows a specific location. The problem I am encountering is an exception on the line in my tabhost that sets up the mapview tab.
Tab Activity (removed package for confidential reasons):
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
public class CustomTabHost extends TabActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost);
setTabs() ;
}
private void setTabs()
{
addTab("Services", R.drawable.servicesicon, services.class);
addTab("Our Work", R.drawable.ourworkicon, ourwork.class);
addTab("RSS", R.drawable.rssicon, RSSReader.class);
addTab("Locate IML", R.drawable.locateicon, Locate.class);
addTab("Contact IML", R.drawable.contacticon, ContactUs.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tabs_bg, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title1);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon1);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
MapActivity:
import java.util.List;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class Locate extends MapActivity {
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.locateiml);
MapView mapview = (MapView) findViewById(R.id.locatemap);
mapview.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapview.getOverlays();
Drawable drawable =this.getResources().getDrawable(R.drawable.locateicon);
Locateoverlay itemoverlay = new Locateoverlay(drawable, this);
GeoPoint point = new GeoPoint(382571066,-85751392);
OverlayItem overlayitem = new OverlayItem(point, "Interactive", null);
itemoverlay.addOverlay(overlayitem);
mapOverlays.add(itemoverlay);
}
protected boolean isRouteDisplayed()
{
return false;
}
}
There is also an overlay, but that is not needed for this question. My question is, I am pretty sure I need to create a parent activity the handles the map activity, but I do not know how to do this.
I know that the Activity in TabHost is not considered as Activity. I think it is managed as View.
In my case, main Activity was inherited Activity and TabHost was implemented through xml configuration. Then I added MapActivity -as a TabSpec- to TabHost, but MapView shows gray background only.So, I make main Activity inherit MapActivity, and it worked.
I don't know whether this is best solution, whether your main Activity must inherit TabActivity, but if there is no obvious reason, I think it is better to inherit MapActivity rather then TabActivity.Maybe my answer is not the best, I wish I could help you in some way.Good luck!
TabActivity caused me a lot of issues in the past. This might be another issue related to it.
Right now TabActivity is deprecated. The doc says:
New applications should use Fragments instead of this class; to
continue to run on older devices, you can use the v4 support library
which provides a version of the Fragment API that is compatible down
to DONUT.
To use a MapView inside a Fragment you will need to use: petedoyle's android-support-v4-googlemaps.
I have created a simple webview with two buttons below it. If I press one of the button the view seems to create a new view called "web" then the page loads and I still have my buttons below but they don't function. If I use the back button on the phone it takes me to the opening view blank page and the buttons work again? Sorry I am new..
I just want it to load in the original view and have the buttons continue to function.
Do I have to suppress the creation of a new view somehow?
Kind Regards,
-Mike
** and I am not sure why my code always has extra crap when I post it because it does't when I copy it to the clipboard. **
Webscreen Class
package com.example.cam;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Webscreen <URL> extends Activity {
WebView webview1;
public static final String URL = "";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String turl = getIntent().getStringExtra(URL);
webview1 = (WebView)findViewById(R.id.webview01);
webview1.clearCache(true);
webview1.loadUrl(turl);
}
}
cam Class
package com.example.cam;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class cam extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Add Click listeners for all buttons
View firstButton = findViewById(R.id.button1);
firstButton.setOnClickListener(this);
View secondButton = findViewById(R.id.button2);
secondButton.setOnClickListener(this);
}
// Process the button click events
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.example.cam.Webscreen.URL,
"http://m.yahoo.com");
startActivity(j);
break;
case R.id.button2:
Intent k = new Intent(this, Webscreen.class);
k.putExtra(com.example.cam.Webscreen.URL,
"http://m.google.com");
startActivity(k);
break;
}
}
}
I don't know why are you not using Button class and using View Class.
Use
Button b1=(Button)findViewById(R.id.button1);
instead of
View b1=findViewById(R.id.button1);
Use relative layout for placement of buttons
On button click you are recreating your activity with the intent of displaying the web page/s.
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.example.cam.Webscreen.URL,
"http://m.yahoo.com");
startActivity(j);
it is only creating new activity on top of the new activity
you need to load the web page on button click instead.
//on button1 click
mWebView.loadUrl("http://m.yahoo.com");