I am a new in android programming, I made a layout with this figure:
Now I want to know when one of these buttons clicked I should run an new activity or change visibility to false and show new layout without run a new activity, what is the best solution?
You consider that count of these buttons are more than ten.
I want show a text with image,..(when clicked) because that is a educational book and these buttons are chapters list of that book
for an example if you want to change only the layout then you could do something like this
FirstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
FirstView();
}
});
/
void FirstView(){
setContentView(R.layout.yourOtherLayout);
// then declare the layout views here.
firstView=false;
}
you can do this in all the buttons just create different methods for each
to handle the Back Button you can declare Boolean variables and use If else Statement to loop through them for example
boolean firstView = true, secondView = true;
#Override
public void onBackPressed(){
if (firstView == false ){
then firstView Is Showing.
// show the view you want and set
firstView = true;
}else if (SO ON)...
else { super.OnBackPressed(); // exit }
}
Related
I use BottomNavigationView in my app (modified from sample project) and I want to change the items in the container. I have 4 items within the container, 5 including the BottomNavigationView. I only want to display 2 items when apps is first start. Then show another 2 items and hide the previous 2 items when one of the Button in BottomNavigationView is clicked.
How to do it?
to hide any view button,textview or edittext just use this code on the button click
view.setVisibility(GONE)
GONE hides the whole view even the height and the width of the view
view.setVisibility(INVISIBLE)
INVISIBLE only hides the content but preserves the height and the width
boolean state; // define boolean variable
home.setVisibility(View.GONE);
info.setVisibility(View.GONE);
In your botton navigation click listener just use
b2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if ( state )
{
state = false;
home.setVisibility(View.VISIBLE);
info.setVisibility(View.VISIBLE);
}
else
{
state = true;
home.setVisibility(View.GONE);
info.setVisibility(View.GONE);
}
}
}
I have dynamic buttons that are created by a variable that can change.I want that these buttons have two functions. I did one option but I don't know how to implement the other option.
the first time I click the button I call a function that do something and the second time that I click the same button I would like to do another action. And I want to repeat this running with all the dynamic buttons created.
My code is:
LinearLayout buttonsLayout = (LinearLayout)findViewById(R.id.linearlayoutUp);
for(int i=0;i<drawView.getNumeroMallas();i++){
Button buttonMalla = new Button(this);
buttonMalla.setText("Malla "+(i+1));
buttonMalla.setId(i+1);
final int index = i;
buttonMalla.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Malla malla = drawView.getMalla(index);
drawView.paintMallaSelected(malla);
}
}
});
buttonsLayout.addView(buttonMalla);
}
}
EDIT - Very important:
I readed your code again, you could use the getTag/setTag to remember the last state of the button (i missed the for part, sorry!)
for(int i=0;i<drawView.getNumeroMallas();i++){
Button buttonMalla = new Button(this);
buttonMalla.setText("Malla "+(i+1));
buttonMalla.setId(i+1);
buttonMaila.setTag(Boolean.FALSE);
Then in setOnClickListener
if (((Boolean)v.getTag()) == Boolean.TRUE)
{
// Do first action
v.setTag(Boolean.FALSE);
}
else
{
// Do second action
v.setTag(Boolean.TRUE);
}
An idea could be to use a variable to know which was the last action.
A boolean variable
private boolean action = false;
If action is false do the first thing and set it to true. If it's true do the second action and set it to false.
It should go out any method (global of the class)
Something like
private boolean action;
buttonMalla.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (action == true)
{
// Do first action
}
else
{
// Do second action
}
action = !action;
Malla malla = drawView.getMalla(index);
drawView.paintMallaSelected(malla);
}
}
});
Anyway if it does something of important you should manage better the button (example button action is not based on a boolean variable but in a specific state.) time ago i builded a "select all" and "unselect all" function in an app and i checked if the user have unselect manually something to let the button act again like a "select".. I hope i gave to you an idea.
Anyway the boolean variable is the most immediate way to do it.
I want to show my list view when the user clicks on a button and hide it again when they click on a button. This is the onClick listener for the button in question:
connectBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(open){
mDbAdapter.close();
connectBtn.setText("Open Database");
open = false;
hideUI();
}else{
mDbAdapter = new ContactsDbAdapter(v.getContext());
mDbAdapter.open();
connectBtn.setText("Close Database");
open = true;
showUI();
//retrieve data
fillData();
}
}
});
This is the showUI() method:
protected void showUI() {
fName.setVisibility(View.VISIBLE);
lName.setVisibility(View.VISIBLE);
fNameBox.setVisibility(View.VISIBLE);
lNameBox.setVisibility(View.VISIBLE);
createBtn.setVisibility(View.VISIBLE);
this.setVisible(true);
createBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDbAdapter.createContact(fNameBox.getText().toString(), lNameBox.getText().toString());
fillData();
}
});
}
and the hideUI() method:
protected void hideUI() {
fName.setVisibility(View.INVISIBLE);
lName.setVisibility(View.INVISIBLE);
fNameBox.setVisibility(View.INVISIBLE);
fNameBox.clearComposingText();
lNameBox.setVisibility(View.INVISIBLE);
lNameBox.clearComposingText();
createBtn.setVisibility(View.INVISIBLE);
this.setVisible(false);
}
It works fine when I set the visibility to true. However when I set it to false I get a black screen but no crash or error. Any idea?
NOTE: this.setVisible(false);. My class extends ListActivity.
setVisibility(View.INVISIBLE);
Just makes you view invisible but the space taken by view will be their itself
use setVisibility(View.GONE); so that the size of view will be lapsed
Use this and let me know if it is helpful
ListActivity is hold list view
if u do this.setVissiblity(false);
it hide the list view and its contents so u r seeing background color in ur case it is black.
Good way is take Listview in xml and get id make vissible nad invissible of that view u feel very confortable with this apprch
http://www.vogella.com/articles/AndroidListView/article.html read this u will get clear idea. and make changes accordingly
I have one custom listview containing imagebuttons and textviews. Right now when I click on the info icon, it opens up the pop up(which is actually a layout that I am making visible on the imagebutton's click) that gives some description and when I click on that icon again it becomes invisible. But I want it to make invisible whenever I click on any other area and not just on that info icon.
//img_Info is the Imagebutton containing i icon
img_Info = (ImageButton)view.findViewById(R.id.img_Info);
img_Info.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//llimg_info is the linearlayout that becomes visible on the click event
if(llimg_info.isShown()) {
llimg_info.setVisibility(llimg_info.INVISIBLE);
}
else {
llimg_info.setVisibility(llimg_info.VISIBLE);
}
}
});
Any suggestions please?
Please try below code for check visibility of ImageView, it will solve your problem.
ImageView llimg_info = (ImageView) findViewById(R.id.img_Info);
if (llimg_info.getVisibility() == 0) {
System.out.println("Visible");
llimg_info.setVisibility(llimg_info.INVISIBLE);
} else{
System.out.println("Invisible");
llimg_info.setVisibility(llimg_info.VISIBLE);
}
Not sure if it will help, but try to register a focus change listener for your pop-up, so that when it will loose focus to make it invisible.
Something like this:
thePopup.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
thePopup.setVisibility(View.GONE);
}
}
});
I have a layout which is invisible when the activity starts.When I click on a button the layout becomes visible.My requirement is when I click the button for the second time, the layout should be invisible.I know this is a silly question but as I am a new to android, I am unable to figure it out.
Try the following code to toggle the visibility of the view:
v.setVisibility(v.getVisibility() == View.INVISIBLE ? View.VISIBLE
: View.INVISIBLE);
You can also implement by using boolean FLAG.
e.g. Declare
boolean visibility_Flag = false;
button..setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(visibility_Flag){
YourView.setVisibility(View.INVISIBLE);
visibility_Flag = false;
} else {
YourView.setVisibility(View.VISIBLE);
visibility_Flag =true;
}
}
});