I want to do following things:
Create grid(3 * 3) of images on one screen
Horizontal Swipe the first screen to next one to display the other grid(i.e remaining images)
Similar to the Grid we see in our android mobile Phone's
Any answer truly Appreciated...
You can achive this by using a ViewPager and a GridLayout or a TableLayout. You can find many samples for using ViewPager.
Here is a complete sample code for you. Just copy it(with layouts) to your project and run it. Sample uses a TableLayout but you can change it to use a GridLayout if you want.
Also read TODO'S, in code;)
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PagerDemo extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_main);
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomViewPagerAdapter(getSupportFragmentManager()));
}
private static class CustomViewPagerAdapter extends FragmentPagerAdapter {
public CustomViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// TODO switch for position value to create different fragments
return PagerFragment.newInstance(position);
}
#Override
public int getCount() {
return 3;
}
}
public static class PagerFragment extends Fragment {
private int index;
// This is a best practice to instantiate a Fragment
public static Fragment newInstance(int index) {
// TODO: Change your newInstance method with new parameters for your need
PagerFragment f = new PagerFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
index = getArguments().getInt("index", 0);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.sample_table_layout, null);
return root;
}
}
}
sample.xml under res/layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
sample_table_layout.xml under res/layout:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
</TableRow>
</TableLayout>
Related
I have added an AutoCompleteTextView to my android project but when I click on the AutoCompleteTextView, the keyboard appears and disappears in a second.
But if I click the AutoCompleteTextView and quickly click on some characters, the keyboard doesn't disappear.
I do not understand why. How can I fix it?
if you need another piece of code ask me.
This is the Fragment.java
package com.example.trasporti;
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.TextView;
import com.example.trasporti.adapter.PlaceAutoSuggestAdapter;
import com.google.android.libraries.places.api.model.Place;
import com.here.sdk.core.errors.InstantiationErrorException;
import com.here.sdk.search.SearchEngine;
/**
* A simple {#link Fragment} subclass.
* Use the {#link LocationFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class LocationFragment extends Fragment {
View layoutView;
TextView text1,text2,text3;
public LocationFragment() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static LocationFragment newInstance(String param1, String param2) {
LocationFragment fragment = new LocationFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
layoutView = inflater.inflate(R.layout.fragment_location,container,false);
MainActivity mainActivity = (MainActivity) getActivity();
Context context = mainActivity.getContextF();
AutoCompleteTextView autoCompleteTextView = layoutView.findViewById(R.id.autocomplete);
autoCompleteTextView.setAdapter(new PlaceAutoSuggestAdapter(context, android.R.layout.simple_list_item_1));
// text1 = layoutView.findViewById(R.id.TextView1);
//text2 = layoutView.findViewById(R.id.TextView2);
//text3 = layoutView.findViewById(R.id.TextView3);
return layoutView;
}
}
This is Fragment layout
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#0F1D49"
tools:context=".LocationFragment">
<!-- TODO: Update blank fragment layout -->
<AutoCompleteTextView
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:background="#color/white"
android:shadowColor="#color/white"
android:textColorHint="#color/grey"
android:textColor="#color/black"
android:textSize="20sp"
android:hint="Cerca la destinazione"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/autocomplete"
/>
<!---<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/TextView1"
android:textColor="#color/white"
android:fontFamily="monospace"
android:padding="12dp"
android:layout_marginTop="32dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/TextView2"
android:textColor="#color/white"
android:fontFamily="monospace"
android:padding="12dp"
android:layout_marginTop="32dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/TextView3"
android:textColor="#color/white"
android:fontFamily="monospace"
android:padding="12dp"
android:layout_marginTop="32dp"
/>-->
</LinearLayout>
I have followed up your code and make it a demo but nothing happens just like you mentioned. If your view is static, then moving any code to the onActivityCreated method is not necessary. But when you - for instance, fill some lists from the adapter, then you should do it in the onActivityCreated method. Please check it out below:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AutoCompleteTextView editText = findViewById(R.id.autocomplete);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.custom_list_item, R.id.text_view_list_item, countriesList);
editText.setAdapter(adapter);
}
}
Your Fragment layout:
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#0F1D49"
>
<AutoCompleteTextView
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:background="#color/white"
android:shadowColor="#color/white"
android:textColorHint="#color/teal_200"
android:textColor="#color/black"
android:textSize="20sp"
android:completionThreshold="1"
android:hint="Cerca la destinazione"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/autocomplete"
/>
</LinearLayout>
text_view_list_item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/text_view_list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#color/teal_200"
android:textSize="16sp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/teal_700" />
</LinearLayout>
i have a custom navigation drawer that opens from left to right using fragment. i want to move my content and action bar to right when navigation opens, i read some sources, they override onDrawerSlide method to do so, but since i using fragment to open and close navigation, i do not have such a method to override, I'll be grateful someone could help me (:
I tried this link to move the contents
and used this to create a custom navigation view
there is main activity :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container">
<TextView
android:id="#+id/homeContents"
android:text="this is gonna be ur main page"
android:layout_width="200dp"
android:layout_height="46dp" />
</FrameLayout>
</LinearLayout>
and this is drawer :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/rootLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/nav_profile"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:layout_width="77dp"
android:layout_height="77dp"
android:src="#drawable/ic_user"
/>
<LinearLayout
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:drawableLeft="#drawable/ic_user_plus"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:id="#+id/sign_in"
android:layout_width="91dp"
android:layout_height="40dp"
android:layout_marginStart="20dp"
android:background="#drawable/more_btn_coop_req"
android:text="SIGNUP"
android:textColor="#color/blue" />
<Button
android:drawableLeft="#drawable/ic_sign_in_alt"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:id="#+id/sign_up"
android:layout_width="91dp"
android:layout_height="40dp"
android:layout_marginStart="25dp"
android:background="#drawable/more_btn_coop_req"
android:text="ENTER"
android:textColor="#color/blue" />
</LinearLayout>
<Spinner
android:layout_marginTop="16dp"
android:background="#drawable/spinner_background"
android:id="#+id/cities_spinner"
android:layout_gravity="center"
android:layout_width="190dp"
android:layout_height="40dp">
</Spinner>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_marginTop="250dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:drawableLeft="#drawable/ic_check"
android:drawablePadding="25dp"
android:id="#+id/bookmark_centers"
android:layout_gravity="center"
android:background="#drawable/drawer_buttons_style"
android:text="تخفیف های نشان شده"
android:layout_width="186dp"
android:layout_height="40dp" />
<Button
android:drawableLeft="#drawable/ic_store"
android:id="#+id/followed_centers"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:background="#drawable/drawer_buttons_style"
android:text="مراکز دنبال شده"
android:layout_width="186dp"
android:layout_height="40dp" />
<Button
android:id="#+id/tems"
android:drawableLeft="#drawable/ic_info"
android:background="#drawable/drawer_buttons_style"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:text="شرایط استفاده"
android:layout_width="186dp"
android:layout_height="40dp" />
<Button
android:id="#+id/contact_us"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:drawableLeft="#drawable/ic_phone"
android:background="#drawable/drawer_buttons_style"
android:text="ارتباط با ما"
android:layout_width="186dp"
android:layout_height="40dp" />
<Button
android:id="#+id/share_us"
android:layout_gravity="center"
android:text="پیشنهاد به دوستان"
android:layout_marginTop="10dp"
android:drawableLeft="#drawable/ic_share_alt"
android:background="#drawable/drawer_buttons_style"
android:layout_width="186dp"
android:layout_height="40dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<Button
android:id="#+id/EXIT"
android:layout_width="72sp"
android:layout_height="40dp"
android:layout_marginStart="32dp"
android:background="#drawable/drawer_buttons_style"
android:drawableLeft="#drawable/ic_sign_out_alt"
android:text="خروج" />
<Button
android:id="#+id/edit"
android:layout_width="105dp"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:background="#drawable/drawer_buttons_style"
android:drawableLeft="#drawable/ic_cog"
android:text="ویرایش" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
there is a custom toolbar and base activity that merged both toolbar and
drawer, but i think it is not necessary.
this is menu fragment class :
package TestNavigationDrawer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.example.deathstroke.uniqueoff1.R;
public class MenuFragment extends Fragment implements
View.OnTouchListener {
GestureDetector gestureDetector;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.slider_menu, container,
false);
RelativeLayout root = rootView.findViewById(R.id.rootLayout);
/*gestureDetector=new GestureDetector(getActivity(),new
OnSwipeListener(){
#Override
public boolean onSwipe(Direction direction) {
if (direction==Direction.up){
//do your stuff
((MainActivity ) getActivity()).hideFragment();
}
if (direction==Direction.down){
//do your stuff
}
return true;
}
});
root.setOnTouchListener(this);*/
return rootView;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
}
and there is mainActivity class :
package com.example.deathstroke.uniqueoff1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import TestNavigationDrawer.*;
public class MainActivity extends BaseActivity {
boolean isFragmentLoaded;
Fragment menuFragment;
TextView title;
ImageView menuButton;
RelativeLayout rootLayout;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.e("onCreate", "onCreate: " );
super.onCreate(savedInstanceState);
initAddlayout(R.layout.activity_main);
Log.e("onCreate", "onCreate: " );
rootLayout = findViewById(R.id.rootLayout);
textView = findViewById(R.id.homeContents);
title = findViewById(R.id.title_top);
menuButton = findViewById(R.id.menu_icon);
title.setText("Menu Activity");
menuButton.setOnClickListener((View)-> {
try {
if (!isFragmentLoaded) {
loadFragment();
title.setText("Profile");
} else {
if (menuFragment != null) {
if (menuFragment.isAdded()) {
hideFragment();
}
}
}
}catch (Exception e){
Log.e("Menu", "onCreate: menu on click, e: " + e);
}
});
}
public void hideFragment(){
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_left,
R.anim.slide_right);
fragmentTransaction.remove(menuFragment);
fragmentTransaction.commit();
menuButton.setImageResource(R.drawable.ic_menu);
isFragmentLoaded = false;
title.setText("Main Activity");
}
public void loadFragment(){
FragmentManager fm = getSupportFragmentManager();
menuFragment = fm.findFragmentById(R.id.container);
menuButton.setImageResource(R.drawable.ic_up_arrow);
if(menuFragment == null){
menuFragment = new MenuFragment();
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_left,
R.anim.slide_right);
fragmentTransaction.add(R.id.container,menuFragment);
fragmentTransaction.commit();
}
isFragmentLoaded = true;
}
}
as you can see, i am using loadFragment and hideFragment to open and close drawer, so how can i move main content (there is a simple textview but consider it whole lots of stuff)
Hey you can use this I guess you want the same output
The below link
I want to make a app like that custom layout ScreenShot: .This is one row and I add like that row 100 more.So the people can clik number or its information(i).
But it is shown like that : two buttons overlapping .But I want two button next to each other it is like that in custom_layout.xml(you can see screen shot).
This is my custom_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<Button
android:id="#+id/button"
android:layout_width="298dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="87dp" />
<Button
android:id="#+id/button2"
android:layout_width="51dp"
android:layout_height="49dp"
android:text="Button"
tools:layout_editor_absoluteX="317dp"
tools:layout_editor_absoluteY="87dp" />
</android.support.constraint.ConstraintLayout>
This is my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView1"
android:layout_width="162dp"
android:layout_height="424dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="87dp" />
</RelativeLayout>
</ScrollView>
And this is my MainActivity.java:
package com.hey.task.attendancesystem;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
Button[] button=new Button[15];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
CustomAdapter customAdapter = new CustomAdapter();
//listeyi customlayouttaki şekilde oluşturdum hepsini
listView.setAdapter(customAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
class CustomAdapter extends BaseAdapter {
#Override
public int getCount() {
return button.length;
}
#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) {
view = getLayoutInflater().inflate(R.layout.custom_layout,null);
Button button1 = (Button) view.findViewById(R.id.button);
Button button2 = (Button) view.findViewById(R.id.button2);
for (int j = 0;j<15;j++)
button1.setText(i + "");
for (int j = 0;j<15;j++)
button2.setText("i");
return view;
}
}
}
I don't know if i get your question but you can simply use LinearLayout with weight. eg:-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/button_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5" // or whatever you want
android:background="?android:attr/selectableItemBackground"
android:gravity="center"
android:padding="10dp"
android:text="Title 1" />
<Button
android:id="#+id/button_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5" // or whatever you want
android:background="?android:attr/selectableItemBackground"
android:gravity="center"
android:padding="10dp"
android:text="Title 2" />
</LinearLayout>
You can use linear layout with a android:weightSum="" attribute in place of constraint layout in your custom_layout.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:weightSum="1.0"
android:orientation="horizontal"
android:gravity="center">
<Button
android:layout_height="50dp"
android:layout_width="match_parent"
android:text="Button"
android:layout_weight="0.2"/>
<Button
android:layout_height="50dp"
android:layout_width="match_parent"
android:text="Button"
android:layout_weight="0.8"/>
</LinearLayout>
weightSum attribute is useful for having the layout rendered correctly for any device, which will not happen if you set width and height directly.
I am trying to implement a step-by-step tutorial at the start of my app. I created 3 fragment instances that the user can scroll through. They are combined using a FragmentPagerAdapter, that is set up and added to a TabLayout so that the fragments are treated as tabs. The tab indicators are given a custom style so that they appear as dots.
The issue I am encountering is that everything looks fine in design view, but when the app is deployed in the emulator, the constraint layouts are not respected and the positioning and sizing of the view controls within the fragment end up in a wonky configuration. The activity is set to portrait only, so display orientation is not an issue.
This is how the fragment appears in design view:
3 separate GIFs are loaded in the WebView instances (they are random for the purposes of this question).
This is how everything actually appears in the emulator:
As you can see, the WebView is the size of the entire fragment, and the TextView and Button are nowhere to be found, even if the WebView is removed.
Here is the entire code and Android Studio project associated with the question: https://github.com/mathusummut/StackOverflowQuestionCode1
Here is the most relevant code:
TutorialActivity.java:
package mathusummut.dabtest;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
public class TutorialActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
Fragment currentPage = new TutorialFragment();
Bundle currentBundle = new Bundle();
currentBundle.putString("tutorialText", "1. Turn the volume up ↑");
currentBundle.putString("tutorialGif", "file:///android_asset/volume.gif");
currentPage.setArguments(currentBundle);
adapter.addFragment(currentPage, "");
currentPage = new TutorialFragment();
currentBundle = new Bundle();
currentBundle.putString("tutorialText", "2. Grab the phone in one hand...");
currentBundle.putString("tutorialGif", "file:///android_asset/step2.gif");
currentPage.setArguments(currentBundle);
adapter.addFragment(currentPage, "");
currentPage = new TutorialFragment();
currentBundle = new Bundle();
currentBundle.putString("tutorialText", "Dab...");
currentBundle.putString("tutorialGif", "file:///android_asset/step3.gif");
currentPage.setArguments(currentBundle);
adapter.addFragment(currentPage, "");
ViewPager viewPager = findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(3);
viewPager.setAdapter(adapter);
((TabLayout) findViewById(R.id.tabs)).setupWithViewPager(viewPager);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()== android.R.id.home) {
finish();
return true;
} else
return super.onOptionsItemSelected(item);
}
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
}
}
fragment_tutorial.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:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
tools:context="mathusummut.dabtest.TutorialFragment">
<WebView
android:id="#+id/tutorialGifView"
android:layout_width="330dp"
android:layout_height="346dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</WebView>
<TextView
android:id="#+id/tutorialTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:text="Instructions"
android:textAlignment="center"
android:textSize="36sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/nextButton"
android:layout_width="124dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="456dp"
android:layout_x="69dp"
android:layout_y="386dp"
android:background="#android:color/holo_red_dark"
android:text="Next"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
activity_tutorial.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:id="#+id/coordinatorLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="#+id/appBarLayout2"
app:layout_constraintTop_toTopOf="#+id/appBarLayout2">
</android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:tabBackground="#drawable/tab_selector"
app:tabGravity="center"
app:tabMode="fixed"
app:tabIndicatorHeight="0dp"/>
</android.support.constraint.ConstraintLayout>
I have tried using RelativeLayout instead of ConstraintLayout, but the change seems to have no effect on the result. What can I do to resolve this issue, please?
This is you code done my change you required.
enter image description here
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View currentFragment = inflater.inflate(R.layout.fragment_tutorial, container, false);
WebView view = currentFragment.findViewById(R.id.tutorialGifView);
Bundle passedArguments = getArguments();
((TextView) currentFragment.findViewById(R.id.tutorialTextView)).setText(passedArguments.getString("tutorialText"));
view.loadDataWithBaseURL(null, TEMPLATE_HTML.replace("gif", passedArguments.getString("tutorialGif")), "text/html", "utf-8", null);
view.setBackgroundColor(Color.TRANSPARENT);
view.setInitialScale(1);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setLoadWithOverviewMode(true);
view.getSettings().setUseWideViewPort(true);
view.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
view.setScrollbarFadingEnabled(false);
return currentFragment;
}
}˚
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:orientation="vertical"
tools:context="mathusummut.dabtest.TutorialFragment">
<TextView
android:id="#+id/tutorialTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="30dp"
android:text="Instructions"
android:textAlignment="center"
android:textSize="36sp" />
<WebView
android:id="#+id/tutorialGifView"
android:layout_width="330dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="30dp" />
<Button
android:id="#+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="20dp"
android:background="#android:color/holo_red_dark"
android:text="Next"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="24sp" />
I pull you code from GitHub .I think it is ok and working fine for you.
I make a separated branch push for you GitHub.
This is where I am stuck now. I am stuck at dynamically adding buttons to gridview.
- My gridview stars with a one button.
- When user clicks that button a context menu , pops up, asking user to enter info, once that is done. A block in the grid view is created with that info
- This is shown in the picture
I have pasted the code. I do not have clear idea on how to do it. I am assuming it will be inflating a new view and adding it to parent(layout or grid ?) view. I do not have much idea on how to code it. I have tried many things from google. I thought I could start from simple image gridview and modify it to my needs but it did not work.
Please provide some directions.
create_team_new.xml(layout where gridview resides)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/parentcreateteam"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/background">
<TextView
android:id="#+id/citytextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/city"
android:layout_alignTop="#+id/teamcity"
android:layout_alignParentLeft="true" />
<AutoCompleteTextView
android:id="#+id/teamname"
android:layout_width="244dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/teamcity"
android:layout_alignRight="#+id/gridviewplayer"
android:text="#string/teamname"
android:textSize="18sp" >
<requestFocus />
</AutoCompleteTextView>
<Button
android:id="#+id/createteam"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="Create Team"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/red_button"
style="#+style/button_text" />
<TextView
android:id="#+id/teamnametextview"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:layout_alignBaseline="#+id/teamname"
android:layout_alignBottom="#+id/teamname"
android:layout_alignParentLeft="true"
android:text="#string/teamname" />
<AutoCompleteTextView
android:id="#+id/teamcity"
android:layout_width="244dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/cricket"
android:layout_alignRight="#+id/gridviewplayer"
android:layout_below="#+id/teamname"
android:layout_marginTop="22dp"
android:ems="10"
android:text="#string/city"
android:textSize="18sp" >
</AutoCompleteTextView>
<TextView
android:id="#+id/radiotextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/teamcity"
android:layout_marginTop="15dp"
android:text="Pick a Sport" />
<GridView
android:id="#+id/gridviewplayer"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_above="#+id/createteam"
android:layout_below="#+id/pick_sport"
android:layout_centerHorizontal="true"
android:background="#drawable/gv_bkg"
android:padding="5dp"
android:numColumns="4" >
</GridView>
<RadioGroup
android:id="#+id/pick_sport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/createteam"
android:layout_below="#+id/radiotextview"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/cricket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cricket" />
<RadioButton
android:id="#+id/soccer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Soccer" />
</RadioGroup>
</RelativeLayout>
gv_createplayer.xml(view to be inflated inside gridview)
<?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"
>
<Button android:id="#+id/btn_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text= "+"
android:background="#drawable/red_button">
</Button>
</LinearLayout>
PlayerAdapter.java(gridview Adapter)
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
public class PlayerAdapter extends BaseAdapter {
private Context mContext;
LayoutInflater inflater;
// Keep all Images in array
public Integer[] mThumbIds = {};
/* R.drawable.gridview_createteam, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5, R.drawable.pic6,
R.drawable.pic7, R.drawable.pic8,
R.drawable.pic9, R.drawable.pic10,
R.drawable.pic11, R.drawable.pic12,
R.drawable.pic13, R.drawable.pic14,
R.drawable.pic15
};
*/
// Constructor
public PlayerAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#SuppressWarnings("static-access")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = new View(mContext);
view.inflate(mContext, R.layout.gv_createplayer,null);
return view;
}
}
1) How to create a list view inside a main view, only using a half of the screen.
This is done by the weight attribute. A rough example for your sketch: The top wrapper (edittexts and buttons) has a weight of .45, the listview has a weight of .45 and the footer has a weight of .1. You can specify the weight-sum in the surrounding view; but default should be 1.
2) How to populate list view in grid manner (a row of two columns will repeat itself)
Well, with the GridView (the adapter API is the same) (: