Android - How do I make buttons clickable when new layouts loads - android

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.

Related

How to load a layout inside another one on click and change button function?

I'm trying to do something like this:
When I go to this activity I have what is in black and some objects like EditText boxes.
Once I press the button I want those EditBoxes an other stuff that is up there to stay visible but unable to be edited (that's easy to do from code overriding onClick).
But at the same time I also want to load some layout down inside the same activity (from an xml) and change the button function to act over the objects of the new layout.
Could anyone give me an idea on how to do this two things staying in the same activity?
Update:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
editText1.setEnabled(false);
editText2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
}
});
}
On the first click I want the button to do that. But once it's pressed I want it to do another thing. How could I do that?
(that's easy to do from code overriding onClick).
Actually I would recommend enable or disable which is easier to trace by using
view.setEnabled(bool);
as for the other question I'd recommend adding the layout from the start with setting visibility to GONE and when needed set the visibility to VISIBLE
view.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
Ok, I've realized it was a dumb question, just add a flag an edit it:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!button_pressed) {
editText1.setEnabled(false);
edittext2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
button_pressed=true;
}
else{
create_button.setText("Second click");
create_button.setEnabled(false);
}
}
});
}
}

how to have a single activity class for many Image Buttons

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
}

In Android: How do you show layout twice using intent?

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

Layout in android

I have three buttons in one activity.when each of the button is clicked ,different layout should be shown with in the same activity.for example if first button is clicked,edit boxes and button should be shown.if second buttojn is clicked listview should be shown etc..
Define different layout files for each layout.
Then after each click event have the intent call this particular activity recalled.
Have setContentView() called conditionally ie determining the particular clickevent and vice versa.
This you can do if you want complete activity to be layuot in diffrent manner. Otherwise if you want some widgets to be displayed on button click then it is pretty easy to show them on click event.
You might wanna consider a "TabWidget" for this. It actually does what you need.
A sample tutorial here.
Why don't you just include the all the layout elements in your single layout, then use the setVisibility attribute to turn them on and off, depending on which button is pressed.
Something like this pseudo code:
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view1.setVisibility(View.GONE);
view2.setVisibility(View.GONE);
view2.setVisibility(View.VISIBLE);
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view1.setVisibility(View.VISIBLE);
view2.setVisibility(View.GONE);
view2.setVisibility(View.GONE);
}
});

How to connect two activities

I have made one screen with two images and I would like to add a button lower on the page which will navigate to a second page when I click it. Do you know how to code this? I know how to create a button but I don't know how to connect the two screens!
This task is accomplished with the startActivity(); method using Intents.
Intent i = new Intent(FromActivity.this, ToActivity.class);
startActivity(i);
In this case the Intent uses your current Activity as the Context in the first parameter, and the destination Activity in the second parameter.
Make sure that you add your second Activity to the manifest also (it resides in the tag)!
<activity android:name=".ToActivity"
android:label="#string/app_name">
</activity>
To sum it up:
ImageView myImage = (ImageView) findViewById(R.id.image);
myImage.setOnClickListener(new OnClickListener() {
#Override
onClick(View v) {
Intent intent = new Intent(FromActivity.this, ToActivity.class);
startActivity(intent);
}
}
);
Intent intent = new Intent(currentActivity.this,nextActivity.class);
this.finish();
startActivity(intent);
Button start_button=(Button)findViewById(R.id.btnsend);
start_button.setonClickListener(new onClickListener( ){
#override
onClick(View view){
Intent i = new Intent(MainActivity.this, NewActivity.class);
startActivity(i);
}
}
);
Lets break the answer in two parts, XML & JAVA part as every activity has each of these two. Assuming we are having only two activity, 'Activity1' being the one with the button which would redirect user to 'Activity2'.
As we have 2 activity, we would be having 4 files related for these 2 activity.
XML
so lets first do the easy way, as soon as you open the .xml file of Activity1, you should shift to design tab.
After reaching the design part you can insert a button from pallet, now you can select button which is inside your layout. After selection you could see the properties of button in right section of your screen where you can effectly change multiple properties of the button.
Here you shall find the "onClick" option, fill the box next to it with anything very simple, or something which you can remember. For example enter "nextAct"
or
Hard way would be entering the onClick property manually by typing follwing line in button code in XML
android:onClick="nextAct"
This is all on XML part.
JAVA
Open the .java file of Activity1, here you have to make a new method. This method should be named same as in the "onClick" property of button. Over here i would be taking "nextAct" as that is what i had used in XML. You can place this new method anywhere inside the class of the java file, i prefer keeping it at the end of the class as i could easily locate it if any issue in future.
Now you have to write the body of nextAct method. This can be sumed up in these two lines
public void nextAct(View v){
Intent i = new Intent(this, Activity2.class);
startActivity(i);
}
After this both should be connected and working fine.
give id to your button and mention it in your MainActivity.class.Then you can call OnClickListener to listen to your click.
Button mButton = (Button)findViewById(R.id.buttonid);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//you can use anything in place of i
Intent i = new Intent(MainActivity.this, NextActivity.class);
startActivity(i);
}
});

Categories

Resources