Android: Keep Only One Cell Highlighted - TableLayout - android

I have been trying to create an application based of the application in the Google Play Store called Timetable. I have been struggling with an issue the past couple of days that seems simple. Basically, I created a grid with TableLayout, and I want a cell to highlight when touched, and then switch activities when touched again. However, I cannot figure out at all how to make the previously highlighted cell stop being highlighted when I select another cell. I'm trying to accomplish on a fragment inside of a navigation drawer activity.
This is how Timetable does it. When a cell is clicked, it is highlighted. When another cell is touched, the previous cell is no longer highlighted. When a highlighted cell is tapped, or a cell that is in the same row as a highlighted cell is tapped, the activity switches.
This is my work. As you can see, two cells are highlighted at once and I don't want that.
I've tried changing my cells from imageviews, to imagebuttons and regular buttons, but I got the same result. I implemented a selector .xml file and the cell would either stay highlighted as I was touching it, or it would do what is shown in the screenshot of my work. It would be amazing if someone could help me with this. Do I need to consider using a GridLayout?
Here is fragment_week.xml (shortened to save space)
<?xml version="1.0" encoding="utf-8"?>
<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=".WeekFragment">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:stretchColumns="*">
<TableRow
android:id="#+id/titleRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:orientation="horizontal"
android:padding="2dp">
<TextView
android:layout_width="10dp"
android:text="" />
<TextView
android:layout_width="20dp"
android:layout_gravity="center"
android:gravity="center"
android:text="Sun"
android:textAppearance="#android:style/TextAppearance.Material.Subhead"
android:textSize="15sp"
tools:targetApi="lollipop" />
<TextView
android:layout_width="20dp"
android:layout_gravity="center"
android:gravity="center"
android:text=" Mon"
android:textAppearance="#android:style/TextAppearance.Material.Subhead"
android:textSize="15sp"
tools:targetApi="lollipop" />
<TextView
android:layout_width="20dp"
android:layout_gravity="center"
android:gravity="center"
android:text=" Tue"
android:textAppearance="#android:style/TextAppearance.Material.Subhead"
android:textSize="15sp"
tools:targetApi="lollipop" />
<TextView
android:layout_width="20dp"
android:layout_gravity="center"
android:gravity="center"
android:text=" Wed"
android:textAppearance="#android:style/TextAppearance.Material.Subhead"
android:textSize="15sp"
tools:targetApi="lollipop" />
<TextView
android:layout_width="20dp"
android:layout_gravity="center"
android:gravity="center"
android:text=" Thu"
android:textAppearance="#android:style/TextAppearance.Material.Subhead"
android:textSize="15sp"
tools:targetApi="lollipop" />
<TextView
android:layout_width="20dp"
android:layout_gravity="center"
android:gravity="center"
android:text=" Fri"
android:textAppearance="#android:style/TextAppearance.Material.Subhead"
android:textSize="15sp"
tools:targetApi="lollipop" />
<TextView
android:layout_width="20dp"
android:layout_gravity="center"
android:gravity="center"
android:text=" Sat"
android:textAppearance="#android:style/TextAppearance.Material.Subhead"
android:textSize="15sp"
tools:targetApi="lollipop" />
</TableRow>
</TableLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:scrollbars="none">
<TableLayout
android:id="#+id/timeTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:stretchColumns="1,2,3,4,5,6,7">
<TableRow
android:id="#+id/sevenAM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/cell_shape">
<TextView
android:layout_width="45dp"
android:layout_height="match_parent"
android:background="#drawable/cell_shape"
android:gravity="end"
android:paddingRight="8dp"
android:paddingTop="2dp"
android:text="7:00\nAM"
android:textAppearance="#android:style/TextAppearance.Material.Subhead"
android:textSize="11sp"
tools:targetApi="lollipop" />
<ImageView
android:layout_height="45dp"
android:layout_width="1dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:layout_width="1dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:layout_width="1dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:layout_width="1dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:layout_width="1dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:layout_width="1dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:layout_width="1dp"
android:background="#drawable/cell_shape" />
</TableRow>
<TableRow
android:id="#+id/eightAM"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/cell_shape">
<TextView
android:layout_width="45dp"
android:layout_height="45dp"
android:background="#drawable/cell_shape"
android:gravity="end"
android:paddingRight="8dp"
android:paddingTop="2dp"
android:text="8:00\nAM"
android:textAlignment="textEnd"
android:textAppearance="#android:style/TextAppearance.Material.Subhead"
android:textSize="11sp"
tools:targetApi="lollipop" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
</TableRow>
<TableRow
android:id="#+id/nineAM"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/cell_shape">
<TextView
android:layout_width="45dp"
android:layout_height="45dp"
android:background="#drawable/cell_shape"
android:gravity="end"
android:paddingRight="8dp"
android:paddingTop="2dp"
android:text="9:00\nAM"
android:textAlignment="textEnd"
android:textAppearance="#android:style/TextAppearance.Material.Subhead"
android:textSize="11sp"
tools:targetApi="lollipop" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
</TableRow>
<TableRow
android:id="#+id/tenAM"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/cell_shape">
<TextView
android:layout_width="45dp"
android:layout_height="45dp"
android:background="#drawable/cell_shape"
android:gravity="end"
android:paddingRight="8dp"
android:paddingTop="2dp"
android:text="10:00\nAM"
android:textAlignment="textEnd"
android:textAppearance="#android:style/TextAppearance.Material.Subhead"
android:textSize="11sp"
tools:targetApi="lollipop" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
</TableRow>
<TableRow
android:id="#+id/elevenAM"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/cell_shape">
<TextView
android:layout_width="45dp"
android:layout_height="45dp"
android:background="#drawable/cell_shape"
android:gravity="end"
android:paddingRight="8dp"
android:paddingTop="2dp"
android:text="11:00\nAM"
android:textAlignment="textEnd"
android:textAppearance="#android:style/TextAppearance.Material.Subhead"
android:textSize="11sp"
tools:targetApi="lollipop" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
</TableRow>
<TableRow
android:id="#+id/twelvePM"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/cell_shape">
<TextView
android:layout_width="45dp"
android:layout_height="45dp"
android:background="#drawable/cell_shape"
android:gravity="end"
android:paddingRight="8dp"
android:paddingTop="2dp"
android:text="12:00\nPM"
android:textAlignment="textEnd"
android:textAppearance="#android:style/TextAppearance.Material.Subhead"
android:textSize="11sp"
tools:targetApi="lollipop" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
<ImageView
android:layout_height="45dp"
android:background="#drawable/cell_shape" />
</TableRow>
</TableLayout>
</ScrollView>
</FrameLayout>
Here is cell_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke android:width="0.05dp" android:color="#19aaa9a9"/>
</shape>
Here is cell_hightlight.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle" >
<solid android:color="#color/colorAccent"/>
</shape>
</item>
<item android:drawable="#drawable/plus" />
</layer-list>
Here is WeekFragment.java
package com.dmelton.classScheduler;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;
public class WeekFragment extends Fragment {
public WeekFragment() {
// 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_week, container, false);
}
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TableLayout timeTable = getActivity().findViewById(R.id.timeTable);
int count = timeTable.getChildCount();
for (int i = 0; i < count; i++) {
View v = timeTable.getChildAt(i);
if (v instanceof TableRow) {
TableRow row = (TableRow) v;
int rowCount = row.getChildCount();
for (int r = 0; r < rowCount; r++) {
final View v2 = row.getChildAt(r);
if (v2 instanceof ImageView) {
final ImageView b = (ImageView) v2;
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
b.setBackground(getResources().getDrawable(R.drawable.cell_highlight));
}
});
}
}
}
}
}
}

