FrameLayout in Android studio

FrameLayout in Android studio is an interesting layout option in Android studio. Mayor difference between frame layout on one side and Relative and Linear Layout on the other side is that FrameLayout allows widgets to flow one over another. With adequate coding, we can control visibility of each widget and make useful application. So let’s start with an empty activity. In initial layout, just replace RelativeLayot with FrameLayout. We can add to ImageView in this FrameLayout, and they will overlap each other, and are positioned in left top corner of the application. Next to be done is to delete onCreateOptionMenu in main java class.

Brisi-onCreateOptionsMenu

Also, MainActivity needs to implement OnClickListener

public class MainActivity extends AppCompatActivity {

→

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Bulb-Implements-method-View-onClickListener

Once we create onClick method, each of ImageView will listen to onClick method code.

public void onClick(View v) {

}

imgPic1.setOnClickListener(this);
imgPic2.setOnClickListener(this);

Whole code of AppCompatActivity is:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

ImageView imgPic1, imgPic2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

imgPic1 =(ImageView)findViewById(R.id.imageView1);

imgPic2 =(ImageView)findViewById(R.id.imageView2);

imgPic1.setOnClickListener(this);

imgPic2.setOnClickListener(this);

}

}

Implements-View-onClickListener-Code

With this code, we achieved that when user touches any of this images, code from onClick(View v) method would be executed. Code of onClick method is as follows:

public void onClick(View v) {

if( v.getId()==R.id.imageView1 )

{

imgPic1.setVisibility(ViewAnimator.GONE);

imgPic2.setVisibility(ViewAnimator.VISIBLE);

}

else

{

imgPic1.setVisibility(ViewAnimator.VISIBLE);

imgPic2.setVisibility(ViewAnimator.GONE);

}

}

Whole code is:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

ImageView imgPic1, imgPic2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

imgPic1 =(ImageView)findViewById(R.id.imageView1);

imgPic2 =(ImageView)findViewById(R.id.imageView2);

imgPic1.setOnClickListener(this);

imgPic2.setOnClickListener(this);

}

public void onClick(View v) {

if( v.getId()==R.id.imageView1 )

{

imgPic1.setVisibility(ViewAnimator.GONE);

imgPic2.setVisibility(ViewAnimator.VISIBLE);

}

else

{

imgPic1.setVisibility(ViewAnimator.VISIBLE);

imgPic2.setVisibility(ViewAnimator.GONE);

}

}

public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

Posted in android Tagged with: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*