Save textView position after drag and Drop using com.jmedeisis.draglinearlayout.DragLinearLayout - java

So I was able to have my textViews drag and drop properly. But when I close the application and re-open it doesn't save the position of the new textViews. In a previous application I was able to save data using SharedPreferences but I couldn't quite get it to work in this scenario. Any insight or help would be appreciated!
Thanks,
DragLinearLayout dragDropAndroidLinearLayout = (DragLinearLayout) findViewById(R.id.drag_drop_layout);
for (int i = 0; i < dragDropAndroidLinearLayout.getChildCount(); i++) {
View child = dragDropAndroidLinearLayout.getChildAt(i);
dragDropAndroidLinearLayout.setViewDraggable(child, child);
}
The code I'm using to implement the drag and drop feature. I guess I'm not sure what part in this code would I use to put into the SharedPreferences if that's the way I would even do it.

Try using setTag and getTag
for(i=0;i<5;i++){
final View items= View.inflate(this, R.layout.yourItemStyle, null);
items.setTag(i);
final TextView text1= (TextView) note.findViewById(R.id.textView1);
text1.setId(i);
dragLinearLayout.addDragView(items, text1,i);
dragLinearLayout.setOnViewSwapListener(new DragLinearLayout.OnViewSwapListener() {
#Override
public void onSwap(View firstView, int firstPosition, View secondView, int secondPosition) {
int ID= Integer.parseInt(String.valueOf(firstView.getTag()))
String strID = String.valueOf(ID);
Log.v("ID",strID );
}
});
}

Related

add view in linearlayout programmatically working slowly

My goal is to add my custom view inside LinearLayout.I have custom arrayList and I would to add custom views with for loop.Here is a my code snippet
public void replaceCustomView() {
for (int i = 0; i < insertDataItems().size(); i++) {
final LayoutInflater inflater = LayoutInflater.from(this);
final View reView = inflater.inflate(R.layout.item_parent_child_listing, null, false);
final TextView parentName = reView.findViewById(R.id.tv_parentName);
final ImageView headerImageView = reView.findViewById(R.id.header_imageView);
final LinearLayout linearLayout_childItems = reView.findViewById(R.id.ll_child_items);
final RelativeLayout headerLayout = reView.findViewById(R.id.header_layout);
final RelativeLayout headerImageLayout = reView.findViewById(R.id.header_image_layout);
parentName.setText(insertDataItems().get(i).getParentName());
if (insertDataItems().get(i).getChildDataItems() != null) {
headerImageLayout.setVisibility(View.VISIBLE);
headerImageLayout.setVisibility(View.VISIBLE);
for (int j = 0; j < insertDataItems().get(i).getChildDataItems().size(); j++) {
final LayoutInflater childInflater = LayoutInflater.from(this);
final View childView = childInflater.inflate(R.layout.z_history_child_item, null, false);
final TextView key = childView.findViewById(R.id.u_key);
final TextView value = childView.findViewById(R.id.u_value);
key.setText(insertDataItems().get(i).getChildDataItems().get(j).getKey());
value.setText(insertDataItems().get(i).getChildDataItems().get(j).getValue());
linearLayout_childItems.addView(childView);
}
} else {
headerImageLayout.setVisibility(View.GONE);
headerLayout.setBackgroundColor(Color.parseColor("#e8e8e8"));
}
linearLayout_childItems.setVisibility(View.GONE);
if (insertDataItems().get(i).getParentName().length() > 0) {
if (insertDataItems().get(i).isAllowDisable()) {
headerImageView.setVisibility(View.GONE);
linearLayout_childItems.setVisibility(View.VISIBLE);
} else {
headerImageView.setVisibility(View.VISIBLE);
linearLayout_childItems.setVisibility(View.GONE);
}
} else {
linearLayout_childItems.setVisibility(View.VISIBLE);
headerLayout.setVisibility(View.GONE);
}
replaceLayout.post(() -> replaceLayout.addView(reView));
}
}
I call this function like this
runOnUiThread(() -> replaceCustomView());
Custom views adding successfully,but my problem is that in a first time activity is slowly.Android need to much time to add view.My custom array's size is 20.Is it a any way to add views step by step ,not add all views each time?
What's a best practice ?
thanks
Please do not use the concept of adding runtime components when the items are more. This will cause the activity to share the memory with view rendering logic and you'll be able to recycle the views. this kind of implementation can be used only when there are a few (may be 2 to 5) items and can't be achieved with recyclerview due to some UI limitations.
Hence use a RecyclerView to load the items and you can use the concept of adding custom items inside a child item to achieve this functionality.

Click listener inside OnBindViewHolder

I have the following code for the recyclerview adapter for an android app that I'm working on right now:
#Override
public void onBindViewHolder(final FeedViewHolder contactViewHolder, final int i) {
final FeedInfo ci = feedInfoList.get(i);
//Set the text of the feed with your data
contactViewHolder.feedText.setText(ci.getFeed());
contactViewHolder.surNameText.setText(ci.getSurName());
contactViewHolder.nameText.setText(ci.getFirstName());
contactViewHolder.feedDate.setText(ci.getDate());
contactViewHolder.numberOfGoingText.setText(ci.getNumber_of_going());
contactViewHolder.numberOfInterestedText.setText(ci.getNumber_of_interested());
//seteaza fotografia de profil in postare
new ProfilePictureDownloadImage(contactViewHolder.profilePicture).execute(ci.getProfileImageURL());
ImageButton interestedButton = contactViewHolder.interestedButton;
interestedButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = i;
FeedInfo fi = feedInfoList.get(position);
int displayedNumberOfInterested = Integer.parseInt(ci.getNumber_of_interested()) + 1;
contactViewHolder.numberOfInterestedText.setText(Integer.toString(displayedNumberOfInterested));
System.out.println("emilutzy interested from within" + fi.getPostID());
contactViewHolder.surNameText.setText("kk");
}
});
}
The problem is the click listener. In theory the button I press should increment the number right next to it. However, since I have to declare onBindViewHolder's arguments as final, only the first click works, the rest of the clicks do not change the value of the number. I am new to Android, so could you please help me find a better solution?
There's a nice method called getAdapterPosition() that you can use in your RecyclerView's ViewHolder.
Instead of setting the click listener in onBindViewHolder, set it in the constructor of your ViewHolder like so:
public class FeedViewHolder extends RecyclerView.ViewHolder {
private TextView feedText;
private TextView surNameText;
private Button interestedButton;
// ... the rest of your viewholder elements
public FeedViewHolder(View itemView) {
super(itemView);
feedtext = itemView.findViewById(R.id.feedtext);
// ... find your other views
interestedButton.setOnClickListener(new View.OnClickListener() {
final FeedInfo fi = feedInfoList.get(getAdapterPosition());
int numInterested = Integer.parseInt(ci.getNumber_of_interested()) + 1;
// setting the views here might work,
// but you will find that they reset themselves
// after you scroll up and down (views get recycled).
// find a way to update feedInfoList,
// I like to use EventBus to send an event to the
// host activity/fragment like so:
EventBus.getDefault().post(
new UpdateFeedInfoListEvent(getAdapterPosition(), numInterested));
// in your host activity/fragment,
// update the list and call
// notifyDatasetChanged/notifyDataUpdated()
//on this RecyclerView adapter accordingly
});
}
}
Don't set your position in onBindViewHolder to final (Android Studio will warn you why).
I'm not sure how the object FeedInfo looks like but you could also at a method called for example increaseNumberOfInterested() which would increase the value of Number_of_interested by one and would persist in the object when the recyclerview recycle the cell. it would like kind of like below
#Override
public void onBindViewHolder(final FeedViewHolder contactViewHolder, final int i) {
final FeedInfo ci = feedInfoList.get(i);
//Set the text of the feed with your data
contactViewHolder.numberOfInterestedText.setText(ci.getNumber_of_interested());
contactViewHolder.interestedButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Increase the number of interested in the object, so it can be persisted when cell is reclycled
ci.setNumberOfInterested(ci.getNumber_of_interested()) + 1);
//Get new value and display
contactViewHolder.numberOfInterestedText.setText(Integer.toString(ci.getNumber_of_interested()));
}

Android TV ImageCardView title wrapping

I am developing app for Android TV and using leanback library.
In example from google they are using public class CardPresenter extends Presenter to display content. The problem is, that I want to display all text in titles, i.e. dont cut it.
I already tried:
1) To fix this problem by programmatically setting LayoutParams: cardView.findViewById(R.id.title_text).setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
2) I looked inside lb_image_card_view.xml file for ImageCard. Funny moment, that TextView with id "title_text" already has android:layout_height="wrap_content" parameter, but looks like it doesn't work?? Is it a bug?
3) For a backup plan I can create my own class for ImageCardView, but this solution seems 2 hard.
Thx.
Update.
I used the answer below, but I have to modificative code for better performance:
cardView.setOnFocusChangeListener((view, isFocused) -> {
if (isFocused) {
((TextView) cardView.findViewById(R.id.title_text)).setMaxLines(5);
}
else {
((TextView) cardView.findViewById(R.id.title_text)).setMaxLines(1);
}
});
Try to check this tutorial if it can help you. it helps you to show or display all the text in title.
Here is the code use for this tutorial
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
final ImageCardView cardView = new ImageCardView(mContext);
cardView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, final boolean isFocused) {
final View infoField = view.findViewById(R.id.info_field);
final TextView contentField = (TextView)view.findViewById(R.id.content_text);
final TextView titleField = (TextView)view.findViewById(R.id.title_text);
final Drawable mainImage = ((ImageView)view.findViewById(R.id.main_image)).getDrawable();
if (isFocused) {
((TextView)cardView.findViewById(R.id.title_text)).setMaxLines(3);
FrameLayout.LayoutParams infoLayout = new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
infoField.setLayoutParams(infoLayout);
RelativeLayout.LayoutParams contentLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
contentLayout.addRule(RelativeLayout.BELOW, R.id.title_text);
contentField.setLayoutParams(contentLayout);
}
else {
((TextView)cardView.findViewById(R.id.title_text)).setMaxLines(1);
}
}
});
cardView.setFocusable(true);
cardView.setFocusableInTouchMode(true);
cardView.setBackgroundColor(mContext.getResources().getColor(R.color.fastlane_background));
return new ViewHolder(cardView);
}
And here is the output of this code.
For more information, check this thread also.

How to use Spinner to Save selection to shared preference

I have a spinner view called Spinner_Gender, I made array, array adapter and made onItemSelectedListener. I want to save the selected item position which is integer to shared preference, I tried using a string with Editor and putInt, it saved well. But when reloading the saved data to the spinner using .setSelection it gives an error because it wants an integer not string. Also while trying Integer in sharedpreference I can't save the selected item position to it because the putInt needs only a string to put int in.
Sorry for long question but I searched a lot and can't find answer. Two more questions please:
what is the integer name for spinner selectedItemPosition? How can I store it to sharedpreference?
Code:
final Spinner spinner = (Spinner) findViewById(R.id.Spinner_Gender);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View itemSelected,
final int selectedItemPosition, long selectedId)
{
int selectedPosition = spinner.getSelectedItemPosition();
Editor editor = mGameSettings.edit();
editor.putInt(myNum,selectedPosition);
editor.commit();
}
}
I don't quite understand what's your issue with SharedPreferences.
When you want to save the value you do something like this :
SharedPreferences test = getSharedPreferences("TEST", MODE_MULTI_PROCESS);
Editor editTest = test.edit();
editTest.putInt("key", id_from_spinner);
editTest.commit();
When you want to get the value you do something like this :
SharedPreferences test = getSharedPreferences("TEST", MODE_MULTI_PROCESS);
int id = test.getInt("key", -1);
if(id != -1) {
//use it in your spinner
} else {
//abort because value was not set
}
To convert an int to a String you can use Integer.toString(theInteger) and to convert a String to an int you can use Integer.parseInt(theString).
You should do something like this
spinner.setSelection(dataAdapter.getPosition(genderstring));
Update :
final Spinner spinner = (Spinner) findViewById(R.id.Spinner_Gender);
spinner.setAdapter(adapter);
spinner.setSelection(mGameSettings.getInt("gender", 0));
spinner.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View itemSelected,
final int selectedItemPosition, long selectedId)
{
Editor editor = mGameSettings.edit();
editor.putInt("gender", selectedItemPosition);
editor.commit();
}
}

android remove all buttons inside a view

I have a LinearLayout which I've looped a number of new Button objects into. How do I go about clearing that div correctly (eg removing all the buttons)? I've tried a number of times (unsuccessfully) to do this, but have nothing to show for it.
** edit **
I'm not sure if this helps, but in flex/AS3 I would do something like:
while(myView.numChildren) myView.removeChildAt(0);
** a little code **
View col1 = findViewById(R.id.col1);
for(final Map.Entry<String,HashMap<String,String>> entry : _nav.entrySet()) {
Button item = new Button(this);
item.setText(entry.getKey());
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
openCol2(entry);
}
});
((LinearLayout) col1).addView(item);
}
private final void openCol2(Map.Entry<String,HashMap<String,String>> entry) {
View col2 = findViewById(R.id.col2);
// here is where I want to clean out col2. Right before I add more buttons.
for(int i = 0; i < _nav.size(); ++i) {
Button item = new Button(this);
//item.setText(entry.getKey());
((LinearLayout) col2).addView(item);
}
}
Try this
LinearLayout col2 = (LinearLayout)findViewById(R.id.col2);
col2.removeAllViews();
Assumption: R.id.col2 is of LinearLayout type else to make it more generic typecast it to ViewGroup. Hope this help!!!

Categories