Set gravity of view children from loop of constraintlayout - java

when I access children which are dynamically created as TextView with a loop of a ConstraintLayout, they are returned as View, which don't have setGravity.
Does anyone know a way to set the gravity of these views (gravity for the TextView/child, not the layout)?
for (int i = 0;i < constraintLayout.getChildCount();i++) {
final View child = constraintLayout.getChildAt(i);
Object tag = child.getTag(R.id.tagId);
if(tag != null) {
//child.setGravity(Gravity.START); // Doesn't work...
} else {} }
I didn't see any option for setting Gravity through
ConstraintLayout.LayoutParams or ConstraintSet either.
Also, I tried final TextView child = (TextView) constraintLayout.getChildAt(i);: However, this resulted in a fatal exception: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView. Note: The children are textviews, not imageviews...

Cast the view to TextView first, then call setGravity.
final View child = constraintLayout.getChildAt(i);
Object tag = child.getTag(R.id.tagId);
if (tag != null) {
if (child instanceof TextView) {
TextView tv = (TextView) child;
tv.setGravity(Gravity.START);
}
} else {}

Related

AppCompatButton cannot be cast to android.view.ViewGroup

I am trying to loops through table layout to check the button for a condition and then to change them with setText.
The problem I am having is before that I am getting a ClassCastException.
I see that it says I can't cast a Button to a ViewGroup, but I'm not sure why that it is happening, I am not trying to change anything at that point.
I believe this line (69), is the problem, but nty sure why.
View view = ((ViewGroup) ((ViewGroup) tableLayoutChild).getChildAt(i));
Code:
public Button aiPlayerPick() {
Button btn = null;
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
for (int rowIndex = 0; rowIndex < tableLayout.getChildCount(); rowIndex++) {
View tableLayoutChild = tableLayout.getChildAt(rowIndex);
if (tableLayoutChild instanceof TableRow) {
for (int i = 0; i < ((ViewGroup) tableLayoutChild).getChildCount(); i++) {
View view = ((ViewGroup) ((ViewGroup) tableLayoutChild).getChildAt(i));
if (view instanceof Button && view.getTag() == aiPickedButton) {
View btn_v = view.findViewWithTag(aiPickedButton);
System.out.println("Button: " + btn_v);
//btn = (Button) findViewById(R.id.btn_v);
break;
} else {
i++;
}
}
}
}
return btn;
}
Error:
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.view.ViewGroup
at com.example.richardcurteis.connect3.MainActivity.aiPlayerPick(MainActivity.java:69)
at com.example.richardcurteis.connect3.MainActivity.playerClick(MainActivity.java:49)
at com.example.richardcurteis.connect3.MainActivity.humanPlayerTurn(MainActivity.java:34)
at com.example.richardcurteis.connect3.MainActivity.receiveClick(MainActivity.java:29)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
Even though you're storing a variable of type View, using the cast (ViewGroup) forces a cast to happen before being stored. You're taking the child of the TableRow and casting it to ViewGroup, but its parent is actually View so it fails.
You don't need the second cast, since getChildAt() returns View:
View view = ((ViewGroup) tableLayoutChild).getChildAt(i);

notifyDataSetChanged() is canceling changing setColor in ListView

as in topic, when I use adapter.notifyDataSetChanged() text color in a cell which i have already changed is seting back to default value. I don't know why it happens im putting here me method for changing color:
for(int l=0;l<list.size();l++){
System.out. println("kolorujemy! "+ list.size() );
LinearLayout root = (LinearLayout) getViewByPosition(l,listView);
((TextView) root.findViewById(R.id.wartosc_calosci)).setTextColor(Color.YELLOW);
I would also add that this part of code is in loop in other thread because the vaules of the cells is updating every 30 seconds. Here is method getViewByPosition:
public View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount();
if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition+1;
return listView.getChildAt(childIndex);
}
}
getView:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ListViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ListViewHolder();
convertView = activity.getLayoutInflater().inflate(R.layout.lista_wlasnych_spolek, null);
listViewHolder.txtFirst = (TextView) convertView.findViewById(R.id.nazwa_spolki);
listViewHolder.txtSecond = (TextView) convertView.findViewById(R.id.wartosc_akt);
listViewHolder.txtThird = (TextView) convertView.findViewById(R.id.wartosc_kupna);
listViewHolder.txtFourth = (TextView) convertView.findViewById(R.id.wartosc_calosci);
convertView.setTag(listViewHolder);
} else {
listViewHolder = (ListViewHolder) convertView.getTag();
}
First of all this line
return listView.getAdapter().getView(pos, null, listView);
makes no sense, because with this call by hand you will internally always create and inflate new row for the list view but this view is never used within your ListView. See that you are always passing second parameter convertView null so internally this method will create new view but this view will be never used inside your ListView.
Tip 1. Don't call getView() method yourself
As you may know ListView stores in memory only as many rows/view as they are visible on the screen when you use ViewHolder pattern properly.
So for now you are setting color for every row that is visible and even those not visible that really not exist in ListView.
Tip 2.
Best way to color or change anything about any of your rows, is to do it just inside getView() method implementation depend on your adapter item state. Don't do it from outside because it looks like some kind of a hack or wrong architecture.

