Show/Hide ImageView on Button click - java

I have a RelativeLayout completely created within java, not using xml layout.
I have a few buttons on the left if my screen. I want to click on one button and show a default image from my res/drawable next to the button and make it disappear again on second click.
What I have tried was toggling the visibility but my onClick() raises a FATAL EXCEPTION, NullPointer Exception.
This is my code so far. Hardcoded the image shows right when I set picview.setVisibility(View.INVISIBLE); by hand. What I am doing wrong in the onClick() ?
private ImageView picview;
//*snip* loads of other code
//Show Image Button
ImageButton show_pic = new ImageButton(this);
show_pic.setBackgroundColor(Color.WHITE);
show_pic.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
if(picview.getVisibility() == View.INVISIBLE)
{
picview.setVisibility(View.VISIBLE);
}
else if (picview.getVisibility() == View.VISIBLE)
{
picview.setVisibility(View.INVISIBLE);
}
}
});
params = new RelativeLayout.LayoutParams(40, 40);
params.topMargin = 10;
params.leftMargin = 10;
params.addRule(RelativeLayout.BELOW, button2_id);
rl.addView(show_pic, params);
//Imageview loaded from drawable
ImageView picview = new ImageView(this);
params = new RelativeLayout.LayoutParams(200, 400);
params.topMargin = 0;
params.leftMargin = 30;
params.addRule(RelativeLayout.RIGHT_OF, button2_id);
picview.setImageResource(R.drawable.my_image);
picview.setVisibility(View.INVISIBLE);
rl.addView(picview, params);
this.setContentView(rl);

You are accidentally creating two copies of picview. Shorten this line:
ImageView picview = new ImageView(this);
To:
picview = new ImageView(this);
(Your field variable private ImageView picview; never changed from null, so when you clicked your Button you see the NullPointerException...)

change
show_pic.setOnClickListener(new OnClickListener() to
show_pic.setOnClickListener(new View.OnClickListener()

Related

How to display an Alert Dialog in a fragment

I am trying to display a custom Alert Dialog in a fragment. The name of my fragment is MoviesFragFragmentActivity.java
Here is my code:
final AlertDialog dialog1 = new AlertDialog.Builder(MoviesFragFragmentActivity.this).create();
View inflate = getLayoutInflater().inflate(R.layout.custom3,null);
dialog1.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog1.setView(inflate);
TextView t1 = (TextView) inflate.findViewById(R.id.t1);
TextView t2 = (TextView) inflate.findViewById(R.id.t2);
TextView t3 = (TextView) inflate.findViewById(R.id.t3);
LinearLayout b1 = (LinearLayout) inflate.findViewById(R.id.b1);
ImageView i1 = (ImageView) inflate.findViewById(R.id.i1);
LinearLayout bg = (LinearLayout) inflate.findViewById(R.id.bg);
t1.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/en_medium.ttf"), 0);
t2.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/sansation_regular.ttf"), 0);
t3.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/sansation_regular.ttf"), 0);
i1.setImageResource(R.drawable.splash);
i1.getDrawable().setColorFilter(Color.parseColor("#008DCD"), PorterDuff.Mode.SRC_IN);
t1.setText("Oh,No...");
t2.setText("This feature is not available yet, try after new update ");
t3.setText("Try again");
_rippleRoundStroke(bg, "#FAFAFA", "#000000", 40, 0, "#000000");
_CardView(b1, 10, 100, "#008DCD", true);
b1.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){
// Default item selected on bottom navigation onCreate
bottomnavigation4.getMenu().findItem(2).setChecked(true);
SketchwareUtil.showMessage(getApplicationContext(), "Try again later");
dialog1.dismiss();
}
});
dialog1.setCancelable(false);
dialog1.show();
On compilation, it gives me this error:
ERROR in /storage/emulated/0/.sketchware/mysc/680/app/src/main/java/com/liquidapps/movieapp/MoviesFragFragmentActivity.java (at line 601)
final AlertDialog dialog1 = new AlertDialog.Builder(MoviesFragFragmentActivity.this).create();
The constructor AlertDialog.Builder(MoviesFragFragmentActivity) is undefined
How does I display my alert dialog in my Fragment?
I tried using the same code in an activity (Main activity.java) and it works fine, but once I use the code in a fragment, I receive the error.
Use getActivity() instead of MoviesFragFragmentActivity.this

How to add an ImageView over a Button

