Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging TextView Text Size in Custom List View in run time
    text
    copied!<p>I'm now implementing a custom <code>ListView</code>. When I tried to change the <code>TextView</code> that is located in <code>CustomAdaper.xml</code> from <code>MainActivity</code>. It show NullPointerException. Isn't it possible to do like this . Help me finding it Please . I get so confused . </p> <p>Below is my <strong>MainActivity.java</strong> Code : </p> <pre><code>setContentView(R.layout.activity_main); txt_Position = (TextView)findViewById(R.id.txtposition);//still ok txt_memberName = (TextView)findViewById(R.id.txtmemberName);// still ok LayoutParams params = (LayoutParams) layout_Date.getLayoutParams(); LayoutParams params1 = (LayoutParams) layout_List.getLayoutParams(); LayoutParams params2 = (LayoutParams) layout_Rotation.getLayoutParams(); int screenSize = getResources().getConfiguration().screenLayout &amp; Configuration.SCREENLAYOUT_SIZE_MASK; switch(screenSize) { case Configuration.SCREENLAYOUT_SIZE_LARGE: Toast.makeText(this, "Large", Toast.LENGTH_LONG).show(); params.height = 100; params1.height = 330 ; params2.height = 200; txt_Position.setTextSize((float)18.0); //locate in CustomAdapter.xml and this is the line logCat Error Pointed . break; </code></pre> <p>CustomAdapter.Java</p> <pre><code>public class CustomAdapter extends BaseAdapter { private Activity activity; private ArrayList data; private static LayoutInflater inflater=null; public Resources res; ListModel getList; int size ; int i=0; public CustomAdapter(Activity a, ArrayList d,Resources resLocal, int size) { activity = a; data=d; res = resLocal; this.size = size; inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public int getCount() { if(data.size()&lt;=0) return 1; return data.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public static class ViewHolder{ public TextView txt_Name; public TextView txt_Status; public TextView txt_Position; public ImageButton imgbtn_senka; public ImageButton imgbtn_fuenka; //public TableRow tblrow_btn; public RelativeLayout layoutbtn; public ImageView image; } public View getView(final int position, View convertView, ViewGroup parent) { View vi=convertView; final ViewHolder holder; if(convertView==null){ /********** Inflate tabitem.xml file for each row ( Defined below ) ************/ vi = inflater.inflate(R.layout.customadapter, null); holder=new ViewHolder(); holder.txt_Name = (TextView)vi.findViewById(R.id.txtmemberName); holder.txt_Status = (TextView)vi.findViewById(R.id.txtmemberStatus); holder.image = (ImageView)vi.findViewById(R.id.imgPlayer); holder.imgbtn_fuenka = (ImageButton) vi.findViewById(R.id.btnfusenka); holder.imgbtn_senka = (ImageButton) vi.findViewById(R.id.btnsenka); holder.txt_Position = (TextView)vi.findViewById(R.id.txtposition); holder.layoutbtn = (RelativeLayout)vi.findViewById(R.id.layoutbutton); vi.setTag(holder); } else holder=(ViewHolder)vi.getTag(); if(data.size()&lt;=0) { holder.txt_Name.setText("No Data"); } else { getList=null; getList = (ListModel) data.get(position); if(getList.getMemberStatus() == 3){ holder.txt_Status.setVisibility(View.INVISIBLE); holder.imgbtn_fuenka.setVisibility(View.VISIBLE); holder.imgbtn_senka.setVisibility(View.VISIBLE); if(getList.getEntry() == 1){ holder.imgbtn_senka.setImageResource(R.drawable.sankagray); holder.imgbtn_senka.setEnabled(false); } else if(getList.getEntry() == 0){ holder.imgbtn_fuenka.setImageResource(R.drawable.fusankagray); holder.imgbtn_fuenka.setEnabled(false); } holder.imgbtn_senka.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String entry = "1"; CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity; sct.updateData(position , entry); holder.imgbtn_senka.setImageResource(R.drawable.sankagray); holder.imgbtn_senka.setEnabled(false); holder.imgbtn_fuenka.setEnabled(true); holder.imgbtn_fuenka.setImageResource(R.drawable.fusanka); } }); holder.imgbtn_fuenka.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String entry = "0"; CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity; sct.updateData(position , entry); holder.imgbtn_fuenka.setImageResource(R.drawable.fusankagray); holder.imgbtn_fuenka.setEnabled(false); holder.imgbtn_senka.setEnabled(true); holder.imgbtn_senka.setImageResource(R.drawable.sanka); } }); } else{ holder.txt_Status.setVisibility(View.VISIBLE); holder.imgbtn_fuenka.setVisibility(View.INVISIBLE); holder.imgbtn_senka.setVisibility(View.INVISIBLE); if(getList.getEntry() == 0){ holder.txt_Status.setText("未確認"); } else if(getList.getEntry() == 1){ holder.txt_Status.setText("参加"); } else if(getList.getEntry() == 2){ holder.txt_Status.setText("不参加"); } else if(getList.getEntry() == 3){ holder.txt_Status.setText("取消"); } } holder.txt_Name.setText(getList.getMemberName()); holder.txt_Position.setText(getList.getPosition()); String url="http://10.0.2.2/football365/Photo/"+getList.getImage(); try { Bitmap bitmap= BitmapFactory.decodeStream((InputStream) new URL(url).getContent()); Bitmap resized = Bitmap.createScaledBitmap(bitmap, 76, 76, false); holder.image.setImageBitmap(resized); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // holder.image.setImageResource(res.getIdentifier("http://10.0.2.2/football365/Photo/"+getList.getImage(),null,null)); } if(size == 1){ holder.txt_Name.setTextSize((float)20.0); } return vi; } public void changeSize(){ ViewHolder holder = new ViewHolder(); holder.txt_Position.setTextSize((float)18.0); holder.txt_Name.setTextSize((float)20.0); } </code></pre> <p>}</p> <p>This is CustomAdapter.xml</p> <pre><code>&lt;TableRow xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:id="@+id/tblrowtotal" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:background="@drawable/blackrow2" android:layout_height="wrap_content"&gt; &lt;ImageView android:id="@+id/imglin1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="2dp" android:src="@drawable/liney" /&gt; &lt;LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" &gt; &lt;ImageView android:layout_marginLeft="3dp" android:id="@+id/imgPlayer" android:layout_width="60dp" android:layout_height="70dp" android:layout_gravity="left|top" android:layout_marginTop="2dp" android:scaleType="center" style="@style/AppBaseTheme" android:background="@null" /&gt; &lt;/LinearLayout&gt; &lt;TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:paddingTop="0dip" &gt; &lt;TableRow&gt; &lt;TextView android:id="@+id/txtmemberName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_marginTop="15dp" android:textColor="#FFFFFF" android:textSize="13sp" /&gt; &lt;/TableRow&gt; &lt;ImageView android:id="@+id/imgview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/linex" /&gt; &lt;TableRow android:layout_width="wrap_content" &gt; &lt;ImageView android:id="@+id/position" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_marginTop="10dp" android:src="@drawable/c_position" /&gt; &lt;TextView android:id="@+id/txtposition" android:layout_width="45dp" android:layout_height="30dp" android:layout_marginLeft="3dp" android:layout_marginTop="7dp" android:textColor="#FFFFFF" android:textSize="15sp" /&gt; &lt;ImageView android:id="@+id/img2" android:layout_width="wrap_content" android:layout_height="match_parent" android:scaleType="center" android:layout_marginLeft="0dp" android:src="@drawable/liney" /&gt; &lt;RelativeLayout android:id="@+id/layoutbutton" android:layout_width="wrap_content" android:gravity="right" android:layout_height="wrap_content" &gt; &lt;TextView android:id="@+id/txtmemberStatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="2dp" android:gravity="center_horizontal" android:textColor="#FFFFFF" android:textSize="16sp" /&gt; &lt;ImageButton android:id="@+id/btnfusenka" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="70dp" android:layout_marginRight="3dp" android:background="@null" android:focusable="false" android:src="@drawable/fusanka"/&gt; &lt;ImageButton android:id="@+id/btnsenka" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="@null" android:focusable="false" android:src="@drawable/sanka" /&gt; &lt;/RelativeLayout&gt; &lt;/TableRow&gt; &lt;ImageView android:id="@+id/img3" android:layout_width="fill_parent" android:layout_marginLeft="0dp" android:layout_height="wrap_content" android:src="@drawable/linex" /&gt; &lt;/TableLayout&gt; &lt;/TableRow&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload