android tab pass intent extras to new tabs activity - android

I have read through the similar questions but do not see one like this. I have a simple calculator application there are two tabs. Each has their own activity class. I initially wrote this with a button on the first screen which onClick would take the inputs and pass them to the results screen which would do some calculation and then display the results. Now I want to do it with a TabHost. I have the two screens all set, but no idea how to take the inputs and pass them to the results activity to do the calculations and display the resulting values.
Thanks in advance
Dean-O

The most natural way to do this would be to provide your own subclass of android.app.Application and use it to store the shared data. Then the first tab would set the values in the data structure, and the second tab would read them and use them to perform whatever calculation you wanted to do. See here: How to declare global variables in Android?
Assuming you don't want to take this approach and really want to use Intent extras to pass the data between Activities within a TabHost, you could do something like the following hack where you use the TabHosts Intent (accessed via getParent().getIntent()) to pass data back and forth.
public class Tab1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_one);
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
EditText x = (EditText) findViewById(R.id.x);
EditText y = (EditText) findViewById(R.id.y);
int a = Integer.parseInt(x.getText().toString());
int b = Integer.parseInt(y.getText().toString());
Intent i = getParent().getIntent();
i.putExtra("a", a);
i.putExtra("b", b);
i.putExtra("tab", 1);
TabActivity ta = (TabActivity) Tab1.this.getParent();
ta.getTabHost().setCurrentTab(1);
}
});
}
}
public class Tab2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView result = new TextView(this);
Intent i = getParent().getIntent();
int a = i.getIntExtra("a", 0);
int b = i.getIntExtra("b", 0);
int sum = a + b;
result.setText(Integer.toString(sum));
setContentView(result);
}
}

Related

android - replace only content in the same activity

I have a MainActivity, which contains ImageView, TextView and 3 clickable Buttons.
After clicking the button, I want to change something in SQlite dtb and according that load different data, but show it again in the same activity.
public void ClickBtn(View v)
{
//insertData(String...
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
}
So generally - in Main Activity.js I am getting the data from ID, which was clicked before and show that data. The MainActivity should be used infinity times to show different data.
The layout will be always the same - ImageView, TextView and 3 clickable Buttons, just the text will be different.
The question is, how can I only change content inside the same Activity?
I don't think Intent intent = new Intent(MainActivity.this, MainActivity.class); from the current activity can open the same activity...
You really need to study the basics.
When you are working in android, XML layout files are merely blueprints which ultimately are parsed into a reflection-created anonymous view instance, which contains as children each of the members of the XML layout, with the valid XML tag parameters applied to them. Therefore, you aren't dealing with 'Layouts', but with java/kotlin objects, which can be:
Referenced
Mutated
Replaced
So, if you want to change the contents, the first steps is to keep a reference to each object: ImageView, TextView and Buttons, and move the code in charge of filling them to a new method, so you can call it either when loading the activity (onCreate), or when clicking the button. That way the same activity can perform the same action over and over.
Finally, constant recreation of an activity is a TERRIBLE idea. For every object you generate (and an activity IS an object, like everything else), you need X+Y memory, where X is the sum of all the members of the object's class, and Y is the sum of all the operations necessary for instantiation, so by recreating the activity constantly, you waste the device resources, with the added problem of generating a huge backstack of identical activities.
Take a look at a java book, then a kotlin one. It will make your life easier.
This is how I solved it. Just replacing text without refreshing activity. Tested it hundred times also via Memory monitoring and absolutely no impact on device memory.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get from dtb
int room = 1; int a1 = 2; int a2 = 3; int a3 =4;
TextView views = findViewById(R.id.text1);
views.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//get from dtb - img, text where room = a1;
TextView vv = findViewById(R.id.textof);
vv.setText("text from dtb");
}
});
TextView view2 = findViewById(R.id.text2);
view2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//get from dtb - img, text where room = a2;
TextView vv = findViewById(R.id.textof);
vv.setText("another text from dtb");
}
});
}

android- Appending textView objects to a LinearLayout by clicking on a button from a different page (multiple times)

I have two Activities. Activity 1 is designed to take in user input (EditText), and has a button that (if clicked) will go to activity 2. In activity 2, there is a LinearLayout and a button that will take you back to activity 1. I can currently add one textView (containing the user input from activity 1) to the LinearLayout in activity 2, but I would like to add several textView objects to the LinearLayout. When I try to add user input any time after the first, it simply replaces the textView object that held the information from the user input that was entered the first time.
From Activity 1 (AddExercise):
public class AddExercise extends AppCompatActivity {
private EditText name;
private EditText weight;
private EditText sets;
private EditText reps;
private String deets;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_exercise);
Button button = (Button) findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToAddWorkout();
}
});
}
private void goToAddWorkout() {
Intent intent = new Intent(this, AddWorkout.class);
name = (EditText) findViewById(R.id.name);
weight = (EditText) findViewById(R.id.weight);
sets = (EditText) findViewById(R.id.sets);
reps = (EditText) findViewById(R.id.reps);
deets = name.getText().toString() + "\n\t\tWeight: " + weight.getText().toString() + "\n\t\tSets: " + sets.getText().toString() + "\n\t\tReps: " + reps.getText().toString();
intent.putExtra("details", deets);
startActivity(intent);
}
}
From Activity 2 (AddWorkout):
public class AddWorkout extends AppCompactActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_workout);
LinearLayout vBox = (LinearLayout) findViewById(R.id.vBox);
Bundle extras = getIntent().getExtras();
if (extras != null) {
TextView tv = new TextView(this);
tv.setText(extras.getString("details").toString());
vBox.addView(tv);
}
Button button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToAddExercise();
}
});
}
}
You can try a public static List of String each time you can add your text to list and then On your second activity create text-view as per your list count. And add to your linear-layout.
For more click here....
So you want to be able to add multiple entries in your second activity, for every time you add one in the first. I would recommend a different and easier approach.
In your second activity, use a listview/recyclerview, instead of adding new textview. This has the added advantage that once you have added enough entries, scrolling won't be an issue.
Maintain a global list of entries, which you add the entries to. And populate the listview using this list.
you should try something like that as said by roshan-
((ArrayAdapter)listView.getAdapter()).insert(data_object, index);
((ArrayAdapter)listView.getAdapter()).notifyDataSetChanged();
use a shared preference for index... every time you come to second activity increment the index... on exit of app just clear the shared pref index variable.
Also you can add textview in your each list view item
There can be several ways to do it according to me, pick the one that suits your relevant application.
have a single activity and host two fragments. So you can share the data between the fragments using single activity. (Recommended way, I guess Fragments will ease the job for you.). Also you can store a local variable in Activity so that each time you start your application you can start it afresh, if its intended!
If not, you can use SharedPreferences. For each button click, add a string to the preference. When you add one more click, append the new data with a separator like ("|", "||").. So in Activity one write to the preference, in activity 2 read from the preference and display it as list view of dynamically create the linear layout and append it to the root layout.
Declare a static ArrayList in your Activity 1 and access it in activity 2. (Really bad way)

images from button press,images enlarged

new to android development and run into a hurdle,im making an app that will have a lot of pictures, what i want to achieve is after i have pressed a button in the main activity to take me to another activity, i would like a page full of buttons on this new activity that once pressed each button will show a image and have some text i would like to add, i would like to do this without having to create a new activity for each photo so i am hoping somebody could kindly help me with this! think of it like a sounbank app that plays a sound when the buttons from each row is pressed! in my case it will show an image for each button, could this be done with just the main activity and an extra activity or will i end up with an activity open in project explorer for each picture.
your help is greatly appreaciated thank you.
Ok lets assume you are using drawables from your resource folder.
In that way you deal with integer ids that represent the images.
These id can be transmitted per Intent to a second activity, that
retrieves the intent, parses the id and loads / shows the image.
In your Activity A, where you have all your buttons, I assume further that
you've added OnClicklisteners to the button like:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//something
}
});
In your click listener you replace //something with:
Intent i = new Intent(getApplicationContext, ActivityToShowImage.class);
i.putExtra(ActivityToShowImage.IMAGE_KEY_SOME_STRING, R.drawable.oneOfYourImages);
startActivity(i);
The first argument is the key to retrieve the id later and the second is the id.
The R.drawable.oneOfYourImages depends on which button got clicked and shoud
be different for every button.
You can do this for every button, or you create a method that returns an
OnClickListener with the id as parameter.
private View.OnClickListener showImageFor(final int resIdOfImage) {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext, ActivityToShowImage.class);
i.putExtra(ActivityToShowImage.IMAGE_KEY_SOME_STRING, resIdOfImage);
startActivity(i);
}
};
}
Now, in Your ActivityToShowImage, you override the onCreate like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
int imageId = 0;
Bundle extras = getIntent().getExtras();
if(extras != null){
imageId = extras.getInt(IMAGE_KEY_SOME_STRING, 0);
}
if(imageId == 0){
//some log message or exception and don't forget to finish in onResume()
}else{
loadImage(imageId);
}
}
And in your loadImage method you load the image via the id into an ImageView.
I hope that is the answer to your question and sorry for the late response.

ListView Click change from another activity

I have a Listview activity and a Mainactivity.
In my Listview activity i have a array that is
public static final String[] link = {
"http://3313.live.streamtheworld.com/WKRKFM_SC",
"http://3573.live.streamtheworld.com/KXXRFMAAC",
"http://2713.live.streamtheworld.com/KXXRFMAAC",
"http://r16---sn-npo7enel.gvt1.com/videoplayback" };
Im using putExtra and passing this array to my Mainactivity using this.
Intent intent =new Intent(MainActivity.this, Linkview.class);
intent.putExtra("link", link[position]);
startActivity(intent);
And i received my extra using this in Mainactivity.
final Bundle bundle = getIntent().getExtras();
String link = bundle.getString("link");
and i can use it very well but in my MainActivity
I have a next button
next.setOnClickListener(new View.OnClickListener() {
#SuppressWarnings("unused")
#Override
public void onClick(View v) {
//??????
}
});
I want that when i pressed the next button the next array comes from listview activity
and i can show this in my MainActivity.
How i can do that??
So from your question, I assume you want to show the next item in the array.
As long as you keep your array as a static final variable (i.e. a constant array), you can directly access it from your main class. You don't need an instance of the class to access a static variable. For instance, the following should work from your main Activity:
String item = ListviewActivity.link[position];
Thus, you can simply pass the position as an integer, look up the string with the above code, and add one to the position to get the next item.

Get text from edittext and receive to listview ( 2 different intents)

I have a problem to receive a text from edittext in one activity intent to a listview on another intent.
This is what I have done so far:
Activity B:
Button btn = (Button) findViewById(R.id.button2);
final EditText edit = (EditText) findViewById(R.id.editText1);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(addnote.this, MainActivity.class);
i.putExtra("text", edit.getText().toString());
startActivity(i);
Activity A: where the list view is I just don't know how to receive this text when I clicked the button 2
Basically all you want to do is call getStringExtra() to grab whatever you originally assigned during putExtra(). So for example: In Activity A's onCreate(), you'll want to do this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
mYourString = getIntent().getStringExtra("text");
}
Keep in mind, that Android recommends prefacing the names of the objects your storing with the application's package name. So instead of using "text" you should use "com.example.myproject.text".
thank you for that but I have problem receiving the data and placing it into my list view in my activity A i have done so far :
Intent i = getIntent();
String mdata = getIntent().getStringExtra("text");
ListView listv = (ListView) findViewById(R.id.notelist);
ArrayAdapter<String> Adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mdata);
listv.setAdapter(Adapter);

Categories

Resources