I have a footer in all the layouts for a android application.
The footer will have Image buttons like "Help", "Home", this Image buttons directly link to Help class and Home class.
Can I have a one single activity class for all the footer Image buttons.
I tried with
public class FooterItems extends Activity implements OnClickListener {
#Override
public void onClick(View view) {
if(view.getId() == R.id.footerBtnHome)
{
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
return;
}
if(view.getId() == R.id.footerBtnFeedback)
{
Intent myIntent = new Intent(view.getContext(), Feedback.class);
startActivityForResult(myIntent, 0);
return;
}
}
}
but I am not getting how to call these in a class... for example the project is having MainActivity class in which I have
ImageButton buttonFeedback = (ImageButton) findViewById(R.id.btnFeedback);
buttonFeedback.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Feedback.class);
startActivityForResult(myIntent, 0);
}
});
When I call Feedback.class with onClick from one Image Button... layout and same footer items appears.
I want to use the generalised FooterItems class so I can have one class for footer and use in every other layout.
I am also using android:onClick="onClick" in xml for Image Buttons for footer only.
But how to call those generalised class FooterItems and make it work.
Looking forward to the reply.
thanks.
I can suggest another variant:
I think, you can add to XML android:onClick, for example:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
android:onClick="selfDestruct" />
And when user click this button, android programm call method selfDistruct. You just have to implement this method. Android tutorial: http://developer.android.com/reference/android/widget/Button.html
What you are asking is not much clear.
You want to have Help, Home, etc. Image Buttons as common to all layouts in your application correct ?
If you click any Image Button, you have to show the layout on top and these buttons also has to appear on screen??
If yes my answer may help you.
You told i will create footer items as one activity, but its not good. I will prefer ViewFlipper in this case. See the layout.
<LinearLayout vertical>
<ViewFlipper id=vf>
<include layout1 />
<include layout2 />
<include layout3 />
</ViewFlipper>
<LinearLayout horizontal>
<ImageButton button1 />
<ImageButton button2 />
<ImageButton button3 />
</LinearLayout>
</LinearLayout>
Initially you will get layout1 and all image buttons on screen. If you want to show layout2 when you click button3 write onClickListener as below.
ViewFlipper vf = (ViewFlipper)findViewById(R.id.vf);
The variable vf is used to change layouts.
button3.setOnClickListener(new View.OnClickListener() {
public void onClick() {
vf.setDisplayChild(1);
}
});
I hope it may help you. Bye.
I am sure you want to implement Footer view with 2 buttons: Help and Home, this should be at bottom of every activities.
If you want to implement a code for once then follow the below steps:
Define a footer layout with 2 buttons, define android:onClick="btnHelp" for help button and android:onClick="btnHome" for home button.
Now you can include this layout inside any activities by using <include>.
Define a base activity with below 2 methods.
Now extends this base activity wherever you implements this footer layout.
public void btnHelpClick(View v)
{
// do your task for Help
}
public void btnHomeClick(View v)
{
// do your task for Home
}
Related
I am trying to build a dynamic UI, but when I add the onClick method to the button whenever I push the button I go back to my previous activity. Any ideas on how to fix it?
my button's code: (the addMenu method is never run in the activities class)
<Button
android:text="New Menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/newButton"
android:layout_weight="1"
android:onClick="addMenu"/>
here is my addmenu code although no matter what goes in here(even if nothing at all) it still won't work
public void addMenu()
{
LinearLayout layout = (LinearLayout) findViewById(R.id.backLayer);
Button newButton = new Button(this);
newButton.setText("menu "+menu);
layout.addView(newButton);
menu++;
}
whenever I push the button I go back to my previous activity.
Sounds like your app is crashing and restarting... read the logcat, and you'd see something along the lines that your method signature is wrong.
android:onClick="addMenu" needs a method of public void addMenu(View v).
Or just use Java to set the button listener and remove android:onClick.
findViewById(R.id.newButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addMenu();
}
}
try this
/**
* #param v android:id="#+id/newButton"
*/
public void addMenu(View v)
{
LinearLayout layout = (LinearLayout) findViewById(R.id.backLayer);
Button newButton = new Button(this);
newButton.setText("menu "+menu);
layout.addView(newButton);
menu++;
}
I have created an Activity that uses the ViewFlipper to Switch between Different elements. Each element represents an Item in a store. I would like to add a "Buy" Button to each View. I am however not sure how to do this, since all the views use the default layout i have created. I have added the information like the Price of the Item etc Programmatically. So i am uncertain how to add a listener to the button, since they will all refer to the same button in the xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/credit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="220dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="104dp" />
<TextView
android:id="#+id/credit_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/credit_button"
android:layout_alignTop="#+id/credit_button"
android:layout_marginRight="22dp"
android:layout_marginTop="21dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:typeface="normal" />
<TextView
android:id="#+id/credit_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/credit_button"
android:layout_alignRight="#+id/credit_type"
android:layout_centerVertical="true"
android:layout_marginBottom="30dp"
android:layout_marginLeft="15dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
As you can see, the Button id is "Credit Button". So now to be able to differentiate between the different store items' buttons what would i have to do?
Note, i am adding store items dynamically as well, so i cant simply create all the views separately using xml.
OK HERE IS THE UPDATED ANSWER. I used everyones responses below to fix teh issue. So Thank you all :)
// PerkView
View PerkView = View.inflate(this, R.layout.store_category, null);
viewFlipper.addView(PerkView);
Button perkButton = (Button)
PerkView.findViewById(R.id.StoreCatItem);
// TitleView
View TitleView = View.inflate(this, R.layout.store_category, null);
viewFlipper.addView(TitleView);
Button titleButton = (Button)
TitleView.findViewById(R.id.StoreCatItem);
// ProfileView
View ProfileView = View.inflate(this, R.layout.store_category, null);
viewFlipper.addView(ProfileView);
Button profileButton = (Button)
ProfileView.findViewById(R.id.StoreCatItem);
I simply Created Multiple Views Programmatically, and then retrieved the buttons from those views afterwards. I then added the listeners to the buttons as follows:
perkButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getBaseContext(), Perks.class);
i.putExtra("player", player);
startActivity(i);
}
});
titleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getBaseContext(), Titles.class);
i.putExtra("player", player);
startActivity(i);
}
});
Thanks A lot :)
There are two ways you can handle the button click distinctly,
At the time of inflating layout for each store, add the button listener with onClick() method there only.
Or you can assign some uniqueId or flag to each store and then handle button click of store by this uniqueID.
Usually you solve this by following stack:
fragment -has-> list -has-> adapter -has-> list of items
Normally if you present one item per screen and you want to swipe between you should use ViewPager with FragmentStatePagerAdapter (allows removing items from ViewPager if needed).
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
For more items in one screen use ListView with BaseAdapter as it allows better control over item view and ListView will recycle views as you scroll or fling.
http://developer.android.com/guide/topics/ui/layout/listview.html
Set unique tag for each store button, in code you can differentiate with respect to tags.Tag is just piece of information you want set for any view, u can use it.Implement on click listener in your activity and then set that to all buttons, so all buttons click run through same code where you can easily diffrentiate between ur store buttons with respect to tags.
like below how you can set and get tag
// for setting tag
Button button = (Button) findViewById(R.id.button1);
button.setTag("unique_tag");
// get tag and then differentiate with the unique_tg
button.getTag();
You can easily create your button programmatically like so :
Button b = new Button(this); // where this = your context
b.setText("Buy");
// b."other attribute" = "other value";
b.setTag("Awesome blue shirt");
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Button buy = (Button) v;
String article = b.getTag();
// do some stuff
}
});
container.addView(b);
You can identify the buy button using its tag, as seen here.
Let's pretend this was my Java Class...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button ScreentwoGameButton = (Button) findViewById(R.id.screentwo);
ScreentwoGameButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent ScreentwoGameIntent = new Intent(Main.this, Screentwo.class);
startActivity(StartGameIntent);
}
});
How do i use this code below but the right way like.
So let's put an example if I click screentwo button the screentwo.xml will show and it will allow me to click inside if any buttons are available. Instead just stare what's in the layout.
I don't want to use the Activity to activity cause the whole point is i'm trying to avoid the flashing looking feel going to another java class.
If you look at the moron test game on Android it says example: press the blue button then red and then green, so if u press the blue button the screen will remain and not flash at all but the image of the blue button will disappear and I'm allowed to click the red and then green.
Hope that helped.
Thanks
Wahid
Button ScreentwoButton = (Button) findViewById(R.id.screentwo);
ScreentwoButton.setOnClickListener(new OnClickListener() {
private Uri Uri;
#Override
public void onClick(View v) {
setContentView(R.layout.Screentwo);
Uri uri=Uri;
Intent i=new Intent(Intent.ACTION_VIEW, uri);
mSoundManager.playSound(1);
}
});
try to use:
setContentView(R.layout.next layout); in your button click.
You could use the viewflipper class and add the different layouts as childs to the viewflipper
and set the active child. Using setcontentView will be trouble some when you use findViewById for a old layout. As findViewById will look in the layout that is specified by setContentView
I'm trying to make buttons clickable when the new layouts loads...
As what happens... I'm on the layout 1 and i have a few buttons shown...
When I press a button it will straight away show me a new layout with buttons from another .xml.
But it won't let me click anything on layout 2.
How do i make it happen?
My code is below to go from layout 1 to layout R.layout.fail.
Button SectiontwoButton = (Button) findViewById(R.id.Sectiontwo);
SectiontwoButton.setOnClickListener(new OnClickListener() {
private Uri Uri;
#Override
public void onClick(View v) {
setContentView(R.layout.fail);
Uri uri=Uri;
Intent i=new Intent(Intent.ACTION_VIEW, uri);
mSoundManager.playSound(1);
}
});
Thanks
Wahid
You can put both your layouts as child of a viewflipper.
And instead calling setContentView again, you can use
viewflipper.setDisplayChild(0);
This is the cleaner way of switching between layouts. This should also solve your click problem.
I have an app in android in which I wanna achieve the following thing:
I want to click on an image on android mobile through python script.
Does anyone know how could I achieve that?
You can create a layout with a single button. Set the background of the button as the image you want. Make the button width and height to match parent. Then register the button in a normal way to start the activity. So something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:id="#+id/button1"
android:layout_height="match_parent"
android:background="#drawable/background">
</Button>
</LinearLayout>
With your activity like this:
public class ActivityA extends Activity implements OnClickListener
{
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
Button buttonA = (Button) findViewById(R.id.button1);
buttonA.setOnClickListener(this);
}
#Override
public void onClick(View v) {
final Intent intent = new Intent(getApplicationContext(), ActivityB.class);
startActivity(intent);
}
}
When the user presses "back", he or she will go back to the giant button activity.
Please try this whenever you want to move from one activity to another activity..
Intent myIntent = new Intent(CurrentActivity.this,NextActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
Above code will take you from CurrentActivity to NextActivity....
Whenever you want that to happen just execute those three statements,
that will do your work....