All three buttons lead to same activity? - java

I'm having an issue that I can't quite figure out. I've got all three of my buttons to take me to another activity, but the problem is that they all lead me to the activity that I only want my first (Easy) button to do.
fragment_main_menu.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.app.whosesloganisthat.MainMenu$PlaceholderFragment" >
<requestFocus />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="#string/welcometowhosesloganisthat"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/buttonHard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonHard"
android:layout_alignParentBottom="true"
android:layout_marginBottom="46dp"
android:onClick="sendMessage"
android:text="#string/hard" />
<Button
android:id="#+id/buttonEasy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="48dp"
android:onClick="sendMessage"
android:text="#string/easy" />
<Button
android:id="#+id/buttonIntermediate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonEasy"
android:layout_centerVertical="true"
android:onClick="sendMessage"
android:text="#string/intermediate" />
</RelativeLayout>
MainMenu.java:
package com.app.whosesloganisthat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.os.Build;
public class MainMenu extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.app.whosesloganisthat.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main_menu); //xml file name which contains button
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, EasyLevelInfo.class);
startActivity(intent);
// Do something in response to button
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_menu,
container, false);
return rootView;
}
}
EasyLevelInfo.java:
package com.app.whosesloganisthat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
public class EasyLevelInfo extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainMenu.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.easy_level_info, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_easy_level_info,
container, false);
return rootView;
}
}
fragment_easy_level_info.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.app.whosesloganisthat.EasyLevelInfo$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:text="#string/youhaveselectedeasy"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Thanks everyone!

Look in your fragment_main_menu.xml
layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="#string/welcometowhosesloganisthat"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/buttonHard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonHard"
android:layout_alignParentBottom="true"
android:layout_marginBottom="46dp"
android:onClick="sendMessage"
android:text="#string/hard" />
<Button
android:id="#+id/buttonEasy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="48dp"
android:onClick="sendMessage"
android:text="#string/easy" />
<Button
android:id="#+id/buttonIntermediate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonEasy"
android:layout_centerVertical="true"
android:onClick="sendMessage"
android:text="#string/intermediate" />
You have three buttons buttonHard, buttonEasy , buttonIntermediate . And these three buttons have attribute android:onClick = "sendMessage" This means if one of these buttons is clicked do sendMessage method . Lets look on that method.
public void sendMessage(View view) {
Intent intent = new Intent(this, EasyLevelInfo.class);
startActivity(intent);
// Do something in response to button
}
This method starts Activity EasyLevelInfo . So if one of three buttons is clicked it will call this sendMessage method and open EasyLevelInfo Activity .

Related

I'm having a trouble creating this new activity - Udacity Project Sunshine Lesson 3

Because the class is somewhat old, and the IDE changed so much, i'm struggling a bit trying to do the same steps of the class.
I have created my Intent and i can send it by clicking on the ListView Item. Then it switches activities from the MainActivity to the DetailActivity. Yet when the DetailActivity is displayed (using DetailActivityFragment) the toolbar disappears. I compared XML's and made some improvements but now the toolbar isn't populated like the previous toolbar. The activity also "fills" the notification bar (the bar that extends to the drawer).
Question is, what am I doing wrong and what can i do to replicate the MainActivity toolbar to the new DetailActivity?
MainActivity.java
package com.example.diomonogatarilaptop.sunshine;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
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);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Something", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#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;
}
if (id == R.id.action_refresh) {
MainActivityFragment.FetchWeatherTask weatherTask = new MainActivityFragment.FetchWeatherTask();
weatherTask.execute("Buraca");
return true;
}
return super.onOptionsItemSelected(item);
}
}
DetailActivity.java:
package com.example.diomonogatarilaptop.sunshine;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container_detail, new DetailActivityFragment())
.commit();
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_detail);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.detail, 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);
}
public static class DetailActivityFragment extends Fragment {
TextView textView;
public DetailActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Intent intent =getActivity().getIntent();
View rootView =inflater.inflate(R.layout.fragment_detail, container, false);
if(intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
String forecastStr = intent.getStringExtra(Intent.EXTRA_TEXT);
textView= (TextView) rootView.findViewById((R.id.detail_text));
textView.setText(forecastStr);
}
return rootView;
}
}
}
Activity_detail.xml:
<android.support.design.widget.CoordinatorLayout 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:id="#+id/container_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.sunshine.app.DetailActivity"
tools:ignore="MergeRootFrame">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_detail"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_detail" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
Content_detail.xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment"
android:name="com.example.diomonogatarilaptop.sunshine.DetailActivity$DetailActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:layout="#layout/fragment_detail"
/>
Fragment_detail.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.diomonogatarilaptop.sunshine.DetailActivity$DetailActivityFragment"
tools:showIn="#layout/activity_detail">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="#+id/detail_text"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Some ScreenCaptures from debugging on my mobile:
MainActivity
DetailActivity Where the bug happens

Can anyone explain me how the execution process runs in this java code for Fragments in Android Studio [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I wrote this code according to the video tutorial found here
about Android Fragments, but I can't understand how the flow of execution runs through these codes. As in the tutorial I have made two fragments alone with the Main Activity. The whole program works fine, but I can't understand how the execution flow goes. Please be kind enough to explain me. Here are my source codes, I am
really sorry as they are too long, I posted all of my code. This is the Main Activity.java
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity implements top_layout_fragment.TopSelectionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//overiding the method from the interface
#Override
public void createMeme(String top, String bottom) {
bottom_layout_fragment bottomLayoutFragment = (bottom_layout_fragment) getSupportFragmentManager().findFragmentById(R.id.fragment2);
bottomLayoutFragment.setText(top, bottom);
}
#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);
}
}
this is the activity_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" 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">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.kasun.fragments.top_layout_fragment"
android:id="#+id/fragment"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
tools:layout="#layout/top_layout_fragment" />
<fragment
android:layout_width="320dp"
android:layout_height="320dp"
android:name="com.example.kasun.fragments.bottom_layout_fragment"
android:id="#+id/fragment2"
tools:layout="#layout/bottum_layout_fragment"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
this is the top_layout_fragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Button;
import android.app.Activity;
import java.util.ServiceConfigurationError;
public class top_layout_fragment extends Fragment {
private static EditText firstText, secondText;
TopSelectionListener activityCommand;
public interface TopSelectionListener {
public void createMeme(String top, String bottom);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
activityCommand = (TopSelectionListener) activity;
}
catch(ClassCastException e){
throw new ClassCastException (activity.toString());
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.top_layout_fragment, container, false);
firstText = (EditText) view.findViewById(R.id.firstText);
secondText = (EditText) view.findViewById(R.id.secondText);
final Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
buttonClicked(v);
}
}
);
return view;
}
public void buttonClicked(View view) {
activityCommand.createMeme(firstText.getText().toString(), secondText.getText().toString());
}
}
this is top_layout_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:id="#+id/firstText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="320dp"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/>
<EditText
android:id="#+id/secondText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="320dp"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_below="#id/firstText"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/secondText"
android:layout_marginTop="20dp"
android:text="#string/button_Texg"
android:id="#+id/button" />
</RelativeLayout>
this is the bottom_layout_fragment.java
package com.example.kasun.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
public class bottom_layout_fragment extends Fragment {
private TextView topText,bottomText;
// need to overide the onCreateViewMethod
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.bottum_layout_fragment,container,false);
topText=(TextView)view.findViewById(R.id.topText);
bottomText=(TextView)view.findViewById(R.id.bottomText);
return view;
}
public void setText(String topText,String bottomText){
this.topText.setText(topText);
this.bottomText.setText(bottomText);
}
}
this is the bottom_layout_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#drawable/image1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/topText"
android:layout_marginTop="34dp"
android:textColor="#FFF"
android:gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/bottomText"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/topText"
android:layout_alignStart="#+id/topText"
android:layout_marginBottom="35dp"
android:textColor="#FFF"
android:gravity="center_horizontal" />
</RelativeLayout>
This is set up so you have one activity displaying two fragments. You can see both fragments are included in the activities layout file.
The top fragment needs to communicate with the bottom one to update its text and does so through an interface called TopSelectionListener. You will notice that your activity implements that interface. The other important part is the top fragment keeps a reference to your activity using the interface. You can see that in the top fragment's onAttach method. The activity displaying this fragment must implement the TopSelectionListener interface or else you will get a ClassCastException and your application will crash.
activityCommand = (TopSelectionListener) activity;
When the button is clicked in the top fragment it will call activityCommand.createMeme(...) which calls the createMeme method in your activity.
The createMeme method then looks up a reference to your bottom Fragment and calls the setText method on it to update the text it shows.
There is another example how this works in the Android documentation.

Fragment as ViewGroup?

When I go into the view hierarchy it show RelativeLayout as the Viewgroup for the TextView and AnalogClock. So where did the fragment view go? Isn't the fragment contain the RelativeLayout, TextView, and AnalogClock in fragment_main.xml since it is the one that is inflated in MainActivity.java? Also, where did the TextView with "TestTest" as its text go?
Here's my code:
MainActivity:
package com.example.micha.solo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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);
}
}
MainActivityFragment:
package com.example.micha.solo;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
activity_main.xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/fragment"
android:name="com.example.micha.solo.MainActivityFragment" tools:layout="#layout/fragment_main"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView android:text="TestTest" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</fragment>
and fragment_main.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"
tools:showIn="#layout/activity_main">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<AnalogClock
android:layout_width="100dp"
android:layout_height="100dp" />
</RelativeLayout>

android widget within a widget not working as intended

I use a FrameLayout as a custom list item. I have an onItemClickListener set up to the listView but my items each have a button within themselves, and while the button in them is clickable, the item itself is not and they're supposed to do different things. How do I make the item as well as the button within it both clickable?
Here's the code:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class ProjectCreateScreen extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondary_layout1);
final TextView noProject = (TextView) findViewById(R.id.NOPROJECT);
Button btn = (Button) findViewById(R.id.addBtn);
final ArrayList<String> listItems=new ArrayList<String>();
final ListAdapter addAdapter = new ArrayAdapter<String>(this,
R.layout.list_item, R.id.listFrame, listItems);
final ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(addAdapter);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
noProject.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
listItems.add("New Project");
((ArrayAdapter) addAdapter).notifyDataSetChanged();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent switchToEdit = new Intent(ProjectCreateScreen.this,
teamCreateScreen.class);
startActivity(switchToEdit);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The custom listView item xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/listFrame"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="20sp"
/>
<Button android:id="#+id/frameBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="top|right"
/>
</FrameLayout>
The xml of the activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rl">
<Button
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="#string/AddProject"
android:id="#+id/addBtn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/noProjectsNotice"
android:id="#+id/NOPROJECT"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="16sp"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/addBtn"
android:id="#+id/lv"
android:visibility="invisible">
</ListView>
</RelativeLayout>
Add the following two attributes for any view you want to be clickable inside the listview:
android:focusable="false"
android:focusableInTouchMode="false"
That should solve most cases. If that is not enough, also add the following attribute to the root layout of your list view layout
android:descendantFocusability="blocksDescendants"

No view found for id 0x7f070053

was testing som things in Android Studios and ran in to this error:
FATAL EXCEPTION: main
Process: com.meisolsson.app, PID: 25610
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.meisolsson.app/com.meisolsson.app.LoginActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f070053 (com.meisolsson.app:id/container) for fragment PlaceholderFragment{4284e750 #0 id=0x7f070053}
Here is my LoginActivity.java
package com.meisolsson.app;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.view.Window;
import android.widget.Button;
public class LoginActivity extends ActionBarActivity {
private Button loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_login);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
this.setContentView(R.layout.fragment_login);
this.loginButton = (Button)this.findViewById(R.id.button);
this.loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_login, container, false);
return rootView;
}
}
}
And my fragment_login.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=".LoginActivity$PlaceholderFragment"
android:focusable="true"
android:background="#fcff00">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:hint="#string/user"
android:padding="10dp"
android:background="#ffffff"
android:layout_above="#+id/editText2"
android:layout_alignLeft="#+id/editText2"
android:layout_alignRight="#+id/editText2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:hint="#string/pass"
android:background="#ffffff"
android:padding="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/login"
android:id="#+id/button"
style="#style/AppTheme"
android:background="#ff5900"
android:shadowColor="#000000"
android:layout_below="#+id/editText2"
android:layout_alignLeft="#+id/editText"
android:layout_alignRight="#+id/editText2"
android:clickable="true"/>
</RelativeLayout>
The problem is the way you're doing this. For the Activity, you don't set the Fragment's view, you set a separate view that has a spot to put a Fragment. Depending on how you did your PlaceHolderFragment, the following ought to get a working app.
First add this file:
activity_main.xml
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then change this stuff in your Activity:
LoginActivity.java
/* Same stuff*/
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
//This is the important change right here!
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
//The stuff that you had handling buttons should go in your
//PlaceHolderFragment class's onCreateView() method
}
I hope this helps. Good Luck!

Categories

Resources