ar3
ar3

Reputation: 409

How to open new activity and jump to a specific child view (Something like hyperlink) in android?)

May seem a small problem, but I'm a beginner so need your help.

I've a "main activity" and there are 2 buttons called "vision and mission" and "Team".

I've "details activity" which has details like about us, vision and mission, team.

Now if I click vision and mission button in "main activity" it should automatically open "details activity" and scroll to / jump to vision and mission section of the activity. The same should work for Team button, wherein it automatically opens details activity and directly shows about team section without requiring user to scroll down.

This is something like a hyperlink to specific section of a website.

Upvotes: 0

Views: 55

Answers (1)

TecCheck
TecCheck

Reputation: 31

You can start the activity by calling this function. Here every section should have a number.

 // 1 is about us, 2 is vision and mission, etc.
public void startDetailActivity(int type){
        Intent startIntent = new Intent(this, DetailsActivity.class);
        startIntent.putExtra("Type", type);
        startActivity(startIntent);
    }

In your DetailsActivity you get the Type argument an scroll to the corresponding position.

    ScrollView scrollView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        // this is the important part
        scrollView = findViewById(R.id.scrollView);
        int type = getIntent().getIntExtra("Type", 0);
        scrollTo(type);
    }

    public void scrollTo(int type){
        if(type == 0)
            return;

        if(type == 1){
            //First argument is x, second is y. Test around a bit with the x value
            scrollView.scrollTo(10, 0); //Use this if you want no animation
            scrollView.smoothScrollTo(10, 0);//Use this if you want a scroll animation
        } //add more if you want
    }

Upvotes: 1

Related Questions