I want to add an ImageView within the onClick() of the Button. But the ImageView should be aligned to the centre of the button.
How can I do this pragmatically?
I'm creating a new instance of the ImageView to be added on top of the View that called it and it's position is equivalent of the View that called it.
How can I get the X and Y coordinates?
EDIT:
#Override
public void onClick(final View v) {
if(v.getWidth() > v.getHeight()) size = v.getHeight();
if(v.getWidth() < v.getHeight()) size = v.getWidth();
RelativeLayout.LayoutParams button = new RelativeLayout.LayoutParams(size,size);
buttonc = new ImageView(getApplicationContext());
buttonc.setBackgroundResource(R.drawable.round);
layout.addView(buttonc,button);
I'm using the above code to attempt to pragmatically set the size of the ImageView to that of the button, and it's working.
As for the coordinates, I still haven't figured it out.
To add clarity: I'm attempting to create a rounded ripple effect over the view that calls it.
I suggest a better solution create an image view before hand place it in the xml as you see fit,at first set its visibility to false .
On click switch its visibilty to true, and the image will appear
This solution is easier than what you are searching.
Feel free to ask questions
to add Imageview
#Override
public void onClick(final View v) {
//LinearLayout
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView
ImageView imageView = new ImageView(this);
//image resource
imageView.setImageResource(R.drawable.specs);
//image position
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
}

How do I get buttons not to sit on top of each other in RelativeLayout

I'm having a problem with this code. I need to dynamically add buttons to my layout. This code works fine, with one exception. The second button sits on top of the first. This must have something to do with LayoutParams, but I'm not sure what.
private void buttonmaker (Button button)
{
RelativeLayout.LayoutParams rlayout = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rlayout.addRule(RelativeLayout.CENTER_VERTICAL);
rlayout.addRule(RelativeLayout.ALIGN_LEFT);
rlayout.width = 100;
button.setId(Atom.count);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = v.getId();
atoms[id].getname();
TextView textview = (TextView)findViewById(R.id.textView2);
textview.setText(textview.getText()+String.valueOf(atoms[id].getname()));
}
});
if (Atom.count > 1) rlayout.addRule(RelativeLayout.RIGHT_OF,Atom.count-1); else rlayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
button.setLayoutParams(rlayout);
RelativeLayout v = (RelativeLayout) findViewById(R.id.rlayout);
v.addView(button);
}
the problem is you set de button in the relativelayout, in this component the objects are being added one above the other, you try create linearlayout global with orientation vertical or horizontal depending on what you want and added buttons, and its all

Create ImageViews dynamically inside a loop

I have written this code that loads an image to in ImageView widget:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
i = (ImageView)findViewById(R.id.imageView1);
new get_image("https://www.google.com/images/srpr/logo4w.png") {
ImageView imageView1 = new ImageView(GalleryActivity.this);
ProgressDialog dialog = ProgressDialog.show(GalleryActivity.this, "", "Loading. Please wait...", true);
protected void onPreExecute(){
super.onPreExecute();
}
protected void onPostExecute(Boolean result) {
i.setImageBitmap(bitmap);
dialog.dismiss();
}
}.execute();
}
bu now, I want to load several images. for this I need create image views dynamically but i don't know how...
I want run my code inside a for loop:
for(int i;i<range;i++){
//LOAD SEVERAL IMAGES. READ URL FROM AN ARRAY
}
my main problem is creating several ImageViews inside a loop dynamically
you can modify the layout , image resource and no of images (may be dynamic as well) according to your requirement...
LinearLayout layout = (LinearLayout)findViewById(R.id.imageLayout);
for(int i=0;i<10;i++)
{
ImageView image = new ImageView(this);
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
image.setMaxHeight(20);
image.setMaxWidth(20);
// Adds the view to the layout
layout.addView(image);
}
You can use this code to create ImageViews
ImageView image = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
image.setLayoutParams(vp);
image.setMaxHeight(50);
image.setMaxWidth(50);
// other image settings
image.setImageDrawable(drawable);
theLayout.addView(image);
where theLayout is the layout you want to add your image views to.
For more customization check out the dev page, where all the possible options are listed.
If your requirement is show in List or Gridview then you should go for Lazy Loading List or grid.
Please go through the below link.
https://github.com/thest1/LazyList
https://github.com/abscondment/lazy-gallery
Try this
rootLayout = (LinearLayout) view1.findViewById(R.id.linearLayout1);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_VERTICAL;
params.setMargins(0, 0, 60, 0);
for(int x=0;x<2;x++) {
ImageView image = new ImageView(getActivity());
image.setBackgroundResource(R.drawable.ic_swimming);
rootLayout.addView(image);
}

Question about arrays and buttons in eclipse + android java project

well im kind of new to eclipse anyways i was wondering is there a way for me to create an array of buttons in the java code rather then a xml file, then define there positions on the layout.
Every Button object is a view itself. Therefore, it can be added to a parent Layout (like the LinearLayout). The easiest way IMHO is creating the XML only for the things you know that will not change, or maybe using a TableLayout. Then, add the buttons.
LinearLayout mainLayout = findViewById(R.id.mainLayout);
Button[] btnArray = new Button[3];
for(Button button : btnArray){
button = new Button(/*Required params */);
// button.something , play with text and onclick and positions...
mainLayout.addView(button);
}
Is this what you're trying to do?
LinearLayout linear = (LinearLayout) findViewById(R.id.linear);
for (int i = 1; i <= 20; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
btn = new Button(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText("button " + id_);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
linear.addView(btn, params);
btn1 = ((Button) findViewById(id_));
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"Button clicked index = " + id_, Toast.LENGTH_SHORT)
.show();
}
});
}

Categories

Resources