Android - Show new view - android

I am starting now to study Android. I came from iOS development. How can I do for show a new view by clicking a button in Android?
Thanks

You should create an on click listener for your button
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// as asked by Pepi, what view are you planning to display??
}
});
Write the code inside depending on what has to be displayed

if you are going for a simple view not a activity. as you said in comment then you should use LayoutInflater on click or any event you want
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View myNewViewToadd = inflater.inflate(R.layout.abc_view, null);
myParentView.removeAllViews();
myParentView.addView(myNewViewToadd, 0);

public void onClick(View v) {
// TODO Auto-generated method stub
if(v == btn_login)
{
Intent login=new Intent(this,Login.class);
Activity.this.startActivity(login);
}
else if(v == btn_register)
{
Intent register=new Intent(this,Register.class);
Activity.this.startActivity(register);
}
}

button.setOnClickListener(new View.onClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent("[package name].NEWVIEW"));
}
});
Then you need to create a new activity section in your manifest file with the name .[what's the name for your new class] and for the action you need to import your "[package name].NEWVIEW" and the .MAIN below this need to be rewrite to .DEFAULT
Hope it helped.

Related

How to know which child view is clicked in a relativelayout

I have a relative layout to which child views are added and removed dynamically(any number can be added or removed)
My question is how to know which view was clicked so that i can add different onclicklisteners depending on the type of child views
Adding and retrieving the tag while click event can help. Here is the code.
For adding tags:
customView1.setTag(someTag);
customView1.setOnClickListener(myClickListner);
For retrieiving:
OnClickListener myClickListener = new onClickListener(){
#Override
public void onClick(View v) {
if(v.getTag() == someTag){
//do stuff
}else if(v.getTag() == otherTag){
//do something else
}
}
in your adapter class you need to write like this am sharing the sample code snippet
public static class ChatListItemsViewHolder extends
RecyclerView.ViewHolder {
public ChatListItemsViewHolder(View v) {
super(v);
// TODO Auto-generated constructor stub
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// write your code here
}
});
}
let me know if you need more clarity.

i want to get position in a list data how to get the position of click of button in android

how to get the position from list on click of data.
i want access button in android.on the click of button of the adapter class it should give the position.
holder.beginDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
you can set the position as the tag
holder.beginDate.setTag(position); // set every time getting the View
and retrieve it using
// put this code where creating a new instance of holder
holder.beginDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int pos = (Integer) view.getTag();
// do anything using the position
}
});
After initialization of your button in getView() method write:
holder.beginDate.setTag(position);
and in your onClick() use:
holder.beginDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int position=Integer.parseInt(((Button)v).getTag().toString());
}
});

Show hidden views of List item on click of button outside the list

I am developing an Android Application. In this app I am making a list of views using code given below. In each item of list there is delete button with visibility "Gone". Now there is another button Edit outside the list, on click of edit button I have to show delete button on each item of list. but using this code delete button shows only in the last item. Please help me to solve the problem. Thanks.
dynamicView();
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// lay.removeAllViews();
addnew.setVisibility(View.INVISIBLE);
btn_red.setVisibility(View.VISIBLE);
edit.setText("Done");
}
});
public void dynamicView() {
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < templates.length; i++) {
final View customView = linflater.inflate(R.layout.order_template_item,
null);
btn_red=(ImageView)customView.findViewById(R.id.btn_negative);
btn_drag=(ImageView)customView.findViewById(R.id.button_drag);
btn_delete=(ImageView)customView.findViewById(R.id.button_delete);
final ImageView image = (ImageView)customView.findViewById(R.id.arrow);
final TextView text = (TextView)customView.findViewById(R.id.date);
final TextView sku = (TextView)customView.findViewById(R.id.time);
final TextView price = (TextView)customView.findViewById(R.id.last);
final TextView names =(TextView)customView.findViewById(R.id.name);
image.setId(i);
text.setId(i);
sku.setId(i);
price.setId(i);
names.setId(i);
btn_red.setId(i);
btn_red.setTag(i);
btn_delete.setId(i);
btn_drag.setId(i);
names.setText(templates[i]);
btn_red.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn_delete.setVisibility(View.VISIBLE);
TranslateAnimation anim = new TranslateAnimation(100,0 , 0, 0);
anim.setInterpolator(new BounceInterpolator());
anim.setDuration(1000);
btn_delete.setAnimation(anim);
}
});
btn_delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
lay.removeView(customView);
}
});
lay.addView(customView);
}
You're holding reference only to the last one btn_red.
You can do something like this.
List<Button> buttons = new LinkedList<Button> ();
Then in your loop after findViewById on btn_red
buttons.add(btn_red);
And finally in your onClickListener
for (Button button: buttons) {
button.setVisibility(View.VISIBLE);
}
Do somethink like below to make visible all the button added to list items-
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addnew.setVisibility(View.INVISIBLE);
for (int i = 0; i < templates.length; i++) {
int id = btn_red.getId(i);
id.setVisibility(View.VISIBLE);
}
edit.setText("Done");
}
});

Android Button Problem

i am making a app which generate buttons according to the value entered by user. each button have have there own function defined in XML. Now my main problem is how to shorten these codes.
name[0].setClickable(true);
name[0].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[0].setText("kjghjbjhb");
}
});
name[2].setClickable(true);
name[2].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[2].setText("kjghjbjhb");
}
});name[1].setClickable(true);
name[1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[1].setText("kjghjbjhb");
}
});
and soo on.....writing these codes again and again is not possible as button generated are dynamic, i dunno how many buttons will be generated. Please tell if there is a some other way to do this.
Something like this?
createButton(int i){
name[i].setClickable(true);
name[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[i].setText("kjghjbjhb");
}
});
}
With this method you can also make a for-loop:
for (int i = 0; i<name.length; i++){
createButton(i);
}
Here I am specifying the steps to be executed.
You must be creating the buttons by new Button(); just hold its reference in a Collection say ArrayList
ArrayList ar = new ArrayList();
Button b1 = new Button();
ar.add(b1);
Now create a private inner class which is implementing the View.OnClickListener. Now as per rules implement theOnClick() and so the stuff which you want to be done at there
class A extends Activity{
// your stuff here for OnCreate and other business logic
private final class MyListener implements View.OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
v.setText("kjghjbjhb");
}
}
}
Notice that I am setting the text with the reference of object v in onClick. Also make this class singleton.
Now set create the instance of this class (as the MyListerner will be singleton the object will be one) in the setOnClickListener() like this:
MyListener listener = MyListener.getInstance();
b.setOnClickListener(listener);
You can opt this way when the buttons are created on some event or user action. In case if you need to create the buttons in loop you can use the 1st and 3rd step in loop.

Android image button problem

imgvw_back.setOnClickListener(this);
imgvw.setOnClickListener(this);
static id=10
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.back:
Log.v("back",""+id--);
break;
case R.id.forward:
Log.v("next",""+id++);
break;
}
}
i am using this kind of concept but mostly fire R.id.back part ,what can i do plz give solution for this problem...
The id may not be what you think it is, you could do something like this.
imgvw_back.setOnClickListener(this);
imgvw.setOnClickListener(this);
static id=10
#Override
public void onClick(View v)
{
if(v == imgvw_back)
{
Log.v("back",""+id--);
}
else if(v == imgvw)
{
Log.v("next",""+id++);
}
}
Use inline onClickListeners for each button.
imgvw_back.setOnClickListener(new onClickListener(){
#Override
public void onClick(View v){
Log.v("back", "")
}
});
Is imgvw your forward button? Just wondering, cause your back button is imgvw_back, would assume forward would be named accordingly imgvw_forward.
You're probably missing missing to set the clickListener to the forward button as well.

Categories

Resources