I want when I click on the menu button a menu appears from the bottom of the screen.
Any suggestions?
Create custom view
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_view"
android:layout_width="match_parent"
android:layout_height="64dp"
android:visibility="gone"
android:layout_alignParentBottom="true"
android:clickable="true">
<!-- Anything you need -->
</RelativeLayout>
In onCreate method initialize view like
mCustomView = (RelativeLayout) findViewById(R.id.custom_view);
In onOptionsItemSelected method do next:
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.some_menu) {
mCustomView.setVisibility(View.VISIBLE);
// do some animation there
}
return super.onOptionsItemSelected(item);
}
I am developing an application only to validate the interaction between activities through intents.
I created 5 ImageButton with an image for each. Each button is a movie and if you click any of them, is directed to a new activity with the synopsis of the film. The activity with the synopsis, there is an "up navigation" that returns the MainActivity (home).
The way I developed left very extensive project since created six activities (main activity and 5 activities, one for each film) and 6 layouts. Also, my apk is to 1.5mb.
Could someone help me with suggestions for best practices for I minimize my code or how developed is correct and could be developed in a real application?
I appreciate!!!
My MainActivity
package luizugliano.com.br.appfilmes;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void onClickBtVideo01(View view){
Intent intent = new Intent(getContext(),ActivityVideo01.class);
startActivity(intent);
}
public void onClickBtVideo02(View view){
Intent intent = new Intent(getContext(),ActivityVideo02.class);
startActivity(intent);
}
public void onClickBtVideo03(View view){
Intent intent = new Intent(getContext(),ActivityVideo03.class);
startActivity(intent);
}
public void onClickBtVideo04(View view){
Intent intent = new Intent(getContext(),ActivityVideo04.class);
startActivity(intent);
}
public void onClickBtVideo05(View view){
Intent intent = new Intent(getContext(),ActivityVideo05.class);
startActivity(intent);
}
private Context getContext(){
return this;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
My ActivityVideo01 (other activities have the same code so I only put this as an example)
package luizugliano.com.br.appfilmes;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
public class ActivityVideo01 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_video01);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == android.R.id.home) {
//O método finish encerrará essa activity
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
My content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<TextView android:text="Sinopse - Filmes" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="22dp"/>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/layout_marginTop">
<ImageButton
android:layout_width="#dimen/layout_width"
android:layout_height="#dimen/layout_height"
android:id="#+id/imageButton01"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical"
android:background="#drawable/btn_img_01"
android:onClick="onClickBtVideo01"/>
<ImageButton
android:layout_width="#dimen/layout_width"
android:layout_height="#dimen/layout_height"
android:id="#+id/imageButton02"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/layout_marginLeft"
android:background="#drawable/btn_img_02"
android:onClick="onClickBtVideo02"/>
<ImageButton
android:layout_width="#dimen/layout_width"
android:layout_height="#dimen/layout_height"
android:id="#+id/imageButton03"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/layout_marginLeft"
android:background="#drawable/btn_img_03"
android:onClick="onClickBtVideo03"/>
<ImageButton
android:layout_width="#dimen/layout_width"
android:layout_height="#dimen/layout_height"
android:id="#+id/imageButton04"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/layout_marginLeft"
android:background="#drawable/btn_img_04"
android:onClick="onClickBtVideo04"/>
<ImageButton
android:layout_width="#dimen/layout_width"
android:layout_height="#dimen/layout_height"
android:id="#+id/imageButton05"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/layout_marginLeft"
android:background="#drawable/btn_img_05"
android:onClick="onClickBtVideo05"/>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
My content_activity_video01.xml (other layout have the same code so I only put this as an example)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_activity_video01"
tools:context="luizugliano.com.br.appfilmes.ActivityVideo01">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Title Synopsis"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Synopsis"
android:id="#+id/textView2"
android:layout_below="#+id/textView"
android:layout_marginTop="51dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
I suggest, that you have two activities, and both these activities will act as container activity for their respective fragments.
In your first activity's fragment have your buttons. In your second activity's fragment, play your movie.
Now when you click on the button, go from first activity's fragment to second activity's fragment. Use interfaces to communicate between your fragments.
This is the way i thought about it, if you have a better way please do share.
Instead Of taking five different activity just take single activity and replace textview's value dynamically on the click of your button.
Just marge the two xml file like that in content_main.xml create another relative layout(which is initially gone view) with height width match parent under the image button linear layout. When one image button is pressed then just gone the view of linear layout of image buttons and visible the textview relative layout and visible the back button of the up navigation. When user pressed the up navigation just gone the textview relative layout, gone the up navigation and visible the linear layout of image button.
you need not to create multiple activities to show synopsis from different films.
you need to create your synopsis activity like a template and pass information about selected film in intent.
now you can change the content of your activity depending upon the information you received from intent.
Basically, whenever you are copying the code you need to ask yourself do you really need to copy the code? almost all the time answer is NO and what you actually need to do is, think of ways you can reuse the code you are copying.
Implement onclicklisstener and use switch case to detect which button has been clicked, Use only one activity to display synopsis instead of four and use bundle along with Intent to send which synopsis has to be shown. Here is the code.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public void onClick(View view)
{
switch (view.getId())
{
//handle multiple view click events
case R.id.imageButton01:
//handle click for imgbutton1;
Intent intent = new Intent(getContext(),ActivityVideo.class);
//send which video should be played along with the intent. use bundle in ActivityVideo class to get this value.
intent.putExtra("Video name", "video1");
startActivity(intent);
case R.id.imageButton02:
//handle click for imgbutton2;
Intent intent = new Intent(getContext(),ActivityVideo.class);
intent.putExtra("Video name", "video2");
startActivity(intent);
case R.id.imageButton03:
//handle click for imgbutton3;
Intent intent = new Intent(getContext(),ActivityVideo.class);
intent.putExtra("Video name", "video3");
startActivity(intent);
case R.id.imageButton04:
//handle click for imgbutton4;
Intent intent = new Intent(getContext(),ActivityVideo.class);
intent.putExtra("Video name", "video4");
startActivity(intent);
}
}
Use ActivityVideo class instid of ActivityVideo01, 02 ......
ActivityVideo.java ->
public class ActivityVideo01 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_video01);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Bundle extras = intent.getExtras();
String VideoName = extras.getString("Video Name");
// VideoName will be having the sent value.
if(VideoName == "video1"){
//code for what do you want to do when imagebutton1 is clicked
}elseif(VideoName == "video2"){
//code for what do you want to do when imagebutton2 is clicked
}elseif(VideoName == "video3"){
//code for what do you want to do when imagebutton3 is clicked
}elseif(VideoName == "video4"){
//code for what do you want to do when imagebutton4 is clicked
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == android.R.id.home) {
//O método finish encerrará essa activity
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Hope it helps!
I’m working on a project in which I have to do a shared transition.
I want to perform a transition on an ImageView from my splash Activity [First Activity] to Login Activity [Second Activity] which has the ImageView in a Fragment. The Image in my 1st Activity and The Image in the fragment of my 2nd Activity are same and Shares The Same Transition Name. I’m unable to perform the transition.
My Splash Activity Code is This
public class Splash extends Activity implements View.OnClickListener{
private ImageView imageView;
private String SharedElementname;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//getWindow().setExitTransition(R.transition.shared_element_transition_a);
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setSharedElementExitTransition(TransitionInflater.from(this).inflateTransition(R.transition.shared_element_transition_a));
}
setContentView(R.layout.activity_splash);
imageView = (ImageView)findViewById(R.id.splashimage);
SharedElementname = getString(R.string.sharedname);
imageView.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_splash, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
ActivityOptionsCompat compat = ActivityOptionsCompat.makeSceneTransitionAnimation(this,v,SharedElementname);
Intent intent = new Intent(this, LoginActivity.class);
ActivityCompat.startActivity(this, intent, compat.toBundle());
}
My Login Activity Code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(BuildConfig.MINT_API_ENABLED) {
Mint.initAndStartSession(LoginActivity.this, BuildConfig.MINT_API_KEY);
}
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setSharedElementEnterTransition(TransitionInflater.from(this).inflateTransition(R.transition.shared_element_transition_a));
}
setContentView(R.layout.activity_login);
}
Some More Codes….
*****************************************************************************
My Fragment Code Within Login Activty
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loginTab = new LoginTab();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setSharedElementReturnTransition(TransitionInflater.from(
getActivity()).inflateTransition(R.transition.shared_element_transition_a));
setExitTransition(TransitionInflater.from(
getActivity()).inflateTransition(android.R.transition.fade));
loginTab.setSharedElementEnterTransition(TransitionInflater.from(
getActivity()).inflateTransition(R.transition.shared_element_transition_a));
loginTab.setEnterTransition(TransitionInflater.from(
getActivity()).inflateTransition(android.R.transition.fade));
}
}
Some More Codes….
*************************************************************************************
My Splash Activity XML File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_new"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:id="#+id/SplashLayout"
>
<ImageView
android:id="#+id/splashimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/eventifyd_logo"
android:transitionName="#string/sharedname"
/>
</RelativeLayout>
</LinearLayout>
My Transition XML File [Shared_element_transition]
<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000">
<changeTransform />
<changeBounds />
<!—
I also tied ChangeImageTranform But Nothing Happened
-->
</transitionSet>
My Fragment XML File
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
>
<ImageView
android:id="#+id/profile_pic_login"
android:layout_above="#+id/login_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/eventifyd_logo"
android:transitionName="#string/sharedname"
/>
<TextView
android:layout_marginTop="30dp"
android:id="#+id/login_text"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/login_text"
style="#style/thin.white.large"/>
</RelativeLayout>
</LinearLayout>
I couldn't do it the way i was try. I changed the Fragments to Activity and it worked as a charm. I think Making transition from Activity to a Fragment of another activity is not Possible. Anyway, My this approach worked.
I have to make a program where what I write in EditText shows up in TextView when I press a button. However when I press the button, the app shuts down. Where am I going wrong? I have tried doing it the way many similar questions' solutions said, but to no use. Please help. PS: I'm a total noob at android programming. Any help would be appreciated.
This is my main class:
public class abc extends Activity implements View.OnClickListener {
EditText editText;
TextView textView;
Button button;
String name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_abc);
EditText editText=(EditText)findViewById(R.id.ed_edit);
TextView textView=(TextView)findViewById(R.id.text_tview);
Button button=(Button)findViewById(R.id.btn_button);
button.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_abc, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_button:
name= editText.getText().toString();
textView.setText(name);
break;
}
}
}
This is the xml code:
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:text="ABD"
android:id="#+id/text_tview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
/>
<Button android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="HERE"
android:id="#+id/btn_button"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ed_edit"/>
</LinearLayout>
You are initializing local variables using findViewById().
Replace :
EditText editText=(EditText)findViewById(R.id.ed_edit);
TextView textView=(TextView)findViewById(R.id.text_tview);
Button button=(Button)findViewById(R.id.btn_button);
With :
editText=(EditText)findViewById(R.id.ed_edit);
textView=(TextView)findViewById(R.id.text_tview);
button=(Button)findViewById(R.id.btn_button);
Why does the WebView scrollbar render as a black rectangle? It's as if the underlying scrollbar graphic isn't being loaded and the entire scrollbar area just ends up being black. I'm loading static HTML. Here's what it looks like:
Here's what I believe it should look like:
Here's the XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<WebView
android:id="#+id/initial_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Here's the MainActivity:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String html = "";
for(int i=0; i<250; i++) {
html += "text"+i+" ";
}
WebView wv = (WebView)findViewById(R.id.initial_webview);
wv.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I'm using SDK 21 and minSDK 16, as well as a Samsung Galaxy S3 profile with API 17, if that helps.