how can i set the images which are in the HorizontalScrollView
As you can see the images are so much wide, i need them to be of normal size.
This is my code for the HorizontalScrollView xml file:
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="5dp"
android:id="#+id/hscrollview"
android:layout_weight="0.2">
<LinearLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
This is what i have tried programmatically.
private ImageView getImageView(final Integer image, final int index) {
imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 0, 0);
imageView.setLayoutParams(lp);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewPager.setCurrentItem(index);
}
});
imageView.setImageResource(image);
return imageView;
}
Change
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
to
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
Related
I have a scrollable layout that shows images vertically :
<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"
tools:context=".chatActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp">
<LinearLayout
android:id="#+id/imageGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</RelativeLayout>
This is how I generate the view in my code :
LinearLayout imageGallery;
File[] fList = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
imageGallery = (LinearLayout) findViewById(R.id.imageGallery);
addImagesToTheGallery();
}
private void addImagesToTheGallery() {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
fList = path.listFiles();
if(fList!=null)
{
int len = fList.length;
for(int i=0; i<len; i++)
{
imageGallery.addView(getImageView(i));
}
}
}
private View getImageView(int image) {
ImageView imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 10, 0);
imageView.setLayoutParams(lp);
Bitmap bitmap = BitmapFactory.decodeFile(fList[image].getAbsolutePath());
imageView.setImageBitmap(bitmap);
return imageView;
}
Now, I want to add an onClick event for each of those images. When someone clicks on an image, I want to display some message specific to that image. So I should either be able to get the image or its position. How can I do that?
Set "imageView" onCLickListener.
private View getImageView(int image) {
ImageView imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 10, 0);
imageView.setLayoutParams(lp);
Bitmap bitmap = BitmapFactory.decodeFile(fList[image].getAbsolutePath());
imageView.setImageBitmap(bitmap);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO do something
Toast.makeText(this, "item num" +
image, Toast.LENGTH_LONG).show();
}
});
return imageView;
}
I want something like that
Linear Layout - vertical
Linear Layout - Horizontal
Frame Layout
Frame Layout
Linear Layout - horizontal
Frame Layout
Frame Layout
view1 | view2
view3 | view4
What should I do to fix this code
Code
public class MainActivity extends AppCompatActivity {
public static LinearLayout padrao;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
padrao = findViewById(R.id.padrao);
videoUnico();
}
#SuppressLint("ResourceType")
private void videoUnico() {
LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
padrao.removeAllViews();
FrameLayout view = new FrameLayout(this);
view.setBackgroundColor(Color.BLACK);
view.setLayoutParams(ll);
ll.setMargins(16, 16, 16, 16);
padrao.addView(view, ll);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewDois();
}
});
}
private void viewDois() {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, .5f);
params.setMargins(16, 16, 16, 16);
padrao.removeAllViews();
FrameLayout view = new FrameLayout(this);
view.setBackgroundColor(Color.BLACK);
padrao.addView(view, params);
FrameLayout view2 = new FrameLayout(this);
view2.setBackgroundColor(Color.RED);
padrao.addView(view2, params);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewTres();
}
});
}
private void viewTres() {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, .5f);
padrao.removeAllViews();
params.setMargins(16, 16, 16, 16);
FrameLayout view = new FrameLayout(this);
view.setBackgroundColor(Color.BLACK);
padrao.addView(view, params);
FrameLayout view2 = new FrameLayout(this);
view2.setBackgroundColor(Color.RED);
padrao.addView(view2, params);
FrameLayout view3 = new FrameLayout(this);
view3.setBackgroundColor(Color.GREEN);
padrao.addView(view3, params);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewQuatro();
}
});
}
private void viewQuatro() {
padrao.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout linearHorizonteBaixo = new LinearLayout(this);
linearHorizonteBaixo.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,.5f));
linearHorizonteBaixo.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout linearHorizonteCima = new LinearLayout(this);
linearHorizonteCima.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,.5f));
linearHorizonteCima.setOrientation(LinearLayout.HORIZONTAL);
FrameLayout view = new FrameLayout(this);
view.setBackgroundColor(Color.BLACK);
FrameLayout view2 = new FrameLayout(this);
view2.setBackgroundColor(Color.RED);
linearHorizonteCima.addView(view);
linearHorizonteCima.addView(view2);
FrameLayout view3 = new FrameLayout(this);
view3.setBackgroundColor(Color.GREEN);
FrameLayout view4 = new FrameLayout(this);
view4.setBackgroundColor(Color.BLUE);
linearHorizonteBaixo.addView(view3);
linearHorizonteBaixo.addView(view4);
padrao.addView(linearHorizonteCima);
padrao.addView(linearHorizonteBaixo);
}
}
Xml code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/padrao"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
This is the entire class
I'm using methods to call the other layout
But my problem is when im trying to create two linear layout inside one
and also get this error
java.lang.ClassCastException:
android.widget.LinearLayout$LayoutParams
cannot be cast to android.widget.FrameLayout$LayoutParams
this code in XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="View2" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:text="View1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="View4" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:text="View3" />
</LinearLayout>
</RelativeLayout>
I'm rather new to this, so apologies if it's simple. I have a GridLayout which is 2 by 4, with images that can be clicked, the image changes and a sound plays.
I would like to add a volume control on top of each image, that will control that image noises volume. How can I do this?
<GridLayout
android:id="#+id/mainGrid"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/whitenoise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowWeight="1"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:onClick="whiteNoiseTapped"
android:scaleType="fitXY"
android:text="White 1"
app:srcCompat="#drawable/whitenoise" />
Use FrameLayout for this.
<FrameLayout>
<ImageView>
android:id="#+id/whitenoise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowWeight="1"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:onClick="whiteNoiseTapped"
android:scaleType="fitXY"
android:text="White 1"
app:srcCompat="#drawable/whitenoise"
<ImageView>
<Button >
</Button>
</FrameLayout>
You can also do like following way
layout.xml to some extent like this...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#000"
tools:context="com.example.tistis.myapplication.MainActivity">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
and your ImageAdapter.java
following is the code. Please note it is a dirty way to code from
adapter rather one should inflate a view.
class ImageAdapter extends BaseAdapter {
private Context mContext;
public int screenHeight,screenWidth;
public ImageAdapter(Context c) {
mContext = c;
DisplayMetrics displaymetrics = new DisplayMetrics();
((Activity)c).getWindowManager().
getDefaultDisplay().getMetrics(displaymetrics);
screenHeight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position,
View convertView, ViewGroup parent) {
ImageView imageView=null;
RelativeLayout frm;
SeekBar seekBar=null;
if (convertView == null) {
// if it's not recycled, initialize some attributes
frm= new RelativeLayout(mContext);
frm.setLayoutParams(new GridView.LayoutParams(screenWidth/2-(8*2+10),screenWidth/2-(8*2+10)));
frm.setPadding(8, 8, 8, 8);
frm.setBackgroundColor(Color.BLACK);
imageView = new ImageView(mContext);
imageView.setLayoutParams(
new FrameLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundColor(Color.BLACK);
frm.addView(imageView);
imageView.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext,
"Item position "+(position+1),
Toast.LENGTH_SHORT).show();
}
});
seekBar=new SeekBar(mContext);
seekBar.setLayoutParams(
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, 40));
RelativeLayout.LayoutParams lp=
(RelativeLayout.LayoutParams)
seekBar.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
seekBar.setMax(15);
// seekBar.setIndeterminate(true);
ShapeDrawable thumb =
new ShapeDrawable(new OvalShape());
thumb.setIntrinsicHeight(80);
thumb.setIntrinsicWidth(30);
seekBar.setThumb(thumb);
seekBar.setProgress(1);
seekBar.setVisibility(View.VISIBLE);
seekBar.setBackgroundColor(Color.TRANSPARENT);
frm.addView(seekBar);
} else {
frm = (RelativeLayout) convertView;
}
if(imageView!=null)
imageView.setImageResource(mThumbIds[position]);
return frm;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.superman, R.drawable.superman,
R.drawable.superman, R.drawable.superman,
};
}
How to add view programmatically to existing view, for example I have textview in XML and now I need to add imageview below that textview programmatically, here is what I tried,
HorizontalScrollView scroll = new HorizontalScrollView(getApplicationContext());
LinearLayout imageLayout = new LinearLayout(getApplicationContext());
scroll.setLayoutParams(new ViewGroup.LayoutParams(imageLayout.getWidth(),
imageLayout.getHeight()));
scroll.addView(imageLayout);
for (int i = 0; i < 15; i++) {
final ImageView imageView = new ImageView(this);
imageView.setTag(i);
imageView.setImageResource(R.drawable.logo);
imageLayout.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("Tag", "" + imageView.getTag());
}
});
}but its not adding any imageview, I'm trying to do this in **AlertDialog**
ScrollView cannot have more than one child. You should wrap your layout inside of a LinearLayout and then after you can add any number of views into that LinearLayout. cheers :)
You are doing with wrong way.
Once check with below code snippet.
LinearLayout yourlayout = (LinearLayout)findViewById(R.id.layoutid);//Layout which is inside scrollview
LinearLayout imageLayout = new LinearLayout(getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// add the rule that places your LinearLayout below your TextView object
params.addRule(RelativeLayout.BELOW, TextView.getId());
// set the layoutParams on the LinearLayout
imageLayout.setLayoutParams(params);
yourlayout.addView(imageLayout);
for (int i = 0; i < 15; i++) {
final ImageView imageView = new ImageView(this);
imageView.setTag(i);
imageView.setImageResource(R.drawable.logo);
imageLayout.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("Tag", "" + imageView.getTag());
}
});
}
I solved it by adding
<HorizontalScrollView
android:id="#+id/horizontal_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
in XML
&
LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
for (int i = 0; i < 10; i++) {
ImageView imageView = new ImageView(this);
imageView.setId(i);
imageView.setPadding(2, 2, 2, 2);
imageView.setImageBitmap(BitmapFactory.decodeResource(
getResources(), R.drawable.ic_launcher));
imageView.setScaleType(ScaleType.FIT_XY);
layout.addView(imageView);
}
in MainActivity.java
ImageView imageView = new ImageView(this.getActivity());
imageView.setId(View.generateViewId());
params.addRule(RelativeLayout.BELOW, imageView.getId());
I am using one one layout and include 5 times on other layout. but i want to user Grid View instance of incluide layout. Here I am sharing Screen shot. please help me to make this layout in android
This is Flipboard layout. and second one is my layout.
GridView does not support this. One has to use a custom linearlayout and add views to it dynamically by setting the layout parameters.
Use the class as layout back bone
public class CompositeView extends LinearLayout {
private ImageView imageView;
private TextView textView;
public CompositeView(Context context) {
super(context);
imageView = new ImageView(context);
imageView.setClickable(true);
imageView.setPadding(3, 3, 3, 3);
LayoutParams params = new LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(params);
imageView.setScaleType(ScaleType.FIT_XY);
textView = new TextView(context);
textView.setTextColor(getResources().getColor(android.R.color.white));
textView.setClickable(true);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
textView.setSingleLine(true);
textView.setEllipsize(TruncateAt.END);
textView.setPadding(3, 3, 3, 3);
textView.setBackgroundResource(R.drawable.strip_bg);
this.setGravity(Gravity.CENTER);
// this.setClickable(true);
this.addView(imageView);
this.addView(textView);
}
public void setText(int resid) {
textView.setText(resid);
}
public void setText(CharSequence text) {
textView.setText(text);
}
public void setIcon(int resid) {
imageView.setImageDrawable(getResources().getDrawable(resid));
}
public ImageView getImageView() {
return imageView;
}
public TextView getTextView() {
return textView;
}
}
In activity or fragment
names = new String[] { "Cloud Bingo", "New Bingo Games", "Politricks",
"Seam Group", "Regular Cloud Bingo" };
urls = new String[] { "http://www.cloudbingo.co.uk/",
"http://www.newbingogames.co.uk/",
"http://www.politricks.co.in", "http://veenagupta.in/",
"http://www.regularbingo.co.uk/" };
for (int i = 0; i < images.length; i++) {
view = new CompositeView(getActivity());
view.setText(names[i]);
// view.setOnClickListener(this);
view.setOrientation(LinearLayout.VERTICAL);
view.setIcon(images[i]);
view.setId(i); // id of whole linear view
view.getTextView().setGravity(Gravity.CENTER);
view.getImageView().setId(i); // id of just this imageview
view.getTextView().setId(i);
}
Use following logic for your purpose
for (int i = 0; i < images.length; i++) {
flag++;
if (flag % 2 == 0) {
// number is even
mainLayout.addView(compositeList.get(i));
}
else {
Log.v("in", "else");
secondaryLayout = new LinearLayout(getActivity());
LayoutParams params = new LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 15, 0, 15);
secondaryLayout.setLayoutParams(params);
secondaryLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout leftLayout = new LinearLayout(getActivity());
leftLayout.setLayoutParams(new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
leftLayout.addView(compositeList.get(i));
secondaryLayout.addView(leftLayout);
i++;
if (i < images.length) {
LinearLayout rightLayout = new LinearLayout(getActivity());
LayoutParams rParams = new LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
rParams.setMargins(10, 0, 0, 0);
rightLayout.setLayoutParams(rParams);
rightLayout.addView(compositeList.get(i));
secondaryLayout.addView(rightLayout);
}
else {
LinearLayout rightLayout = new LinearLayout(getActivity());
LayoutParams rParams = new LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
rParams.setMargins(10, 0, 0, 0);
rightLayout.setLayoutParams(rParams);
secondaryLayout.addView(rightLayout);
}
mainLayout.addView(secondaryLayout);
}
}
flag is equal to -1 initially , I show one image (with text at bottom) for even rows, two images for odd rows
Here is the layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash_bg"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="5"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" >
<LinearLayout
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>