Missing button while executing first official Android tutorial - android

I am following official Android tutorial, but when I start the application, I obtain
instead of getting
Hereunder main files of the sample project
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndroidTest</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
activity_main.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.androidtest.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/edit_message" />
</RelativeLayout>
MainActivity.java
package com.example.androidtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
System details:
Fedora 20;
Android 4.4.4;
Eclipse 4.4;
ADB

I think you never attach the fragment into the activity.
Try to copy the fragment_main.xml into activity_main.xml!

The problem is that you have to add the fragment to the main activity.
There you can see how:
http://developer.android.com/training/basics/fragments/creating.html
You must add to your main activity layout, in the place where you want to place fragment.
<fragment android:name="com.example.android.MainFragment"
android:id="#+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
Then you had to create the class of the fragment, and inflate the main activity with the main layout

Related

How to create a drop-down list in Android Studio?

I am trying to create a drop-down list for an activity in Android Studio. I have tried using the Spinner.
Here is my xml code:
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown"/>
Here is my Java code:
Spinner dropdown = findViewById(R.id.spinner1);
String[] items = new String[]{"1", "2", "3"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
This is a screenshot of what it looks like:
As you can see, the drop-down list appears as a pop-up, however, I want the drop-down list to appear in a different way. See the image below as an example of what I want:
Does anyone have any ideas on how I could do this?
Actually it does look like there is a way with the Material design library called ExposedDropdownMenu
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/menu"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
/>
</com.google.android.material.textfield.TextInputLayout>
Obviously this is with TextInput and not a spinner so there will still be some custom logic to prevent typing if you dont want that
I personally have not tried it myself but it looks like what you want
Some ideas:
As #tyczj said, that is android's default layout that is showing, so there are some alternatives:
Create tour own custom layout
Use an external library like SearchableSpinner, it incorporates the search function and the item layout is similar from what you want.
Here I have found a useful example for a ComboBox/Spinner on Android:
https://abhiandroid.com/ui/spinner
Here is the code:
https://github.com/abhisheksaini4/SpinnerExamples/blob/master/SpinnerExamples.7z
The main codes include MainActivity.java:
package example.abhiandriod.spinnerexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] bankNames={"BOI","SBI","HDFC","PNB","OBC"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the bank name list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,bankNames);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
//Performing action onItemSelected and onNothing selected
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
Toast.makeText(getApplicationContext(), bankNames[position], Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Spinner
android:id="#+id/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" />
</RelativeLayout>
menu_main.xml:
<menu 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" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
And AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.abhiandriod.spinnerexample" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Add onclick listener to icons on toolbar [duplicate]

This question already has answers here:
Android, How to create option Menu
(11 answers)
Closed 5 years ago.
[my tool bar][1]
Do anyone have any solution?
I've seen your toolbar.
Here is my solution:
First create a menu xml resource file. (This will contain your "heart icon", res/menu/menu_example.xml)
Name it as you like, For example purpose I will name it menu_example.
menu_example.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_addfav"
android:title=""
app:showAsAction="always"
android:icon="#drawable/YOUR_ADDFAV_DRAWABLE" />
</menu>
Then create your activity code
activity_example.java
package com.example;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
public class activity_example extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Layout xml init
setContentView(R.layout.activity_example);
//Actionbar
//Back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
//Inflate the menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_example, menu);
return true;
}
//Handling Action Bar button click
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
//Back button
case android.R.id.home:
//If this activity started from other activity
finish();
/*If you wish to open new activity and close this one
startNewActivity();
*/
return true;
case R.id.action_addfav:
//addfav (heart icon) was clicked, Insert your after click code here.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void startNewActivity(){
Intent intent = new Intent(this,ACTIVITY_YOU_WANT_TO_START.class);
startActivity(intent);
finish();
}
}
Activity layout xml
(You can change the root layout to whatever layout you want)
<?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">
<!-- YOUR LAYOUT HERE -->
</RelativeLayout>
Last thing, make sure your app has toolbar in the theme
go to res/values/styles.xml
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<! -- Parent got to have actionbar in order for toolbat to appear -->
</style>
If you wish that the toolbar will be only in one specific activity take a look at: Apply a theme to an activity in Android?
You need to check R.id.img_bar_back. Weather it is the ImageView id you are finding or that is something else. This can happen due to finding wrong view id.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/tool_bar_color"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/toolbar_iv_image1"
android:layout_width="?actionBarSize"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:clickable="true"
android:gravity="center_vertical"
android:text="#string/e_wallet"
android:textColor="#android:color/white"
android:textSize="#dimen/font_20sp" />
<ImageButton
android:id="#+id/iv_invite"
android:layout_width="#dimen/padding_40dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="?selectableItemBackgroundBorderless"
android:clickable="true"
android:src="#drawable/invite_whit_icon" />
<ImageButton
android:id="#+id/iv_notifications"
android:layout_width="?actionBarSize"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="?selectableItemBackgroundBorderless"
android:clickable="true"
android:src="#drawable/notifiction_white" />
</LinearLayout>
</android.support.v7.widget.Toolbar>

Android Layout does not display controls

I am writing my first android app after reading a tutorial. I am excited and thought its easy. However, after adding a simple edit text and a hello world message, i run and was expecting to see something yet nothing showed up. Neither the textbox nor the default hello world. Pls where do go wrong ? Below is my layout (i.e activity_main.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:paddingLeft="16dp"
android:paddingRight="16dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:textColor="#FF00FF00"/>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textColor="#FF0000FF"
android:id="#+id/et_Text" />
</RelativeLayout>
My java code is the default/code generated one. I was expecting to see atleast the message for a start yet didn't show any control. Just a screen with a phone dialers on the rigth hand.
package com.example.mytest;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Since you are using RelativeLayout, you need to tell Android how each control is relative to each other. Change the layout as follow:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:id="#+id/textView1"
android:textColor="#FF00FF00"/>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#+id/textView1"
android:textColor="#FF0000FF"
android:id="#+id/et_Text" />
This will tell the system to place the EditText below the TextView.

Button not showing up on screen Android Application

I have created an application where users can send and share text that they have input, if they click the send button, the text that they input will be displayed, if they click the share button, the application opens up a list of sharing methods (GMail, Messaging etc..), what I want is though, to allow the users to view there text and then share it, hwoever, when the user clicks send and it goes to the file activity_display, the text shows, but the button does not. Any ideas why? Could you fix this? Here's the code;
activity_display_message.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="shareMessage"
android:layout_below="#+id/text_view"
android:text="test" />
</RelativeLayout>
MAIN PROBLEM: Button Share does not show.
EDIT: I suspect I may need to include something to this file, here's the code, any help?
DisplayMessageActivity.java:
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
public class DisplayMessageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(20);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Since you are using RelativeLayout you need to specify where to layout your views. Try this for example:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="shareMessage"
android:layout_below="#+id/text_view"
android:text="test" />
you are using relative layout. use linear layout instead. this will solve your problem.
You are using a relative layout. The objects are overlapping because you have not set them up to be LeftOf or Below or something like that.
Just replace your button with:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:onClick="shareMessage"
android:text="#string/button_share" />
And TextView with this:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />

How to pull random image within layout xml?

Within my layout xml file I would like the 'android:src=""' to pull a random image from a 'bg' folder within drawable.
I know this can be done pragmatically but I'd like to keep it within the layout files.
Is there any way to create an array of everything in the bg folder and pull from it from within the layout xml?
short answer is no, but I can provide source code to help do it programmatically
EDIT: You need to place all the images you want to use in your drawables folder, then in bg.xml put the ones that you want to appear in the button, see the example below, goodluck!
MainActivity.java
package com.example.stackoverflow17462606;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.TypedArray;
import android.view.Menu;
import android.widget.ImageView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageResource(getRandomImage());
}
private int getRandomImage() {
TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs);
// or set you ImageView's resource to the id
int id = imgs.getResourceId(new Random().nextInt(imgs.length()), -1); //-1 is default if nothing is found (we don't care)
imgs.recycle();
return id;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
bg.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="random_imgs">
<item>#drawable/ic_launcher</item>
<item>#drawable/ic_settings</item>
<!-- ... -->
</string-array>
</resources>
activity_main.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>

Categories

Resources