20160919
//activity_main.xmlに<Button と <fragment />を入れてFragmentを入れ替える
package com.example.kunihiro.myapplication;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ChangeFragment(View view){
android.app.Fragment fragment;
if(view == findViewById(R.id.button)){
fragment = new BlankFragment1();
FragmentManager fm=getFragmentManager();
android.app.FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.fragment_place,fragment);
ft.commit(); }
if(view == findViewById(R.id.button2)){
fragment = new BlankFragment2();
FragmentManager fm=getFragmentManager();
android.app.FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.fragment_place,fragment);
ft.commit();
}
}
}
//
package com.example.kunihiro.myapplication;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank_fragment1, container, false);
}
}
//
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment2 extends android.app.Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
}
}
//avtivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_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.kunihiro.myapplication.MainActivity"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment1"
android:id="@+id/button"
android:onClick="ChangeFragment" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment2"
android:id="@+id/button2"
android:onClick="ChangeFragment" />
<!--defalt layout.FragmentOne android:name="layout.FragmentOne" -->
<fragment
android:name="com.example.kunihiro.myapplication.BlankFragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_place"
tools:layout="@layout/fragment_blank_fragment1"></fragment>
</LinearLayout>
//fragment_blank_fragment1.xml
<FrameLayout
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"
tools:context="com.example.kunihiro.myapplication.BlankFragment1"
android:background="#ca3412">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40dp"
android:text="Fragment1" />
</FrameLayout>
//fragment_blank_fragment2.xml
<FrameLayout
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"
tools:context="com.example.kunihiro.myapplication.BlankFragment1"
android:background="#999999">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40dp"
android:text="Fragment2" />
</FrameLayout>
20160909
//activity_main.xmlに<fragment />を入れる
package com.example.kunihiro.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
//
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class BlankFragment extends Fragment {
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
// 引数で渡ってきたinflaterを使ってレイアウトをinfrateする
//inflate: 拡張する,膨張させる 吹き込む
//メインのlayoutに別のレイアウトを動的に挿入することができる
final View layout = inflater.inflate(R.layout.fragment_blank, container);
// 必要に応じてUIにコールバックを設定する
layout.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getContext(), "Hello Fragment!!", Toast.LENGTH_SHORT).show();
}
});
// infrateされたViewをそのまま返す
return layout;
}
}
//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:id="@+id/activity_main"
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.kunihiro.myapplication.MainActivity">
<fragment
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:name="com.example.kunihiro.myapplication.BlankFragment"
android:id="@+id/fragment"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
//fragment_blank.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"
tools:context="com.example.kunihiro.myapplication.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="28dp"
android:layout_marginStart="28dp"
android:layout_marginTop="62dp"
android:id="@+id/button" />
</RelativeLayout>
20170327
package com.example.wata.myapplication;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements BlankFragment.OnFragmentInteractionListener{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BlankFragment blankFragment = new BlankFragment();
getSupportFragmentManager().beginTransaction().add(R.id.activity_main,blankFragment).commit();
}
public void onFragmentInteraction(String data) {
Toast.makeText(this,data,Toast.LENGTH_LONG).show();
}
}
//
package com.example.wata.myapplication;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment extends Fragment {
private OnFragmentInteractionListener mListener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mListener.onFragmentInteraction("aaaaaaaaaaaaa");
return inflater.inflate(R.layout.fragment_blank, container, false);
}
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(String data);
}
}
20170326
package com.example.wata.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements BlankFragment.OnFragmentInteractionListener{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BlankFragment blankFragment=BlankFragment.newInstance("Test");
android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(
R.id.activity_main,
blankFragment,
blankFragment.getTag()
).commit();
}
public void onFragmentInteraction(String data) {
Toast.makeText(this,data,Toast.LENGTH_LONG).show();
}
}
//
package com.example.wata.myapplication;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment extends Fragment {
private static final String ARG_PARAM = "param";
private String mParam;
private OnFragmentInteractionListener mListener;
public static BlankFragment newInstance(String str) {
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM, str);
fragment.setArguments(args);
return fragment;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam = getArguments().getString(ARG_PARAM);
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mListener.onFragmentInteraction(mParam+"---add");
return inflater.inflate(R.layout.fragment_blank, container, false);
}
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(String data);
}
}
20161114
Fragment mCallback Button Click
//MainActivity
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity implements BlankFragment.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BlankFragment blankFragment = new BlankFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.activity_main, blankFragment);
transaction.commit();
}
@Override
public void onClick() {
Log.v("LOG:", "from BlankFragment");
}
}
//BlankFragment
package com.example.kunihiro.myapplication;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment extends Fragment {
OnClickListener mCallback;
//Activityにイベントを通知する役割を持つ
public interface OnClickListener {
void onClick();
}
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_blank, container, false);
Button button=(Button)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCallback.onClick();
}
});
return view;
}
public void onAttach(Context context) {
super.onAttach(context);
try {
mCallback = (OnClickListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().toString() + "must implement OnArticleSelectedListener.");
}
}
}
//activity_main
<?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:id="@+id/activity_main"
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.kunihiro.myapplication.MainActivity">
</RelativeLayout>
//fragment_blank
<FrameLayout 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"
tools:context="com.example.kunihiro.myapplication.BlankFragment">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button" />
</FrameLayout>
20161113
Activity側から渡された値をFlagmentで取得する。
//MainActivity
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Fragment側に渡す変数を用意します。
Bundle args = new Bundle();
args.putString("KEY", "Data from MainActivity");
// FragmentTransactionを生成。
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// BlankFragmentを生成。
BlankFragment blankfragment = new BlankFragment();
//fragmetnに渡す変数をセット
blankfragment.setArguments(args);
// FragmentTransactionに、BlankFragmentをセット
transaction.add(R.id.activity_main, blankfragment);
// FragmentTransactionをコミット
transaction.commit();
}
}
//BlankFragment
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class BlankFragment extends Fragment{
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_blank, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Activity側から渡された値を取得する。
String string = getArguments().getString("KEY");
TextView textView=(TextView)view.findViewById(R.id.textView);
textView.setText(string);
}
}
//activity_main
<?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:id="@+id/activity_main"
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.kunihiro.myapplication.MainActivity">
</RelativeLayout>
//fragment_blank
<FrameLayout 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"
tools:context="com.example.kunihiro.myapplication.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
</FrameLayout>
20161112
activity_mainにコードからFragmentを追加
//MainActivity
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// コードからFragmentを追加
// Fragmentを作成します
BlankFragment blankFragment = new BlankFragment();
// Fragmentの追加や削除といった変更を行う際は、Transactionを利用します
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// 新しく追加を行うのでaddを使用します
// 他にも、メソッドにはreplace removeがあります
// メソッドの1つ目の引数は対象のViewGroupのID、2つ目の引数は追加するfragment
transaction.add(R.id.activity_main, blankFragment);
// 最後にcommitを使用することで変更を反映します
transaction.commit();
}
}
//BlankFragment
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
private TextView mTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
// 先ほどのレイアウトをここでViewとして作成します
View view=inflater.inflate(R.layout.fragment_main, container, false);
return view;
}
// Viewが生成し終わった時に呼ばれるメソッド
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// TextViewをひも付けます
mTextView = (TextView) view.findViewById(R.id.textView);
// Buttonのクリックした時の処理を書きます
view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTextView.setText("Fragment");
}
});
}
}
//activity_main
<?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:id="@+id/activity_main"
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.kunihiro.myapplication.MainActivity">
</RelativeLayout>
//fragment_blank
<LinearLayout 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"
tools:context="com.example.kunihiro.myapplication.MainFragment"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
/>
</LinearLayout>
20161112
activity_mainにfragmentをいれる
//MainActivity
package com.example.kunihiro.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
//BlankFragment
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class BlankFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//第1引数:レイアウトXMLファイルのリソースID、
//第2引数:
//第3引数:trueにするかfalseにするかで戻り値となるルートビュー(View)が変わる。
// trueの場合には、第2引数で渡したViewGroup、falseの場合には第1引数で渡したリソースIDがルートビューになる
View view = inflater.inflate(R.layout.fragment_blank, container, false);
TextView text = (TextView)view.findViewById(R.id.text_view);
text.setText("これはBlankFragmentだよ。");
return view;
}
}
//activity_main
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kunihiro.myapplication.MainActivity">
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.example.kunihiro.myapplication.BlankFragment" />
</RelativeLayout>
//fragment_blank
<?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:orientation="vertical"
android:gravity="center"
android:background="#eeeeee"
>
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textSize="30sp" />
</LinearLayout>
20161110
NavigationDrawer->Flagment1(ListView)に値をセットし->Flagment2(TextView)で値を受け取る。
//MainActivity
if (id == R.id.nav_camera) {
BlankFragment1 blankFragment1=new BlankFragment1();
FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.content_main,blankFragment1,blankFragment1.getTag()).commit();
//BlankFlagment1
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class BlankFragment1 extends Fragment {
public BlankFragment1() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_blank_fragment1, container, false);
String[] menuItems={"a","b","c","d","e"};
ListView listView=(ListView)view.findViewById(R.id.listView);
ArrayAdapter<String> listViewAdapter=new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
menuItems);
listView.setAdapter(listViewAdapter);listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 0){
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
BlankFragment2 blankFragment2=new BlankFragment2();
Bundle bundle = new Bundle();
bundle.putString("KEY","Oh my god" );
blankFragment2.setArguments(bundle);
ft.replace(R.id.content_main, blankFragment2).commit();
}else if(position == 1){
Toast.makeText(getActivity(),"You clicked the second item",Toast.LENGTH_LONG).show();
}
}
});
return view;
}
}
//fragment_blank_fragment1.xml
<FrameLayout 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"
tools:context="com.example.kunihiro.myapplication.BlankFragment1">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"/>
</FrameLayout>
//BlankFlagment2
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class BlankFragment2 extends Fragment {
public BlankFragment2() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
String string = getArguments().getString("KEY");
TextView textView=(TextView) view.findViewById(R.id.textView);
textView.setText(string);
return view;
}
}
//fragment_blank_flagment2.xml
<FrameLayout 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"
tools:context="com.example.kunihiro.myapplication.BlankFragment2">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
</FrameLayout>
20161109
FlagmentにSurfaceViewを設置する。
SeekBarをスライドさせると文字の大きさが変化する。しかし、SeekBarをスライドさせるとコピペでは動作がおかしくなる。よって、BlankFragment.javaだけを以下に示した。
package com.example.kunihiro.myapplication;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import static com.example.kunihiro.myapplication.BlankFragment.seekprg1;
public class BlankFragment extends Fragment {
public static int seekprg1=50;
private SurfaceView surfaceView;
private MainSurfaceView mainSurfaceView;
public BlankFragment() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_blank, container, false);
SeekBar seekbar=(SeekBar) view.findViewById(R.id.seekBar);
seekprg1=seekbar.getProgress();
seekbar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
seekprg1=seekBar.getProgress();
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {}
}
);
surfaceView = (SurfaceView)view.findViewById(R.id.surfaceView);
mainSurfaceView = new MainSurfaceView(this, surfaceView);
return view;
}
}
class MainSurfaceView implements SurfaceHolder.Callback, Runnable {
private SurfaceHolder holder;
private static final String TAG = "MainSurfaceView";
private Thread thread;
public MainSurfaceView(BlankFragment context, SurfaceView sv) {
holder = sv.getHolder();
holder.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
thread = new Thread(this);
thread.start();
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
Canvas canvas = arg0.lockCanvas();
canvas.drawColor(Color.BLACK);
arg0.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
thread=null;
}
@Override
public void run() {
while (thread != null) {
doDraw(holder);
}
}
private void doDraw(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas();
if (canvas != null) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(3);
canvas.drawColor(Color.LTGRAY);
int width = canvas.getWidth();
int height = canvas.getHeight();
int a = width / 2;//原点のx座標
int b = height / 2;//原点のx座標
int xb = a - (width / 2) / 10;//x座標の描画限界
canvas.drawLine(a, 0, a, height, paint);//x軸
canvas.drawLine(0, b, width, b, paint);//y軸
paint.setTextSize(seekprg1);
canvas.drawText("sring",300,300,paint);
}
holder.unlockCanvasAndPost(canvas);
}}
20161108
FlagmentにSurfaceViewを設置する。
SeekBarをスライドさせると文字の大きさが変化する。
どこがいけなかったのかやっと正常に動作した。
//MainActivity.java
NavigationDrawerで
if (id == R.id.nav_camera) {
BlankFragment blankFragment=new BlankFragment();
FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.content_main,blankFragment,blankFragment.getTag()).commit();
} else if (id == R.id.nav_gallery) {
をpaste
//BlankFragment.java
package com.example.kunihiro.myapplication;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import static com.example.kunihiro.myapplication.BlankFragment.seekprg1;
public class BlankFragment extends Fragment {
public static int seekprg1=50;
private SurfaceView surfaceView;
private MainSurfaceView mainSurfaceView;
public BlankFragment() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_blank, container, false);
SeekBar seekbar=(SeekBar) view.findViewById(R.id.seekBar);
seekprg1=seekbar.getProgress();
seekbar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
seekprg1=seekBar.getProgress();
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {}
}
);
surfaceView = (SurfaceView)view.findViewById(R.id.surfaceView);
mainSurfaceView = new MainSurfaceView(this, surfaceView);
return view;
}
}
class MainSurfaceView implements SurfaceHolder.Callback, Runnable {
private SurfaceHolder holder;
private Thread thread;
public MainSurfaceView(BlankFragment context, SurfaceView sv) {
holder = sv.getHolder();
holder.addCallback(this);
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
thread = new Thread(this);
thread.start();
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
Canvas canvas = arg0.lockCanvas();
canvas.drawColor(Color.BLACK);
arg0.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
thread=null;
}
public void run() {
while (thread != null) {
doDraw(holder);
}
}
private void doDraw(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas();
if (canvas != null) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(seekprg1);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(5);
canvas.drawText("f(x)" , 500, 500, paint);
holder.unlockCanvasAndPost(canvas);
}}}
//flagment_blank.xml
<LinearLayout 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"
tools:context="com.example.kunihiro.myapplication.BlankFragment"
android:orientation="vertical"
android:weightSum="1"
android:id="@+id/layoutForFragment">
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
android:max="300"
android:progress="100"
android:layout_weight="0.05" />
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/surfaceView"
android:layout_weight="0.95" />
</LinearLayout>
20161106
FlagmentにViewを入れる。
fragment_blank.xmlをLinearLayoutにしてこのレイアウト内にさらにLinearLayoutを入れ子構造にしてその中にViewを入れる。この方法でうまくいった。
20161106
Flagmentに描画
//fragment_blank.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"
tools:context="com.example.kunihiro.myapplication.BlankFragment"
android:id="@+id/relativelayout"
android:weightSum="1"
>
</RelativeLayout>
//BlankFragment.java
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
public class BlankFragment extends Fragment {
public BlankFragment() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_blank, container, false);
RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.relativelayout);
relativeLayout.addView(new Rectangle(getActivity()));
return view;
}
}
//Rectangle.java
package com.example.kunihiro.myapplication;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
class Rectangle extends View {
Paint paint = new Paint();
public Rectangle(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.GREEN);
Rect rect = new Rect(20, 56, 200, 112);
canvas.drawRect(rect, paint );
}
}
20161105
activity_main.xmlに<fragment />を2個入れてFragmentを表示する
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kunihiro.myapplication.MainActivity"
android:orientation="vertical">
<fragment
android:layout_width="match_parent"
android:layout_height="245dp"
android:name="com.example.kunihiro.myapplication.BlankFragment1"
android:id="@+id/blankfragment1"
/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.kunihiro.myapplication.BlankFragment2"
android:id="@+id/blankfragment2"
/>
</LinearLayout>
//fragment_blank_fragment1.xml
<LinearLayout 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"
tools:context="com.example.kunihiro.myapplication.BlankFragment1"
android:id="@+id/blankfragment1">
<CalendarView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/calendarView3"
android:layout_weight="1" />
</LinearLayout>
//fragment_blank_fragment2.xml
<LinearLayout 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"
tools:context="com.example.kunihiro.myapplication.BlankFragment2"
android:id="@+id/blankfragment2">
<TimePicker
android:layout_width="wrap_content"
android:layout_height="485dp"
android:id="@+id/timePicker" />
</LinearLayout>
//BlanhFragment1.java
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment1 extends Fragment {
public BlankFragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank_fragment1, container, false);
}
}
//BlanhFragment1.java
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment2 extends Fragment {
public BlankFragment2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
}
}
20161105
activity_main.xmlに<fragment />を入れてFragmentを表示する
//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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kunihiro.myapplication.MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.kunihiro.myapplication.BlankFragment"
android:id="@+id/fragment"
/>
</RelativeLayout>
//fragment_blank.xml
<FrameLayout 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"
tools:context="com.example.kunihiro.myapplication.BlankFragment">
<CalendarView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/calendarView" />
</FrameLayout>
//BlanhFragment.java
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment extends Fragment {
public BlankFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
}
//MainActivity.java
package com.example.kunihiro.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
20161104
FragmentにSurfaceView
package com.example.kunihiro.myapplication;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment extends Fragment {
public BlankFragment() {
}
public static BlankFragment getInstance() {
BlankFragment fragment = new BlankFragment();
fragment.setRetainInstance(true);
return fragment;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return new MySurfaceView(getActivity());
}
class MySurfaceView extends SurfaceView {
private SurfaceHolder surfaceHolder;
public MySurfaceView(Context context) {
super(context);
init();
}
public MySurfaceView(Context context,
AttributeSet attrs) {
super(context, attrs);
init();
}
public MySurfaceView(Context context,
AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
surfaceHolder = getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas(null);
drawSomething(canvas);
holder.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
}
protected void drawSomething(Canvas canvas) {
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setTextSize(100);
canvas.drawText("test", 200, 200, paint);
}
}
}
20161102
NavigationDrawerからBlankFragment1(Listview)に遷移、BlankFragment1からStringをBlankFragment2に渡す、BlankDragment1からBlankFrag2に画面遷移する。
①MainActivityは
if (id == R.id.nav_camera) {
BlankFragment1 blankFragment1=new BlankFragment1();
FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.content_main,blankFragment1,blankFragment1.getTag()).commit();
②BlankFragment1は
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment1 extends Fragment {
public BlankFragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_blank_fragment1, container, false);
String[] menuItems={"a","b","c","d","e"};
ListView listView=(ListView)view.findViewById(R.id.listView);
ArrayAdapter<String> listViewAdapter=new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
menuItems);
listView.setAdapter(listViewAdapter);listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 0){
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
BlankFragment2 blankFragment2=new BlankFragment2();
Bundle bundle = new Bundle();
bundle.putString("KEY","Oh my god" );
blankFragment2.setArguments(bundle);
ft.replace(R.id.content_main, blankFragment2).commit();
}else if(position == 1){
Toast.makeText(getActivity(),"You clicked the second item",Toast.LENGTH_LONG).show();
}
}
});
return view;
}
}
③fragment_blank_flagment1.xmlは
<FrameLayout 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"
tools:context="com.example.kunihiro.myapplication.BlankFragment1">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"/>
</FrameLayout>
④BlankFragment2は
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment2 extends Fragment {
public BlankFragment2() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String string = getArguments().getString("KEY");
Log.i("TAG", "Fron BlankFragment1->" + string);
return inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
}
}
20161102
NavigationDrawerからBlankFragment1(Listview)に遷移、BlankDragment1からBlankFrag2に画面遷移する。
①MainActivityは
if (id == R.id.nav_camera) {
BlankFragment1 blankFragment1=new BlankFragment1();
FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.content_main,blankFragment1,blankFragment1.getTag()).commit();
②BlankFragment1は
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment1 extends Fragment {
public BlankFragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_blank_fragment1, container, false);
String[] menuItems={"a","b","c","d","e"};
ListView listView=(ListView)view.findViewById(R.id.listView);
ArrayAdapter<String> listViewAdapter=new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
menuItems);
listView.setAdapter(listViewAdapter);listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 0){
Toast.makeText(getActivity(),"You clicked the first item",Toast.LENGTH_LONG).show();
BlankFragment2 blankFragment2=new BlankFragment2();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_main, blankFragment2).commit();
}else if(position == 1){
Toast.makeText(getActivity(),"You clicked the second item",Toast.LENGTH_LONG).show();
}
}
});
return view;
}
}
③fragment_blank_flagment1.xmlは
<FrameLayout 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"
tools:context="com.example.kunihiro.myapplication.BlankFragment1">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"/>
</FrameLayout>
④BlankFragment2にはカレンダーとか時計とか入れる。
20161029
EditText1 に yama 、EditText2 に kawa を入力すると、correct と Toastに表示
①if (id == R.id.nav_camera) {
BlankFragment blankFragment=new BlankFragment();
FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.content_main,blankFragment,blankFragment.getTag()).commit();
を追加して
②BlankFragmentをNewする
fragment_blank.xmlにCalendarViewなどを追加する
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
EditText editText1,editText2;
Button button;
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_blank, container, false);
editText1=(EditText)view.findViewById(R.id.editText);
editText2=(EditText)view.findViewById(R.id.editText2);
button=(Button)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(editText1.getText().toString().equals("yama")&&editText2.getText().toString().equals("kawa")) {
Toast.makeText(BlankFragment.this.getActivity(), "Correct", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(BlankFragment.this.getActivity(), "Incorrect", Toast.LENGTH_LONG).show();
}
}
});
return view;
}
}
package com.example.kunihiro.myapplication;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@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, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@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);
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);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
BlankFragment blankFragment=new BlankFragment();
FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.content_main,blankFragment,blankFragment.getTag()).commit();
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
<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"
tools:context="com.example.kunihiro.myapplication.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_alignTop="@+id/editText2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="117dp"
android:layout_marginEnd="117dp"
android:layout_marginTop="19dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/editText"
android:layout_marginLeft="54dp"
android:layout_marginStart="54dp"
android:layout_marginTop="21dp"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:layout_alignRight="@+id/button"
android:layout_alignEnd="@+id/button"
android:layout_marginRight="13dp"
android:layout_marginEnd="13dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CalendarView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:id="@+id/calendarView" />
</RelativeLayout>