Created
November 21, 2022 23:58
-
-
Save NizarETH/a63599d4fc4fb0511a6ce2124bf0a984 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
======================================================== | |
layout activity | |
======================================================== | |
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout 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:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<ScrollView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="5dp" | |
android:orientation="vertical"> | |
<EditText | |
android:id="@+id/name" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="5dp" | |
android:hint="Nom" | |
/> | |
<EditText | |
android:id="@+id/surname" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="5dp" | |
android:hint="Prénom" | |
/> | |
<EditText | |
android:id="@+id/age" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="5dp" | |
android:hint="Age" | |
/> | |
<CheckBox | |
android:id="@+id/man" | |
android:text="Homme" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
<CheckBox | |
android:id="@+id/woman" | |
android:text="Femme" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
<Button | |
android:id="@+id/save" | |
android:layout_width="wrap_content" | |
android:layout_height="match_parent" | |
android:layout_gravity="center" | |
android:text="Enregistrer"/> | |
</LinearLayout> | |
</ScrollView> | |
<FrameLayout | |
android:id="@+id/frame" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
</RelativeLayout> | |
======================================================== | |
layout item | |
======================================================== | |
<?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="60dp"> | |
<TextView | |
android:id="@+id/row_item" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_margin="4dp" | |
android:gravity="center_vertical" | |
android:textSize="16sp" | |
android:textColor="@color/black" | |
android:text="value"/> | |
</LinearLayout> | |
======================================================== | |
layout list_data | |
======================================================== | |
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@color/white"> | |
<ListView | |
android:id="@+id/list_view" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
</LinearLayout> | |
======================================================== | |
ListFragment | |
======================================================== | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ListView; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
import androidx.fragment.app.Fragment; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ListFragment extends Fragment { | |
private View v; | |
@Nullable | |
@Override | |
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
v = inflater.inflate(R.layout.list_data, container, false); | |
ListView listView = v.findViewById(R.id.list_view); | |
List<String> data = new ArrayList<>(); | |
data.add("AAAAA"); | |
data.add("BBBBB"); | |
data.add("CCCCC"); | |
data.add("DDDDD"); | |
data.add("EEEEE"); | |
listView.setAdapter(new MyAdapter(data, getActivity())); | |
return v; | |
} | |
} | |
======================================================== | |
MyAdapter | |
======================================================== | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.util.List; | |
public class MyAdapter extends BaseAdapter { | |
private List<String> data; | |
private Context context; | |
public MyAdapter(List<String> data, Context context) { | |
this.data = data; | |
this.context = context; | |
} | |
@Override | |
public int getCount() { | |
return data.size(); | |
} | |
@Override | |
public Object getItem(int i) { | |
return null; | |
} | |
@Override | |
public long getItemId(int i) { | |
return 0; | |
} | |
@Override | |
public View getView(int i, View view, ViewGroup viewGroup) { | |
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
view = inflater.inflate(R.layout.item, null); | |
TextView textView = view.findViewById(R.id.row_item); | |
view.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Toast.makeText(context,"Checked value : "+data.get(i).toString(), Toast.LENGTH_SHORT ).show(); | |
} | |
}); | |
textView.setText(data.get(i).toString()); | |
return view; | |
} | |
} | |
======================================================== | |
MainActivity | |
======================================================== | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
getSupportFragmentManager().beginTransaction().replace( R.id.frame, new ListFragment()).commit(); | |
} | |
}); | |
} | |
} | |
======================================================== | |
Android Test | |
======================================================== | |
import static androidx.test.espresso.Espresso.onData; | |
import static androidx.test.espresso.Espresso.onView; | |
import static androidx.test.espresso.action.ViewActions.click; | |
import static androidx.test.espresso.action.ViewActions.scrollTo; | |
import static androidx.test.espresso.action.ViewActions.typeText; | |
import static androidx.test.espresso.assertion.ViewAssertions.matches; | |
import static androidx.test.espresso.matcher.ViewMatchers.isChecked; | |
import static androidx.test.espresso.matcher.ViewMatchers.isNotChecked; | |
import static androidx.test.espresso.matcher.ViewMatchers.withId; | |
import android.content.Context; | |
import android.os.SystemClock; | |
import androidx.test.ext.junit.rules.ActivityScenarioRule; | |
import androidx.test.platform.app.InstrumentationRegistry; | |
import androidx.test.ext.junit.runners.AndroidJUnit4; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import static org.hamcrest.Matchers.anything; | |
import static org.junit.Assert.*; | |
/** | |
* Instrumented test, which will execute on an Android device. | |
* | |
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | |
*/ | |
@RunWith(AndroidJUnit4.class) | |
public class ExampleInstrumentedTest { | |
public @Rule | |
ActivityScenarioRule<MainActivity> asr = new ActivityScenarioRule<>(MainActivity.class); | |
@Test | |
public void useAppContext() { | |
// Context of the app under test. | |
onView(withId(R.id.name)).perform(typeText("my name")); | |
onView(withId(R.id.surname)).perform(typeText("my last name")); | |
onView(withId(R.id.age)).perform(typeText("20")); | |
onView(withId(R.id.man)).check(matches(isNotChecked())).perform(scrollTo(), click()); | |
SystemClock.sleep(1500); | |
onView(withId(R.id.save)).perform(click()); | |
onData(anything()).atPosition(2).perform(click()); | |
SystemClock.sleep(1500); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment