-1

I have a ListView that every row shows different percentage.

and I wanna change one of inner layouts width (inside inflated counterView) for this purpose .

for example:

if first value item in listview is 10% then width set to 10px too

if second value item is 50% then width change to 50px too

something like this:
enter image description here

How can I do that ?

I tried these codes but it doesn't work and the width doesn't change: (I defined match_parent width for layout inside XML and I wanna change it programmatically)

public class MyAdapterScores extends BaseAdapter {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView  = layoutInflater.inflate(R.layout.scores_layout,null,true);

            //I wanna change 'relativeLayoutRightSqure' width inside 'scores_layout'
            View layout = convertView.findViewById(R.id.relativeLayoutRightSqure);
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) layout.getLayoutParams();
            //variable precentage has value                 
            p.width=precentage ;
            layout.requestLayout();
Amin SZ
  • 359
  • 3
  • 11

3 Answers3

0

You can't just set the width to a percentage I think. I haven't tested this solution, but try it out! Might work!

One solution could be to set the width to a percentage of the total width of the display maybe:

 WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int layoutWidth = size.x * (percentage / 100)

Then you can use that variable "layoutWidth" to set the width on the layout you want.

Andreas Rolén
  • 508
  • 5
  • 15
  • thank you for your reply , but I don't want to set width as percentage.The problem is that I can't change width at all , I wanna find a solution to change the width to any value – Amin SZ Jul 30 '15 at 08:39
  • You could use 'setLayout(width,height)' maybe? – Andreas Rolén Jul 30 '15 at 08:57
  • When I get the initial width by 'layout.getWidth()' , the width was 0 , I think I should change the width somewhere else (except 'getView()') but I don't know where !! – Amin SZ Jul 30 '15 at 09:24
0

please use the following code

 RelativeLayout layout = convertView.findViewById(R.id.relativeLayoutRightSqure);
  RelativeLayout.LayoutParams param=layout.getLayoutParams();
  param.width=precentage ;
  layout.setLayoutParams(param);
Salah Nour ElDin
  • 510
  • 2
  • 13
  • Or you can set Progress bar as item of listview and set percent to progress bar so you didn't need to adjust width of layout progress bar do this – Salah Nour ElDin Jul 30 '15 at 11:48
  • I checked your solution , but it doesn't work, it causes nothing. I don't want use progress bar too , I need different width sizes for different ListView rows in my app , Thank you any way – Amin SZ Jul 31 '15 at 07:37
  • Are you checked progress bar solution – Salah Nour ElDin Jul 31 '15 at 18:43
  • Please set xml to check it because if relativeLayoutRightSqure is the parent layout in xml nothing will change please let me see ur xml code thanks – Salah Nour ElDin Jul 31 '15 at 18:47
  • I found the solution , the problem was in the `xml` file , I wrote my problem as an answer , thank you – Amin SZ Aug 01 '15 at 16:14
0

I found the problem !

the problem was defining android:layout_toRightOf and android:layout_toLeftOf for my inner counterView layout view simultaneously that causes setting width doesn't work !!

( Referred to above code (relativeLayoutRightSqure):

View layout = convertView.findViewById(R.id.relativeLayoutRightSqure);

)

Amin SZ
  • 359
  • 3
  • 11