I have updated your code, see below..
public class WeekFragment extends Fragment {
//Create a global View instance like below
View previousView;
public WeekFragment() {
// 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_week, container, false);
}
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TableLayout timeTable = getActivity().findViewById(R.id.timeTable);
int count = timeTable.getChildCount();
for (int i = 0; i < count; i++) {
View v = timeTable.getChildAt(i);
if (v instanceof TableRow) {
TableRow row = (TableRow) v;
int rowCount = row.getChildCount();
for (int r = 0; r < rowCount; r++) {
final View v2 = row.getChildAt(r);
if (v2 instanceof ImageView) {
final ImageView b = (ImageView) v2;
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
b.setBackground(getResources().getDrawable(R.drawable.cell_highlight));
//Check for the first time for null value
if(previousView!=null&&previousView.equals(b)){
/*You can make cell normal here by adding b.setBackgroundResource(R.drawable.normal);*/
switchActivities();
}
previousView=null;
//Assign new view
previousView=b;
}
});
}
}
}
}
}
}

Related

Why can't I access the layout of a class for testing

UPDATE: I've expanded the code, my app works as expected but still getting a NullPointerException when trying to test the activity_main.xml.
I'm trying to test the views in a layout for a calculator but I keep getting NullPointerException even after calling onCreate in the test file, I've also tried setting content view inside the test file directly to the activity_main but no luck.
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
Calculator calculator = new Calculator();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
void onCreateHelper() {
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
}
public void onNumberClick(View view) {
Button button = (Button) view;
String result = this.getButtonText(button);
this.setValueView(result);
int number = Integer.parseInt(result);
calculator.numberClick(number);
}
public void onOperatorClick(View view) {
Button button = (Button) view;
String value = this.getButtonText(button);
calculator.operatorClick(value);
String result = String.valueOf(calculator.getRunningTotal());
binding.editText.setText(result);
}
public void onClearClick (View view) {
Button button = (Button) view;
String value = this.getButtonText(button);
binding.editText.setText("");
calculator.clearClick();
}
String getButtonText(Button button) {
String result = button.getText().toString();
return result;
}
void setValueView(String value){
binding.editText.setText(binding.editText.getText() + value);
}
}
Test:
public class MainActivityTest<T extends MainActivity> {
private MainActivity mainActivity;
#Before
public void before() {
mainActivity = new MainActivity();
mainActivity.onCreateHelper();
}
#Test
public void OnNumberClickTest() throws Exception {
mainActivity.onNumberClick(mainActivity.binding.buttonSeven);
assertEquals(7, mainActivity.calculator.getRunningTotal());
}
#Test
public void OnOperatorClickTest() throws Exception {
mainActivity.onOperatorClick(mainActivity.binding.buttonAdd);
assertEquals('+', mainActivity.calculator.getPreviousOperator());
}
#Test
public void onClearClickTest() throws Exception {
mainActivity.onClearClick(mainActivity.binding.buttonClear);
assertEquals(0, mainActivity.calculator.getRunningTotal());
}
#Test
public void getButtonTextTest() throws Exception {
String buttonText = mainActivity.getButtonText(mainActivity.binding.buttonEight);
assertEquals(8, buttonText);
}
#Test
public void setValueViewTest() throws Exception {
mainActivity.setValueView("TEST");
assertEquals("TEST", mainActivity.binding.editText.getText());
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="99dp"
android:orientation="vertical" android:layout_weight="0.67">
<TextView
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:gravity="right"
android:paddingRight="10sp"
android:paddingTop="5sp"
android:textSize="15pt"
android:textStyle="bold" />
<TextView
android:id="#+id/txtInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:gravity="right"
android:paddingRight="10sp"
android:paddingTop="5sp"
android:textSize="15pt"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="75dp"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonClear"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="5"
android:textStyle="bold"
android:textSize="40sp"
android:text="Clear" />
<Button
android:id="#+id/buttonBack"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight=".51"
android:textStyle="bold"
android:textSize="40sp"
android:text="BS" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="75dp"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonSeven"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="7" />
<Button
android:id="#+id/buttonEight"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="8" />
<Button
android:id="#+id/buttonNine"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="9" />
<Button
android:id="#+id/buttonDivide"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1.15"
android:textStyle="bold"
android:textSize="40sp"
android:text="/" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="75dp"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonFour"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textSize="40sp"
android:textStyle="bold"
android:text="4" />
<Button
android:id="#+id/buttonFive"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="5" />
<Button
android:id="#+id/buttonSix"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="6" />
<Button
android:id="#+id/buttonMultiply"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight=".98"
android:textStyle="bold"
android:textSize="40sp"
android:text="x" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="75dp"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="1" />
<Button
android:id="#+id/buttonTwo"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="2" />
<Button
android:id="#+id/buttonThree"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="40sp"
android:text="3" />
<Button
android:id="#+id/buttonSubtract"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1.27"
android:textStyle="bold"
android:textSize="40sp"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="75dp"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonDot"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1.22"
android:textStyle="bold"
android:textSize="40sp"
android:text="." />
<Button
android:id="#+id/buttonZero"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight=".98"
android:textStyle="bold"
android:textSize="40sp"
android:text="0" />
<Button
android:id="#+id/buttonEqual"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight=".95"
android:textStyle="bold"
android:textSize="40sp"
android:text="=" />
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight=".93"
android:textStyle="bold"
android:textSize="40sp"
android:text="+" />
</LinearLayout>
</LinearLayout>

how to scroll table horizontally in android

I am trying to implement table layout in android.I have table Header and row designed in one xml file and am passing data using adapter.So What is happening is I got scrolling horizontally but with multiple table header repeating on each row.
What I want to achieve is table header should be seen only once and all the rows below it should scroll along with table header.
Here's my xml file
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView android:id="#+id/horizontalView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:scrollbars="horizontal|vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:stretchColumns="*"
android:divider="#color/black"
android:showDividers="middle"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Name"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Type"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Fare"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Tax"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Airline Pnr"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Ticekt Numner"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="18sp"
android:id="#+id/name"/>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/type"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/fare"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/tax"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/air_pnr"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="#+id/ticket"
android:textColor="#color/black"
android:textSize="18sp" />
<CheckBox
android:id="#+id/check_bx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CANCEL" />
</TableRow>
</TableLayout>
</HorizontalScrollView>
And here's the image what it looks likescrolling table
UPDATE (2) HERE's Adapter Code
public class Flight_cancel_Adapter extends BaseAdapter {
Context context;
public static ArrayList<Flight_cancel_Details> rowItems;
public static int available_seats;
public static PolicyAdapter adapter;
int TOTAL;
public static ArrayList<String> arraySeat=new ArrayList<String>();
Flight_cancel_Adapter(Context context, ArrayList<Flight_cancel_Details> rowItems) {
this.context = context;
this.rowItems = rowItems;
}
#Override
public int getCount() {
return rowItems.size();
}
#Override
public Object getItem(int position) {
return rowItems.get(position);
}
#Override
public long getItemId(int position)
{
return rowItems.indexOf(getItem(position));
}
/* private view holder class */
private class ViewHolder {
TextView type;
TextView fare;
TextView tax;
TextView air_pnr;
TextView ticket;
TextView namee;
CheckBox box;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.test, null);
holder = new ViewHolder();
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.namee = (TextView) convertView.findViewById(R.id.name);
holder.type = (TextView) convertView.findViewById(R.id.type);
holder.fare = (TextView) convertView.findViewById(R.id.fare);
holder.tax = (TextView) convertView.findViewById(R.id.tax);
holder.air_pnr = (TextView) convertView.findViewById(R.id.air_pnr);
holder.ticket = (TextView) convertView.findViewById(R.id.ticket);
holder.box = (CheckBox) convertView.findViewById(R.id.check_bx);
holder.box.setTag(position);
try
{
final Flight_cancel_Details row_pos = rowItems.get(position);
String name=String.valueOf(row_pos.getName());
String typ=String.valueOf(row_pos.gettypes());
String far=String.valueOf(row_pos.getfares());
String tax=String.valueOf(row_pos.gettax());
String pnr=String.valueOf(row_pos.getair_pnr());
String ticket=String.valueOf(row_pos.getticket());
System.out.print("ROW POS-"+row_pos.getStatus());
if(row_pos.getStatus().equals("CANCELLED"))
{
holder.namee.setText(name);
holder.type.setText(typ);
holder.fare.setText(far);
holder.tax.setText(tax);
holder.air_pnr.setText(pnr);
holder.ticket.setText(ticket);
holder.box.setEnabled(false);
holder.box.setText("CANCELLED");
}else {
holder.namee.setText(name);
holder.type.setText(typ);
holder.fare.setText(far);
holder.tax.setText(tax);
holder.air_pnr.setText(pnr);
holder.ticket.setText(ticket);
holder.box.setEnabled(true);
holder.box.setText("CANCEL");
}
if(row_pos.getTarvel_Status().equals("TRAVELLED"))
{
holder.namee.setText(name);
holder.type.setText(typ);
holder.fare.setText(far);
holder.tax.setText(tax);
holder.air_pnr.setText(pnr);
holder.ticket.setText(ticket);
}
}
catch (Exception e)
{
Log.e("BUS_CANCEL_ADAP ERROR:", e.getMessage());
}
convertView.setTag(holder);
holder.box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
int position = (int) buttonView.getTag();
System.out.println("--CLICKED--" + position);
String contactId = (holder.air_pnr.getText().toString());
System.out.println("--VALUE--" + contactId);
arraySeat.add(contactId);
}
else
{
int position = (int) buttonView.getTag();
System.out.println("--UNCHEKED--" + position);
String contactId = (holder.air_pnr.getText().toString());
System.out.println("--UNCHEKED VALUE--" + contactId);
arraySeat.remove(contactId);
}
}
});
return convertView;
}
}
Use Recyclerview for horizontal scrollview for a better performance
test.xml
<?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">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Name"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="name"
android:textColor="#color/black"
android:textSize="18sp"
android:id="#+id/name"/>
</LinearLayout>
recyclerview.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView android:id="#+id/horizontalView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:scrollbars="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:listitem="#layout/layout">
</android.support.v7.widget.RecyclerView>
Here my XML file, you should put title for all row TableView (TextView) on LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView android:id="#+id/horizontalView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:scrollbars="horizontal|vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--Title for all row, once for all row -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Name"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Type"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Fare"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Tax"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Airline Pnr"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Ticekt Numner"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
</LinearLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:stretchColumns="*"
android:divider="#color/black"
android:showDividers="middle"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="My Name"
android:textColor="#color/black"
android:textSize="18sp"
android:id="#+id/name"/>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/type"
android:text="My Type"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/fare"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/tax"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/air_pnr"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="#+id/ticket"
android:textColor="#color/black"
android:textSize="18sp" />
<CheckBox
android:id="#+id/check_bx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CANCEL" />
</TableRow>
</TableLayout>
</LinearLayout>
</HorizontalScrollView>

How to give priority to layout which should be at top?

In the building, tic-tac-toe grid layout occupies the upper layer and Linearlayout occupies the lower layer just below the grid layout. where the Linear layout should appear if any player wins the match. In linear layout the player who wins and a button to set the game to the starting position. So initially I set it to invisible when the game ends linear layout comes back side of grid layout. How can I make the Linear Layout on top of grid layout?
MainActivity.java
package com.example.achyu.tictactao;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//0=yellow 1=red
int currentPlayer=0;
int[] isItDone={2, 2, 2, 2, 2, 2, 2, 2, 2};
//2=yellow 1=red
int[] whoWon=new int[9];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void currentCounter(View view){
ImageView counter= (ImageView) view;
int tap=Integer.parseInt(counter.getTag().toString());
if(isItDone[tap]==2){
counter.setTranslationY(-1000f);
if(currentPlayer==0){
counter.setImageResource(R.drawable.yellow);
currentPlayer=1;
whoWon[tap]=2;
if((whoWon[0]==2&&whoWon[1]==2&&whoWon[2]==2)||(whoWon[3]==2&&whoWon[4]==2&&whoWon[5]==2)||
(whoWon[6]==2&&whoWon[7]==2&&whoWon[8]==2)||(whoWon[0]==2&&whoWon[3]==2&&whoWon[6]==2)||
(whoWon[1]==2&&whoWon[4]==2&&whoWon[7]==2)||(whoWon[2]==2&&whoWon[5]==2&&whoWon[8]==2)||
(whoWon[0]==2&&whoWon[4]==2&&whoWon[8]==2)||(whoWon[2]==2&&whoWon[4]==2&&whoWon[6]==2))
{
Toast.makeText(this,"Yellow Has Won",Toast.LENGTH_LONG).show();
LinearLayout layout = (LinearLayout)findViewById(R.id.linearlayout);
layout.setVisibility(View.VISIBLE);
TextView winnerText= (TextView) findViewById(R.id.winner);
winnerText.setText("Yello Won");
winnerText.setTextColor(Color.WHITE);
}
}
else
{
counter.setImageResource(R.drawable.red);
currentPlayer=0;
whoWon[tap]=1;
if((whoWon[0]==1&&whoWon[1]==1&&whoWon[2]==1)||(whoWon[3]==1&&whoWon[4]==1&&whoWon[5]==1)||
(whoWon[6]==1&&whoWon[7]==1&&whoWon[8]==1)||(whoWon[0]==1&&whoWon[3]==1&&whoWon[6]==1)||
(whoWon[1]==1&&whoWon[4]==1&&whoWon[7]==1)||(whoWon[2]==1&&whoWon[5]==1&&whoWon[8]==1)||
(whoWon[0]==1&&whoWon[4]==1&&whoWon[8]==1)||(whoWon[2]==1&&whoWon[4]==1&&whoWon[6]==1))
{
Toast.makeText(this,"Red Has Won",Toast.LENGTH_LONG).show();
LinearLayout layout = (LinearLayout)findViewById(R.id.linearlayout);
layout.setVisibility(View.VISIBLE);
TextView winnerText= (TextView) findViewById(R.id.winner);
winnerText.setText("Red Won");
winnerText.setTextColor(Color.WHITE);
}
}
counter.animate().translationYBy(1000f).setDuration(300);
isItDone[tap]=3;
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="com.example.achyu.tictactao.MainActivity">
<RelativeLayout
android:layout_width="395dp"
android:layout_height="587dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/orange"
android:orientation="vertical"
android:padding="60dp"
android:visibility="invisible">
<TextView
android:id="#+id/winner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="TextView"
android:textSize="40sp"
tools:textColor="#ffffff" />
<Button
android:id="#+id/playagain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<GridLayout
android:layout_width="360dp"
android:layout_height="360dp"
android:background="#drawable/board"
android:columnCount="3"
android:rowCount="3"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<ImageView
android:id="#+id/imageView0"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:tag="0"
android:layout_row="0"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="1"
android:layout_marginLeft="20dp"
android:tag="1"
android:layout_row="0"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="2"
android:layout_marginLeft="25dp"
android:tag="2"
android:layout_row="0"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:tag="3"
android:layout_row="1"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="1"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:tag="4"
android:layout_row="1"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="2"
android:layout_marginLeft="25dp"
android:layout_marginTop="30dp"
android:tag="5"
android:layout_row="1"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:tag="6"
android:layout_row="2"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="1"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:tag="7"
android:layout_row="2"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="2"
android:layout_marginLeft="25dp"
android:layout_marginTop="30dp"
android:tag="8"
android:layout_row="2"
android:onClick="currentCounter" />
</GridLayout>
</RelativeLayout>
Just put your linear layout below your gridLayout in your relative layout

Weird behavior on item click

I'm building an app that sends USSD commands to check for balance, etc.
The apps acts weirdly when I try to launch activities from item click, here from image button: I have to touch two times to launch an activity. I don't know what is wrong.
Have a look at my code:
MainActivity.java:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
ImageView accountView;
ImageView activationsView;
ImageView servicesView;
ImageView othersView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openAccountView(View view){
accountView = (ImageView) findViewById(R.id.accountView);
accountView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, AccountActivity.class);
startActivity(i);
}
});
}
public void openActivationsView(View view){
activationsView = (ImageView) findViewById(R.id.activationsView);
activationsView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, ActivationsActivity.class);
startActivity(i);
}
});
}
public void openServicesView(View view){
servicesView = (ImageView) findViewById(R.id.servicesView);
servicesView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, ServicesActivity.class);
startActivity(i);
}
});
}
public void openOthersView(View view){
}
}
activity_main.xml:
<?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:id="#id/layout_dashboard_parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/c_dashboard_btn_quick_clean_color"
android:orientation="horizontal">
<Button
android:id="#+id/buttonCB"
style="#android:style/Widget.Material.Light.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="2"
android:background="#color/c_main_back_ground_color"
android:elevation="7dp"
android:text="#string/check_balance"
tools:ignore="ButtonStyle"
tools:targetApi="lollipop" />
<Button
android:id="#+id/buttonLB"
style="#android:style/Widget.Material.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_marginStart="2dip"
android:layout_weight="2"
android:background="#color/c_main_back_ground_color"
android:elevation="7dp"
android:text="#string/load_balance"
tools:ignore="ButtonStyle"
tools:targetApi="lollipop" />
</LinearLayout>
<ScrollView
android:id="#id/main_scroll_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/webview_ads"
tools:ignore="UnknownIdInLayout">
<LinearLayout
android:id="#id/layout_dashboard_scroll_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<FrameLayout
android:id="#id/dashboard_alert_section"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<include layout="#layout/dashboard_feature_tip_item" />
</FrameLayout>
<FrameLayout
android:id="#id/main_control_layout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="345">
<FrameLayout
android:id="#id/dashboard_progress_bar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="#dimen/dashboard_clean_progess_mar_top"
android:elevation="#dimen/dashboard_progressbar_elevation"
android:visibility="gone"
tools:ignore="UselessParent"
tools:targetApi="lollipop">
<ImageView
android:layout_width="#dimen/dashboard_progress_clean_all_image_dimension"
android:layout_height="#dimen/dashboard_progress_clean_all_image_dimension"
android:layout_gravity="center"
android:contentDescription=""
android:src="#drawable/btn_ram_checkbox"
tools:ignore="ContentDescription" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
</FrameLayout>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#id/layout_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#color/c_main_back_ground_color">
<RelativeLayout
android:id="#id/layout_account"
android:layout_width="#dimen/dashboard_feature_layout_width"
android:layout_height="#dimen/dashboard_feature_layout_height"
android:layout_marginEnd="#dimen/dashboard_feature_middle_margin"
android:layout_marginLeft="#dimen/dashboard_feature_leftmost_margin"
android:layout_marginRight="#dimen/dashboard_feature_middle_margin"
android:layout_marginStart="#dimen/dashboard_feature_leftmost_margin"
android:layout_marginTop="#dimen/dashboard_feature_top_margin"
android:background="#drawable/dashboard_bg_selector"
android:clickable="false"
android:elevation="6dp"
android:focusable="false"
tools:targetApi="lollipop">
<ImageView
android:id="#+id/accountView"
android:layout_width="130dip"
android:layout_height="130dip"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:adjustViewBounds="false"
android:background="#color/checkbox_button_color"
android:clickable="true"
android:contentDescription="#string/account"
android:cropToPadding="false"
android:onClick="openAccountView"
android:visibility="visible"
app:srcCompat="#drawable/account" />
<TextView
android:id="#+id/accountTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:gravity="center_vertical|center_horizontal|center"
android:text="#string/account"
android:textColor="#color/body_text_color"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/accountView"
android:paddingBottom="2dip"
android:paddingEnd="2dip"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:paddingStart="2dip"
android:text="#string/account_detail"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="12sp" />
</RelativeLayout>
<RelativeLayout
android:id="#id/layout_activations"
android:layout_width="#dimen/dashboard_feature_layout_width"
android:layout_height="#dimen/dashboard_feature_layout_height"
android:layout_marginEnd="#dimen/dashboard_feature_rightmost_margin"
android:layout_marginLeft="#dimen/dashboard_feature_middle_margin"
android:layout_marginRight="#dimen/dashboard_feature_rightmost_margin"
android:layout_marginStart="#dimen/dashboard_feature_middle_margin"
android:layout_marginTop="#dimen/dashboard_feature_top_margin"
android:layout_toEndOf="#id/layout_account"
android:layout_toRightOf="#id/layout_account"
android:background="#drawable/dashboard_bg_selector"
android:clickable="false"
android:elevation="#dimen/dashboard_feature_bg_elevation"
android:focusable="false"
android:onClick="openActivationsView"
tools:targetApi="lollipop">
<ImageView
android:id="#+id/activationsView"
android:layout_width="130dip"
android:layout_height="130dip"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:clickable="true"
android:contentDescription="#string/activations_packs"
android:onClick="openActivationsView"
app:srcCompat="#drawable/activations" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/activationsView"
android:elevation="7dp"
android:paddingBottom="2dip"
android:paddingEnd="2dip"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:paddingStart="2dip"
android:text="#string/activations_detail"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="12sp" />
<TextView
android:id="#+id/activationsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:gravity="center_vertical|center_horizontal|center"
android:text="#string/activations_packs"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#id/layout_services"
android:layout_width="#dimen/dashboard_feature_layout_width"
android:layout_height="#dimen/dashboard_feature_layout_height"
android:layout_below="#id/layout_account"
android:layout_marginBottom="10dip"
android:layout_marginEnd="#dimen/dashboard_feature_middle_margin"
android:layout_marginLeft="#dimen/dashboard_feature_leftmost_margin"
android:layout_marginRight="#dimen/dashboard_feature_middle_margin"
android:layout_marginStart="#dimen/dashboard_feature_leftmost_margin"
android:layout_marginTop="#dimen/dashboard_feature_ram_security_top_margin"
android:background="#drawable/dashboard_bg_selector"
android:clickable="false"
android:elevation="5dp"
android:focusable="false"
android:onClick="openServicesView"
tools:targetApi="lollipop">
<TextView
android:id="#+id/servicesTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:gravity="center_vertical|center_horizontal|center"
android:text="#string/services"
android:textColor="#android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/servicesView"
android:layout_width="130dip"
android:layout_height="130dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="#string/services"
app:srcCompat="#drawable/services" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/servicesView"
android:paddingBottom="2dip"
android:paddingEnd="2dip"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:paddingStart="2dip"
android:text="#string/services_detail"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="12sp" />
</RelativeLayout>
<RelativeLayout
android:id="#id/layout_others"
android:layout_width="#dimen/dashboard_feature_layout_width"
android:layout_height="#dimen/dashboard_feature_layout_height"
android:layout_below="#id/layout_activations"
android:layout_marginBottom="10dip"
android:layout_marginEnd="#dimen/dashboard_feature_rightmost_margin"
android:layout_marginLeft="#dimen/dashboard_feature_middle_margin"
android:layout_marginRight="#dimen/dashboard_feature_rightmost_margin"
android:layout_marginStart="#dimen/dashboard_feature_middle_margin"
android:layout_marginTop="#dimen/dashboard_feature_ram_security_top_margin"
android:layout_toEndOf="#id/layout_services"
android:layout_toRightOf="#id/layout_services"
android:background="#drawable/dashboard_bg_selector"
android:clickable="false"
android:elevation="#dimen/dashboard_feature_bg_elevation"
android:focusable="false"
tools:targetApi="lollipop">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:text="#string/other_detail"
android:textColor="#color/black"
android:textSize="12sp" />
<ImageView
android:id="#+id/otherView"
android:layout_width="130dip"
android:layout_height="130dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="#string/other"
app:srcCompat="#drawable/help" />
<TextView
android:id="#+id/otherTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:clickable="true"
android:gravity="center_vertical|center_horizontal|center"
android:onClick="openOthersView"
android:text="#string/other"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
You are calling openAccountView method by onClick imageView by this:
android:onClick="openAccountView"
So, no need to add onClicklistener to your imageView.
Remove onClicklistener in openAccountView, Use the below code :
public void openAccountView(View view){
Intent i = new Intent(MainActivity.this, AccountActivity.class);
startActivity(i);
}
Hope this helps.