getParseObject() returns null

I don't know what's wrong with my code, that it always returns null when I use getParseObject().
I'm using parse.com to save my data, and in one table I used one file as a pointer. I have a Game class that has ImgName as a Pointer<Gallery> to a gallery class.
Now I want to retrieve the ImgName value, so this is what I did:
public Adapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery query = new ParseQuery("Game");
query.include("ImgName");
return query;
}
});
}
// Customize the layout by overriding getItemView
#Override
public View getItemView(final ParseObject object, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.list_item_landing_cards, null);
}
ParseObject gallery = object.getParseObject("ImgName");
String name=gallery.getString("name");
TextView nameTextView = (TextView) v.findViewById(R.id.text);
nameTextView.setText(name);
But I'm getting null all the time.
Any suggestions?
Use this for the re-use issue:
ParseObject gallery = object.getParseObject("ImgName");
if (gallery != null) {
String name=gallery.getString("name");
TextView nameTextView = (TextView) v.findViewById(R.id.text);
nameTextView.setText(name);
} else {
nameTextView.setText(""); // or any other default value you want to set
}
NOTE:
The cell re-use issue is not on Parse. Cell re-use is a general concept used by the ListView. The cells are recycled for performance by Android. We just have to protect it from re-using old values.

Android TableLayout.addView() does not really work

I'm trying to fill my TableLayout with some views with the method addView.
This is what I try to do:
private void createNewTable(){
tableLayout = (TableLayout) getView().findViewById(R.id.tableLayout1);
for(int i = 0; i < objectList.size(); i++){
if(objectList.get(i).getType() == 0){
tableLayout.addView(createLocationObjectInTable((LocationObject)objectList.get(i)), i);
}
}
}
and the createObjectInList method:
private View createLocationObjectInTable(LocationObject locObject) {
TableRow tr = new TableRow(getActivity());
View v = LayoutInflater.from(getActivity()).inflate(R.layout.view_layout, tr, false);
TextView textViewCityName = (TextView) v.findViewById(R.id.textView_City_Name);
TextView textViewCityProvider = (TextView) v.findViewById(R.id.textView_City_Provider);
textViewCityName.setText(locObject.getTitle());
textViewCityProvider.setText(locObject.getSubTitle());
return v;
}
But the views aren't displayed in the tableLayout. Logcat doesn't give me any error message, and when I try to do stuff after the line
tableLayout.addView(createLocationObjectInTable((LocationObject)objectList.get(i)), i);
nothing happend. The app just do nothing after this line.
Hope anyone can tell me my mistake?
You don't add your view to TableRow, instead you just add view to TableLayout. Try this:
private View createLocationObjectInTable(LocationObject locObject) {
...
textViewCityProvider.setText(locObject.getSubTitle());
tr.addView(v);
return tr;
}

add linearlayout dynamically into a Relativelayout

I have this code and i want to add CheckBoxes dynamically inside a LinearLayout that nested inside a ScrollView that nested inside a RelativeLayout( RelativeLayout->ScrollView->LinearLayout->My ChechBoxes)
li = (RelativeLayout) findViewById(R.id.mainlayout);
ScrollView sv = new ScrollView(this);
final LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
li.addView(sv);
sv.addView(ll);
for(int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText("I'm dynamic!");
ll.addView(cb);
}
this.setContentView(sv);
but i get this error:
03-12 20:32:14.840: E/AndroidRuntime(945): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
My RelativeLayout declared in my XML file already
how i can fix this?
this.setContentView(sv);
This tries to add your ScrollView to the FrameLayout android.R.id.content, but you already made li the parent of sv... hence "The specified child already has a parent."
I believe you can remove this.setContentView(sv); since it looks like you only want to add the ScrollView (et al.) to the RelativeLayout, not replace the entire existing layout.
Check this http://developer.android.com/training/animation/screen-slide.html
When you download the sample app, go through LayoutChangesActivity.java
The following is the code to add an item..
private void addItem() {
// Instantiate a new "row" view.
final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(
R.layout.list_item_example, mContainerView, false);
// Set the text in the new row to a random country.
((TextView) newView.findViewById(android.R.id.text1)).setText(
COUNTRIES[(int) (Math.random() * COUNTRIES.length)]);
// Set a click listener for the "X" button in the row that will remove the row.
newView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Remove the row from its parent (the container view).
// Because mContainerView has android:animateLayoutChanges set to true,
// this removal is automatically animated.
mContainerView.removeView(newView);
// If there are no rows remaining, show the empty view.
if (mContainerView.getChildCount() == 0) {
findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
}
}
});
// Because mContainerView has android:animateLayoutChanges set to true,
// adding this view is automatically animated.
mContainerView.addView(newView, 0);
}

Categories

Resources