I'am testing a new library called BottomBar of roughike, and this library help you to create a BottomBar like BottomNavigationView and this libries define your tabs in an XML resource file.
Example:
res/xml/bottombar_tabs.xml:
how can I inflate this menu? I already tried a lot of things. Obs: I can't move this file to another place.
In an old version of this library, I did this:
res/menu/bottombar_tabs
public boolean onCreateOptionaMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
I already tried this:
public boolean onCreateOptionalMenu(Xml xml){
getLayoutInflater().inflate(R.xml.bottombar_tabs, xml);
return true;
}
but getLayout and getMenu can't do that, and don't exist an getXml...
--Edit--
I realize that the main problem is that when I select a tab in the menu
only the Toast message appears:
if(tabId == R.id.perfil){
Toast.makeText(MainActivity.this, "Perfil", Toast.LENGTH_SHORT).show();
PerfilFragment f = new PerfilFragment();
getFragmentManager().beginTransaction().replace(R.id.bottomBar, f).commit();
}
Here is the PerfilFragment:
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
View v = inflater.inflate(R.layout.perfil, container, false);
return v;
}
Here the perfil layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:text="teste"/>
When I select the user icon in the menu the app should take me to the user page but only the Toast appears.
Here my activity_main
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomBar"/>
<com.roughike.bottombar.BottomBar
android:id="#+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
app:bb_tabXmlResource="#xml/bottombar_tabs" />
Perfil = User.
Sorry for my english, I'm learning.
Related
I'm trying to set on click method in my project but, I can't connect my layout to the given fragment. When you press the icon of Instagram it should open persons Instagram but it only crashes.
Fragment code:
public class SupportFragment extends Fragment {
#Nullable
ImageView vedoIg;
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_support,container,false);
vedoIg = v.findViewById(R.id.vedo_ig);
vedoIg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://instagram.com/v");
Intent instagram = new Intent(Intent.ACTION_VIEW, uri);
instagram.setPackage("com.instagram.android");
try {
startActivity(instagram);
}
catch (Exception e){
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://instagram.com/_u/v")));
}
}
});
return inflater.inflate(R.layout.fragment_support,container,false);
}
}
Layout folder:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/pozadina"
tools:context=".SupportFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="Contact us: "
android:textColor="#ffffff"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
android:text="Vedo"
android:textColor="#ffffff"
android:textSize="30dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/vedo_ig"
android:layout_width="72dp"
android:layout_height="48dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="150dp"
app:srcCompat="#drawable/w_instagram"/>
Any tip would mean the world. Thanks in advance :D <3
This bit of code is problematic:
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_support,container,false);
vedoIg = v.findViewById(R.id.vedo_ig);
// ...
return inflater.inflate(R.layout.fragment_support,container,false);
}
In the first line, you're inflating one copy of a view... but then you return a different copy of that same view. This means that what the user sees won't match the view you've been searching in and setting click listeners on.
Change the last line to return v to fix this issue.
please try https://www.instagram.com/v/ you must change http with https
I want to change from layout 1 to layout 2, after rotate still keep content. Can somebody show me how to do that?
from this: https://www.dropbox.com/sc/y2nyrzard859hf2/AAD0qVjWoLzKcnQV9a4FTQi_a
to this: https://www.dropbox.com/sc/29hhlbfm31cfs0j/AADCWsFNzD7DKHx4q9i2FlbDa
this is my code but seem like it didn't work
if(config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_create_bill3);
}
else {
setContentView(R.layout.activity_create_bill2);
}
if(fragmentManager.findFragmentByTag("fragment_product")==null) {
fragment_product = new Fragment_Product();
fragmentTransaction.replace(R.id.fragment_product,fragment_product,"fragment_product");
}
else
fragmentTransaction.replace(R.id.fragment_product,fragmentManager.findFragmentByTag("fragment_product"));
if(fragmentManager.findFragmentByTag("fragment_product_chosen")==null) {
fragment_product_chosen = new Fragment_Product_Chosen();
fragmentTransaction.replace(R.id.fragment_product_chosen,fragment_product_chosen,"fragment_product_chosen");
}
else
fragmentTransaction.replace(R.id.fragment_product_chosen,fragmentManager.findFragmentByTag("fragment_product_chosen"),"fragment_product_chosen");
fragmentTransaction.commit();
I using 2 diffent layout, it has a same view but one in horizontal and another in vertical, when rotate, fragment_product still keep content, but fragment_product_chosen are disappear.
You should have 3 clases:
FragmentMain
FragmentSide
MainActivity
click here to see your layout folder
Code in your MainActivity Class:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Code in your FragmentMain Class:
public class FragmentMain extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
Code in your FragmentSide Class:
public class FragmentSide extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_side, container, false);
}
}
Then in your activity_main.xml:
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_main"
android:layout_marginTop="230dp"
class="au.com.example.multi_fragments.FragmentMain" />
<fragment
android:layout_width="match_parent"
android:layout_height="220dp"
android:id="#+id/fragment_side"
class="au.com.example.multi_fragments.FragmentSide" />
/>
same way in your activity_main.xml(land):
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="250dp"
android:id="#+id/fragment_main"
class="au.com.example.multi_fragments.FragmentMain" />
<fragment
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="#+id/fragment_side"
class="au.com.example.multi_fragments.FragmentSide" />
in your fragment_main.xml:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View 2"
android:textSize="20sp"
android:padding="20dp"
android:textStyle="bold"
android:id="#+id/textViewMain" />
In your fragment_side.xml:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View 1"
android:textSize="20sp"
android:padding="20dp"
android:textStyle="bold"
android:id="#+id/textViewMain" />
Click here to see the output
I hope this solution is the one you want. Good luck :)
To have different layouts for landscape and portrait modes, create two folders under the res folder: layout and layout-land. All XML files should have the same names in both folders. For more details, read Designing for Multiple Screens. Even though this article is for different screen sizes, the techniques apply to different device orientations as well.
As for saving and restoring data, this is the same as destroying the activity without an orientation change.
I have a view pager consisting of 4 tabs. Each tab holds its own fragment.
How would I be able to use fragment transaction to replace the fragment in tab 3 with a new fragment?
I've tried a lot of things, but I have not been able to come up with a solution at all. I've tried looking around on google and stackoverflow, but with no success.
I assume that your fragment has a button that is put in the center. By clicking on it, you can change the layout stays under of this button. The content/layout of the first fragment you mentioned should be replaced with wrapperA and the content/layout of the second one should be replaced with wrapperB. I put a simple red background for wrapperA to distinguish it with wrapperB, wrapperB is also green due to the same reason. I hope this is what you want:
public class SwitchingFragment extends Fragment {
Button switchFragsButton;
RelativeLayout wrapperA, wrapperB;
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.layout_switch, container, false);
wrapperA = (RelativeLayout) rootView.findViewById(R.id.wrapperA);
wrapperB = (RelativeLayout) rootView.findViewById(R.id.wrapperB);
switchFragsButton = (Button) rootView.findViewById(R.id.switchFragsButton);
switchFragsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (wrapperB.getVisibility() == View.GONE) {
wrapperB.setVisibility(View.VISIBLE);
// There is no need to change visibility of wrapperA since it stays behind when wrapperB is visible.
// wrapperA.setVisibility(View.GONE);
}
else {
wrapperB.setVisibility(View.GONE);
// Again, there is no need.
// wrapperA.setVisibility(View.VISIBLE);
}
}
});
return rootView;
}
}
The layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<!-- The content of first fragment should be in "wrapperA". -->
<RelativeLayout
android:id="#+id/wrapperA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/red"
android:visibility="visible">
</RelativeLayout>
<!-- The content of second fragment should be in "wrapperB". -->
<RelativeLayout
android:id="#+id/wrapperB"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/green"
android:visibility="gone">
</RelativeLayout>
<!-- As I said, I assume that the layout switcher button is stable
and so it should be in front of the switching layouts. -->
<Button
android:id="#+id/switchFragsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#color/black"
android:text="Switch"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#color/white"/>
</RelativeLayout>
Alternatively, you can try to change the fragment directly by notifying your FragmentPagerAdapter as described in this link:
Replace fragment with another fragment inside ViewPager
Hi there (and thanks in advance),
I have an application with a Google Play Store-like layout (using PagerTabStrip with 5 sub fragments). In some of those fragments I will need to display a little info regarding the last time the data was updated.
I immediately thought of creating a fragment (LastUpdatedFragment) which I would then add (nest) to the fragments I needed. At first, and since the last updated date was supposed to be the same for every screen, things were working (I simply added the fragment in the xml of the parent Fragments I needed and inside onCreateView I would put the date in the TextView), but after some changes I now need to send a specific date for each one of these LastUpdatedFragment instances.
I came up with one way - creating new custom attributes for my Fragment and then I would set the date I wanted. After some reading I stumbled across a easier way (Fragments within Fragments - dinamically adding the fragment using FragmentManager) - I simply needed the parent Fragment to handle the input parameters and pass it to the child fragment.
The problem is that, although I get no errors I also get no child fragment, it just does not display. I would be grateful if you could guide me in the right direction.
ScoreBoardFragment.java -> Parent Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_scoreboard, container,
false);
for (int i = 0; i < tvCount; i++) {
tvName = "f_scoreboard_header_tv" + String.valueOf(i);
resID = getResources().getIdentifier(tvName, "id",
_activity.getPackageName());
header = (TextView) rootView.findViewById(resID);
header.setTypeface(MyGlobalConfig.getInstance().getHeadersFont());
}
FragmentManager childFragMan = getChildFragmentManager();
FragmentTransaction childFragTrans = childFragMan.beginTransaction();
LastUpdatedFragment fragB = new LastUpdatedFragment();
childFragTrans.add(R.id.FRAGMENT_PLACEHOLDER, fragB);
childFragTrans.addToBackStack("B");
childFragTrans.commit();
return rootView;
}
fragment_scoreboard.xml (simplified but displaying everything else)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/FRAGMENT_PLACEHOLDER"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
style="#style/ListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/f_scoreboard_header_tv0"
style="#style/ListViewRowHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#" />
</LinearLayout>
<ListView
android:id="#+id/f_scoreboard_lvbody"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" >
</ListView>
</LinearLayout>
LastUpdatedFragment.java -> Child Fragment
public class LastUpdatedFragment extends Fragment{
private View rootView;
private TextView tv;
private Context ctx;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(
getActivity()));
this.ctx = getActivity().getApplicationContext();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_date, container,
false);
tv = (TextView) rootView.findViewById(R.id.f_date_tv);
tv.setTypeface(MyGlobalConfig.getInstance().getBodyFont());
tv.setText(getString(R.string.lastupdated) + ": " + Util.getLastUpdated(ctx));
return rootView;
}
}
fragment_date.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="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/f_date_tv"
style="#style/LastUpdatedTV"
android:layout_width="match_parent"
android:text="Data de última actualização: 27/12/2014 21:30" />
</LinearLayout>
Try to use FrameLayout instead of LinearLayout as Placeholder.
This is my initial layout of which the following EditText with id: assign_edit and weight_edit are part of. The name of this layout is new_class_container.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/assign_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="#string/example_assign"
android:singleLine="true"
android:inputType="textCapWords"
android:layout_weight="1">
</EditText>
<EditText
android:id="#+id/weight_edit"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="#string/example_weight"
android:layout_marginLeft="3dp"
android:gravity="center_horizontal"
android:inputType="number"
android:singleLine="true">
</EditText>
</LinearLayout>
The layout that I want to inflate is named new_class. In this layout, I copy pasted the exact same EditText's mentioned above. The following is my java file
public class DefiningClass extends Activity {
private ViewGroup mContainerView;
private EditText assignName;
private EditText assignWeight;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.new_class_container);
mContainerView = (ViewGroup) findViewById(R.id.container);
}
#Override
public boolean onCreateOptionsMenu(Menu add){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, add);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.btn_save:
//INSERT INSTRUCTIONS TO SAVE
String str = assignName.getText().toString();
Toast msg = Toast.makeText(getBaseContext(),str,Toast.LENGTH_LONG);
msg.show();
break;
case R.id.btn_add:
//findViewById(android.R.id.empty).setVisibility(View.GONE);
addItem();
break;
default:
return true;
}
return super.onOptionsItemSelected(item);
}
private void addItem() {
final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(
R.layout.new_class, mContainerView, false);
assignName = (EditText) newView.findViewById(R.id.assign_edit);
assignWeight = (EditText) newView.findViewById(R.id.weight_edit);
mContainerView.addView(newView, 0);
}
}
I put a Toast in my btn_save to see the work it does. When I run it, it only shows the most recent EditText. How can I make it so the toast recognizes all the edittexts that I have "inflated" and placed in it. For instance, if I had three edittexts, first said Hello, 2nd, Welcome, 3rd, Bye. The toast only shows Bye and not the other two.
What can I do for it to recognize the other two? When I save it in the future I want all the edittexts to be saved.
Use
<EditText
android:id="#+id/assign_edit"/> // missing #+
Your findViewById looks for a view with the id in the current infalted layout.
So having the same ids in different layout xml files is not a problem.
Check the topic id in the below link
http://developer.android.com/guide/topics/ui/declaring-layout.html
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).