Save dynamicaly added items in layout - java

How do I save dynamicaly (through code) added items in layout? I mean, I added two imageViews into layout through code, but when I switch to another layout, and then get back to this one, imageViews are gone (they aren't here).
Code:
LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
ImageView imageView = new ImageView(this);
imageView.setId(i);
imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
layout.addView(imageView);

When you say a second layout, do you mean a second activity ?
If yes, you can use the intent to transmit the images to the second layout, and then transmit it back to the first activity.
ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
bitmaps.add(yourimage);
Intent intent = new Intent(A1.this, A2.class);
intent.putParcelableArrayListExtra("bitmaps", bitmaps);
startActivity(intent);
You can take it in the second Activity with :
Intent intent = getIntent();
if(intent.hasExtra("bitmaps")){
ArrayList<Bitmap> bitmaps= intent.getExtras().getParcelableArrayList("bitmaps");
}

Related

How to add multiple edit text automatically on Android?

I am new to the Android and I am currently working on my android project.
I want to do this...
On the first Activity, the user will enter the number of strings he/she wants to input. For example, 3. (I am already done with this.)
And then, on the second activity, three edit text will appear for the entering the first, second and third string based on the user input in the first activity. If he/she enters 2, two edit text will appear on the second Java Activity. (How to do this one?)
In your Activity1, pass the user's entry to the next activity:
int userSelectedVal=somevalue;
Intent mIntent = new Intent(Activity1.this, Activity2.class);
mIntent.putExtra("userSelectedVal", userSelectedVal);
startActivity(mIntent);
In your Activity2, retrieve this value and programmatically add the Edittext's depending on this value:
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
int noOfEditTexts = extras.getInt("userSelectedVal");
LinearLayout mLinearlayout = new LinearLayout(this);
// specifying vertical orientation
mLinearlayout.setOrientation(LinearLayout.VERTICAL);
// creating LayoutParams
LayoutParams mLayoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// set LinearLayout as a root element of the screen
setContentView(mLinearlayout, mLayoutParam);
for (int i = 0; i < noOfEditTexts; i++) {
EditText mEditText = new EditText(context); // Pass it an Activity or Context
myEditText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mLinearlayout.addView(mEditText);
}
}

Android: How to add a layer on top no matter how many activities started?

I want to play a gif animation and this view will move on the screen every second, but if I start an activity I need to add this view again and the position will change.
See the attached image below. I need to stick this over every view. Any way to do it?
Create one BaseActivity for all activities in your app to show Overlay View or Layout.
you can inherit BaseActivity on other Activity
You can add gif supported views or layouts
for example i added overlay_layout in my showOverlay method.
you can call showOverlay method where ever you want to show and you can remove with removeOverlay with conditions.
Please note that showOverlay and removeOverlay should be in BaseActivity
void showOverlay(){
LayoutInflater inflater = activity.getLayoutInflater();
layout = inflater.inflate(R.layout.overlay_layout, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.gravity = Gravity.BOTTOM;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.type = WindowManager.LayoutParams.TYPE_APPLICATION;
final WindowManager mWindowManager = (WindowManager);
activity.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
mWindowManager.addView(layout, params);
}
void removeOverlay(){
windowManager.removeView(view);
}
set this view into WindowManager and it will always show on top

Android ListView - onItemClickListener

I'm beginning in mobile application development and I want to know how to send an item from a ListView to appear in another activity; Then I can show more information about each item.
I managed to display the title and description with the following code:
MainActivity
TextView txt = (TextView)findViewById(R.id.textView1);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("title", txt.getText());
startActivity(intent);
SecondActivity
TextView txt = (TextView)findViewById(R.id.show_textView1);
Bundle b = this.getIntent().getExtras();
String val = b.getString("name");
txt.setText(val);
but how do I set putExtra for an image to display in the next Activity ?
P.S. everything is working fine, the xml file where the item details will be shown is set.
What is the source of the image?
If a file, pass in the String to the file location.
bundle.putString("file-loc",fileLocation);
If a resource, pass in the resource.
bundle.putInt("resource-id",resid);
If it's just a bitmap, you can actually pass it directly, as a Parcelable.
bundle.putParcelable("bitmap",bitmap);
You can send the ImageView's id (R.drawable.your_image) and then fetch it and show it in the ImageView of other activity

Relative layout on click listener doesn't take the correct for-loop index

I have created a For-loop that create dinamycally some relative Layouts. I have an arrayList with some elements, called "photosPathList". The size of this arrayList is used to define the max number of loops.
This code that i'm writing is inside the onResume() method of an activity in Android. There is one thing that i'm not understanding.
If i log the i index inside the loop, it is increased correctly, and at each loop it take +1 value.
But if i click on the relative layout listener, the i index has always the last loop position. So if i create 4 relative layouts and i click on the first one, the log inside the relative layout show to me the the number 3. This isn't correctly because it should show the 0 number. Do you agree?
So what i'm doing wrong?
for (i=0; i<= photosPathList.size()-1; i++) {
//RELATIVE LAYOUT
RelativeLayout relativeLayout = new RelativeLayout(this); //create a new relative layout
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, //set main params about the width and height
RelativeLayout.LayoutParams.FILL_PARENT));
relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor)); //set background color
LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
relativeParams.setMargins(20, 20, 20, 0);
relativeLayout.setLayoutParams(relativeParams); //set declared params about layout to the relativeLayout
relativeLayout.requestLayout();
Log.i("index",""+i);
Log.i("current path",""+photosPathList.get(i).toString());
relativeLayout.setOnClickListener(new View.OnClickListener(){ //create a listener about the layout. When a user press a point inside the relative layout a new activity should be created
#Override
public void onClick(View v){
Intent photoDetailsActivity = new Intent(
getApplicationContext(),
PhotoDetails.class //assign the class for create a new intent
);
Log.i("index2",""+i);
photoDetailsActivity.putExtra("photoDetailsImagePath", photosPathList.get(i).toString());
photoDetailsActivity.putStringArrayListExtra("photosPathList" , photosPathList);
photoDetailsActivity.putStringArrayListExtra("formatPhotoList", formatPhotoList);
photoDetailsActivity.putStringArrayListExtra("numberCopiesPhotoList", numberCopiesPhotoList);
photoDetailsActivity.putStringArrayListExtra("pricePhotoList", pricePhotoList);
startActivity(photoDetailsActivity); //let's start the new activity
}
});
}
Thanks
Your "i" variable isn't final.
Something you need to understand here:
When your onClick method invokes, it won't know what the value of "i" was during this loop. You need to find a way to pass "i" to the onClicklistener.
My suggestion, create a new class which implements OnClickListener and accepts an int variable.
Save this variable as a class member, and then use that in the OnClick method.
Hope this helps :)
You are using same variable i for every click event. You should use different counter variables.
// try this way
for (i=0; i<= photosPathList.size(); i++) {
//RELATIVE LAYOUT
RelativeLayout relativeLayout = new RelativeLayout(this); //create a new relative layout
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, //set main params about the width and height
RelativeLayout.LayoutParams.FILL_PARENT));
relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor)); //set background color
LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
relativeParams.setMargins(20, 20, 20, 0);
relativeLayout.setLayoutParams(relativeParams); //set declared params about layout to the relativeLayout
relativeLayout.requestLayout();
relativeLayout.setTag(i);
relativeLayout.setOnClickListener(new View.OnClickListener(){ //create a listener about the layout. When a user press a point inside the relative layout a new activity should be created
#Override
public void onClick(View v){
Intent photoDetailsActivity = new Intent(
getApplicationContext(),
PhotoDetails.class //assign the class for create a new intent
);
int index = (Integer)v.getTag();
Log.i("index2",""+index);
photoDetailsActivity.putExtra("photoDetailsImagePath", photosPathList.get(index).toString());
photoDetailsActivity.putStringArrayListExtra("photosPathList" , photosPathList);
photoDetailsActivity.putStringArrayListExtra("formatPhotoList", formatPhotoList);
photoDetailsActivity.putStringArrayListExtra("numberCopiesPhotoList", numberCopiesPhotoList);
photoDetailsActivity.putStringArrayListExtra("pricePhotoList", pricePhotoList);
startActivity(photoDetailsActivity); //let's start the new activity
}
});
}

Transfer textview data between activities?

I'm currently making an app that has edittext and 2 buttons, everytime I write something in to edittext and then press button1, a new textview is added. This is the code:
public void novVnos (View v){
EditText eText = (EditText) findViewById(R.id.editText1);
LinearLayout mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mLayout.addView(createNewTextView(eText.getText().toString()));
}
private TextView createNewTextView (String text){
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView newTextView = new TextView(this);
newTextView.setLayoutParams(lparams);
newTextView.setText(text);
return newTextView;
}
Basically what I'm doing is adding new "players" and once I've added them all I want to press button2. Button2 opens a new activity and the new activity should read all the textviews that I've added in the previous activity.
What I would do is create a "Player" class and make a static ArrayList<String> players inside your Player.java class. Every time you call createNewTextView(textView) add whatever variable text is to the players ArrayList.
In your next Activity you just call Player.players.get(index) or whatever ArrayList function you need to do whatever work you want with it in the next class. You also could create this ArrayList in your current Activity and pass it as an extra in the Intent but I think creating a separate class for the players would be easiest.
Your ArrayList obviously doesn't need to hold Strings. It could hold whatever you want including a Player object.
ArrayList Docs
this is an example :
in the first activity :
Button search = (Button) findViewById(R.id.Button02);
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, result.class);
Bundle b = new Bundle();
EditText searchtext = (EditText) findViewById(R.id.searchtext);
b.putString("searchtext", searchtext.getText().toString());
//Add the set of extended data to the intent and start it
intent.putExtras(b);
startActivityForResult(intent,RESULT_ACTIVITY);
}
});
and in the second activity at onCreat :
Bundle b = getIntent().getExtras();
String searchtext = b.getString("searchtext"); //here you get data then use it as you want
//here I use it to show data in another edittext
EditText etSearch = (EditText) findViewById(R.id.searchtext2);
etSearch.setText(searchtext);
Implementing a "Player" class will be a way to go. In that regard, you don't have to worry about transferring data between two activities. You can use Singleton method to make sure your class has been defined only once. You need to make sure it gets defined only once because let's say if you user closes application or kills app, then when he opens app again, it would create another class, so all of your data will be lost.

Categories

Resources