Hello how can i set the item liner Layout background color from this array that it have just 3 color , position one color one , position two color two , position three color three and then position one color one and so on..
the position of the list view
int[] androidColors = context.getResources().getIntArray(R.array.randomColor);
viewHolder.linearLayout.setBackgroundColor(androidColors[position]);
You can try to create your own custom adapter and implement getView function like this :
public View getView (int position, View convertView, ViewGroup parent){
if( convertView == null ){
//We must create a View:
convertView = inflater.inflate(R.layout.my_list_item, parent, false);
}
//Here we can do changes to the convertView, such as set a text on a TextView or set the color of every single item of your view.
//or an image on an ImageView.
return convertView;
}
try to have a look to this post:
Change background colour of current listview item in adapter getView method
Related
I am creating an activity to show timetable. I have a list of lectures, and the respective day and duration of each lecture. I am trying to inflate a compound view from a layout xml file, and then adding the inflated view to one of the relative layouts in an arraylist, where one element of the arraylist represents one column or a day.
days is an arraylist of relativelayouts. The below function is a member of the activity, and is the logic to add the lectures. According to the length of the lecture, I adjust the height with ViewGroup LayoutParams for the card inside the view, and then add the view in relative layout with relative layout params. But the views do not show up.
Edit : The Code is working fine, The arraylist of the model class was a problem.
private void addTimeTableViews(){
for (LectureModel lecture : lectures) {
LayoutInflater inflater = LayoutInflater.from(this);
int currentDay = lecture.getDayOfTheWeek();
RelativeLayout layout = days.get(currentDay);
View view = inflater.inflate(R.layout.layout_tt_box, layout);
int boxHeight = getTimeDifference(lecture.getStartTime(),lecture.getEndTime());
//to adjust height of the card according to length of lecture
MaterialCardView card = view.findViewById(R.id.tt_box_card);
int cardWidth = card.getLayoutParams().width;
ViewGroup.LayoutParams cardParams = new ViewGroup.LayoutParams(cardWidth, boxHeight - 4);
card.setLayoutParams(cardParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.topMargin = getTimeInMinutes(lecture.getStartTime());
layout.addView(view, params);
}
}
How can I add a int value to just one cell in a gridView? for example, using only the first cells and assigning them a value . Is there a way to do that?
This is my grid in the xml:
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:numColumns="3" >
</GridView>
this is my java:
int first = 5;
int second = 6
GridView grid = (GridView) findViewById(R.id.grid);
//I'm missing here a line something like: grid.setContentOfCell (1, first); grid.setContentOfCell (2, second)
I want to assign the int A to the first cell in the grid and B to the second.
In GridView getView Method Check the position is grater than 4 and also put other Cell value according to postion value that you want.
public View getView(int position, View convertView, ViewGroup arg2) {
if(position>3)
{
productPriceTextView.setText("1-4")
}else if(position==a)
Display your value
}else if(position==b)
Display your value
}
Thanks
I select data from a database.
Data in one column, you must convert to kilograms, I need to multiply by a factor of one column. The data in column sets_weight in the pound, and it is necessary to display in kilograms, with without modifying to the database
How to do it correctly. Any ideas! Thanks!
onSets = db.getSets(exesIdsColExes, toprog_dif);
listSets.setAdapter(new SimpleCursorAdapter(this,
R.layout.itemsets, onSets,
new String[] {"sets_ids", "sets_weight", "sets_ones"},
new int[] {R.id.itemsets_ids, R.id.itemsets_weight, R.id.itemsets_ones}) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
// Here we get the textview and set the color
Typeface fontTitleProg = Typeface.createFromAsset(getAssets(), "AGHELVETICA.TTF");
TextView itemsets_ids = (TextView) row.findViewById(R.id.itemsets_ids);
itemsets_ids.setTypeface(fontTitleProg, 1);
itemsets_ids.setGravity(0x05);
TextView itemsets_weight = (TextView) row.findViewById(R.id.itemsets_weight);
itemsets_weight.setTypeface(fontTitleProg, 1);
itemsets_weight.setGravity(0x01);
TextView itemsets_ones = (TextView) row.findViewById(R.id.itemsets_ones);
itemsets_ones.setTypeface(fontTitleProg, 1);
itemsets_ones.setGravity(0x01);
return row;
}
});
I've never done any android development, but the documentation of SimpleCursorAdapter says:
First, if a SimpleCursorAdapter.ViewBinder is available, setViewValue(android.view.View, android.database.Cursor, int) is invoked. If the returned value is true, binding has occured. If the returned value is false and the view to bind is a TextView, setViewText(TextView, String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(ImageView, String) is invoked.
So you just need to pass a custom instance of SimpleCursorAdapter which will, if the column index is 1 (second column), get the value in pounds from the cursor, multiply it to transform pounds into kg, set the result as the text of the view, and return true. For the other columns, return false.
I am developing an app which basically should download a list of events from a website and then views them in a in a calendar like layout. I already covered the first part. But I don't have an idea how to make a layout which would be a grid (tume on one axis and date on second axis) and the events in form of listview would be placed on the grid based on the start and end time. I just need a hint how to start this. The problem is that i have to add the events depending on user choices.
This will create calender like grid programtically..
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
/* INFLATE MAIN TABLE LAYOUT */
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.table_root, null);
TableLayout tbl = (TableLayout) root.findViewById(R.id.tblLayout);
this.setContentView(root);
ViewGroup row = (ViewGroup) inflater.inflate(R.layout.table_row, root, false);
/* GENERIC LOOP FOR CREATING TABLE */
int count = 1;
for (int day = 0; day < calendarCount; day++)
{
row.addView(day);
if (count <= weekCount) {
root.addView(row);
row = (ViewGroup) inflater.inflate(R.layout.popup_row, root, false);
}
count++;
}
Also you can see..
http://w2davids.wordpress.com/android-simple-calendar/
I have a ListView that shows the closest word matches to a search.
For example if I search "hi" I get the following results in the ListView
...
hi
hi five
hi-five
high
highlight
....
I am using
ListView.setSelection(wordList.indexOf(searchWord));
ListView.setSelected(true);
The above code puts the selected word "hi" at the top and doesnt highlight the selection.
I want the "hi" to be centrally positioned , selected and highlighted automatically.
See below
...
hello
hello there
hi
hi-five
hi five
...
What code can I use to achieve the above?
Many thanks.
ListView view = (ListView)findViewById(R.id.YourListView);
int height = view.getHeight();
int itemHeight = view.getChildAt(0).getHeight();
view.setSelectionFromTop(position, height/2 - itemHeight/2);
The position (int) is the listitem you want to center in the listview!!
try setSelectionFromTop() you'll have to do the math yourself. setSelection() bring the selected item to the top of the view, which is what you are seeing.
try this.
first, get visible item count of listview
int count=0;
for (int i = 0; i <= listview.getLastVisiblePosition(); i++)
{
if (listview.getChildAt(i)!= null)
{
count++;
}
}
second, scroll the item to the center of listview.
int target = position-count/2;
if (target<0) target = 0;
listview.setSelection(target);
Try this:
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
for more information :
http://developer.android.com/resources/tutorials/views/hello-listview.html