I'm trying to use a RelativeLayout to produce two rows of items, one row of three buttons up top with a scrollview below them. I want the scrollView to occupy as much of the space as possible.
I believe a RelativeLayout is the best approach here. I'm trying to do this at runtime and not via an xml layout file. My code is as follows:
RelativeLayout.LayoutParams exitparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
exitparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
RelativeLayout.LayoutParams zimparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams zoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams scrollparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
zimparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
scrollparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM):
// Exit button
Button exitbutton = new Button(this);
exitbutton.setText("Exit");
exitbutton.setGravity(Gravity.CENTER);
exitbutton.setWidth(200);
exitbutton.setLayoutParams(exitparams);
// Zoom In button
Button zoomOutButton = new Button(this);
zoomOutButton.setText("Zoom Out");
zoomOutButton.setGravity(Gravity.CENTER);
zoomOutButton.setWidth(200);
zoomOutButton.setLayoutParams(zimparams);
// Zoom In button
Button zoomInButton = new Button(this);
zoomInButton.setText("Zoom In");
zoomInButton.setGravity(Gravity.CENTER);
zoomInButton.setWidth(200);
zoutparams.addRule(RelativeLayout.LEFT_OF, zoomInButton.getId());
zoomInButton.setLayoutParams(zoutparams);
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));
layout1.addView(exitbutton);
layout1.addView(zoomInButton);
layout1.addView(zoomOutButton);
exitbutton.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
zoomInButton.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
increaseZoom();
onResume();
}
});
zoomOutButton.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
reduceZoom();
onResume();
}
});
drawView = new DrawView(this, height, width, SMD, zoomLevel);
drawView.setBackgroundColor(Color.WHITE);
ScrollView scrollView = new ScrollView(this);
scrollView.addView(drawView);
scrollView.setLayoutParams(scrollparams);
layout1.addView(scrollView);
setContentView(layout1);
Yet the only thing that shows up is the scrollView. I'm guessing I'm missing something small here... I read the documentation and I believe I did this correctly but the fact that it is not working suggests I either did something wrong or cannot accomplish what I want to do.
A visual of how I want this to look is as follows:
You need to call setContentView(layout1) at the end to actually add the layout.
EDIT:
Okay so Iv'e fixed you code up and it's working on my phone. However I couldn't' work with DrawView obviously so if there is something wrong with that I can't help you. Give this a go.
RelativeLayout.LayoutParams exitparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
exitparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
RelativeLayout.LayoutParams zimparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams zoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams scrollparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
zimparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// Exit button
Button exitbutton = new Button(this);
exitbutton.setText("Exit");
exitbutton.setGravity(Gravity.CENTER);
exitbutton.setWidth(200);
exitbutton.setLayoutParams(exitparams);
exitbutton.setId(2);
// Zoom In button
Button zoomOutButton = new Button(this);
zoomOutButton.setText("Zoom Out");
zoomOutButton.setGravity(Gravity.CENTER);
zoomOutButton.setWidth(200);
zoomOutButton.setLayoutParams(zimparams);
zoomOutButton.setId(1);
// Zoom In button
Button zoomInButton = new Button(this);
zoomInButton.setText("Zoom In");
zoomInButton.setGravity(Gravity.CENTER);
zoomInButton.setWidth(200);
zoutparams.addRule(RelativeLayout.LEFT_OF, 1);
zoutparams.addRule(RelativeLayout.RIGHT_OF, 2);
zoomInButton.setLayoutParams(zoutparams);
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));
layout1.addView(exitbutton);
layout1.addView(zoomInButton);
layout1.addView(zoomOutButton);
exitbutton.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
zoomInButton.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
//increaseZoom();
onResume();
}
});
zoomOutButton.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
//reduceZoom();
onResume();
}
});
//drawView = new DrawView(this, height, width, SMD, zoomLevel);
// drawView.setBackgroundColor(Color.WHITE);
ScrollView scrollView = new ScrollView(this);
//scrollView.addView(drawView);
scrollparams.addRule(RelativeLayout.BELOW,1);
scrollView.setLayoutParams(scrollparams);
layout1.addView(scrollView);
setContentView(layout1);
Related
I write code to generate dynamic TextViews as a user what he looked like and any time
, But the problem each text view has only one click,
And any other click on textView does not reach onClick
I found some answers but not work with my code... Please help me.
This is my code
RelativeLayout mainBackground = findViewById(R.id.mainBackground);
private void AddTextView() {
RelativeLayout.LayoutParams lparams = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
final TextView tv = new TextView(this);
tv.setId(textCount);
tv.setLayoutParams(lparams);
tv.setText(text);
tv.setTextColor(colorChooser);
tv.setTextSize(size);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("textID",view.getId()+"");
}
});
this.mainBackground.addView(tv);
}
As I am beginner, I am able to implement the pop-up code for single button that is pasted below..
Requirement: I need to implement multiple pop-up's to show different text(information) on each pop-up.
public void init() {
popupButton = (Button) findViewById(R.id.textview1);
popupText = new TextView(this);
insidePopupButton = new Button(this);
layoutOfPopup = new LinearLayout(this);
LinearLayout lt=new LinearLayout(this);
view=new ScrollView(this);
insidePopupButton.setText("OK");
popupText.setText("This is Popup Window.press OK to dismiss it.");
popupText.setBackgroundColor(Color.WHITE);
popupText.setPadding(0, 0, 0, 20);
layoutOfPopup.setOrientation(1);
lt.addView(popupText);
layoutOfPopup.setBackgroundColor(Color.BLACK);
layoutOfPopup.addView(view);
layoutOfPopup.addView(insidePopupButton,350,50);
view.addView(lt);
}
public void popupInit() {
popupButton.setOnClickListener(this);
insidePopupButton.setOnClickListener(this);
popupMessage = new PopupWindow(layoutOfPopup, LayoutParams.FILL_PARENT,
LayoutParams.MATCH_PARENT);
popupMessage.setContentView(layoutOfPopup);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.textview1) {
popupMessage.showAsDropDown(popupButton, 0, 0);
}
else {
popupMessage.dismiss();
}
}
}
and my requirement is showing through the image.
Use switch case for handle click on other views.
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.textview1:
case R.id.textview2:
case R.id.textview3:
case R.id.textview4: popupMessage.showAsDropDown(popupButton, 0, 0);
break;
default:popupMessage.dismiss();
}
}
Use a dialog fragment and design a layout that will contain all of the required buttons that you need.
How do I make a single button width to fill parent programmatically? I have done this but it cannot seem to work it is still located on top left with width just wrapping the content... here is some of the code on the button creation...
public class TEST_GIFDisplay extends Activity implements View.OnClickListener {
SurfaceView sview;
GifRun gr = new GifRun();
Button btnBack;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sview = new SurfaceView(this);
sview.setZOrderOnTop(true);
gr.LoadGiff(sview, this, R.drawable.smiley);
LinearLayout linearLayout1 = new LinearLayout(this);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
linearLayout1.setBackgroundColor(Color.parseColor("#27ae60"));
linearLayout1.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
LinearLayout linearLayout2 = new LinearLayout(this);
linearLayout2.setOrientation(LinearLayout.VERTICAL);
linearLayout2.setBackgroundColor(Color.parseColor("#27ae60"));
linearLayout2.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
p.weight = 1.0f;
btnBack = new Button (this);
btnBack.setText("Back");
btnBack.setBackgroundColor(Color.parseColor("#f1c40f"));
btnBack.setGravity(Gravity.CENTER_HORIZONTAL);
btnBack.setLayoutParams(p);
btnBack.setOnClickListener(this);
linearLayout2.addView(btnBack);
linearLayout1.addView(linearLayout2);
linearLayout1.addView(sview);
setContentView(linearLayout1);
}
#Override
public void onClick(View view) {
btnBack = (Button) view;
btnBack.setBackgroundColor(Color.parseColor("#2980b9")); //color change
new CountDownTimer(100, 50) {
#Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
#Override
public void onFinish() {
btnBack.setBackgroundColor(Color.parseColor("#f1c40f")); // original color
}
}.start();//end color changed
finish();
}
}
You are adding the Button to linearLayout2. You should change the linearLayout2's width to MATCH_PARENT
linearLayout2.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
I hope this helps.
P.S: You can create Button's selectors for pressed and selected states, instead of using timer to show a pressed button effect. Here is a basic link that can help you in achieving this : android button selector
I followed this to create a simple animation.
The example creates manually an ImageView in the main layout, having a resource like this android:src="#anim/anim_android
The problem is, when I make a dynamic ImageView, and I set the resource like this myImageView.setImageResource(R.anim.anim_android);, the animation doesn't work, just views the first image of the sequence.
Any help?
Thanks.
you sould start the animation from you imageView :
myImageView.setImageResource(R.anim.anim_android);
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myImageViewn.getDrawable();
myImageView.post(
new Runnable(){
#Override
public void run() {
myAnimationDrawable.start();
}
});
}
EDIT : i think you didn't add the ImageView to your Layout ( contentView of your Activity ) ; try this :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rootLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
rootLayout.setLayoutParams(params);
ImageView myAnimation = new ImageView(this);
RelativeLayout.LayoutParams paramsImage = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsImage.addRule(RelativeLayout.CENTER_IN_PARENT);
myAnimation.setLayoutParams(paramsImage);
myAnimation.setImageDrawable(getResources().getDrawable(R.anim.anim_android));
rootLayout.addView(myAnimation);
setContentView(rootLayout);
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myAnimation.getDrawable();
myAnimation.post(new Runnable() {
#Override
public void run() {
myAnimationDrawable.start();
}
});
}
I want to put elements in my view depending on the position of the other ones. Let me explain : First, I have two textviews and I've put successfully the second (villeOffre) one under the first one (titreOffre) :
titreOffre = (TextView) findViewById(R.id.offreTitre);
titreOffre.setText(capitalizedString(offre.getTitle()));
villeOffre = (TextView) findViewById(R.id.offreVille);
villeOffre.setText(offre.getLocality());
infos = (TextView) findViewById(R.id.infos);
ViewTreeObserver vto = titreOffre.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(
{
#Override
public void onGlobalLayout()
{
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) villeOffre.getLayoutParams();
params.setMargins(15,titreOffre.getBottom()+12,15,0);
villeOffre.setLayoutParams(params);
titreOffre.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
This works perfectly, but now I want to put a third textview (infos) which is depending on villeOffre position. How to do that ?
Thanks a lot.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rr = new RelativeLayout(this);
b1 = new Button(this);
b1.setId(R.id.Button01);
recent = b1;
b1.setText("Click me");
rr.addView(b1);
setContentView(rr);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1 = new TextView(can.this);
tv1.setId((int)System.currentTimeMillis());
lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, recent.getId());
tv1.setText("Time: "+System.currentTimeMillis());
rr.addView(tv1, lp);
recent = tv1;
}
});
}