I want add ImageView on ViewFlipper and try this code
ViewFlipper flipVertical = (ViewFlipper)findViewById(R.id.slideVer);
Bitmap bitmap = BitmapFactory.decodeFile("sdcard/vm/picture/vertical/VB-Logo.png");
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bitmap);
flipVertical.addView(imageView);
nothing effect with that code, same as when I use
ViewFlipper flipVertical = (ViewFlipper)findViewById(R.id.slideVer);
Uri uriImage = Uri.parse("file:///sdcard/vm/picture/vertical/VB-Logo.png");
imageView.setImageURI(uriImage);
flipVertical.addView(imageView);
What should I do?? :(
You can try as below...
ViewFlipper flipVertical = (ViewFlipper)findViewById(R.id.slideVer);
Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath() + "/vm/picture/vertical/VB-Logo.png");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bitmap);
flipVertical.addView(imageView, params);
If you didn't add the permission to access SDCard then add these permission in the Manifest.xml file...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Related
I'm try to create programmatically textView for a project. My problem is that I don't now how to set up the position of the textview on the screen.
I tried with the code below but instead of moving the textview it moves all the margin. The code is this:
constraintLayout = (ConstraintLayout) findViewById(R.id.constraint);
TextView valueTV = new TextView(getApplicationContext());
valueTV.setText("TEST");
valueTV.setTextColor(R.color.black);
valueTV.setId(id);
valueTV.setHeight(50);
valueTV.setWidth(50);
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)
constraintLayout.getLayoutParams();
params.setMargins(100, 100, 100, 100);
valueTV.setLayoutParams(params);
constraintLayout.addView(valueTV);
the code is inside the oncreate. Thanks for help
You can try something like that:
TextView valueTV = new TextView(getApplicationContext());
valueTV.setText("TEST");
valueTV.setTextColor(R.color.black);
valueTV.setId(id);
valueTV.setHeight(50);
valueTV.setWidth(100);
constraintLayout.addView(valueTV);
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) valueTV.getLayoutParams();
layoutParams.rightToRight = constraintLayout.getId();
layoutParams.topToTop = constraintLayout.getId();
layoutParams.bottomToBottom = constraintLayout.getId();
layoutParams.leftToLeft = constraintLayout.getId();
valueTV.setLayoutParams(layoutParams);
I am new to android and not able to figure out why app crashes when i add below code to play music when i tap on Image view (image).
Below is the main activity code.
ImageView one = (ImageView) this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.cow);
one.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mp.start();
}
});}
Below is the ImageView code in XML file
<ImageView
android:id="#+id/button1"
android:layout_width="350dp"
android:layout_height="400dp"
android:src="#drawable/tr"/>
MP3 file is placed under raw folder.
Please help me in fixing this issue. Thanks in advance.
App crashing because you imageview getting wrong id.
just replace this
ImageView one = (ImageView) this.findViewById(R.id.button1);
to
ImageView one = (ImageView)findViewById(R.id.button4);
also play audio file from Raw folder:
int resID=getResources().getIdentifier("YourAudioFileName", "raw", getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
mediaPlayer.start();
don't forget to add permission in Manifest
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Hope it will help you!!
how to convert an image from drawable file to an Image view
because this won't work
ImageView image= R.drawable.pic;
Try this
ImageView imageview = findViewById(R.id.imageView);
imageview.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.pic));
Try this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
fab.setImageDrawable(getResources().getDrawable(R.drawable.pic,this.getTheme()));
}else {
fab.setImageDrawable(getResources().getDrawable(R.drawable.pic));
Try this
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.pic);
or
ImageView image = new ImageView(this);
image.setImageDrawable(getResources().getDrawable(R.drawable.pic));
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView img=new ImageView(this);
img.setImageResource(R.drawable.img);
img.setScaleType(ImageView.ScaleType(CENTER_CROP));
setContentView(img) }
I want to set the size of image view in Java and I could not find the syntax working.
Try code(edit as per your view names):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout picLL = new LinearLayout(this);
picLL.layout(0, 0, 100, 100);
picLL.setLayoutParams(new LayoutParams(100, 100));
picLL.setOrientation(LinearLayout.HORIZONTAL);
ImageView myImage = new ImageView(this);
myImage.setImageResource(R.drawable.ic_launcher);
picLL.addView(myImage);
setContentView(picLL);
}
And set height or width:
myImageView..getLayoutParams().height = your_size_value;
myImageView..getLayoutParams().width = your_size_value;
<ImageView
android:id="#+id/imageView1"
android:layout_margin="20dp"
android:src="#drawable/stop"
android:layout_width="50dp"
android:layout_height="50dp"/>
change width and height attribute with your values.
//Linear layout setting here
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.bull);
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
linearLayout.addView(imageView);
setContentView(linearLayout);
Let's say I have a LinearLayout, and I want to add two View to it. The first one contain editText, and the other one contain listview. I have been try code in java follows:
EditText inputViaText;
ListView historyInput;
protected static LinearLayout askTextLayout = null;
askTextLayout = new LinearLayout(this);
askTextLayout.setVisibility(LinearLayout.VISIBLE);
askTextLayout.setOrientation(LinearLayout.HORIZONTAL);
inputViaText = new EditText(this);
LinearLayout.LayoutParams askTextParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
historyInput = new ListView(this);
LinearLayout.LayoutParams historyInputParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,70);
askTextLayout.addView(historyInput,historyInputParams);
askTextLayout.addView(inputViaText,askTextParams);
FrameLayout.LayoutParams frameAskTextParams = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
setContentView(R.layout.activity_main);
addContentView(askTextLayout, frameAskTextParams);
But, It just show the first one that i add. So when i code like follows:
askTextLayout.addView(historyInput,historyInputParams);
askTextLayout.addView(inputViaText,askTextParams);
It just show listView. When i code like follows:
askTextLayout.addView(inputViaText,askTextParams);
askTextLayout.addView(historyInput,historyInputParams);
It just show edittext. Anyone can help me?
Try this code in oncreate Method of your activity
context = this;
setContentView(R.layout.activity_main);
container = (LinearLayout) findViewById(R.id.Linear);
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setWeightSum(100);
ListView v1 = new ListView(context);
v1.setBackgroundColor(Color.CYAN);
LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(0,
50);
p1.weight = 90;
v1.setLayoutParams(p1);
EditText v2 = new EditText(context);
v2.setText("Hello");
v2.setBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams p2 = new LinearLayout.LayoutParams(0,
50);
p2.weight = 10;
v2.setLayoutParams(p2);
linearLayout.addView(v1, p1);
linearLayout.addView(v2, p2);
View view = new View(MainActivity.this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
1);
view.setLayoutParams(lp);
view.setBackgroundColor(Color.BLACK);
container.addView(linearLayout);
container.addView(view);
Write this in activity_main:
<LinearLayout
android:id="#+id/Linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>