Unexpected behavior with button visibility - Android - android

im having problems with a simple task.
I have 2 Buttons and i want only one to be visible at same time, so when you touch one it hides and the other appears.
This is my code:
fromAnex = new Button(this);
fromAnex.setText("from");
fromAnex.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONClickListener", "from anex");
returnFromAnex();
}
});
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF,plano.getId());
params.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(fromAnex,params);
fromAnex.setVisibility(View.GONE);
toAnex = new Button(this);
toAnex.setText("to");
toAnex.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONClickListener", "Show anex");
showAnex();
}
});
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.RIGHT_OF,plano.getId());
params2.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(toAnex,params2);
private void showAnex()
{
fromAnex.setVisibility(View.VISIBLE);
toAnex.setVisibility(View.GONE);
private void returnFromAnex()
{
fromAnex.setVisibility(View.GONE);
toAnex.setVisibility(View.VISIBLE);
I really cant understand why but that first call to fromAnex.setVisibility(View.GONE); is working as expected, the first call to showAnex() at fromAnex.setVisibility(View.VISIBLE);
is working too but toAnex.setVisibility(View.GONE); right under doesnt work.
And after that nothing happens with buttons visibility when i touch.
Someone can see whats wrong with my code?
Sorry for my bad english and thanks.
EDIT:
That two methods arent complete, the rest are not related with this buttons visibility problem.
When i set visibility to gone just after creating the button it works but then i cant set visibility to gone again, thats the problem.

Taking your snippet I tried run the following and it works perfectly.I guess you are making some small mistake some where as DevTest said.
public class MainActivity extends Activity {
Button fromAnex,toAnex;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rl=new RelativeLayout(getApplicationContext());
RelativeLayout.LayoutParams relPra=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
rl.setLayoutParams(relPra);
fromAnex = new Button(this);
fromAnex.setText("from");
fromAnex.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONClickListener", "from anex");
returnFromAnex();
}
});
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(fromAnex,params);
fromAnex.setVisibility(View.GONE);
fromAnex.setId(1);
toAnex = new Button(this);
toAnex.setId(2);
toAnex.setText("to");
toAnex.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONClickListener", "Show anex");
showAnex();
}
});
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.RIGHT_OF,fromAnex.getId());
params2.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(toAnex,params2);
setContentView(rl);
}
private void showAnex()
{
fromAnex.setVisibility(View.VISIBLE);
toAnex.setVisibility(View.GONE);
}
private void returnFromAnex()
{
fromAnex.setVisibility(View.GONE);
toAnex.setVisibility(View.VISIBLE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Related

Dynamically add LinearLayout into LinearLayout

I have a issue trying to dynamically add some linear layout into a linearlayout container after user have clicked on a button.
private void AddView() {
MyView myView1 = new MyView("Name");
this.mainLinearLayout.addView(myView1);
}
This code works great in activity's onCreate method but not after handling user event.
Do you have any idea why it's not working ? (I mean nothing appears on UI)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AddView(); => works great
}
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddView(); => not working
}
});
Thanks,
I think that AddView is wrong.
private void addView() {
LayoutParams lparams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
MyView myView1 = new MyView("Name");
myView1.setLayoutParams(lparams);
this.mainLinearLayout.addView(myView1);
}

how to implement pop-up code for multiple buttons on the screen in the android?

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.

Datepicker to parse date

What i would like is when i select a date it filters the rss feed to display the roadworks on the day that is selected. Only for now i start an intent to take me to the next activity which displays all the roadworks and not the ones on the day selected. How would i go about this?
public class ChooseDate extends Activity {
RelativeLayout rl;
Button pick;
DatePicker date;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.date_picker);
rl = (RelativeLayout) findViewById(R.id.rl);
pick = (Button) findViewById(R.id.button1);
date = new DatePicker (ChooseDate.this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
date.setLayoutParams(params);
rl.addView(date);
pick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Date is : " +
date.getDayOfMonth() + "-"+(date.getMonth()+1) +
"-"+ date.getYear(), Toast.LENGTH_SHORT).show();
startActivity(new Intent("com.mobilerwts.robert.MainActivityOther"));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
If I understand this right, you want to pass the date from the first activity to the second. If you are going to do that, you need to include it in the Intent. Basically, you have to include the data in the intent. See How do I pass data between Activities in Android application? for how to do that.

Text size not decreasing

I'm working on rendering a text on textview. I'm trying to add two buttons to zoom in and out the text inside text view and the code goes like this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chapter_view);
String chapter;
chapter=getIntent().getExtras().getString("ChapterName");
final StringBuffer buffer = new StringBuffer();
int id= getResources().getIdentifier(chapter,"raw",getPackageName());
try{
DataInputStream dataIO= new DataInputStream(getResources().openRawResource(id));
String strLine= null;
while((strLine = dataIO.readLine())!=null){
buffer.append(strLine);
buffer.append("\n");
}
dataIO.close();
}catch(Exception e){
}
final TextView tv=(TextView) findViewById(R.id.chapter);
tv.setText(buffer.toString());
Button zoomIn = (Button) findViewById(R.id.zoomin);
zoomIn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
textSize = tv.getTextSize();
tv.setTextSize((float) (textSize+0.25));
}
});
Button zoomOut = (Button) findViewById(R.id.zoomout);
zoomOut.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
textSize = tv.getTextSize();
tv.setTextSize((float) (textSize-0.25));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.chapter_view, menu);
return true;
}
}
But the problem I'm getting is the even upon clicking zoom out button, it still increasing the font size of the text. Please help me out in this. Also once I close one chapter and opens another, the text size will reset to its default value. Are there any solutions regarding this. I'm thinking of using the namevalue pair for this solution.
The problem is that the unit passed to setTextSize(float size) is in scaled pixels, whereas getTextSize() reports in pixels. Try using setTextSize(int unit, float size) instead, setting unit to TypedValue.COMPLEX_UNIT_PX.
I've tried a sample at my machine. Check the code below. You have to place the variable at the right place with the scope.
public class MainActivity extends Activity {
float textSize;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView)findViewById(R.id.textView1);
textSize = tv.getTextSize();
Button btnPlus = (Button)findViewById(R.id.button1);
btnPlus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView)findViewById(R.id.textView1);
Log.v("TextSizeP", String.valueOf(textSize));
textSize = (float) (textSize+0.25);
tv.setTextSize(textSize);
Log.v("TextSizeP", String.valueOf(textSize));
}
});
Button btnMinus = (Button)findViewById(R.id.button2);
btnMinus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView)findViewById(R.id.textView1);
Log.v("TextSize", String.valueOf(textSize));
textSize = (float) (textSize-0.25);
tv.setTextSize(textSize);
Log.v("TextSize", String.valueOf((float) (textSize-0.25)));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
For example I have my textSize variable declared at the class level. And the textSize variable is increased and decreased when the relevant button is clicked.
This works fine for me.
As for the size that returns to its default , you will have to use shared preferences to save the value of the last text size .
As for the text size , why don't you increment the value one by one , for instance tv.setTextSize(textsize++) when zoomOut is clicked
or tv.setTextSize(textsize--) when zoomIN is clicked
public class MainActivity extends Activity {
TextView tv;
Button in,out;
static float textSize;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,30);
textSize = tv.getTextSize();
in = (Button) findViewById(R.id.zoomin);
out = (Button) findViewById(R.id.zoomout);
in.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textSize = (float)(textSize + 2);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
}
});
out.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textSize = (float)(textSize - 2);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

Android : How to set TextViews positions depending on other ones programatically?

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;
}
});
}

Categories

Resources