how to get cross button/image in custom dialog box border

I have used cutsom dialog box in my MainActivity for popup screen.I have added cross button to close
the popup screen. here in my code, that cross button is showing inside
the popup screen.
Now what i want is it has to show at the border of the popup screen.
please help me to get this..
Below is my code..any help would be appreciated...thanks in advance..
Java File
HomePage.java
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v4.app.DialogFragment;
public class HomePage extends Activity {
final Context context = this;
Button b1;
Button b2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
b1 = (Button) findViewById(R.id.button6);
b2 = (Button) findViewById(R.id.button7);
b2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(HomePage.this,RegisterPage.class);
startActivity(intent);
}
});
b1.setOnClickListener(new OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.activity_login_page);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay(); // getting the screen size of device
Point size = new Point();
display.getSize(size);
int width = size.x - 20; // Set your heights
int height = size.y - 80; // set your widths
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = width;
lp.height = height;
dialog.getWindow().setAttributes(lp);
dialog.show();
ImageView image = (ImageView) dialog.findViewById(R.id.cancel_btn);
image.setImageResource(R.drawable.cancel2);
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
Button dialogButton = (Button) dialog.findViewById(R.id.button1);
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//dialog.dismiss();
Intent intent = new Intent(HomePage.this,CategoryPage.class);
startActivity(intent);
}
});
dialog.show();
}
});
}
}
XML File
activity_home_page.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:background="#fff"
tools:context=".MainActivity" >
<LinearLayout android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dp"
android:src="#drawable/miiskylogo" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/header" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="163dp"
android:orientation="vertical"
android:paddingBottom="0dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin" >
<Button
android:id="#+id/button6"
android:layout_width="230dp"
android:layout_height="50dp"
android:background="#00b0ff"
android:layout_weight="0.1666"
android:textSize="18dp"
android:textColor="#fff"
android:paddingLeft="3dp"
android:layout_gravity="center"
android:layout_marginTop="32dp"
android:drawableLeft="#drawable/lock"
android:text="Login with SVAPP" />
<Button
android:id="#+id/button7"
android:layout_width="230dp"
android:layout_height="50dp"
android:background="#00b0ff"
android:layout_weight="0.1666"
android:textSize="18dp"
android:textColor="#fff"
android:paddingLeft="3dp"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:drawableLeft="#drawable/regis"
android:text="Register with SVAPP" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
activity_login_page.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:background="#fff"
tools:context="com.example.miiskyproject.Login" >
<ImageView
android:id="#+id/cancel_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/cancel2" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="130dp"
android:layout_height="90dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:src="#drawable/miiskylogo" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText1"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_marginTop="190dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="10dp"
android:background="#drawable/loginedit"
android:ems="6"
android:hint="User Name"
android:padding="8dp"
android:textColor="#000"
android:textStyle="bold"
android:textColorHint="#bdbdbd"
android:textSize="13dp" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_marginTop="190dp"
android:layout_marginRight="15dp"
android:background="#drawable/loginedit"
android:ems="6"
android:hint="Password"
android:inputType="textPassword"
android:padding="8dp"
android:textColor="#000"
android:textStyle="bold"
android:textColorHint="#bdbdbd"
android:textSize="13dp" />
<Button
android:id="#+id/button1"
android:layout_width="70dp"
android:layout_height="35dp"
android:layout_marginRight="20dp"
android:layout_marginTop="190dp"
android:background="#drawable/loginbutton"
android:ems="7"
android:text="Login"
android:textColor="#fff"
android:textSize="12dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelInsurance7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="225dp"
android:text="Forget Your Password?"
android:textSize="13dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/textView2"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="280dp"
android:padding="10dp"
android:layout_marginRight="20dp"
android:text="Lorem Ipsum dolor sit amet,"
android:background="#drawable/loginedit"
android:textSize="18dp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
I want cross button something like the image below.
Click Here
Try this solution:
<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:background="#fff"
tools:context="com.example.miiskyproject.Login" >
<LinearLayout
android:id="#+id/cancel_layuot"
android:layout_width="match_parent"
android:gravity="right"
android:background="#android:color/transparent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/cancel_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="130dp"
android:layout_height="90dp"
android:layout_below="#+id/cancel_layuot"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:src="#drawable/left_arrow" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#+id/cancel_layuot" >
<EditText
android:id="#+id/editText1"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="10dp"
android:layout_marginTop="190dp"
android:background="#drawable/ic_launcher"
android:ems="6"
android:hint="User Name"
android:padding="8dp"
android:textColor="#000"
android:textColorHint="#bdbdbd"
android:textSize="13dp"
android:textStyle="bold" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_marginRight="15dp"
android:layout_marginTop="190dp"
android:background="#drawable/ic_launcher"
android:ems="6"
android:hint="Password"
android:inputType="textPassword"
android:padding="8dp"
android:textColor="#000"
android:textColorHint="#bdbdbd"
android:textSize="13dp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="70dp"
android:layout_height="35dp"
android:layout_marginRight="20dp"
android:layout_marginTop="190dp"
android:background="#drawable/ic_launcher"
android:ems="7"
android:text="Login"
android:textColor="#fff"
android:textSize="12dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelInsurance7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="225dp"
android:text="Forget Your Password?"
android:textSize="13dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#+id/cancel_layuot"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp" >
<TextView
android:id="#+id/textView2"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginTop="280dp"
android:background="#drawable/ic_launcher"
android:padding="10dp"
android:text="Lorem Ipsum dolor sit amet,"
android:textSize="18dp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
You will see something like this:
Just replace image ic_launcher with the appropriated image.

Categories

Resources