ViewPager change to another page on textview click - android

I have used an example code from the following site:
http://www.journaldev.com/10096/android-viewpager-example-tutorial
It works but I need to add another page where there is a textview one can click to go to one of the pages (for eg. BLUE page). I have put the TextView in the xml file with the 2 lines:
android:clickable="true"
android:onClick="click"
I have also added the following incomplete method in the MainActivity.
public void click(View view) {
switch (view.getId()) {
case R.id.change_page:
//what do I need to add in here?
}
}
I am not sure I am setting this up the right way. Any advice will be helpful.

case R.id.change_page :
mViewPager.setCurrentPage(yourpageindex);
break;
case: R.id.another_text view:
//go to another page

create another activity, and separate layout file, and use an explicit intent in the main activity,
in the new layout file , use the desired ViewPager
probably this may work .

as #hash-set suggested. setCurrentPage(yourpageindex); will do the trick.
all you need to know is what is index value of your BLUE page. and set it in place of yourpageindex.
for Example : let's assume index of Your Blue page is "1".
public void click(View view) {
switch (view.getId()) {
case R.id.change_page:
viewPager.setCurrentItem(1);
}
}

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

Get Drawable of a button on Another Activity in Android

I Have a button with a top drawable image, onclicking the button a intent is created i want to pass this drawable to the intent. How to do it pls help me out thanks in advance..
Button
Image to be showed on next activity
Assuming that your buttons are all atrological signs, and that you know what image resource you are using for each, why don't you just assign all your buttons the same onClick listener, and use a switch case to determine which sign it is.
public void onClick(View v) {
Intent i = new Intent(this, yourSecondActivity.class);
switch(v.getId()){
case R.id.ariesBtn:
i.putExra("sign", "aries");
break;
case R.id.cancerBtn:
i.putExtra("sign", "cancer");
break;
}
startActivity(i);
}
Then in your other activity check for extras "sign" and act accordingly
Try this:
#drawable/filename
or post the code to better undertastand

Detect which child is clicked on click of the parent

I have a layout which contains lots of images. What I have to do is when an image is clicked, I have to show its details. But I don't want to have onClickListeners for all the images. How can I achieve this?
You don't have to have different handlers for all the images. Instead use one handler for all the images. This would make your code cleaner, manageable and solve your problem too.
public void onCreate(Bundle bundle) {
//...
OnClickListener mHandler = new OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.img1:
//..
break;
case R.id.img2:
//....
break;
}
}
};
ImageButton btn1 = (ImageButton)findViewById(R.id.img1);
ImageButton btn2 = (ImageButton)findViewById(R.id.img2);
//...
btn1.SetOnClickListener(mHandler);
btn2.SetOnClickListener(mHandler);
//...
}
One Listener to rule them all.
Implement onClick() on an object, register it as listener
In onClick(), examine the View object passed as parameter to determine which of the images was clicked. You can do anything from getId() to casting it to (ImageView) and getting the actual image out.
Once you know which image was clicked, do what you will with it.
If you're looking to implement custom behavior for an ImageView (or whatever), and then have multiple instances of that type of view, you should subclass the ImageView and put your listener in there. Then you've got an encapsulated View that implements the custom behavior you want, and if you decide later that you want more or less or them, or to put them in another place, it's easy to move the View and its behavior without ripping apart your Activity.

Android remove button dynamically

I have a button and when I press it, i want to remove it (not make it invisible). I read that I can do that using layout.removeView(mybutton) but what is the layout ? and how can I get it in my activity
Button showQuestion;
private void initialize() {
showQuestion = (Button) findViewById(R.id.bAnswerQuestionShowQuestion);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.bAnswerQuestionShowQuestion:
showQuestion.setVisibility(View.INVISIBLE);
//Here i want to delete the button
question.setVisibility(View.VISIBLE);
theAnswer.setVisibility(View.VISIBLE);
answerQuestion.setVisibility(View.VISIBLE);
showChoices.setVisibility(View.VISIBLE);
showHint.setVisibility(View.VISIBLE);
break;
}
}
see link
ViewGroup layout = (ViewGroup) button.getParent();
if(null!=layout) //for safety only as you are doing onClick
layout.removeView(button);
i have a button and when i press it , i want to remove it (not make it
invisible)
=> You did as below:
showQuestion.setVisibility(View.INVISIBLE);
Try with:
showQuestion.setVisibility(View.GONE);
FYI, INVISIBLE just hide the view but physically present there and GONE hide as well remove the presence physically as well.
You can use
button.setVisibility(View.GONE);
Layout is the parent Layout of your Button, usually a RelativeLayout or LinearLayout.
You can get it as follows:
ViewParent layout = button.getParent();

Android, screwy behavior

Cant figure this out (am a noob) but I have a default activity that starts with just one image button, clicking that image button opens the second activity that contains a bunch of image buttons but only one has an onclick, this is the exact code:
<ImageButton android:src="#drawable/level1"
android:layout_width="wrap_content" android:id="#+id/imageButton1"
android:layout_height="wrap_content"
android:onClick="button_clicked1">
</ImageButton>
in the java file this is my code:
public void button_clicked1(View v) {
//text1.setText("clicked");
//ib2.setImageResource(R.drawable.level5);
}
The screwy part, if that above function is empty nothing happens but no matter what I put in the above function... for example even a simple settext like the above commented one, and it force closes on me :((
the full java file if you are interested: http://pastebin.com/W4sJUKXH
and the manifest file (as the problem might be there, like I said, am a noobie) http://pastebin.com/yEuG1su7
** Where i think the error is:
In the manifest file I only have declared the second activity, nothing else...
Set id for the ImageViews, this is how you handle many Buttons with one method
public void button_clicked1(View v) {
switch(v.getId())
{
case R.id.myfirstimage:
//do something
break;
case R.id.mysecondimage:
// do something
break;
// add more cases
default:
// do something if none of the cases is your image view or do nothing
}
}
Set setContentView( ) before you do anything with the UI components. (Thats why you get some Exception)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_levels_screen);
text1 = (TextView) findViewById(R.id.textView1);
ib2 = (ImageButton) findViewById(R.id.imageButton2);
}

Categories

Resources