I want to display the video thumbnails in listview, I use images instead of thumbnails and add ImageView (image play button) on the front but failed my xml code like this
<?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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txttgl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:paddingLeft="10dip"
android:gravity="center_horizontal"/>
<TextView
android:id="#+id/tv_batas_unread"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:paddingLeft="12dip"
android:background="#drawable/counter_shape_birudonker"
android:visibility="gone"
android:gravity="center_horizontal"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ll_chat_item"
android:orientation="horizontal" >
<com.ltvie.chatkrawala.ImageViewRounded
android:id="#+id/img_photo_pp_chatbox"
android:layout_width="50dp"
android:layout_height="50dp"
android:paddingBottom="0dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:adjustViewBounds="true"
android:paddingTop="0dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="3dp"
android:src="#drawable/gada_photo"
android:scaleType="fitXY" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical"
android:layout_marginBottom="12dp"
android:layout_weight="1"
android:cacheColorHint="#android:color/transparent"
>
<TextView
android:id="#+id/txtKodeGbr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="#+id/txtPesan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="#+id/comment"
android:layout_marginTop="9dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buble_kiri"
android:text="Bismillahirohmanirrohim"
android:textColor="#000"
android:textSize="15sp" />
<LinearLayout
android:id="#+id/rowFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="wrap_content"
>
<ImageView
android:id="#+id/img_dilvChat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:contentDescription="#string/descGambar"
android:background="#drawable/buble_kanan"
android:src="#drawable/gada_photo"
/>
<ImageView
android:id="#+id/img_btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/descGambar"
android:layout_centerInParent="true"
android:src="#drawable/play_icon"
/>
<ProgressBar
android:id="#+id/chatBox_progress_img"
style="?android:attr/progressBarStyleSmallInverse"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<LinearLayout
android:id="#+id/rowFileDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_gravity="bottom"
android:orientation="vertical">
<TextView
android:id="#+id/txtFileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:gravity="left"
android:text="Nama File : test.zip"
android:textSize="12sp" />
<TextView
android:id="#+id/txtFileSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:gravity="left"
android:text="Ukuran : 2324342 bytes"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/img_statusBaca"
android:layout_marginTop="2dip"
android:layout_width="15dip"
android:layout_height="15dip"
android:layout_marginRight="1dip"
android:background="#drawable/indicator_sending"
android:contentDescription="#string/descGambar"
/>
<TextView
android:id="#+id/txtjamchatMasuk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:gravity="left"
android:text="setatus baca"
android:textSize="12sp" />
<TextView
android:id="#+id/txtKiriKanan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:text="right"
android:visibility="gone"
/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/imgselected"
android:layout_width="20dp"
android:layout_height="20dp"
android:contentDescription="#string/kosongan"
android:src="#drawable/ico_member" />
</LinearLayout>
</LinearLayout>
my java code
public class ChatboxArrayAdapter extends ArrayAdapter<OneComment> implements SectionIndexer{
private TextView countryName;
private LinearLayout wrapper,rowFileDesc,ll_chat_item;
private TextView TanggalMasuk,txtKodeGbr,txtPesan,txtJam,txtKiriKanan,txtUkuranFile;
private ImageView img_upload,img_statusbaca,imgPp_round;
ImageView imgPhoto,imgCheck,imgBtnPlay;
String namafileGambar,strPath,varStsBaca,tampilTgl;
public CacheImageLoader imageLoader;
Context ctx;
Options opts = new BitmapFactory.Options();
String TAG="chatboxArrayAdapter";
File dir = new File(Environment.getExternalStorageDirectory()+"");
File dirImage=new File(dir+"/a");
File dirVideo=new File(dir+"/a");
List<OneComment> countries;
#Override
public void add(OneComment object) {
countries.add(object);
super.add(object);
}
public void hapus_semua(){
countries.clear();
}
public void hapus_item(int nomerx){
countries.remove(nomerx);
notifyDataSetChanged();
}
public void refresh_lv(){
notifyDataSetChanged();
Log.d("notify", "datachange");
}
public ChatboxArrayAdapter(Context context, int textViewResourceId, List<OneComment> datanya) {
super(context, textViewResourceId);
this.countries=datanya;
this.ctx=context;
imageLoader=new CacheImageLoader(context.getApplicationContext());
}
public int getCount() {
return this.countries.size();
}
#Override
public OneComment getItem(int index) {
return this.countries.get(index);
}
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
try{
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.chatbox_detail, parent, false);
}
OneComment isiOneComen = getItem(position);
wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
rowFileDesc=(LinearLayout) row.findViewById(R.id.rowFileDesc);
ll_chat_item=(LinearLayout) row.findViewById(R.id.ll_chat_item);
imgPhoto = (ImageView) row.findViewById(R.id.img_dilvChat);
countryName = (TextView) row.findViewById(R.id.comment);
txtKiriKanan = (TextView) row.findViewById(R.id.txtKiriKanan); //isi dari coment untuk menentukan letak buble di kiri ato dikanan,hanya untuk buble file untuk menentukan filenya masih di server apa sudah di downloa
TanggalMasuk= (TextView) row.findViewById(R.id.txttgl);
img_upload=(ImageView) row.findViewById(R.id.img_dilvChat);
txtKodeGbr = (TextView) row.findViewById(R.id.txtKodeGbr);
txtPesan=(TextView) row.findViewById(R.id.txtPesan);
txtJam=(TextView) row.findViewById(R.id.txtjamchatMasuk);
txtUkuranFile=(TextView) row.findViewById(R.id.txtFileSize);
imgCheck=(ImageView) row.findViewById(R.id.imgselected);
img_statusbaca=(ImageView) row.findViewById(R.id.img_statusBaca);
imgPp_round=(ImageView) row.findViewById(R.id.img_photo_pp_chatbox);
imgBtnPlay=(ImageView) row.findViewById(R.id.img_btn_play);
txtPesan.setVisibility(View.GONE);
txtKodeGbr.setVisibility(View.GONE);
img_upload.setVisibility(View.GONE);
rowFileDesc.setVisibility(View.GONE);
if(isiOneComen.comment.trim().equalsIgnoreCase("video")){
//error goes here
countryName.setVisibility(View.GONE);
Log.d("fileDesc", isiOneComen.Filedesc);
//strPath = namafileGambar;
strPath = moduleGlobal.dirImageSent+"/"+isiOneComen.Filedesc;
imgPhoto.setImageBitmap(changeSize(strPath));
scaleImage(imgPhoto, ((int) isiOneComen.lebarScreen)-((int) isiOneComen.lebarScreen/4));
imgBtnPlay.setVisibility(View.VISIBLE);
}else{ //run work well
countryName.setVisibility(View.VISIBLE);
try{
countryName.setText(getSmiledText(getContext(),isiOneComen.comment.toString()));
}catch (Exception e) {
// TODO: handle exception
Log.e("addsmiley", e.toString());
}
countryName.setBackgroundResource(isiOneComen.left ? R.drawable.buble_kiri : R.drawable.buble_kanan);
}
}catch (Exception ez) {
ll_chat_item.setVisibility(View.GONE);
Log.e(TAG, ez.toString());
}
return row;
}
private Bitmap changeSize(String path){
opts.inSampleSize = 1;
Bitmap bm = BitmapFactory.decodeFile(path);
if (bm.getHeight() > 150 || bm.getWidth() > 150) {
final int halfHeight = bm.getHeight() / 2;
final int halfWidth = bm.getWidth() / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / opts.inSampleSize) > 150
&& (halfWidth / opts.inSampleSize) > 150) {
opts.inSampleSize *= 2;
}
}
bm = BitmapFactory.decodeFile(path,opts); // this bitmap will be 1/8 the size of the original
return bm;
}
private void scaleImage(ImageView view, int boundBoxInDp)
{
// Get the ImageView and its bitmap
Drawable drawing = view.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// Determine how much to scale: the dimension requiring less scaling is
// closer to the its side. This way the image always stays inside your
// bounding box AND either x/y axis touches it.
float xScale = ((float) boundBoxInDp) / width;
float yScale = ((float) boundBoxInDp) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
// Create a matrix for the scaling and add the scaling data
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
// Create a new bitmap and convert it to a format understood by the ImageView
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
#SuppressWarnings("deprecation")
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
width = scaledBitmap.getWidth();
height = scaledBitmap.getHeight();
// Apply the scaled bitmap
view.setImageDrawable(result);
// Now change ImageView's dimensions to match the scaled image
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
public Bitmap decodeToBitmap(byte[] decodedByte) {
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
/**tambah smiley
*
*/
private static final HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
static {
emoticons.put(":)", R.drawable.s1);
emoticons.put(":D", R.drawable.s2);
emoticons.put(":(", R.drawable.s3);
emoticons.put("8o|", R.drawable.s16);
emoticons.put(":/", R.drawable.s17);
}
// Get image for each text smiles
public static Spannable getSmiledText(Context context, String text) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
int index;
for (index = 0; index < builder.length(); index++) {
for (Entry<String, Integer> entry : emoticons.entrySet()) {
int length = entry.getKey().length();
if (index + length > builder.length())
continue;
if (builder.subSequence(index, index + length).toString().equals(entry.getKey())) {
builder.setSpan(new ImageSpan(context, entry.getValue()), index, index + length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += length - 1;
break;
}
}
}
return builder;
}
#Override
public int getPositionForSection(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public int getSectionForPosition(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object[] getSections() {
// TODO Auto-generated method stub
return null;
}
}
everything work well before i add relative layout,I want to add image view center with other imageview but I get error log
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
Problem with
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
This line Change this line to
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
Because you forgot to Add
</LinearLayout>
</LinearLayout>
at the end of your xml.
Use this
<?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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:gravity="center_horizontal"
android:paddingLeft="10dip" />
<LinearLayout
android:id="#+id/ll_chat_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_weight="1"
android:cacheColorHint="#android:color/transparent"
android:gravity="left"
android:orientation="vertical" >
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<LinearLayout
android:id="#+id/rowFile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/img_dilvChat"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="3dp"
android:background="#drawable/buble_kanan"
android:contentDescription="#string/descGambar" />
<ImageView
android:id="#+id/img_btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="#string/descGambar"
android:src="#drawable/play_icon" />
</RelativeLayout>
<TextView
android:id="#+id/tv4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:gravity="center_horizontal"
android:paddingLeft="10dip" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Related
My Fragment is taking so much time in update layout It is because of a lot of code in fragment how to resolve this please help me
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/lyttabs"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:gravity="center"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.bugfree.caviar.fonts.BabesNeueProBold
android:textStyle="bold"
android:layout_weight="1"
android:textColor="#color/black_text"
android:textSize="30dp"
android:layout_width="0dp"
android:text="My Events That I am..."
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/btn_plus"
android:src="#drawable/plus"
android:layout_width="20dp"
android:layout_height="20dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabLayout
app:tabIndicatorColor="#color/button_red_back"
android:id="#+id/tabs"
android:layout_marginTop="15dp"
app:tabBackground="#drawable/tab_indicator_color"
app:tabTextAppearance="#style/MyCustomTextAppearance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
<androidx.viewpager.widget.ViewPager
android:layout_marginTop="30dp"
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
</LinearLayout>
<LinearLayout android:id="#+id/lytCreateEvent"
android:visibility="gone"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.CreateEvent">
<ImageView
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="30dp"
android:id="#+id/imgBack"
android:src="#drawable/back_black"
android:layout_width="20dp"
android:layout_height="20dp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp">
<com.bugfree.caviar.fonts.BabesNeueProBold
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create event"
android:textColor="#color/black"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="vertical">
<com.bugfree.caviar.fonts.BabesNeueProBoldRegular
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Event title" />
<EditText
android:singleLine="true"
android:paddingStart="15dp"
android:layout_marginTop="10dp"
android:textColorHint="#color/black"
android:background="#drawable/edit_back"
android:id="#+id/edtEventTitle"
android:textSize="18dp"
android:layout_gravity="center"
android:hint="Beach Yoga!"
android:layout_width="match_parent"
android:layout_height="50dp" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.bugfree.caviar.fonts.BabesNeueProBoldRegular
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Event type" />
</LinearLayout>
<RelativeLayout
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#drawable/edit_back"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<com.bugfree.caviar.fonts.BabesNeueProBoldRegular
android:layout_centerVertical="true"
android:paddingStart="15dp"
android:layout_width="wrap_content"
android:textColor="#color/black"
android:layout_height="wrap_content"
android:text="Business"
android:textSize="18dp" />
<ImageView
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="15dp"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_alignParentEnd="true"
android:src="#drawable/down_arrow" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<com.bugfree.caviar.fonts.BabesNeueProBoldRegular
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event description" />
<com.bugfree.caviar.fonts.BabesNeueProBoldRegular
android:layout_marginEnd="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="94/300" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/custombutton_border_edittext"
android:orientation="vertical">
<EditText
android:textSize="18dp"
android:id="#+id/edtDescription"
android:textColorHint="#color/black_text_new"
android:padding="10dp"
android:layout_gravity="start"
android:gravity="start"
android:hint="Beach yoga with Alissia is a lifestyle focused on health,
wellness, and self-care through the practice of"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#android:color/transparent" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<ImageView
android:layout_marginEnd="10dp"
android:layout_centerVertical="true"
android:tint="#color/black"
android:layout_alignParentEnd="true"
android:src="#drawable/down_arrowsmall"
android:layout_width="10dp"
android:layout_height="10dp" />
</RelativeLayout>
<EditText
android:paddingLeft="10dp"
android:layout_gravity="start"
android:gravity="start"
android:id="#+id/edtSplReq"
android:layout_width="match_parent"
android:layout_marginTop="15dp"
android:layout_height="100dp"
android:singleLine="true"
android:padding="10dp"
android:background="#drawable/lay_without_border"
android:hint="Add a special request[optional]"
android:textColorHint="#848484"
android:textSize="18dp" />
<Button
android:outlineProvider="bounds"
android:stateListAnimator="#null"
style="?android:attr/borderlessButtonStyle"
android:layout_marginBottom="35dp"
android:id="#+id/btnSubmit"
android:layout_marginTop="30dp"
android:textSize="18dp"
android:textAllCaps="false"
android:text="Submit"
android:textColor="#color/space_white"
android:background="#drawable/custombutton"
android:layout_width="match_parent"
android:layout_height="35dp" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/lytTab"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabLayout
app:tabBackground="#drawable/tab_indicator_color"
app:tabIndicatorColor="#color/button_red_back"
android:id="#+id/tabsLOc"
android:layout_marginTop="10dp"
app:tabTextAppearance="#style/MyCustomTextAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPagerLoc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
and here is my fragment
public class EventFragment extends Fragment {
private View view;
ViewPagerAdapter viewPagerAdapter;
LocationPagerAdapter locationPagerAdapter;
private String current = "";
private String ddmmyyyy = "MMDDYYYY";
private Calendar cal = Calendar.getInstance();
FragmentEventBinding binding;
private OnFragmentInteractionListener mListener;
public EventFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(
inflater, R.layout.fragment_event, container, false);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Bebas Neue Pro Regular.otf");
binding.lytTopImg.setOnClickListener(v -> {
binding.lytLocDetails.setVisibility(View.VISIBLE);
binding.lytPartLocNew.setVisibility(View.GONE);
});
binding.imgTopEvent.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.dummy),-70));
binding.imgBackLocDetail.setOnClickListener(v -> {
binding.lytLocDetails.setVisibility(View.GONE);
binding.lytPartLocNew.setVisibility(View.VISIBLE);
/* Intent intent = new Intent(PartnerLocationActivity.this, LoactionDetails.class);
startActivity(intent);*/
});
binding.btnPlus.setOnClickListener(v -> {
binding.lyttabs.setVisibility(View.GONE);
binding.lytCreateEvent.setVisibility(View.VISIBLE);
});
binding.imgBack.setOnClickListener(v -> {
mListener.changeFragment(2);
binding.lyttabs.setVisibility(View.VISIBLE);
binding.lytCreateEvent.setVisibility(View.GONE);
});
binding.buttonCreateEvent.setOnClickListener(v -> {
binding.lyttabs.setVisibility(View.VISIBLE);
binding.lytCreateEvent.setVisibility(View.GONE);
});
binding.imgBackPartnerLoc.setOnClickListener(v -> {
binding.lytPartLocNew.setVisibility(View.GONE);
binding.lytCreateEvent.setVisibility(View.VISIBLE);
/* Intent intent = new Intent(getActivity(), CreateEvent.class);
startActivity(intent);*/
});
binding.lytTopPartLoc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
binding.lytLocDetails.setVisibility(View.VISIBLE);
binding.lytPartLocNew.setVisibility(View.GONE);
}
});
binding.buttonCreateEvent.setTypeface(font);
binding.edtEventTitle.setTypeface(font);
binding.edtDescription.setTypeface(font);
binding.edtNameLoc.setTypeface(font);
binding.edtAddressLoc.setTypeface(font);
binding.rdbOne.setTypeface(font);
binding.rdbGroup.setTypeface(font);
binding.buttonPartnerloc.setOnClickListener(v -> {
binding.lytPartLocNew.setVisibility(View.VISIBLE);
binding.lytCreateEvent.setVisibility(View.GONE);
/* Intent intent = new Intent(CreateEvent.this, PartnerLocationActivity.class);
startActivity(intent);*/
});
binding.buttonCreateEvent.setOnClickListener(v -> {
binding.lyttabs.setVisibility(View.VISIBLE);
binding.lytCreateEvent.setVisibility(View.GONE);
});
binding.edtDob.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals(current)) {
String clean = s.toString().replaceAll("[^\\d.]|\\.", "");
String cleanC = current.replaceAll("[^\\d.]|\\.", "");
int cl = clean.length();
int sel = cl;
for (int i = 2; i <= cl && i < 6; i += 2) {
sel++;
}
if (clean.equals(cleanC)) sel--;
if (clean.length() < 8) {
clean = clean + ddmmyyyy.substring(clean.length());
} else {
int day = Integer.parseInt(clean.substring(0, 2));
int mon = Integer.parseInt(clean.substring(2, 4));
int year = Integer.parseInt(clean.substring(4, 8));
mon = mon < 1 ? 1 : mon > 12 ? 12 : mon;
cal.set(Calendar.MONTH, mon - 1);
year = (year < 1900) ? 1900 : (year > 2100) ? 2100 : year;
cal.set(Calendar.YEAR, year);
day = (day > cal.getActualMaximum(Calendar.DATE)) ? cal.getActualMaximum(Calendar.DATE) : day;
clean = String.format("%02d%02d%02d", day, mon, year);
}
clean = String.format("%s/%s/%s", clean.substring(0, 2),
clean.substring(2, 4),
clean.substring(4, 8));
sel = sel < 0 ? 0 : sel;
current = clean;
binding.edtDob.setText(current);
binding.edtDob.setSelection(sel < current.length() ? sel : current.length());
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
new Thread(new Runnable() {
#Override
public void run() {
try {
}catch (Exception ignored){
}
}
}).start();
viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
binding.viewPager.setAdapter(viewPagerAdapter);
binding.tabs.setupWithViewPager( binding.viewPager);
/*binding.imgBack.setOnClickListener(v -> {
binding.lytLocDetails.setVisibility(View.GONE);
binding.lytPartLocNew.setVisibility(View.VISIBLE);
});*/
locationPagerAdapter = new LocationPagerAdapter(getFragmentManager());
binding.viewPagerLoc.setAdapter(locationPagerAdapter);
binding.tabsLOc.setupWithViewPager(binding.viewPagerLoc);
binding.edtSplReq.setTypeface(font);
binding.edtEmail.setTypeface(font);
binding.edtPhone.setTypeface(font);
binding.edtLastName.setTypeface(font);
binding.edtFirstname.setTypeface(font);
binding.btnSubmit.setTypeface(font);
binding.btnFindTable.setTypeface(font);
binding.btnFindTable.setOnClickListener(v -> {
binding.lytBtnFind.setVisibility(View.GONE);
binding.lytTab.setVisibility(View.GONE);
binding.lytBookTable.setVisibility(View.VISIBLE);
});
binding.btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
binding.lyttabs.setVisibility(View.VISIBLE);
binding.lytLocDetails.setVisibility(View.GONE);
binding.lytPartLocNew.setVisibility(View.GONE);
binding.lytCreateEvent.setVisibility(View.GONE);
binding.lytBookTable.setVisibility(View.GONE);
binding.lytBtnFind.setVisibility(View.VISIBLE);
binding.lytTab.setVisibility(View.VISIBLE);
binding.viewPager.setCurrentItem(1);
binding.tabs.getSelectedTabPosition();
}
});
return view = binding.getRoot();
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public Bitmap SetBrightness(Bitmap src, int value) {
// original image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
// scan through all pixels
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
// increase/decrease each channel
R += value;
if(R > 255) { R = 255; }
else if(R < 0) { R = 0; }
G += value;
if(G > 255) { G = 255; }
else if(G < 0) { G = 0; }
B += value;
if(B > 255) { B = 255; }
else if(B < 0) { B = 0; }
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
// return final image
return bmOut;
}
}
There are two parts to this answer.
As per https://developer.android.com/topic/performance/rendering/optimizing-view-hierarchies
Android Layouts allow you to nest UI objects in the view hierarchy. This nesting can also impose a layout cost.
Your layout has multiple levels of nesting, this can have a large cost to layout.
Simplifying you layout with a more flexible layout like a ConstraintLayout Might reduce the layout cost.
Sometimes you are just trying to do too much when creating a view, do some of the work in a background thread and update the view when done (optionally putting up a busy type of progress bar while doing it)
Some idea's based on your code shown.
Instead of scanning every pixel of an image with the SetBrightness method which could take some time depending on the size of the image.
Add the image to the layout without the Brightness adjusted, start background task adjust image Brightness and when done replace the original un-adjusted image with the adjusted image.
This could probably be done without a busy progress bar.
Delay the creation of the offscreen viewpager pages until they are really needed.
Instead of each Fragment in the viewpager creating it's view in onCreateView when the Viewpager is created, create a placeholder view in the Fragment's onCreateView and update it in the Fragment's onResume which is only called when the Fragment is shown on screen (note need a recent'ish version of Viewpager for this behaviour)
I'm trying to recreate the following layout in Android:
The grid does not have to grow dynamically, it will always be 5 columns & 2 rows. The orientation is fixed to landscape. Each cell has the exact same layout, just the data in the TextViews can change.
My current approach is to fill a GridLayout with TableLayouts and setting appropriate layout_row & layout_column for each TableLayout. Like so :
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="2"
android:columnCount="4"
android:orientation="vertical">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:background="#color/colorAccent">
Which results in this:
Code from picture above : https://pastebin.com/khr9aGWf
I'm running into a few problems.
I cannot get the cells to automatically scale to fit the screen.
The size of this layout file is starting to become very large and hard to manage
Perhaps my approach is completely wrong and I need to go towards something where I define the layout of a cell once and dynamically fill a grid with it?
Any help would be greatly appreciated.
You can create this kind of layout using GridView
<GridView
android:id="#+id/list"
android:numColumns="5"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"/>
your gridview item layout
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:background="#color/colorPrimary">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
GridviewAdapter.java
public class GridviewAdapter extends BaseAdapter {
Activity context;
private LayoutInflater mInflater;
TableLayout.LayoutParams params;
public GridviewAdapter(Activity context)
{
this.context=context;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
params=new TableLayout.LayoutParams(new TableLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
(int) ((getDeviceHeight(context)-getStatusBarHeight()) / 2)));
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position%2==0)
{
convertView.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
}else{
convertView.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
}
convertView.setLayoutParams(params);
return convertView;
}
#Override
public int getCount() {
return 10;
}
#Override
public String getItem(int position) {
return "";
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
}
public static int getDeviceHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
Point point = new Point();
wm.getDefaultDisplay().getSize(point);
return point.y;
} else {
return wm.getDefaultDisplay().getHeight();
}
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
and in your activity class add code like this
GridviewAdapter itemsAdapter = new GridviewAdapter(this);
GridView listView = (GridView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
your output look like this
I'm wondering if there's a more efficient way to handle horizontal scrolling with a good number of images.
Right now I'm using a HorizontalScrollView component with a Frame Layout as its only child. Then I'm laying out the ImageViews on the Layout. It works but I'm noticing that on certain devices the scrolling is very choppy but on others its very smooth.
So I was wondering if there is a better and more efficient way to handle it.
Some code snippets:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/app_background"
tools:context="com.toyota.fanmojis.PostActivity">
<RelativeLayout
android:layout_height="265dp"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:id="#+id/keyboardLayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#C9C9C9"
android:layout_alignParentTop="true"
android:id="#+id/shareLayout">
<RelativeLayout
android:id="#+id/socialLayout"
android:layout_centerHorizontal="true"
android:layout_width="217dp"
android:layout_height="40dp">
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/fb_button"
android:layout_marginLeft="8dp"
android:layout_centerVertical="true"
android:background="#drawable/fb_color"
android:scaleType="fitXY"
android:contentDescription="Post via Facebook" />
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/twitter_button"
android:layout_toRightOf="#id/fb_button"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:background="#drawable/twitter_color"
android:scaleType="fitXY"
android:contentDescription="Post via Twitter" />
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/sms_button"
android:layout_toRightOf="#id/twitter_button"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:background="#drawable/sms_color"
android:scaleType="fitXY"
android:contentDescription="Post via SMS" />
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/email_button"
android:layout_toRightOf="#id/sms_button"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:background="#drawable/email_color"
android:scaleType="fitXY"
android:contentDescription="Post via SMS" />
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/copy_paste_button"
android:layout_toRightOf="#id/email_button"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:background="#drawable/copy"
android:scaleType="fitXY"
android:contentDescription="Post via SMS" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="#string/share"
android:id="#+id/textView"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/socialLayout"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/fanmojisLayout"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/shareLayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="32dp"
android:id="#+id/bottomViewLayout"
android:layout_alignParentBottom="true"
android:background="#drawable/red_background">
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/homeButton"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:background="#drawable/home"
android:scaleType="fitXY"
android:contentDescription="Go back to home" />
<ImageButton
android:layout_width="44dp"
android:layout_height="31dp"
android:id="#+id/backspaceButton"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:background="#drawable/delete_selector"
android:layout_marginRight="1dp"
android:layout_marginLeft="4dp"
android:scaleType="fitXY"
android:contentDescription="Go back to home" />
<Button
android:layout_width="44dp"
android:layout_height="31dp"
android:id="#+id/clearButton"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/backspaceButton"
android:background="#drawable/clear_selector"
android:scaleType="fitXY"
android:contentDescription="Go back to home" />
</RelativeLayout>
<com.upinteractive.fanmojis.components.FanmojiHorizontalScrollView
android:id="#+id/fanmojisScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/bottomViewLayout"
android:scrollbars="none"
android:animationCache="false"
android:orientation="horizontal">
<FrameLayout
android:id="#+id/fanmojiContentLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</com.upinteractive.fanmojis.components.FanmojiHorizontalScrollView>
</RelativeLayout>
</RelativeLayout>
<com.upinteractive.fanmojis.components.ChatBubble
android:id="#+id/chatBubble"
android:layout_width="220dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:background="#drawable/chat"/>
<RelativeLayout
android:id="#+id/errorLayout"
android:layout_width="300dp"
android:layout_height="180dp"
android:layout_marginTop="80dp"
android:layout_centerHorizontal="true"
android:background="#drawable/error_layout_bg"
android:visibility="invisible">
<TextView
android:id="#+id/errorText"
android:textColor="#android:color/white"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
And the code that loads the frame layout with the images:
protected void setupScroll() {
double hspacing = (displaymetrics.widthPixels - (IMAGE_SIZE * 6)) / 7;
double hfixedspacing = hspacing;
double vspacing = 5.0;
double max = 0;
double topSpacing = 20.0;
double xPosition = hspacing;
double yPosition = topSpacing;
int row = 0;
int col = 0;
int viewcount = imageArray.length() / 18;
int mod = (imageArray.length() % 18);
if (mod > 6)
viewcount++;
FrameLayout fl = (FrameLayout) this.findViewById(R.id.fanmojiContentLayout);
fl.setBackgroundColor(getResources().getColor(R.color.kb_bg_color));
scrollView.setBackgroundColor(getResources().getColor(R.color.kb_bg_color));
int inSampleSize = 0;
try {
for (int i = 0; i < imageArray.length(); i++) {
JSONObject emoji = imageArray.getJSONObject(i);
String basename = emoji.getString("name");
String fullname = "f" + basename + "_small";
int resourceID = getResources().getIdentifier(fullname, "drawable", getPackageName());
if(i == 0){
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), resourceID, options);
inSampleSize = Util.calculateInSampleSize(options,IMAGE_SIZE,IMAGE_SIZE);
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = inSampleSize;
Bitmap b = BitmapFactory.decodeResource(getResources(), resourceID, options);
FMImageView iv = new FMImageView(this);
iv.name = basename;
iv.fullname = fullname;
iv.setImageBitmap(b);
FrameLayout.LayoutParams imageParams = new FrameLayout.LayoutParams(IMAGE_SIZE, IMAGE_SIZE);
imageParams.leftMargin = (int) xPosition;
imageParams.topMargin = (int) yPosition;
fl.addView(iv, imageParams);
col++;
if (col > 5) {
col = 0;
row++;
if (row > 2) {
xPosition += hspacing + IMAGE_SIZE;
hfixedspacing = xPosition;
} else {
xPosition = hfixedspacing;
}
yPosition += vspacing + IMAGE_SIZE;
} else {
xPosition += hspacing + IMAGE_SIZE;
}
if (xPosition > max)
max = xPosition;
if (row > 2) {
xPosition = hfixedspacing;
yPosition = topSpacing;
row = 0;
col = 0;
}
}
} catch (JSONException je) {
je.printStackTrace();
}
fl.setMinimumWidth((int) max + 200);
}
You would probably get better performance with a scrolling component that recycles its views.
Try using a RecyclerView with a GridLayoutManager that has the orientation property set to "horizontal".
You'll need to code an adapter for the RecyclerView.
I have list of button in bottom of the page which is in horizontal listview, i want to show only 4 row in listview, when screen become bigger it's item size should also become bigger so only 4 item shows.
for example
here you can see that after increasing size of width it's related horizontal listview's listitem also increasing
is it possible? if possible than can any body suggest me how we can handle it ?
Create one custom layout class like below..
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLinearLayout(Context context) {
super(context);
}
public void setLayoutParams(int width) {
LayoutParams params = new LayoutParams(width / 4,
LayoutParams.WRAP_CONTENT);
this.setLayoutParams(params);
}
#Override
public void setLayoutParams(android.view.ViewGroup.LayoutParams params) {
super.setLayoutParams(params);
}
}
put this horizontal scrollview in your xml file
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<com.example.widget.MyLinearLayout
android:id="#+id/llMainHome"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/orange_bg"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/home" />
</com.example.widget.MyLinearLayout>
<com.example.widget.MyLinearLayout
android:id="#+id/llHomeSearch"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/green_bg"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/search" />
</com.example.widget.MyLinearLayout>
<com.example.widget.MyLinearLayout
android:id="#+id/llHomeFavorite"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/green_bg"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/favorite" />
</com.example.widget.MyLinearLayout>
<com.example.widget.MyLinearLayout
android:id="#+id/llHomeGrocery"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/green_bg"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/make_juice" />
</com.example.widget.MyLinearLayout>
<com.example.widget.MyLinearLayout
android:id="#+id/llHomeSettings"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/green_bg"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/settings" />
</com.example.widget.MyLinearLayout>
</LinearLayout>
</HorizontalScrollView>
And in your activity class file declare
private MyLinearLayout llHomeSearch, llHomeFavorite,
llHomeGrocery, llHomeSettings,llMainHome;
oncreate Method code is
llHomeFavorite = (MyLinearLayout) findViewById(R.id.llHomeFavorite);
llHomeFavorite.setOnClickListener(this);
llMainHome = (MyLinearLayout) findViewById(R.id.llMainHome);
llMainHome.setOnClickListener(this);
llHomeSearch = (MyLinearLayout) findViewById(R.id.llHomeSearch);
llHomeSearch.setOnClickListener(this);
llHomeGrocery = (MyLinearLayout) findViewById(R.id.llHomeGrocery);
llHomeGrocery.setOnClickListener(this);
llHomeSettings = (MyLinearLayout) findViewById(R.id.llHomeSettings);
llHomeSettings.setOnClickListener(this);
Display display = getWindowManager().getDefaultDisplay();
Point size = getDisplaySize(display);
int width = size.x;
llMainHome.setLayoutParams(width);
llHomeFavorite.setLayoutParams(width);
llHomeGrocery.setLayoutParams(width);
llHomeSearch.setLayoutParams(width);
llHomeSettings.setLayoutParams(width);
and getDisplaysize method is
#SuppressLint("NewApi")
private static Point getDisplaySize(final Display display) {
final Point point = new Point();
try {
display.getSize(point);
} catch (java.lang.NoSuchMethodError ignore) { // Older device
point.x = display.getWidth();
point.y = display.getHeight();
}
return point;
}
here you go..it will work nice..
there is no need to take horizontal list view if you have fix number of item in your tab bar..
Maybe try to get Screen width and then set List's element to 1/4 of that width?
Something like in onResume/onCreate
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x; //remember that width
And in getView
LayoutParams params=new LayoutParams(width/4, -1);
element.setLayoutParams(params)
help me solve my problem. There is a main layout RelativeLayout it is Linearlayout where I want to programmatically add view. Here is my code.
public class GenBarCodeActivity extends Activity {
BarCodeView Barview = null;
LinearLayout.LayoutParams layoutParams;
LinearLayout ll;
RelativeLayout rl;
Button getBarCode;
private static final int ID = 34646456;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barcode);
getBarCode = (Button)findViewById(R.id.btnGetBarCode);
getBarCode.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ll = (LinearLayout)findViewById(R.id.linearCodeView);
EANTEXT = edBarText.getText().toString();
ImageView old = (ImageView) ll.findViewById(ID);
if (old != null) {
((LinearLayout) old.getParent()).removeViewInLayout(old);
}
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(Barview, layoutParams);
rl.invalidate();
}
});
fillLinear();
setBarCode();
}
private void fillLinear(){
rl = (RelativeLayout)findViewById(R.id.layout_barcode);
ll = (LinearLayout)findViewById(R.id.linearCodeView);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageView imBar = new ImageView(this);
Bitmap imbitmap = BitmapFactory.decodeResource(getResources(), R.drawable.barcode);
imBar.setLayoutParams(layoutParams);
imBar.setImageBitmap(imbitmap);
imBar.setId(ID);
ll.setBackgroundColor(Color.TRANSPARENT);
ll.addView(imBar);
}
private void setBarCode(){
Barview = new BarCodeView(GenBarCodeActivity.this);
Barview.setBarCodeNum(0);
}
}
This is my View
public class BarCodeView extends View {
private static int numcode = 0;
private static String codedata = null;
public BarCodeView(Context context) {
super(context);
}
public void setBarCodeText(String text){
this.codedata = text;
}
public String getBarCodeText(){
return this.codedata;
}
public void setBarCodeNum(int num){
this.numcode = num;
}
public int getBarCodeNum(){
return this.numcode;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
switch (numcode) {
// ean 13
case 0:
try{
showEAN13(canvas);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
private static void showEAN13(Canvas canvas) throws Exception {
EAN13 barcode = new EAN13();
/*
EAN 13 Valid data char set:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (Digits)
EAN 13 Valid data length: 12 digits only, excluding the last checksum digit
*/
barcode.setData(codedata);
// for EAN13 with supplement data (2 or 5 digits)
/*
barcode.setSupData("12");
// supplement bar height vs bar height ratio
barcode.setSupHeight(0.8f);
// space between barcode and supplement barcode (in pixel)
barcode.setSupSpace(15);
*/
// Unit of Measure, pixel, cm, or inch
barcode.setUom(IBarcode.UOM_PIXEL);
// barcode bar module width (X) in pixel
barcode.setX(2f);
// barcode bar module height (Y) in pixel
barcode.setY(90f);
// barcode image margins
barcode.setLeftMargin(10f);
barcode.setRightMargin(10f);
barcode.setTopMargin(10f);
barcode.setBottomMargin(10f);
// barcode image resolution in dpi
barcode.setResolution(72);
// disply barcode encoding data below the barcode
barcode.setShowText(true);
// barcode encoding data font style
barcode.setTextFont(new AndroidFont("Arial", Typeface.NORMAL, 10));
// space between barcode and barcode encoding data
barcode.setTextMargin(6);
barcode.setTextColor(AndroidColor.black);
// barcode bar color and background color in Android device
barcode.setForeColor(AndroidColor.black);
barcode.setBackColor(AndroidColor.white);
/*
specify your barcode drawing area
*/
RectF bounds = new RectF(120, 120, 0, 0);
barcode.drawBarcode(canvas, bounds);
}
}
here is my xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_barcode"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0e0a2"
android:orientation="vertical" >
<ImageView
android:id="#+id/closeBarCode"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/txtInfo"
android:layout_marginRight="3dp"
android:src="#drawable/delete" />
<TextView
android:id="#+id/txtInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="3dp"
android:layout_marginTop="3dp"
android:text="#string/barcode_title"
android:textColor="#000"
android:textSize="20dip" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/closeBarCode" >
<TextView
android:id="#+id/txtEAN"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_ean"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spEAN"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtInfo"
android:layout_below="#+id/linearLayout1" >
<TextView
android:id="#+id/txtCodeColor"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_color"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spCodeColor"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout2"
android:layout_below="#+id/linearLayout2" >
<TextView
android:id="#+id/txtCodeBackColor"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_back_color"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spCodeBackColor"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout3" >
<TextView
android:id="#+id/txtCodePosition"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_position"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spCodePosition"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearCodeView"
android:layout_width="300dip"
android:layout_height="200dip"
android:layout_below="#+id/edBarCode"
android:layout_marginRight="45dp"
android:layout_marginTop="25dip"
android:layout_toLeftOf="#+id/closeBarCode"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/btnSetBarCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnGetBarCode"
android:layout_alignBottom="#+id/btnGetBarCode"
android:layout_toLeftOf="#+id/closeBarCode"
android:text="#string/set_code" />
<Button
android:id="#+id/btnGetBarCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:text="#string/get_code" />
<EditText
android:id="#+id/edBarCode"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearCodeView"
android:layout_alignRight="#+id/linearCodeView"
android:layout_below="#+id/txtCodeText"
android:layout_marginTop="23dp"
android:ems="10" />
<TextView
android:id="#+id/txtCodeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/edBarCode"
android:layout_below="#+id/txtInfo"
android:layout_marginRight="86dp"
android:text="#string/code_text"
android:textColor="#000"
android:textSize="20dip" />
</RelativeLayout>
I need to Linearlayout "#+id/linearCodeView" insert my BarCodeView.
View is not visible after adding LinearLayout.
RelativeLayout.LayoutParams paramsBottom = new RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
rl.addView(ll, paramsBottom); //add linearlayout in relativelayout
Edit:-
LinearLayout.LayoutParams paramsBottom2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.addView(Barview, paramsBottom2);//add Barview in linearlayout
OR
RelativeLayout.LayoutParams paramsBottom2 = new RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
rl.addView(Barview, paramsBottom2);//add Barview in relativelayout