getview in listadapter - android

In my listadapter code, I would like to indent my view. To do that i add some left padding but that doesnt seem to work.
EDIT: main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout
android:layout_width="80dip" android:layout_height="fill_parent"
android:gravity="right|center_vertical">
<ImageView android:id="#+id/item_image" android:layout_width="wrap_content"
android:src="#drawable/icon" android:layout_height="wrap_content"></ImageView>
</LinearLayout>
<FrameLayout android:id="#+id/frame"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
any suggestions why padding would not work ?? I want to move the image x dp to the right.
#Override
public final View getView(final int position, final View convertView, final ViewGroup parent) {
if (convertView == null) {
final LinearLayout layout = (LinearLayout) layoutInflater.inflate( R.layout.main, null);
layout.setPadding(100, 8,8,8);
return layout;
} else {
//do stuff
}
}
thanks

try this code
#Override
public final View getView(final int position, final View convertView, final ViewGroup parent) {
final View layout = View.inflate(context, R.layout.main, null);
layout.setPadding(100, 8,8,8);
return layout;
}

You are taking the reference of a xml file ..... you should get reference to the layout defined in xml file or any other view of xml file to set params ......
You can set param on the view not on xml file....

If setting padding does not work, you could always try:
layout.offsetLeftAndRight(100);

I guess .. you can give padding in your xml layout using android:padding...... (left or right or top or bottom) . Specifying them in xml layout file will indent the ImageView as per requirements.

Related

Android Custom Adapter Doesn't Work With RelativeLayout as root

I created a custom adapter with an xml with root layout as linear Layout. When i tried to change the root to RelativeLayout to add a button at the right end, each time i inflate the adapter by pressing the button the app stops working
This is my xml with the relative layout that isn't working when run
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/laybackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="#dimen/list_item_height"
android:background="#color/tan_background"
android:orientation="horizontal">
<ImageView
android:id="#+id/theImage"
android:layout_width="#dimen/list_item_height"
android:layout_height="#dimen/list_item_height"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="#id/theImage">
<TextView
android:id="#+id/mokWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
tools:text="mok"
android:textColor="#android:color/white"/>
<TextView
android:id="#+id/engWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
tools:text="eng"
android:textColor="#android:color/white"/>
</LinearLayout>
</RelativeLayout>
And this is my Adapter
public class WordAdapterClass extends ArrayAdapter<MokAndEngClass> {
public int col=0;
public WordAdapterClass(Activity context, #NonNull List<MokAndEngClass> objects,int backroundColor) {
super(context, 0, objects);
col=backroundColor;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View v = convertView;
if(v==null)
{
LayoutInflater lv = LayoutInflater.from(getContext());
v=lv.inflate(R.layout.englishandmoaki,parent,false);
}
MokAndEngClass listItem = getItem(position);
if(listItem!=null){
TextView writtenInMok = (TextView)v.findViewById(R.id.mokWord);
TextView writtenInEng =v.findViewById(R.id.engWord);
ImageView realtedImage =v.findViewById(R.id.theImage);
LinearLayout theTextLayout =(LinearLayout) v.findViewById(R.id.laybackground);
writtenInEng.setText(listItem.getEngWord());
writtenInMok.setText(listItem.getMokWord());
if(listItem.putAnImage()==0)
realtedImage.setVisibility(View.GONE);
else
realtedImage.setBackgroundResource(listItem.putAnImage());
theTextLayout.setBackgroundColor(theTextLayout.getResources().getColor(col));
}
return v;
}
}
The problem is you have changed the layout type from LinearLayout to RelativeLayout without changing the code in the getView() method to reflect that change--you are still trying to cast the root view to an LinearLayout!
Change this line of code:
LinearLayout theTextLayout =(LinearLayout) v.findViewById(R.id.laybackground);
to
RelativLayout theTextLayout =(RelativeLayout) v.findViewById(R.id.laybackground);
and everything should work as before.
Please Note:
When you post a question in the future on SO and you are reporting an issue, please make it clear that "isn't working" actually means "getting an error" and you should post the stack trace from the logcat. This issue would have been clear-up in 10 seconds by the SO community.

Open Listview "Subitems"

I would like to populate my listview with subitems.
My target is, when someone clicks at a listview row, this listview shows a subitem, just like this:
Image
I shouldn't call this a subitem, but I found that this was the best way to describe what I'm trying to do.
I'm not using libraries to do that, I just included in my listview rows an layout, and I'm trying to hide and show it when someone clicks on it.
My Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="400sp"
android:orientation="horizontal"
android:padding="2sp"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200sp"
android:id="#+id/relativeLayout3">
</RelativeLayout>
<include layout="#layout/activity_subitem"
android:layout_width="wrap_content"
android:id="#+id/subitem"
android:layout_height="200sp"
android:layout_below="#+id/relativeLayout3"
android:layout_alignParentStart="true" />
</RelativeLayout>
I built a simple adapter, and I'm trying to implement there my ideas, but is not working, because nothing happens
public View getView(final int position, final View convertView,
final ViewGroup parent) {
View v = super.getView(position, convertView, parent);
layout = (RelativeLayout) v.findViewById(R.id.layout);
subitem = v.findViewById(R.id.subitem);
if (flag){
subitem.setVisibility(View.GONE);
layout.getLayoutParams().height = 200;
}
v.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
subitem.setVisibility(View.VISIBLE);
layout.getLayoutParams().height = 400;
flag = false;
}
});
return v;
}
Can you guys help me?
Thank you.

Change view background color into a listview

I'm trying to change background color on a view component, but not success
public View getView(int position, final View convertView, ViewGroup parent) {
View view = convertView;
try {
if (view == null) {
LayoutInflater vi = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.listview_accounts, null); // --CloneChangeRequired(list_item)
}
final Account listItem = (Account) mList.get(position); // --CloneChangeRequired
if (listItem != null) {
int color = listItem.getColor();
View vColor = (View) view
.findViewById(R.id.lv_account_view_color);
vColor.setBackgroundColor(color);
}
}
} catch (Exception e) {
}
return view;
}
I can set some text in textview, but set color not working.
Can anybody helps me how to set the color? Thanks
The example color used is: -16711717
edit
Listview Item layout:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<View
android:id="#+id/lv_account_view_color"
android:layout_width="#dimen/activity_horizontal_margin"
android:layout_height="wrap_content"
android:background="#167117" />
<TextView
android:id="#+id/lv_account_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_horizontal_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:textColor="#color/black" />
</LinearLayout>
background cannot be setted in xml, it's a dynamic color
Try this: vColor.setBackgroundColor(Color.parse("#yourcolorcode"));
You can directly set the background color of the component in the listview_accounts.xml file of your project. For example
<component>
android:background="#color/color_name"
</component>
You have to make a color.xml file in your values folder(maybe already present) and add the color value ex:-16711717.
try this and let me know
<View
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#167117" />
you can find solution on This link
Just you have to change one thing. In delete.onclick method replace list.removeView(customView); with Customview.setbackground(Color.red); etc. This may help you. Best of luck.

ImageView over ImageView in a GridView

i am trying to use an ImageView over an ImageView in a GridView. I already found this quesiton Android, ImageView over ImageView and it is really familiar with my problem, but i want to use this Layout descripted in the question inside a GridView.
So i created a .xml-file containing the <GridView></GridView> (i think this is not really useful to show, so i skip this file) and a .xml-file with the layout i want to use in every cell in the gridview. This is the following file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rl_act_question_list_alternative" >
<ImageView
android:id="#+id/iv_question_list_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/germany" />
<ImageView
android:id="#+id/iv_question_list_solved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/icon_question_solved" />
As the next step i am creating a subclass of a BaseAdapter overriding the method getView(int position, View convertView, ViewGroup parent) with the following code:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(ActivityLevelsAlternative.this).inflate(R.layout.act_question_list_alternative_item, null);
}
ImageView mCountry = (ImageView) convertView.findViewById(R.id.iv_question_list_country);
ImageView mIconSolved = (ImageView) convertView.findViewById(R.id.iv_question_list_solved);
final VOQuestion question = mLevel.getAllQuestions().get(position);
mCountry.setImageResource(question.getPicture());
mIconSolved.setImageResource(mPrefManager.isQuestionSolved(question.getId()) ? R.drawable.icon_question_solved : R.drawable.icon_question_unsolved);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), ActivityQuestion.class);
i.putExtra(ActivityQuestion.QUESTION_ID, question.getId());
ActivityLevelsAlternative.this.startActivity(i);
}
});
return convertView;
}
But the the result is not the expected... see: http://q63i.img-up.net/Screenshotc656.png
My question: how can i get the check mark over the country image?
Try to change your check mark image like this:
<ImageView
android:id="#+id/iv_question_list_solved"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignTop="#id/iv_question_list_country"
android:layout_alignLeft="#id/iv_question_list_country"
android:layout_alignRight="#id/iv_question_list_country"
android:layout_alignBottom="#id/iv_question_list_country"/>

Android setting ListView row heights depending on the screen resolution

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/venueImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
I have an ordinary listview and a custom adapter using the xml above for the row.
I will only have 3 rows, but I want those 3 rows to have equal heights to fit the height of the screen's resolution. At the moment I don't even know how to set the height of the rows because setting the LinearLayout's height doesnt do anything.
You can set the height of your ImageView to get what you want. To set it programmatically, you can do this on the getView() of your custom adapter:
public View getView(int position, View convertView, ViewGroup parent) {
View cv = convertView;
if (convertView == null) {
cv = inflater.inflate(R.layout.listview_item, null);
}
ImageView venueImage = (ImageView) cv.findViewById(R.id.venueImage);
LinearLayout.LayoutParams vi_params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, (int)(screenHeight*0.33);
venueImage.setLayoutParams(vi_params);
return cv;
}
You can get the screen height by adding this code on your main activity:
int screenHeight = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();

Categories

Resources